From 6d9ce88f22d5817d988c67b39a626a2c23805ecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nam=20Ph=E1=BA=A1m?= Date: Mon, 16 Oct 2023 10:59:32 +0700 Subject: [PATCH 01/12] [Yield-Aggregator] update Parallel metadata --- .../koni/api/yield/parallelLiquidStaking.ts | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts b/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts index 8b90ab7b93..279670fe0a 100644 --- a/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts +++ b/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts @@ -5,13 +5,14 @@ import { COMMON_CHAIN_SLUGS } from '@subwallet/chain-list'; import { _ChainAsset, _ChainInfo } from '@subwallet/chain-list/types'; import { ExtrinsicType, OptimalYieldPath, OptimalYieldPathParams, RequestCrossChainTransfer, RequestYieldStepSubmit, SubmitYieldStepData, YieldPoolInfo, YieldPositionInfo, YieldPositionStats, YieldStepType } from '@subwallet/extension-base/background/KoniTypes'; import { ALL_ACCOUNT_KEY } from '@subwallet/extension-base/constants'; +import { PalletStakingStakingLedger } from '@subwallet/extension-base/koni/api/staking/bonding/relayChain'; import { createXcmExtrinsic } from '@subwallet/extension-base/koni/api/xcm'; import { YIELD_POOL_STAT_REFRESH_INTERVAL } from '@subwallet/extension-base/koni/api/yield/helper/utils'; import { _SubstrateApi } from '@subwallet/extension-base/services/chain-service/types'; import { _getChainNativeTokenSlug, _getTokenOnChainAssetId } from '@subwallet/extension-base/services/chain-service/utils'; import { SubmittableExtrinsic } from '@polkadot/api/types'; -import { BN } from '@polkadot/util'; +import { BN, BN_ZERO } from '@polkadot/util'; interface BlockHeader { number: number @@ -21,13 +22,23 @@ export function subscribeParallelLiquidStakingStats (chainApi: _SubstrateApi, ch async function getPoolStat () { const substrateApi = await chainApi.isReady; - const [_exchangeRate, _currentBlockHeader, _currentTimestamp] = await Promise.all([ + const [_exchangeRate, _currentBlockHeader, _currentTimestamp, _stakingLedgers] = await Promise.all([ substrateApi.api.query.liquidStaking.exchangeRate(), substrateApi.api.rpc.chain.getHeader(), - substrateApi.api.query.timestamp.now() + substrateApi.api.query.timestamp.now(), + substrateApi.api.query.liquidStaking.stakingLedgers.entries() ]); - const exchangeRate = _exchangeRate.toPrimitive() as number; // TODO + let tvl = BN_ZERO; + + for (const _stakingLedger of _stakingLedgers) { + const _ledger = _stakingLedger[1]; + const ledger = _ledger.toPrimitive() as unknown as PalletStakingStakingLedger; + + tvl = tvl.add(new BN(ledger.total.toString())); + } + + const exchangeRate = _exchangeRate.toPrimitive() as number; const currentBlockHeader = _currentBlockHeader.toPrimitive() as unknown as BlockHeader; const currentTimestamp = _currentTimestamp.toPrimitive() as number; @@ -42,7 +53,7 @@ export function subscribeParallelLiquidStakingStats (chainApi: _SubstrateApi, ch const beginTimestamp = _beginTimestamp.toPrimitive() as number; const beginExchangeRate = _beginExchangeRate.toPrimitive() as number; - // const decimals = 10 ** 10; + const decimals = 10 ** 18; const apy = (exchangeRate / beginExchangeRate) ** (365 * 24 * 60 * 60000 / (currentTimestamp - beginTimestamp)) - 1; @@ -54,7 +65,7 @@ export function subscribeParallelLiquidStakingStats (chainApi: _SubstrateApi, ch { slug: poolInfo.rewardAssets[0], apy: apy * 100, - exchangeRate: 1.266 + exchangeRate: exchangeRate / decimals } ], maxCandidatePerFarmer: 1, @@ -62,7 +73,7 @@ export function subscribeParallelLiquidStakingStats (chainApi: _SubstrateApi, ch minJoinPool: '10000000000', minWithdrawal: '0', totalApy: apy * 100, - tvl: '12812000000000000' + tvl: tvl.toString() } }); } From 2e07453edb9d12724df093116579c0506c61f01c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nam=20Ph=E1=BA=A1m?= Date: Mon, 16 Oct 2023 16:08:05 +0700 Subject: [PATCH 02/12] [Yield-Aggregator] update Parallel & Interlay metadata --- .../src/koni/api/yield/index.ts | 4 ++-- .../src/koni/api/yield/interlayLending.ts | 23 +++++++++++++++---- .../koni/api/yield/parallelLiquidStaking.ts | 2 +- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/packages/extension-base/src/koni/api/yield/index.ts b/packages/extension-base/src/koni/api/yield/index.ts index 782ef10b74..13eac6a146 100644 --- a/packages/extension-base/src/koni/api/yield/index.ts +++ b/packages/extension-base/src/koni/api/yield/index.ts @@ -50,11 +50,11 @@ export function subscribeYieldPoolStats (substrateApiMap: Record void) { - function getPoolStat () { +export function subscribeInterlayLendingStats (chainApi: _SubstrateApi, chainInfoMap: Record, poolInfo: YieldPoolInfo, assetInfoMap: Record, callback: (rs: YieldPoolInfo) => void) { + async function getPoolStat () { + const substrateApi = await chainApi.isReady; + const inputTokenSlug = poolInfo.inputAssets[0]; + const inputTokenInfo = assetInfoMap[inputTokenSlug]; + + const _exchangeRate = await substrateApi.api.query.loans.exchangeRate(_getTokenOnChainInfo(inputTokenInfo)); + + const exchangeRate = _exchangeRate.toPrimitive() as number; + const decimals = 10 ** 18; + // eslint-disable-next-line node/no-callback-literal callback({ ...poolInfo, @@ -25,7 +34,7 @@ export function subscribeInterlayLendingStats (poolInfo: YieldPoolInfo, callback { slug: poolInfo.rewardAssets[0], apr: 1.29, - exchangeRate: 1 / 49.77 + exchangeRate: exchangeRate / decimals }, { slug: poolInfo.rewardAssets[1], @@ -42,9 +51,13 @@ export function subscribeInterlayLendingStats (poolInfo: YieldPoolInfo, callback }); } - getPoolStat(); + function getStatInterval () { + getPoolStat().catch(console.error); + } + + getStatInterval(); - const interval = setInterval(getPoolStat, YIELD_POOL_STAT_REFRESH_INTERVAL); + const interval = setInterval(getStatInterval, YIELD_POOL_STAT_REFRESH_INTERVAL); return () => { clearInterval(interval); diff --git a/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts b/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts index 279670fe0a..6d0fc16627 100644 --- a/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts +++ b/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts @@ -18,7 +18,7 @@ interface BlockHeader { number: number } -export function subscribeParallelLiquidStakingStats (chainApi: _SubstrateApi, chainInfoMap: Record, poolInfo: YieldPoolInfo, callback: (rs: YieldPoolInfo) => void, substrateApiMap: Record) { +export function subscribeParallelLiquidStakingStats (chainApi: _SubstrateApi, poolInfo: YieldPoolInfo, callback: (rs: YieldPoolInfo) => void) { async function getPoolStat () { const substrateApi = await chainApi.isReady; From 589377067e85b2b2cc1f9d161ef3ba652840e3ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nam=20Ph=E1=BA=A1m?= Date: Tue, 17 Oct 2023 15:01:44 +0700 Subject: [PATCH 03/12] [Yield-Aggregator] update Acala metadata --- .../src/koni/api/yield/acalaLiquidStaking.ts | 53 +++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/packages/extension-base/src/koni/api/yield/acalaLiquidStaking.ts b/packages/extension-base/src/koni/api/yield/acalaLiquidStaking.ts index d65e71fcf6..07656914b6 100644 --- a/packages/extension-base/src/koni/api/yield/acalaLiquidStaking.ts +++ b/packages/extension-base/src/koni/api/yield/acalaLiquidStaking.ts @@ -11,21 +11,64 @@ import { HandleYieldStepData } from '@subwallet/extension-base/koni/api/yield/in import { _SubstrateApi } from '@subwallet/extension-base/services/chain-service/types'; import { _getChainNativeTokenSlug, _getTokenOnChainInfo } from '@subwallet/extension-base/services/chain-service/utils'; import { sumBN } from '@subwallet/extension-base/utils'; +import fetch from 'cross-fetch'; import { SubmittableExtrinsic } from '@polkadot/api/types'; import { BN } from '@polkadot/util'; // const YEAR = 365 * 24 * 60 * 60 * 1000; +const GRAPHQL_API = 'https://api.polkawallet.io/acala-liquid-staking-subql'; +const EXCHANGE_RATE_REQUEST = 'query { dailySummaries(first:30, orderBy:TIMESTAMP_DESC) {nodes { exchangeRate timestamp }}}'; + +interface BifrostLiquidStakingMeta { + data: { + dailySummaries: { + nodes: BifrostLiquidStakingMetaItem[] + } + } +} + +interface BifrostLiquidStakingMetaItem { + exchangeRate: string, + timestamp: string +} + export function subscribeAcalaLiquidStakingStats (chainApi: _SubstrateApi, chainInfoMap: Record, poolInfo: YieldPoolInfo, callback: (rs: YieldPoolInfo) => void) { async function getPoolStat () { const substrateApi = await chainApi.isReady; - const [_toBondPool, _totalStakingBonded] = await Promise.all([ + const stakingMetaPromise = new Promise(function (resolve) { + fetch(GRAPHQL_API, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + query: EXCHANGE_RATE_REQUEST + }) + }).then((res) => { + resolve(res.json()); + }).catch(console.error); + }); + + const [_toBondPool, _totalStakingBonded, _stakingMeta] = await Promise.all([ substrateApi.api.query.homa.toBondPool(), - substrateApi.api.query.homa.totalStakingBonded() + substrateApi.api.query.homa.totalStakingBonded(), + stakingMetaPromise ]); + const stakingMeta = _stakingMeta as BifrostLiquidStakingMeta; + const stakingMetaList = stakingMeta.data.dailySummaries.nodes; + const latestExchangeRate = parseInt(stakingMetaList[0].exchangeRate); + const decimals = 10 ** 10; + + const endingBalance = parseInt(stakingMetaList[0].exchangeRate); + const beginBalance = parseInt(stakingMetaList[29].exchangeRate); + + const diff = endingBalance / beginBalance; + const apy = diff ** (365 / 30) - 1; + const toBondPool = new BN(_toBondPool.toString()); const totalStakingBonded = new BN(_totalStakingBonded.toString()); @@ -36,15 +79,15 @@ export function subscribeAcalaLiquidStakingStats (chainApi: _SubstrateApi, chain assetEarning: [ { slug: poolInfo.rewardAssets[0], - apr: 20.86, - exchangeRate: 1 / 7.46544 + apy: apy * 100, + exchangeRate: latestExchangeRate / decimals } ], maxCandidatePerFarmer: 1, maxWithdrawalRequestPerFarmer: 1, minJoinPool: '50000000000', minWithdrawal: '0', - totalApr: 20.86, + totalApy: apy * 100, tvl: totalStakingBonded.add(toBondPool).toString() } }); From 67f558235e5e0fce7ed45bad433b35e796206317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nam=20Ph=E1=BA=A1m?= Date: Tue, 17 Oct 2023 15:13:03 +0700 Subject: [PATCH 04/12] [Yield-Aggregator] update min unstaking --- .../extension-base/src/koni/api/yield/acalaLiquidStaking.ts | 2 +- .../extension-base/src/koni/api/yield/parallelLiquidStaking.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/extension-base/src/koni/api/yield/acalaLiquidStaking.ts b/packages/extension-base/src/koni/api/yield/acalaLiquidStaking.ts index 07656914b6..73f73b50a2 100644 --- a/packages/extension-base/src/koni/api/yield/acalaLiquidStaking.ts +++ b/packages/extension-base/src/koni/api/yield/acalaLiquidStaking.ts @@ -86,7 +86,7 @@ export function subscribeAcalaLiquidStakingStats (chainApi: _SubstrateApi, chain maxCandidatePerFarmer: 1, maxWithdrawalRequestPerFarmer: 1, minJoinPool: '50000000000', - minWithdrawal: '0', + minWithdrawal: '50000000000', totalApy: apy * 100, tvl: totalStakingBonded.add(toBondPool).toString() } diff --git a/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts b/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts index 6d0fc16627..69e690979a 100644 --- a/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts +++ b/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts @@ -71,7 +71,7 @@ export function subscribeParallelLiquidStakingStats (chainApi: _SubstrateApi, po maxCandidatePerFarmer: 1, maxWithdrawalRequestPerFarmer: 1, minJoinPool: '10000000000', - minWithdrawal: '0', + minWithdrawal: '5000000000', totalApy: apy * 100, tvl: tvl.toString() } From 62bef4816931959e87541881e48b902674a19a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nam=20Ph=E1=BA=A1m?= Date: Tue, 17 Oct 2023 16:40:48 +0700 Subject: [PATCH 05/12] [Yield-Aggregator] update Interlay metadata --- packages/extension-base/src/koni/api/yield/data.ts | 3 +-- .../extension-base/src/koni/api/yield/interlayLending.ts | 6 +----- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/extension-base/src/koni/api/yield/data.ts b/packages/extension-base/src/koni/api/yield/data.ts index 452619b859..4335368222 100644 --- a/packages/extension-base/src/koni/api/yield/data.ts +++ b/packages/extension-base/src/koni/api/yield/data.ts @@ -167,8 +167,7 @@ export const YIELD_POOLS_INFO: Record = { 'interlay-LOCAL-qDOT' ], rewardAssets: [ - 'interlay-LOCAL-DOT', - 'interlay-NATIVE-INTR' + 'interlay-LOCAL-DOT' ], feeAssets: [ 'interlay-NATIVE-INTR', diff --git a/packages/extension-base/src/koni/api/yield/interlayLending.ts b/packages/extension-base/src/koni/api/yield/interlayLending.ts index 46ec92fa30..80b6dc3a30 100644 --- a/packages/extension-base/src/koni/api/yield/interlayLending.ts +++ b/packages/extension-base/src/koni/api/yield/interlayLending.ts @@ -35,17 +35,13 @@ export function subscribeInterlayLendingStats (chainApi: _SubstrateApi, chainInf slug: poolInfo.rewardAssets[0], apr: 1.29, exchangeRate: exchangeRate / decimals - }, - { - slug: poolInfo.rewardAssets[1], - apr: 12.32 } ], maxCandidatePerFarmer: 1, maxWithdrawalRequestPerFarmer: 1, minJoinPool: '10000000000', minWithdrawal: '0', - totalApr: 13.61, + totalApr: 1.29, tvl: '291890000000000' } }); From 0f9f0ccfc3f334edb17deccc8b35f1f8d93f5d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nam=20Ph=E1=BA=A1m?= Date: Tue, 17 Oct 2023 18:50:23 +0700 Subject: [PATCH 06/12] [Yield-Aggregator] update Bifrost fast unstaking --- .../src/koni/api/yield/bifrostLiquidStaking.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/extension-base/src/koni/api/yield/bifrostLiquidStaking.ts b/packages/extension-base/src/koni/api/yield/bifrostLiquidStaking.ts index 8adb70e2ab..5d5d185db3 100644 --- a/packages/extension-base/src/koni/api/yield/bifrostLiquidStaking.ts +++ b/packages/extension-base/src/koni/api/yield/bifrostLiquidStaking.ts @@ -210,10 +210,12 @@ export async function getBifrostLiquidStakingExtrinsic (address: string, params: export async function getBifrostLiquidStakingRedeem (params: OptimalYieldPathParams, amount: string): Promise<[ExtrinsicType, SubmittableExtrinsic<'promise'>]> { const substrateApi = await params.substrateApiMap[params.poolInfo.chain].isReady; // @ts-ignore - const rewardTokenSlug = params.poolInfo.derivativeAssets[0]; - const rewardTokenInfo = params.assetInfoMap[rewardTokenSlug]; + // const rewardTokenSlug = params.poolInfo.derivativeAssets[0]; + // const rewardTokenInfo = params.assetInfoMap[rewardTokenSlug]; - const extrinsic = substrateApi.api.tx.vtokenMinting.redeem(_getTokenOnChainInfo(rewardTokenInfo), amount); + // const extrinsic = substrateApi.api.tx.vtokenMinting.redeem(_getTokenOnChainInfo(rewardTokenInfo), amount); + + const extrinsic = substrateApi.api.tx.stablePool.swap(0, 1, 0, amount, 0); return [ExtrinsicType.REDEEM_VDOT, extrinsic]; } From 0654feedbb79114bbd87abaa1a12f6f214f75f10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nam=20Ph=E1=BA=A1m?= Date: Tue, 17 Oct 2023 18:52:06 +0700 Subject: [PATCH 07/12] [Yield-Aggregator] remove validate yield withdrawal --- packages/extension-base/src/koni/api/yield/index.ts | 4 ---- .../src/koni/background/handlers/Extension.ts | 13 +------------ 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/packages/extension-base/src/koni/api/yield/index.ts b/packages/extension-base/src/koni/api/yield/index.ts index 13eac6a146..79ad88ab52 100644 --- a/packages/extension-base/src/koni/api/yield/index.ts +++ b/packages/extension-base/src/koni/api/yield/index.ts @@ -403,10 +403,6 @@ export async function validateYieldProcess (address: string, params: OptimalYiel return await validateEarningProcess(address, params, path, balanceService); } -export function validateYieldRedeem (address: string, poolInfo: YieldPoolInfo, amount: string): TransactionError[] { - return []; -} - export interface HandleYieldStepData { txChain: string, extrinsicType: ExtrinsicType, diff --git a/packages/extension-base/src/koni/background/handlers/Extension.ts b/packages/extension-base/src/koni/background/handlers/Extension.ts index 6de278f807..d74248a05a 100644 --- a/packages/extension-base/src/koni/background/handlers/Extension.ts +++ b/packages/extension-base/src/koni/background/handlers/Extension.ts @@ -22,7 +22,7 @@ import { getPoolingBondingExtrinsic, getPoolingUnbondingExtrinsic, getRelayPools import { getERC20TransactionObject, getERC721Transaction, getEVMTransactionObject } from '@subwallet/extension-base/koni/api/tokens/evm/transfer'; import { getPSP34TransferExtrinsic } from '@subwallet/extension-base/koni/api/tokens/wasm'; import { createXcmExtrinsic } from '@subwallet/extension-base/koni/api/xcm'; -import { generateNaiveOptimalPath, handleYieldRedeem, handleYieldStep, validateYieldProcess, validateYieldRedeem } from '@subwallet/extension-base/koni/api/yield'; +import { generateNaiveOptimalPath, handleYieldRedeem, handleYieldStep, validateYieldProcess } from '@subwallet/extension-base/koni/api/yield'; import { YIELD_EXTRINSIC_TYPES } from '@subwallet/extension-base/koni/api/yield/helper/utils'; import KoniState from '@subwallet/extension-base/koni/background/handlers/State'; import { _API_OPTIONS_CHAIN_GROUP, _DEFAULT_MANTA_ZK_CHAIN, _MANTA_ZK_CHAIN_GROUP, _ZK_ASSET_PREFIX } from '@subwallet/extension-base/services/chain-service/constants'; @@ -3946,17 +3946,6 @@ export default class KoniExtension { .generateBeforeHandleResponseErrors([new TransactionError(BasicTxErrorType.INTERNAL_ERROR)]); } - const yieldValidation: TransactionError[] = validateYieldRedeem( - address, - yieldPoolInfo, - amount - ); // TODO: validate, set to fail upon submission - - if (yieldValidation.length > 0) { - return this.#koniState.transactionService - .generateBeforeHandleResponseErrors(yieldValidation); - } - const [extrinsicType, extrinsic] = await handleYieldRedeem( { address, From 884d725adbd0b4226c5317e137d8cf471e8e57fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nam=20Ph=E1=BA=A1m?= Date: Tue, 17 Oct 2023 19:50:01 +0700 Subject: [PATCH 08/12] [Yield-Aggregator] update unclaimed staking reward cron --- packages/extension-base/src/koni/background/cron.ts | 2 ++ .../src/components/Earning/HorizontalEarningItem.tsx | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/extension-base/src/koni/background/cron.ts b/packages/extension-base/src/koni/background/cron.ts index b6a060ff85..c95f998646 100644 --- a/packages/extension-base/src/koni/background/cron.ts +++ b/packages/extension-base/src/koni/background/cron.ts @@ -127,6 +127,7 @@ export class KoniCron { // MantaPay reloadMantaPay && this.removeCron('syncMantaPay'); + commonReload && this.removeCron('refreshPoolingStakingReward'); // NFT (commonReload || needUpdateNft) && this.resetNft(address); @@ -136,6 +137,7 @@ export class KoniCron { if (this.checkNetworkAvailable(serviceInfo)) { // only add cron job if there's at least 1 active network (commonReload || needUpdateNft) && this.addCron('refreshNft', this.refreshNft(address, serviceInfo.chainApiMap, this.state.getSmartContractNfts(), this.state.getActiveChainInfoMap()), CRON_REFRESH_NFT_INTERVAL); reloadMantaPay && this.addCron('syncMantaPay', this.syncMantaPay, CRON_SYNC_MANTA_PAY); + commonReload && this.addCron('refreshPoolingStakingReward', this.refreshStakingRewardFastInterval(currentAccountInfo.address), CRON_REFRESH_STAKING_REWARD_FAST_INTERVAL); } else { this.setStakingRewardReady(); } diff --git a/packages/extension-koni-ui/src/components/Earning/HorizontalEarningItem.tsx b/packages/extension-koni-ui/src/components/Earning/HorizontalEarningItem.tsx index c3d4c17c7c..7be3fca250 100644 --- a/packages/extension-koni-ui/src/components/Earning/HorizontalEarningItem.tsx +++ b/packages/extension-koni-ui/src/components/Earning/HorizontalEarningItem.tsx @@ -296,7 +296,7 @@ const Component: React.FC = (props: Props) => { } { - yieldPoolInfo.type === YieldPoolType.NOMINATION_POOL && nominationPoolReward &&
+ yieldPoolInfo.type === YieldPoolType.NOMINATION_POOL &&
{t('Unclaimed rewards:')} Date: Wed, 18 Oct 2023 11:49:56 +0700 Subject: [PATCH 09/12] [Yield-Aggregator] update fast redeem --- .../src/koni/api/yield/acalaLiquidStaking.ts | 9 ++++++++- .../src/koni/api/yield/bifrostLiquidStaking.ts | 9 ++++++++- .../src/koni/api/yield/parallelLiquidStaking.ts | 11 ++++++++--- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/extension-base/src/koni/api/yield/acalaLiquidStaking.ts b/packages/extension-base/src/koni/api/yield/acalaLiquidStaking.ts index 73f73b50a2..5d0fe82ec9 100644 --- a/packages/extension-base/src/koni/api/yield/acalaLiquidStaking.ts +++ b/packages/extension-base/src/koni/api/yield/acalaLiquidStaking.ts @@ -231,6 +231,13 @@ export async function getAcalaLiquidStakingExtrinsic (address: string, params: O export async function getAcalaLiquidStakingRedeem (params: OptimalYieldPathParams, amount: string): Promise<[ExtrinsicType, SubmittableExtrinsic<'promise'>]> { const substrateApi = await params.substrateApiMap[params.poolInfo.chain].isReady; + const exchangeRate = params.poolInfo.stats?.assetEarning?.[0].exchangeRate || 1; + const formattedAmount = parseInt(amount) / (10 ** 10); + const minAmount = formattedAmount * exchangeRate * 0.9; + const formattedMinAmount = Math.floor(minAmount * (10 ** 10)); + + console.log('formattedMinAmount', exchangeRate, formattedAmount * exchangeRate); + const extrinsic = substrateApi.api.tx.aggregatedDex.swapWithExactSupply( // Swap path [ @@ -245,7 +252,7 @@ export async function getAcalaLiquidStakingRedeem (params: OptimalYieldPathParam // Supply amount amount, // Min target amount - 0 // should always set a min target to prevent unexpected result + formattedMinAmount // should always set a min target to prevent unexpected result ); return [ExtrinsicType.REDEEM_LDOT, extrinsic]; diff --git a/packages/extension-base/src/koni/api/yield/bifrostLiquidStaking.ts b/packages/extension-base/src/koni/api/yield/bifrostLiquidStaking.ts index 5d5d185db3..274b55b880 100644 --- a/packages/extension-base/src/koni/api/yield/bifrostLiquidStaking.ts +++ b/packages/extension-base/src/koni/api/yield/bifrostLiquidStaking.ts @@ -215,7 +215,14 @@ export async function getBifrostLiquidStakingRedeem (params: OptimalYieldPathPar // const extrinsic = substrateApi.api.tx.vtokenMinting.redeem(_getTokenOnChainInfo(rewardTokenInfo), amount); - const extrinsic = substrateApi.api.tx.stablePool.swap(0, 1, 0, amount, 0); + const exchangeRate = params.poolInfo.stats?.assetEarning?.[0].exchangeRate || 1; + const formattedAmount = parseInt(amount) / (10 ** 10); + const minAmount = formattedAmount * exchangeRate * 0.9; + const formattedMinAmount = Math.floor(minAmount * (10 ** 10)); + + console.log('formattedMinAmount', exchangeRate, formattedAmount * exchangeRate); + + const extrinsic = substrateApi.api.tx.stablePool.swap(0, 1, 0, amount, formattedMinAmount); return [ExtrinsicType.REDEEM_VDOT, extrinsic]; } diff --git a/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts b/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts index 69e690979a..f490802833 100644 --- a/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts +++ b/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts @@ -192,10 +192,15 @@ export async function getParallelLiquidStakingExtrinsic (address: string, params export async function getParallelLiquidStakingRedeem (params: OptimalYieldPathParams, amount: string, address: string): Promise<[ExtrinsicType, SubmittableExtrinsic<'promise'>]> { const substrateApi = await params.substrateApiMap[params.poolInfo.chain].isReady; - // const rewardTokenSlug = params.poolInfo.derivativeAssets[0]; - // const rewardTokenInfo = params.assetInfoMap[rewardTokenSlug]; - const extrinsic = substrateApi.api.tx.liquidStaking.fastMatchUnstake([address]); + const exchangeRate = params.poolInfo.stats?.assetEarning?.[0].exchangeRate || 1; + const formattedAmount = parseInt(amount) / (10 ** 10); + const minAmount = formattedAmount * exchangeRate * 0.9; + const formattedMinAmount = Math.floor(minAmount * (10 ** 10)); + + console.log('formattedMinAmount', exchangeRate, formattedAmount * exchangeRate); + + const extrinsic = substrateApi.api.tx.ammRoute.swapExactTokensForTokens(['1001', '101'], amount, formattedMinAmount); return [ExtrinsicType.REDEEM_SDOT, extrinsic]; } From afa8612594b86f516d5391955e53729ea6a8f42d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nam=20Ph=E1=BA=A1m?= Date: Wed, 18 Oct 2023 15:08:58 +0700 Subject: [PATCH 10/12] [Yield-Aggregator] update fast redeem for Interlay --- .../src/services/transaction-service/index.ts | 15 +++++++++- .../Transaction/variants/FastWithdraw.tsx | 12 ++++---- .../Popup/Transaction/parts/FreeBalance.tsx | 5 ++-- .../variants/Yield/YieldWithdrawPosition.tsx | 29 +++++++++++++++++-- 4 files changed, 50 insertions(+), 11 deletions(-) diff --git a/packages/extension-base/src/services/transaction-service/index.ts b/packages/extension-base/src/services/transaction-service/index.ts index 86e0162042..8b48925a9b 100644 --- a/packages/extension-base/src/services/transaction-service/index.ts +++ b/packages/extension-base/src/services/transaction-service/index.ts @@ -548,7 +548,20 @@ export default class TransactionService { break; } - case ExtrinsicType.REDEEM_QDOT: + case ExtrinsicType.REDEEM_QDOT: { + const data = parseTransactionData(transaction.data); + + if (data.yieldPoolInfo.derivativeAssets) { + const inputTokenSlug = data.yieldPoolInfo.inputAssets[0]; + const inputTokenInfo = this.chainService.getAssetBySlug(inputTokenSlug); + + historyItem.amount = { value: data.amount, symbol: _getAssetSymbol(inputTokenInfo), decimals: _getAssetDecimals(inputTokenInfo) }; + eventLogs && parseLiquidStakingFastUnstakeEvents(historyItem, eventLogs, chainInfo, extrinsicType); + } + + break; + } + case ExtrinsicType.REDEEM_LDOT: case ExtrinsicType.REDEEM_SDOT: diff --git a/packages/extension-koni-ui/src/Popup/Confirmations/variants/Transaction/variants/FastWithdraw.tsx b/packages/extension-koni-ui/src/Popup/Confirmations/variants/Transaction/variants/FastWithdraw.tsx index 8a8bbf04b0..24904d0847 100644 --- a/packages/extension-koni-ui/src/Popup/Confirmations/variants/Transaction/variants/FastWithdraw.tsx +++ b/packages/extension-koni-ui/src/Popup/Confirmations/variants/Transaction/variants/FastWithdraw.tsx @@ -22,9 +22,11 @@ const Component: React.FC = (props: Props) => { const { assetRegistry } = useSelector((state) => state.assetRegistry); - const derivativeAsset = useMemo(() => { - return assetRegistry[yieldPoolInfo.derivativeAssets?.[0] || '']; - }, [assetRegistry, yieldPoolInfo.derivativeAssets]); + const assetInfo = useMemo(() => { + const tokenSlug = yieldPoolInfo.slug === 'DOT___interlay_lending' ? yieldPoolInfo.inputAssets?.[0] : yieldPoolInfo.derivativeAssets?.[0]; + + return assetRegistry[tokenSlug || '']; + }, [assetRegistry, yieldPoolInfo.derivativeAssets, yieldPoolInfo.inputAssets, yieldPoolInfo.slug]); return (
@@ -37,9 +39,9 @@ const Component: React.FC = (props: Props) => { hasBackgroundWrapper > diff --git a/packages/extension-koni-ui/src/Popup/Transaction/parts/FreeBalance.tsx b/packages/extension-koni-ui/src/Popup/Transaction/parts/FreeBalance.tsx index e5e7d05bb5..481298499b 100644 --- a/packages/extension-koni-ui/src/Popup/Transaction/parts/FreeBalance.tsx +++ b/packages/extension-koni-ui/src/Popup/Transaction/parts/FreeBalance.tsx @@ -12,12 +12,13 @@ import styled, { useTheme } from 'styled-components'; type Props = ThemeProps & { address?: string, tokenSlug?: string; + customTokenBalance?: string; // TODO: used only for earning label?: string; chain?: string; onBalanceReady?: (rs: boolean) => void; } -const Component = ({ address, chain, className, label, onBalanceReady, tokenSlug }: Props) => { +const Component = ({ address, chain, className, customTokenBalance, label, onBalanceReady, tokenSlug }: Props) => { const { t } = useTranslation(); const { token } = useTheme() as Theme; const { error, isLoading, nativeTokenBalance, nativeTokenSlug, tokenBalance } = useGetBalance(chain, address, tokenSlug); @@ -59,7 +60,7 @@ const Component = ({ address, chain, className, label, onBalanceReady, tokenSlug size={14} suffix={tokenBalance?.symbol} unitColor={token.colorTextTertiary} - value={tokenBalance.value} + value={customTokenBalance || tokenBalance.value} /> ) diff --git a/packages/extension-koni-ui/src/Popup/Transaction/variants/Yield/YieldWithdrawPosition.tsx b/packages/extension-koni-ui/src/Popup/Transaction/variants/Yield/YieldWithdrawPosition.tsx index 2775fe658b..6f58798605 100644 --- a/packages/extension-koni-ui/src/Popup/Transaction/variants/Yield/YieldWithdrawPosition.tsx +++ b/packages/extension-koni-ui/src/Popup/Transaction/variants/Yield/YieldWithdrawPosition.tsx @@ -67,20 +67,42 @@ const Component: React.FC = () => { return yieldPoolInfo?.stats?.minWithdrawal || '0'; }, [yieldPoolInfo?.stats?.minWithdrawal]); + const isInterlayPool = useMemo(() => { + return yieldPoolInfo && yieldPoolInfo.slug === 'DOT___interlay_lending'; + }, [yieldPoolInfo]); + const activeBalance = useMemo(() => { + if (isInterlayPool) { + const exchangeRate = yieldPoolInfo?.stats?.assetEarning?.[0].exchangeRate || 1; + const inputTokenSlug = yieldPoolInfo?.inputAssets[0] || ''; + const inputTokenDecimals = _getAssetDecimals(assetRegistry[inputTokenSlug]); + + const formattedBalance = parseInt(yieldPosition?.balance[0]?.activeBalance || '0') / (10 ** inputTokenDecimals); + const inputAmount = formattedBalance * exchangeRate; + const formattedAmount = Math.floor(inputAmount * (10 ** inputTokenDecimals)); + + return formattedAmount.toString(); + } + return yieldPosition?.balance[0]?.activeBalance || '0'; - }, [yieldPosition?.balance]); + }, [assetRegistry, isInterlayPool, yieldPoolInfo?.inputAssets, yieldPoolInfo?.stats?.assetEarning, yieldPosition?.balance]); const tokenDecimals = useMemo(() => { if (!yieldPoolInfo) { return; } + if (isInterlayPool) { + const inputTokenSlug = yieldPoolInfo.inputAssets[0]; + + return _getAssetDecimals(assetRegistry[inputTokenSlug]); + } + const tokenSlug = yieldPoolInfo.derivativeAssets ? yieldPoolInfo?.derivativeAssets[0] : yieldPoolInfo?.inputAssets[0]; const tokenInfo = assetRegistry[tokenSlug]; return _getAssetDecimals(tokenInfo); - }, [assetRegistry, yieldPoolInfo]); + }, [assetRegistry, isInterlayPool, yieldPoolInfo]); const onFieldsChange: FormCallbacks['onFieldsChange'] = useCallback((changedFields: FormFieldData[], allFields: FormFieldData[]) => { // TODO: field change @@ -165,9 +187,10 @@ const Component: React.FC = () => { address={from} chain={chain} className={'free-balance'} + customTokenBalance={isInterlayPool ? activeBalance : undefined} label={t('Available balance:')} onBalanceReady={setIsBalanceReady} - tokenSlug={yieldPoolInfo?.derivativeAssets?.[0]} + tokenSlug={isInterlayPool ? yieldPoolInfo?.inputAssets?.[0] : yieldPoolInfo?.derivativeAssets?.[0]} /> Date: Wed, 18 Oct 2023 16:25:04 +0700 Subject: [PATCH 11/12] [Yield-Aggregator] update estimated receivables for fast redeem --- .../src/koni/api/yield/acalaLiquidStaking.ts | 13 +++---- .../koni/api/yield/bifrostLiquidStaking.ts | 13 +++---- .../src/koni/api/yield/helper/utils.ts | 15 +++++++- .../koni/api/yield/parallelLiquidStaking.ts | 13 +++---- .../Transaction/variants/FastWithdraw.tsx | 36 +++++++++++++++++-- 5 files changed, 68 insertions(+), 22 deletions(-) diff --git a/packages/extension-base/src/koni/api/yield/acalaLiquidStaking.ts b/packages/extension-base/src/koni/api/yield/acalaLiquidStaking.ts index 5d0fe82ec9..8eb0e94138 100644 --- a/packages/extension-base/src/koni/api/yield/acalaLiquidStaking.ts +++ b/packages/extension-base/src/koni/api/yield/acalaLiquidStaking.ts @@ -6,7 +6,7 @@ import { _ChainAsset, _ChainInfo } from '@subwallet/chain-list/types'; import { ExtrinsicType, OptimalYieldPath, OptimalYieldPathParams, RequestCrossChainTransfer, RequestYieldStepSubmit, SubmitYieldStepData, TokenBalanceRaw, YieldPoolInfo, YieldPositionInfo, YieldPositionStats, YieldStepType } from '@subwallet/extension-base/background/KoniTypes'; import { ALL_ACCOUNT_KEY } from '@subwallet/extension-base/constants'; import { createXcmExtrinsic } from '@subwallet/extension-base/koni/api/xcm'; -import { YIELD_POOL_STAT_REFRESH_INTERVAL } from '@subwallet/extension-base/koni/api/yield/helper/utils'; +import { convertDerivativeToOriginToken, YIELD_POOL_STAT_REFRESH_INTERVAL } from '@subwallet/extension-base/koni/api/yield/helper/utils'; import { HandleYieldStepData } from '@subwallet/extension-base/koni/api/yield/index'; import { _SubstrateApi } from '@subwallet/extension-base/services/chain-service/types'; import { _getChainNativeTokenSlug, _getTokenOnChainInfo } from '@subwallet/extension-base/services/chain-service/utils'; @@ -231,12 +231,13 @@ export async function getAcalaLiquidStakingExtrinsic (address: string, params: O export async function getAcalaLiquidStakingRedeem (params: OptimalYieldPathParams, amount: string): Promise<[ExtrinsicType, SubmittableExtrinsic<'promise'>]> { const substrateApi = await params.substrateApiMap[params.poolInfo.chain].isReady; - const exchangeRate = params.poolInfo.stats?.assetEarning?.[0].exchangeRate || 1; - const formattedAmount = parseInt(amount) / (10 ** 10); - const minAmount = formattedAmount * exchangeRate * 0.9; - const formattedMinAmount = Math.floor(minAmount * (10 ** 10)); + const derivativeTokenSlug = params.poolInfo.derivativeAssets?.[0] || ''; + const originTokenSlug = params.poolInfo.inputAssets[0] || ''; - console.log('formattedMinAmount', exchangeRate, formattedAmount * exchangeRate); + const derivativeTokenInfo = params.assetInfoMap[derivativeTokenSlug]; + const originTokenInfo = params.assetInfoMap[originTokenSlug]; + + const formattedMinAmount = convertDerivativeToOriginToken(amount, params.poolInfo, derivativeTokenInfo, originTokenInfo); const extrinsic = substrateApi.api.tx.aggregatedDex.swapWithExactSupply( // Swap path diff --git a/packages/extension-base/src/koni/api/yield/bifrostLiquidStaking.ts b/packages/extension-base/src/koni/api/yield/bifrostLiquidStaking.ts index 274b55b880..5461b14728 100644 --- a/packages/extension-base/src/koni/api/yield/bifrostLiquidStaking.ts +++ b/packages/extension-base/src/koni/api/yield/bifrostLiquidStaking.ts @@ -6,7 +6,7 @@ import { _ChainAsset, _ChainInfo } from '@subwallet/chain-list/types'; import { ExtrinsicType, OptimalYieldPath, OptimalYieldPathParams, RequestCrossChainTransfer, RequestYieldStepSubmit, SubmitYieldStepData, TokenBalanceRaw, YieldPoolInfo, YieldPositionInfo, YieldPositionStats, YieldStepType } from '@subwallet/extension-base/background/KoniTypes'; import { ALL_ACCOUNT_KEY } from '@subwallet/extension-base/constants'; import { createXcmExtrinsic } from '@subwallet/extension-base/koni/api/xcm'; -import { YIELD_POOL_STAT_REFRESH_INTERVAL } from '@subwallet/extension-base/koni/api/yield/helper/utils'; +import { convertDerivativeToOriginToken, YIELD_POOL_STAT_REFRESH_INTERVAL } from '@subwallet/extension-base/koni/api/yield/helper/utils'; import { HandleYieldStepData } from '@subwallet/extension-base/koni/api/yield/index'; import { _SubstrateApi } from '@subwallet/extension-base/services/chain-service/types'; import { _getAssetDecimals, _getChainNativeTokenSlug, _getTokenOnChainInfo } from '@subwallet/extension-base/services/chain-service/utils'; @@ -215,12 +215,13 @@ export async function getBifrostLiquidStakingRedeem (params: OptimalYieldPathPar // const extrinsic = substrateApi.api.tx.vtokenMinting.redeem(_getTokenOnChainInfo(rewardTokenInfo), amount); - const exchangeRate = params.poolInfo.stats?.assetEarning?.[0].exchangeRate || 1; - const formattedAmount = parseInt(amount) / (10 ** 10); - const minAmount = formattedAmount * exchangeRate * 0.9; - const formattedMinAmount = Math.floor(minAmount * (10 ** 10)); + const derivativeTokenSlug = params.poolInfo.derivativeAssets?.[0] || ''; + const originTokenSlug = params.poolInfo.inputAssets[0] || ''; - console.log('formattedMinAmount', exchangeRate, formattedAmount * exchangeRate); + const derivativeTokenInfo = params.assetInfoMap[derivativeTokenSlug]; + const originTokenInfo = params.assetInfoMap[originTokenSlug]; + + const formattedMinAmount = convertDerivativeToOriginToken(amount, params.poolInfo, derivativeTokenInfo, originTokenInfo); const extrinsic = substrateApi.api.tx.stablePool.swap(0, 1, 0, amount, formattedMinAmount); diff --git a/packages/extension-base/src/koni/api/yield/helper/utils.ts b/packages/extension-base/src/koni/api/yield/helper/utils.ts index b6ed8db789..a1c7910508 100644 --- a/packages/extension-base/src/koni/api/yield/helper/utils.ts +++ b/packages/extension-base/src/koni/api/yield/helper/utils.ts @@ -1,7 +1,9 @@ // Copyright 2019-2022 @subwallet/extension-base // SPDX-License-Identifier: Apache-2.0 -import { ExtrinsicType, YieldStepDetail, YieldStepType } from '@subwallet/extension-base/background/KoniTypes'; +import { _ChainAsset } from '@subwallet/chain-list/types'; +import { ExtrinsicType, YieldPoolInfo, YieldStepDetail, YieldStepType } from '@subwallet/extension-base/background/KoniTypes'; +import { _getAssetDecimals } from '@subwallet/extension-base/services/chain-service/utils'; export interface RuntimeDispatchInfo { weight: { @@ -46,3 +48,14 @@ export const DEFAULT_YIELD_FIRST_STEP: YieldStepDetail = { export const YIELD_EXTRINSIC_TYPES = [ExtrinsicType.MINT_VDOT, ExtrinsicType.MINT_LDOT, ExtrinsicType.MINT_SDOT, ExtrinsicType.MINT_QDOT]; export const YIELD_POOL_STAT_REFRESH_INTERVAL = 300000; + +export function convertDerivativeToOriginToken (amount: string, poolInfo: YieldPoolInfo, derivativeTokenInfo: _ChainAsset, originTokenInfo: _ChainAsset) { + const derivativeDecimals = _getAssetDecimals(derivativeTokenInfo); + const originDecimals = _getAssetDecimals(originTokenInfo); + + const exchangeRate = poolInfo.stats?.assetEarning?.[0].exchangeRate || 1; + const formattedAmount = parseInt(amount) / (10 ** derivativeDecimals); // TODO: decimals + const minAmount = formattedAmount * exchangeRate * 0.9; + + return Math.floor(minAmount * (10 ** originDecimals)); +} diff --git a/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts b/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts index f490802833..e00e8e98ca 100644 --- a/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts +++ b/packages/extension-base/src/koni/api/yield/parallelLiquidStaking.ts @@ -7,7 +7,7 @@ import { ExtrinsicType, OptimalYieldPath, OptimalYieldPathParams, RequestCrossCh import { ALL_ACCOUNT_KEY } from '@subwallet/extension-base/constants'; import { PalletStakingStakingLedger } from '@subwallet/extension-base/koni/api/staking/bonding/relayChain'; import { createXcmExtrinsic } from '@subwallet/extension-base/koni/api/xcm'; -import { YIELD_POOL_STAT_REFRESH_INTERVAL } from '@subwallet/extension-base/koni/api/yield/helper/utils'; +import { convertDerivativeToOriginToken, YIELD_POOL_STAT_REFRESH_INTERVAL } from '@subwallet/extension-base/koni/api/yield/helper/utils'; import { _SubstrateApi } from '@subwallet/extension-base/services/chain-service/types'; import { _getChainNativeTokenSlug, _getTokenOnChainAssetId } from '@subwallet/extension-base/services/chain-service/utils'; @@ -193,12 +193,13 @@ export async function getParallelLiquidStakingExtrinsic (address: string, params export async function getParallelLiquidStakingRedeem (params: OptimalYieldPathParams, amount: string, address: string): Promise<[ExtrinsicType, SubmittableExtrinsic<'promise'>]> { const substrateApi = await params.substrateApiMap[params.poolInfo.chain].isReady; - const exchangeRate = params.poolInfo.stats?.assetEarning?.[0].exchangeRate || 1; - const formattedAmount = parseInt(amount) / (10 ** 10); - const minAmount = formattedAmount * exchangeRate * 0.9; - const formattedMinAmount = Math.floor(minAmount * (10 ** 10)); + const derivativeTokenSlug = params.poolInfo.derivativeAssets?.[0] || ''; + const originTokenSlug = params.poolInfo.inputAssets[0] || ''; - console.log('formattedMinAmount', exchangeRate, formattedAmount * exchangeRate); + const derivativeTokenInfo = params.assetInfoMap[derivativeTokenSlug]; + const originTokenInfo = params.assetInfoMap[originTokenSlug]; + + const formattedMinAmount = convertDerivativeToOriginToken(amount, params.poolInfo, derivativeTokenInfo, originTokenInfo); const extrinsic = substrateApi.api.tx.ammRoute.swapExactTokensForTokens(['1001', '101'], amount, formattedMinAmount); diff --git a/packages/extension-koni-ui/src/Popup/Confirmations/variants/Transaction/variants/FastWithdraw.tsx b/packages/extension-koni-ui/src/Popup/Confirmations/variants/Transaction/variants/FastWithdraw.tsx index 24904d0847..34c023bc80 100644 --- a/packages/extension-koni-ui/src/Popup/Confirmations/variants/Transaction/variants/FastWithdraw.tsx +++ b/packages/extension-koni-ui/src/Popup/Confirmations/variants/Transaction/variants/FastWithdraw.tsx @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { RequestYieldFastWithdrawal } from '@subwallet/extension-base/background/KoniTypes'; +import { convertDerivativeToOriginToken } from '@subwallet/extension-base/koni/api/yield/helper/utils'; import { CommonTransactionInfo, MetaInfo } from '@subwallet/extension-koni-ui/components'; import { useSelector } from '@subwallet/extension-koni-ui/hooks'; import CN from 'classnames'; @@ -20,13 +21,33 @@ const Component: React.FC = (props: Props) => { const { t } = useTranslation(); - const { assetRegistry } = useSelector((state) => state.assetRegistry); + const assetRegistry = useSelector((state) => state.assetRegistry.assetRegistry); + + const isInterlayPool = useMemo(() => { + return yieldPoolInfo.slug === 'DOT___interlay_lending'; + }, [yieldPoolInfo.slug]); const assetInfo = useMemo(() => { - const tokenSlug = yieldPoolInfo.slug === 'DOT___interlay_lending' ? yieldPoolInfo.inputAssets?.[0] : yieldPoolInfo.derivativeAssets?.[0]; + const tokenSlug = isInterlayPool ? yieldPoolInfo.inputAssets?.[0] : yieldPoolInfo.derivativeAssets?.[0]; return assetRegistry[tokenSlug || '']; - }, [assetRegistry, yieldPoolInfo.derivativeAssets, yieldPoolInfo.inputAssets, yieldPoolInfo.slug]); + }, [assetRegistry, isInterlayPool, yieldPoolInfo.derivativeAssets, yieldPoolInfo.inputAssets]); + + const receivedAssetInfo = useMemo(() => { + const tokenSlug = yieldPoolInfo.inputAssets?.[0]; + + return assetRegistry[tokenSlug || '']; + }, [assetRegistry, yieldPoolInfo.inputAssets]); + + const estimatedReceivables = useMemo(() => { + const derivativeTokenSlug = yieldPoolInfo.derivativeAssets?.[0] || ''; + const originTokenSlug = yieldPoolInfo.inputAssets[0] || ''; + + const derivativeTokenInfo = assetRegistry[derivativeTokenSlug]; + const originTokenInfo = assetRegistry[originTokenSlug]; + + return convertDerivativeToOriginToken(amount, yieldPoolInfo, derivativeTokenInfo, originTokenInfo); + }, [amount, assetRegistry, yieldPoolInfo]); return (
@@ -45,6 +66,15 @@ const Component: React.FC = (props: Props) => { value={amount} /> + { + !isInterlayPool && + } + { estimateFee && ( Date: Wed, 18 Oct 2023 16:45:00 +0700 Subject: [PATCH 12/12] [Yield-Aggregator] update estimated receivables for fast redeem --- packages/extension-base/src/koni/api/yield/helper/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/extension-base/src/koni/api/yield/helper/utils.ts b/packages/extension-base/src/koni/api/yield/helper/utils.ts index a1c7910508..5a269f187a 100644 --- a/packages/extension-base/src/koni/api/yield/helper/utils.ts +++ b/packages/extension-base/src/koni/api/yield/helper/utils.ts @@ -55,7 +55,7 @@ export function convertDerivativeToOriginToken (amount: string, poolInfo: YieldP const exchangeRate = poolInfo.stats?.assetEarning?.[0].exchangeRate || 1; const formattedAmount = parseInt(amount) / (10 ** derivativeDecimals); // TODO: decimals - const minAmount = formattedAmount * exchangeRate * 0.9; + const minAmount = formattedAmount * exchangeRate * 0.95; return Math.floor(minAmount * (10 ** originDecimals)); }