Skip to content

Commit

Permalink
Refactor parseAction
Browse files Browse the repository at this point in the history
Retaining all existing functionality
  • Loading branch information
Jon-edge committed Sep 4, 2024
1 parent 9842b01 commit faa04ba
Showing 1 changed file with 123 additions and 158 deletions.
281 changes: 123 additions & 158 deletions src/fio/FioEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1997,7 +1997,7 @@ const getUnlockDate = (txDate: Date): Date => {
return new Date(blockTimeBeginingOfGmtDay + STAKING_LOCK_PERIOD)
}

const checkUnStakeTx = (otherParams: TxOtherParams): boolean => {
const isUnstakeRewardTx = (otherParams: TxOtherParams): boolean => {
return (
otherParams.name === 'unstakefio' ||
(otherParams.data != null && otherParams.data.memo === STAKING_REWARD_MEMO)
Expand All @@ -2016,194 +2016,159 @@ export const parseAction = ({
getTransactionList
}: ParseActionParams): ParseActionResult => {
const {
act: { name: trxName, data, account, authorization }
act: { name: actName, data, account, authorization }
} = action.action_trace
let nativeAmount
let actorSender
let networkFee = '0'

// Throw away the action in some cases:
if (actName == null)
throw new Error(
'FIO parseAction found with null actName at txId: ' +
action.action_trace.trx_id
)
if (action.block_num <= highestTxHeight) {
return { blockNum: action.block_num }
}

let otherParams: TxOtherParams = {
account,
name: trxName,
name: actName,
authorization,
data,
meta: {}
}
const ourReceiveAddresses = []
if (action.block_num <= highestTxHeight) {
return { blockNum: action.block_num }
const index = findTransaction(currencyCode, action.action_trace.trx_id)
let existingTx: EdgeTransaction | undefined
if (index > -1) {
existingTx = getTransactionList(currencyCode)[index]
// Change this part to match the original logic
if (existingTx.otherParams?.meta?.isTransferProcessed != null) {
return { blockNum: action.block_num }
}
}
let updateStakingStatus: UpdateStakingStatus | undefined

// Transfer funds transaction
if (
trxName === 'trnsfiopubky' ||
trxName === 'unstakefio' ||
trxName === 'regaddress'
) {
nativeAmount = '0'

if (trxName === 'regaddress') {
// The action must have been authorized by the engine's actor in order
// for use to consider this a spend transaction.
// Otherwise, we should ignore regaddress actions which are received
// address, until we have some metadata explaining the receive.
if (
action.action_trace.act.authorization.some(auth => auth.actor === actor)
) {
networkFee = String(action.action_trace.act.data.max_fee ?? 0)
nativeAmount = `-${networkFee}`
}
}
// Process a new action
let networkFee = '0'
let nativeAmount = '0'
const exchangeAmount = data.quantity?.split(' ')[0] ?? '0'
const fioAmount = mul(exchangeAmount, denom.multiplier)
let updateStakingStatus: UpdateStakingStatus | undefined
const ourReceiveAddresses = []

if (trxName === 'trnsfiopubky' && data.amount != null) {
nativeAmount = data.amount.toString()
actorSender = data.actor
switch (actName) {
case 'trnsfiopubky':
nativeAmount = fioAmount
if (data.payee_public_key === publicKey) {
ourReceiveAddresses.push(publicKey)
if (actorSender === actor) {
nativeAmount = '0'
}
} else {
nativeAmount = `-${nativeAmount}`
}
}

const index = findTransaction(currencyCode, action.action_trace.trx_id)
// Check if fee transaction have already added
if (index > -1) {
const existingTrx = getTransactionList(currencyCode)[index]
otherParams = {
...existingTrx.otherParams,
...otherParams,
data: {
...(existingTrx.otherParams?.data ?? {}),
...otherParams.data
},
meta: {
...(existingTrx.otherParams?.meta ?? {}),
...otherParams.meta
}
}

if (otherParams.meta.isTransferProcessed != null) {
return { blockNum: action.block_num }
}
if (otherParams.meta.isFeeProcessed != null) {
if (trxName === ACTIONS_TO_TX_ACTION_NAME[ACTIONS.transferTokens]) {
nativeAmount = sub(nativeAmount, existingTrx.networkFee)
networkFee = existingTrx.networkFee
if (data.actor === actor) {
nativeAmount = '0' // Self-transfer should not affect balance
} else {
nativeAmount = existingTrx.nativeAmount
networkFee = '0'
nativeAmount = `-${nativeAmount}`
}
} else {
throw new Error(
'processTransaction error - existing spend transaction should have isTransferProcessed or isFeeProcessed set'
)
}
}
otherParams.meta.isTransferProcessed = true
break

if (checkUnStakeTx(otherParams)) {
case 'unstakefio':
updateStakingStatus = {
nativeAmount: data.amount != null ? data.amount.toString() : '0',
nativeAmount,
blockTime: action.block_time,
txId: action.action_trace.trx_id,
txName: trxName
txName: actName
}
}
if (existingTx != null) {
otherParams = {
...existingTx.otherParams,
...otherParams,
data: {
...existingTx.otherParams?.data,
...data
},
meta: {
...existingTx.otherParams?.meta,
...otherParams.meta
}
}
}
otherParams.meta.isTransferProcessed = true
break

otherParams.meta.isTransferProcessed = true
case 'regaddress':
if (
action.action_trace.act.authorization.some(auth => auth.actor === actor)
) {
networkFee = String(action.action_trace.act.data.max_fee ?? 0)
nativeAmount = `-${networkFee}`
}
otherParams.meta.isTransferProcessed = true
break

const transaction: EdgeTransaction = {
blockHeight: action.block_num > 0 ? action.block_num : 0,
currencyCode,
date: getUTCDate(action.block_time) / 1000,
isSend: nativeAmount.startsWith('-'),
memos: [],
nativeAmount,
networkFee,
otherParams,
ourReceiveAddresses,
signedTx: '',
tokenId: null,
txid: action.action_trace.trx_id,
walletId
}
return { blockNum: action.block_num, transaction, updateStakingStatus }
}
case 'transfer':
nativeAmount = data.to === actor ? `${fioAmount}` : `-${fioAmount}`
networkFee = data.to === actor ? `-${fioAmount}` : fioAmount

// Fee / Reward transaction
if (trxName === 'transfer' && data.quantity != null) {
const [amount] = data.quantity.split(' ')
const exchangeAmount = amount.toString()
const fioAmount = mul(exchangeAmount, denom.multiplier)
if (data.to === actor) {
nativeAmount = `${fioAmount}`
networkFee = `-${fioAmount}`
} else {
nativeAmount = `-${fioAmount}`
networkFee = fioAmount
}
if (existingTx != null) {
otherParams = {
...otherParams,
...existingTx.otherParams,
data: {
...otherParams.data,
...(existingTx.otherParams?.data ?? {})
},
meta: {
...otherParams.meta,
...(existingTx.otherParams?.meta ?? {})
}
}

const index = findTransaction(currencyCode, action.action_trace.trx_id)
// Check if transfer transaction have already added
if (index > -1) {
const existingTrx = getTransactionList(currencyCode)[index]
otherParams = {
...otherParams,
...existingTrx.otherParams,
data: {
...otherParams.data,
...(existingTrx.otherParams?.data ?? {})
},
meta: {
...otherParams.meta,
...(existingTrx.otherParams?.meta ?? {})
// Change this part to match the original logic
if (otherParams.meta.isFeeProcessed != null) {
return { blockNum: action.block_num }
}
}
if (otherParams.meta.isFeeProcessed != null) {
return { blockNum: action.block_num }
}
if (otherParams.meta.isTransferProcessed != null) {
if (data.to !== actor) {
nativeAmount = sub(existingTrx.nativeAmount, networkFee)
if (otherParams.meta.isTransferProcessed != null) {
if (data.to !== actor) {
nativeAmount = sub(existingTx.nativeAmount, networkFee)
} else {
networkFee = '0'
}
} else {
networkFee = '0'
throw new Error(
'processTransaction error - existing spend transaction should have isTransferProcessed or isFeeProcessed set'
)
}
} else {
throw new Error(
'processTransaction error - existing spend transaction should have isTransferProcessed or isFeeProcessed set'
)
}
}

if (checkUnStakeTx(otherParams)) {
updateStakingStatus = {
nativeAmount: fioAmount,
blockTime: action.block_time,
txId: action.action_trace.trx_id,
txName: trxName
// Some transfers might be rewards/yield from unstaking
if (isUnstakeRewardTx(otherParams)) {
updateStakingStatus = {
nativeAmount: fioAmount,
blockTime: action.block_time,
txId: action.action_trace.trx_id,
txName: actName
}
}
}
otherParams.meta.isFeeProcessed = true
break

otherParams.meta.isFeeProcessed = true
const transaction: EdgeTransaction = {
blockHeight: action.block_num > 0 ? action.block_num : 0,
currencyCode,
date: getUTCDate(action.block_time) / 1000,
isSend: nativeAmount.startsWith('-'),
memos: [],
nativeAmount,
networkFee,
otherParams,
ourReceiveAddresses: [],
signedTx: '',
tokenId: null,
txid: action.action_trace.trx_id,
walletId
}
return { blockNum: action.block_num, transaction, updateStakingStatus }
default:
// Unrecognized action, don't create a tx
return { blockNum: action.block_num }
}

const transaction: EdgeTransaction = {
blockHeight: action.block_num > 0 ? action.block_num : 0,
currencyCode,
date: getUTCDate(action.block_time) / 1000,
isSend: nativeAmount.startsWith('-'),
memos: [],
nativeAmount,
networkFee,
otherParams,
ourReceiveAddresses,
signedTx: '',
tokenId: null,
txid: action.action_trace.trx_id,
walletId
}

return { blockNum: action.block_num }
return { blockNum: action.block_num, transaction, updateStakingStatus }
}

0 comments on commit faa04ba

Please sign in to comment.