Skip to content

Commit

Permalink
Add stakingStatus in fioEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
andreyvEze authored and samholmes committed Jan 29, 2022
1 parent 05f98d6 commit 3e2d51a
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/fio/fioConst.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export const FEE_ACTION_MAP = {
export const DEFAULT_BUNDLED_TXS_AMOUNT = 100
export const DEFAULT_APR = 450
export const STAKING_REWARD_MEMO = 'Paying Staking Rewards'
export const STAKING_LOCK_PERIOD = 1000 * 60 * 60 * 24 * 7 // 7 days
export const DAY_INTERVAL = 1000 * 60 * 60 * 24

export type FioRequest = {
fio_request_id: string,
Expand Down
123 changes: 118 additions & 5 deletions src/fio/fioEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ import {
ACTIONS_TO_END_POINT_KEYS,
ACTIONS_TO_FEE_END_POINT_KEYS,
BROADCAST_ACTIONS,
DAY_INTERVAL,
DEFAULT_BUNDLED_TXS_AMOUNT,
FEE_ACTION_MAP,
FIO_REQUESTS_TYPES,
HISTORY_NODE_ACTIONS,
HISTORY_NODE_OFFSET,
STAKING_LOCK_PERIOD,
STAKING_REWARD_MEMO
} from './fioConst'
import { fioApiErrorCodes, FioError } from './fioError'
Expand Down Expand Up @@ -435,6 +437,15 @@ export class FioEngine extends CurrencyEngine {
)
}
}

try {
this.currencyEngineCallbacks.onStakingStatusChanged({
stakedAmounts: [],
...this.otherData.stakingStatus
})
} catch (e) {
this.error(`doInitialBalanceCallback onStakingStatusChanged`, e)
}
}

updateBalance(tk: string, balance: string) {
Expand All @@ -459,6 +470,95 @@ export class FioEngine extends CurrencyEngine {
)
}

updateStakingStatus(
nativeAmount: string,
blockTime: string,
txId: string
): void {
// Might not be necessary, but better to be safe than sorry
if (
this.otherData.stakingStatus == null ||
this.otherData.stakingStatus.stakedAmounts == null
) {
this.otherData.stakingStatus = {
stakedAmounts: []
}
}

const stakedAmountIndex =
this.otherData.stakingStatus.stakedAmounts.findIndex(
({ otherParams }) => {
if (otherParams == null || otherParams.date == null) return false

return (
new Date(otherParams.date).toDateString() ===
new Date(blockTime).toDateString()
)
}
)

if (stakedAmountIndex < 0) {
const blockTimeBeginingOfGmtDay =
Math.floor(new Date(blockTime).getTime() / DAY_INTERVAL) * DAY_INTERVAL
const unlockDate = new Date(
blockTimeBeginingOfGmtDay + STAKING_LOCK_PERIOD
)
this.otherData.stakingStatus.stakedAmounts.push({
nativeAmount,
unlockDate,
otherParams: {
date: new Date(blockTime),
txs: [{ txId, nativeAmount, blockTime }]
}
})
} else {
const stakedAmount = {
...this.otherData.stakingStatus.stakedAmounts[stakedAmountIndex],
nativeAmount: '0'
}
const addedTxIndex = stakedAmount.otherParams.txs.findIndex(
({ txId: itemTxId }) => itemTxId === txId
)

if (addedTxIndex < 0) {
stakedAmount.otherParams.txs.push({
txId,
nativeAmount,
blockTime
})
} else {
stakedAmount.otherParams.txs[addedTxIndex] = {
txId,
nativeAmount,
blockTime
}
}

for (const tx of stakedAmount.otherParams.txs) {
stakedAmount.nativeAmount = bns.add(
stakedAmount.nativeAmount,
tx.nativeAmount
)
}

this.otherData.stakingStatus.stakedAmounts[stakedAmountIndex] =
stakedAmount
}

this.localDataDirty()
try {
this.currencyEngineCallbacks.onStakingStatusChanged({
...this.otherData.stakingStatus
})
} catch (e) {
this.error('onStakingStatusChanged error')
}
}

async getStakingStatus(): Promise<EdgeStakingStatus> {
return { ...this.otherData.stakingStatus }
}

processTransaction(
action: FioHistoryNodeAction,
actor: string,
Expand Down Expand Up @@ -500,6 +600,10 @@ export class FioEngine extends CurrencyEngine {
}
}

if (currencyCode === lockedTokenCode) {
nativeAmount = data.amount != null ? data.amount.toString() : '0'
}

const index = this.findTransaction(
currencyCode,
action.action_trace.trx_id
Expand Down Expand Up @@ -655,12 +759,18 @@ export class FioEngine extends CurrencyEngine {
this.addTransaction(currencyCode, edgeTransaction)
}

if (
currencyCode === this.currencyInfo.currencyCode &&
this.checkUnStakeTx(otherParams)
) {
this.processTransaction(action, actor, lockedTokenCode)
if (this.checkUnStakeTx(otherParams)) {
if (currencyCode === this.currencyInfo.currencyCode)
this.processTransaction(action, actor, lockedTokenCode)

if (currencyCode === lockedTokenCode)
this.updateStakingStatus(
nativeAmount || '0',
action.block_time,
action.action_trace.trx_id
)
}

return action.block_num
}

Expand Down Expand Up @@ -1262,6 +1372,9 @@ export class FioEngine extends CurrencyEngine {
[FIO_REQUESTS_TYPES.PENDING]: []
}
this.otherData.fioRequestsToApprove = {}
this.otherData.stakingStatus = {
stakedAmounts: []
}
}

// ****************************************************************************
Expand Down
5 changes: 5 additions & 0 deletions src/fio/fioPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ export function makeFioPlugin(opts: EdgeCorePluginOptions): EdgeCurrencyPlugin {
[FIO_REQUESTS_TYPES.PENDING]: []
}
}
if (currencyEngine.otherData.stakingStatus == null) {
currencyEngine.otherData.stakingStatus = {
stakedAmounts: []
}
}

const out: EdgeCurrencyEngine = currencyEngine
return out
Expand Down

0 comments on commit 3e2d51a

Please sign in to comment.