UNPKG

@broxus/js-core

Version:

MobX-based JavaScript Core library

66 lines (65 loc) 3.73 kB
import BigNumber from 'bignumber.js'; import { DEFAULT_NATIVE_CURRENCY_DECIMALS } from '../../constants'; import { stakingVaultContract } from '../../models/staking-vault/contracts'; import { getFullContractState, resolveTvmAddress, toInt } from '../../utils'; export class StakingVaultUtils { static async deposit(provider, stakingVaultAddress, params, args) { const fixedAmount = toInt(3, DEFAULT_NATIVE_CURRENCY_DECIMALS); const amount = BigNumber(params.amount).plus(fixedAmount).toFixed(); return stakingVaultContract(provider, stakingVaultAddress) .methods.deposit({ _amount: params.amount, _nonce: params.nonce }) .sendDelayed({ amount, ...args }); } static async removePendingWithdraw(provider, stakingVaultAddress, nonce, args) { return stakingVaultContract(provider, stakingVaultAddress) .methods.removePendingWithdraw({ _nonce: nonce }) .sendDelayed({ amount: toInt(3, DEFAULT_NATIVE_CURRENCY_DECIMALS), ...args, }); } static async getAccountAddress(connection, stakingVaultAddress, userAddress, cachedState) { const state = cachedState ?? await getFullContractState(connection, stakingVaultAddress); const result = await stakingVaultContract(connection, stakingVaultAddress) .methods.getAccountAddress({ _user: resolveTvmAddress(userAddress), answerId: 0 }) .call({ cachedState: state, responsible: true }); return result.value0; } static async getDepositExpectedAmount(connection, stakingVaultAddress, amount, cachedState) { const state = cachedState ?? await getFullContractState(connection, stakingVaultAddress); const result = await stakingVaultContract(connection, stakingVaultAddress) .methods.getDepositStEverAmount({ _amount: amount }) .call({ cachedState: state }); return result.value0; } static async getWithdrawExpectedAmount(connection, stakingVaultAddress, amount, cachedState) { const state = cachedState ?? await getFullContractState(connection, stakingVaultAddress); const result = await stakingVaultContract(connection, stakingVaultAddress) .methods.getWithdrawEverAmount({ _amount: amount }) .call({ cachedState: state }); return result.value0; } static async getDetails(connection, stakingVaultAddress, cachedState) { const state = cachedState ?? await getFullContractState(connection, stakingVaultAddress); const result = await stakingVaultContract(connection, stakingVaultAddress) .methods.getDetails({ answerId: 0 }) .call({ cachedState: state, responsible: true }); return result.value0; } static async encodeDepositPayload(connection, stakingVaultAddress, nonce, cachedState) { const state = cachedState ?? await getFullContractState(connection, stakingVaultAddress); const result = await stakingVaultContract(connection, stakingVaultAddress) .methods.encodeDepositPayload({ _nonce: nonce }) .call({ cachedState: state }); return result.depositPayload; } static decodeEvent(connection, stakingVaultAddress, args) { return stakingVaultContract(connection, stakingVaultAddress).decodeEvent(args); } static decodeTransaction(connection, stakingVaultAddress, args) { return stakingVaultContract(connection, stakingVaultAddress).decodeTransaction(args); } static decodeTransactionEvents(connection, stakingVaultAddress, transaction) { return stakingVaultContract(connection, stakingVaultAddress).decodeTransactionEvents({ transaction }); } }