Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FIO amounts in parseAction #833

Merged
merged 3 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
## Unreleased

- fixed: (EVM) Fixed ETH requires a resync or log back in to see new transaction

- fixed: (FIO) Fix amount handling in parseActions

## 4.24.2 (2024-09-12)

- fixed: (EVM) Merge duplicate token transaction data from evmscan fixing incorrect transaction native amounts
Expand Down
55 changes: 31 additions & 24 deletions src/fio/FioEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,7 @@ export class FioEngine extends CurrencyEngine<FioTools, SafeFioWalletInfo> {

const { name, params } = asFioAction(otherParams.action)

let nativeAmount = quantity
let fee: string
let txParams: FioTxParams | undefined
switch (name) {
Expand All @@ -1409,6 +1410,7 @@ export class FioEngine extends CurrencyEngine<FioTools, SafeFioWalletInfo> {
}
case ACTIONS.stakeFioTokens: {
const { fioAddress } = asFioAddressParam(params)
nativeAmount = '0'
fee = await this.getFee(EndPoint.stakeFioTokens, fioAddress)
txParams = {
account: 'fio.staking',
Expand All @@ -1424,6 +1426,7 @@ export class FioEngine extends CurrencyEngine<FioTools, SafeFioWalletInfo> {
}
case ACTIONS.unStakeFioTokens: {
const { fioAddress } = asFioAddressParam(params)
nativeAmount = '0'
fee = await this.getFee(EndPoint.unStakeFioTokens, fioAddress)
txParams = {
account: 'fio.staking',
Expand Down Expand Up @@ -1641,7 +1644,7 @@ export class FioEngine extends CurrencyEngine<FioTools, SafeFioWalletInfo> {
date: 0,
isSend: true,
memos,
nativeAmount: sub(`-${quantity}`, `${fee}`),
nativeAmount: `-${add(nativeAmount, fee)}`,
networkFee: `${fee}`,
otherParams: {
...otherParams,
Expand Down Expand Up @@ -2039,41 +2042,37 @@ export const parseAction = ({
let networkFee = '0'
let nativeAmount = '0'
let updateStakingStatus: UpdateStakingStatus | undefined
let assetAction: EdgeTransaction['assetAction'] | undefined

switch (actName) {
case 'trnsfiopubky':
if (data.payee_public_key === publicKey) {
ourReceiveAddresses.push(publicKey)
if (data.actor === actor) {
nativeAmount = '0' // Self-transfer should not affect balance
networkFee = dataMaxFee
nativeAmount = `-${networkFee}`
} else {
nativeAmount = dataAmount // Receiving funds
networkFee = '0'
}
} else if (data.actor === actor) {
nativeAmount = `-${add(dataAmount, dataMaxFee)}` // Sending funds
networkFee = dataMaxFee
nativeAmount = `-${add(dataAmount, networkFee)}` // Sending funds
} else {
// This action doesn't involve our account, so we should ignore it
return { blockNum: action.block_num }
}

if (existingTx != null) {
// If we've already processed this transaction, we should ignore this action
if (existingTx.otherParams?.meta?.isTransferProcessed != null) {
return { blockNum: action.block_num }
}
// If this is a new action for an existing transaction, we should update the existing transaction
nativeAmount = add(existingTx.nativeAmount, nativeAmount)
}

otherParams.meta.isTransferProcessed = true
break

case 'stakefio': {
nativeAmount = `-${dataAmount}`
networkFee = dataMaxFee
nativeAmount = `-${networkFee}`
otherParams.meta.isTransferProcessed = true
assetAction = {
assetActionType: 'stake'
}
break
}

Expand All @@ -2084,11 +2083,11 @@ export const parseAction = ({
txId: action.action_trace.trx_id,
txName: actName
}

// Unstake actions should have a corresponding reward 'transfer' action
// that was parsed right before this action, which only reports the reward
// portion of the unstake.
networkFee = dataMaxFee
if (existingTx != null) {
// Unstake actions should have a corresponding reward 'transfer' action
// that was parsed right before this action, which only reports the reward
// portion of the unstake.
otherParams = {
...existingTx.otherParams,
...otherParams,
Expand All @@ -2101,12 +2100,15 @@ export const parseAction = ({
...otherParams.meta
}
}
nativeAmount = sub(existingTx.nativeAmount, networkFee)
} else {
nativeAmount = `-${networkFee}`
}
// Add the additional unstake amount that was requested by these actions
// to the already saved reward amount, if any.
nativeAmount = add(dataAmount, existingTx?.nativeAmount ?? '0')

otherParams.meta.isTransferProcessed = true
assetAction = {
assetActionType: 'unstakeOrder'
}
break

case 'regaddress':
Expand All @@ -2126,9 +2128,6 @@ export const parseAction = ({
// Fee or (unstake) reward transaction
case 'transfer':
{
const isRecipient = data.to === actor
nativeAmount = isRecipient ? `${fioAmount}` : `-${fioAmount}`

// Some transfers might be rewards/yield from unstaking
const isUnstakeRewardTx =
otherParams.data != null &&
Expand All @@ -2141,8 +2140,14 @@ export const parseAction = ({
txName: actName
}
networkFee = '0'
nativeAmount = fioAmount
assetAction = {
assetActionType: 'unstake'
}
} else {
networkFee = isRecipient ? `-${fioAmount}` : fioAmount
const isRecipient = data.to === actor
networkFee = isRecipient ? `0` : fioAmount
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional change: since networkFee is typically dataMaxFee and these are the exceptions, do the assignment at the beginning with a let so there's less to mentally process per case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny, I purposely kept the initial assignment of networkFee to zero and reassigned it close to nativeAmount to make it easier for myself to understand.

nativeAmount = isRecipient ? '0' : `-${networkFee}`
}

if (existingTx != null) {
Expand All @@ -2162,6 +2167,7 @@ export const parseAction = ({

if (otherParams.meta.isTransferProcessed != null) {
if (data.to !== actor) {
networkFee = dataMaxFee
nativeAmount = sub(existingTx.nativeAmount, networkFee)
} else {
networkFee = '0'
Expand All @@ -2181,6 +2187,7 @@ export const parseAction = ({
}

const transaction: EdgeTransaction = {
assetAction,
blockHeight: action.block_num > 0 ? action.block_num : 0,
currencyCode,
date: getUTCDate(action.block_time) / 1000,
Expand Down
Loading