Skip to content

Commit

Permalink
refactor: use queryMulti for staking computation
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill committed Mar 6, 2023
1 parent 55ad737 commit 0555e15
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
40 changes: 35 additions & 5 deletions src/parachain/rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { AssetRegistryAPI, InterbtcPrimitivesVaultId, LoansAPI } from "../parachain";
import { TransactionAPI } from "../parachain/transaction";
import { WrappedCurrency, CollateralCurrencyExt, CurrencyExt } from "../types";
import { SignedFixedPoint } from "..";

export interface RewardsAPI {
/**
Expand Down Expand Up @@ -161,11 +162,40 @@ export class DefaultRewardsAPI implements RewardsAPI {
this.loansAPI,
vaultId.currencies.collateral
);
const [stake, slashPerToken, slashTally] = await Promise.all([
this.getStakingPoolStake(collateralCurrency, vaultId.accountId, nominatorId),
this.getStakingPoolSlashPerToken(collateralCurrency, vaultId.accountId),
this.getStakingPoolSlashTally(collateralCurrency, vaultId.accountId, nominatorId),
]);
const nonce = await this.getStakingPoolNonce(collateralCurrency, vaultId.accountId);
const [stake, slashPerToken, slashTally] = await this.api.queryMulti<[
SignedFixedPoint,
SignedFixedPoint,
SignedFixedPoint,
]>([
[
this.api.query.vaultStaking.stake,
[
nonce,
[
vaultId,
nominatorId
]
]
],
[
this.api.query.vaultStaking.slashPerToken,
[
nonce,
vaultId
]
],
[
this.api.query.vaultStaking.slashTally,
[
nonce,
[
vaultId,
nominatorId
]
]
],
]).then((data) => data.map((value) => decodeFixedPointType(value)));
const toSlash = computeLazyDistribution(stake, slashPerToken, slashTally);
return newMonetaryAmount(
stake.sub(toSlash),
Expand Down
14 changes: 2 additions & 12 deletions src/parachain/vaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
CollateralCurrencyExt,
WrappedCurrency,
GovernanceCurrency,
CurrencyExt,
} from "../types";
import { RewardsAPI } from "./rewards";
import { UnsignedFixedPoint } from "../interfaces";
Expand Down Expand Up @@ -628,15 +627,6 @@ export class DefaultVaultsAPI implements VaultsAPI {
return annualRewardRate.mul(100);
}

async getLockedCollateral(
vaultAccountId: AccountId,
collateralCurrency: CollateralCurrencyExt
): Promise<MonetaryAmount<CurrencyExt>> {
// TODO: This might be inaccurate if the "reserved" value is incremented by other protocols
// See comment: https://github.com/interlay/interbtc-api/pull/464#discussion_r954909315
return (await this.tokensAPI.balance(collateralCurrency, vaultAccountId)).reserved;
}

async computeReward(
vaultAccountId: AccountId,
collateralCurrency: CollateralCurrencyExt,
Expand Down Expand Up @@ -970,7 +960,7 @@ export class DefaultVaultsAPI implements VaultsAPI {
async getAPY(vaultAccountId: AccountId, collateralCurrency: CollateralCurrencyExt): Promise<Big> {
const [feesWrapped, lockedCollateral, blockRewardsAPY] = await Promise.all([
this.getWrappedReward(vaultAccountId, collateralCurrency),
this.getLockedCollateral(vaultAccountId, collateralCurrency),
this.getCollateral(vaultAccountId, collateralCurrency),
this.getBlockRewardAPY(vaultAccountId, collateralCurrency),
]);
return (await this.feeAPI.calculateAPY(feesWrapped, lockedCollateral)).add(blockRewardsAPY);
Expand Down Expand Up @@ -1060,7 +1050,7 @@ export class DefaultVaultsAPI implements VaultsAPI {
const [vaultExt, liquidationRateThreshold, lockedCollateral] = await Promise.all([
this.get(vaultAccountId, collateralCurrency),
this.getLiquidationCollateralThreshold(collateralCurrency),
this.getLockedCollateral(vaultAccountId, collateralCurrency),
this.getCollateral(vaultAccountId, collateralCurrency),
]);

if (liquidationRateThreshold.eq(0)) {
Expand Down
6 changes: 6 additions & 0 deletions test/integration/parachain/staging/setup/initialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,12 @@ describe("Initialize parachain state", () => {
const vaultInterBtcApi = new DefaultInterBtcApi(api, "regtest", vaultKeyringPair, ESPLORA_BASE_PATH);
const collateralAmount = new MonetaryAmount(aUsd, 10000);
await vaultInterBtcApi.vaults.registerNewCollateralVault(collateralAmount);
// sanity check the collateral computation
const actualCollateralAmount = await vaultInterBtcApi.vaults.getCollateral(vaultAccountId, aUsd);
assert.equal(
actualCollateralAmount.toString(),
collateralAmount.toString(),
);
}
});

Expand Down
6 changes: 3 additions & 3 deletions test/unit/mocks/vaultsTestMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export const prepareLiquidationRateMocks = (
// if we have less than 2x collateral (in BTC) compared to BTC, we need to liquidate
sinon.stub(vaultsApi, "getLiquidationCollateralThreshold").returns(Promise.resolve(Big(mockLiquidationThreshold)));

// mock this.getLockedCollateral return value
const mockLockedCollateral = new MonetaryAmount(collateralCurrency, mockCollateralTokensNumber);
sinon.stub(vaultsApi, "getLockedCollateral").returns(Promise.resolve(mockLockedCollateral));
// mock this.getCollateral return value
const mockCollateral = new MonetaryAmount(collateralCurrency, mockCollateralTokensNumber);
sinon.stub(vaultsApi, "getCollateral").returns(Promise.resolve(mockCollateral));
};

0 comments on commit 0555e15

Please sign in to comment.