Skip to content

Commit

Permalink
Merge pull request #647 from interlay/peter/fix-amm-claimable-rewards
Browse files Browse the repository at this point in the history
fix(amm): read farming reward curencies from farmingRewards pallet
  • Loading branch information
peterslany committed Jul 3, 2023
2 parents 48a0ba1 + 3d8fe9d commit 944ff7c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@interlay/interbtc-api",
"version": "2.3.4",
"version": "2.3.5",
"description": "JavaScript library to interact with interBTC",
"main": "build/src/index.js",
"typings": "build/src/index.d.ts",
Expand Down
29 changes: 21 additions & 8 deletions src/parachain/amm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,22 +594,35 @@ export class DefaultAMMAPI implements AMMAPI {
return { extrinsic: swapExtrinsic, event: this.api.events.dexStable.CurrencyExchange };
}

private async _getFarmingRewardCurrencyIds(
lpTokenCurrencyId: InterbtcPrimitivesCurrencyId
): Promise<Array<InterbtcPrimitivesCurrencyId>> {
const rewardCurrenciesRaw = await this.api.query.farmingRewards.rewardCurrencies(lpTokenCurrencyId);

const rewardCurrencies = Array.from(rewardCurrenciesRaw.values());

return rewardCurrencies;
}

private async _getClaimableFarmingRewardsByPool(
accountId: AccountId,
lpToken: LpCurrency,
pool: LiquidityPool
lpToken: LpCurrency
): Promise<Array<MonetaryAmount<CurrencyExt>>> {
const lpTokenCurrencyId = newCurrencyId(this.api, lpToken);
const rewardCurrencyIds = pool.rewardAmountsYearly.map(({ currency: rewardCurrency }) =>
newCurrencyId(this.api, rewardCurrency)
);

const rewardCurrencyIds = await this._getFarmingRewardCurrencyIds(lpTokenCurrencyId);

const farmingRewards = await Promise.all(
rewardCurrencyIds.map((rewardCurrencyId) =>
this.api.rpc.reward.computeFarmingReward(accountId, lpTokenCurrencyId, rewardCurrencyId)
)
);
const rewardAmounts = pool.rewardAmountsYearly.map(({ currency: rewardCurrency }, index) =>
newMonetaryAmount(farmingRewards[index].amount.toString(), rewardCurrency)

const rewardAmounts = Promise.all(
rewardCurrencyIds.map(async (currencyId, index) => {
const currency = await currencyIdToMonetaryCurrency(this.api, currencyId);
return newMonetaryAmount(farmingRewards[index].amount.toString(), currency);
})
);

return rewardAmounts;
Expand All @@ -627,7 +640,7 @@ export class DefaultAMMAPI implements AMMAPI {
// Return empty array for pools without liquidity.
return [];
}
return this._getClaimableFarmingRewardsByPool(accountId, currency, pool);
return this._getClaimableFarmingRewardsByPool(accountId, currency);
})
);

Expand Down

0 comments on commit 944ff7c

Please sign in to comment.