Skip to content

Commit

Permalink
Merge pull request #596 from gregdhill/chore/fixme
Browse files Browse the repository at this point in the history
chore: resolve todos and fixmes
  • Loading branch information
gregdhill authored Mar 6, 2023
2 parents 2c79007 + 4ef24c8 commit 676fb75
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 29 deletions.
1 change: 0 additions & 1 deletion .env.staging
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export ESPLORA_BASE_PATH = "https://btc-testnet.interlay.io";

export BITCOIN_CORE_NETWORK="testnet";

# TODO: Set the following values
export USER_1_URI="//Charlie";
export USER_2_URI="//Dave";
export BITCOIN_CORE_HOST="0.0.0.0";
Expand Down
8 changes: 8 additions & 0 deletions src/utils/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ import { CollateralCurrencyExt, CurrencyExt, WrappedCurrency } from "../types";
import { newMonetaryAmount } from "../utils";
import { AssetRegistryAPI, LoansAPI, VaultsAPI } from "../parachain";
import { AddressOrPair } from "@polkadot/api/types";
import { stringToU8a, u8aConcat } from "@polkadot/util";

const EMPTY_H256 = new Uint8Array(32);
const MOD_PREFIX = stringToU8a("modl");

export function intoAccountTruncating(api: ApiPromise, palletId: Uint8Array): AccountId {
return api.createType("AccountId", u8aConcat(MOD_PREFIX, palletId, EMPTY_H256));
}

/**
* Converts endianness of a Uint8Array
Expand Down
23 changes: 14 additions & 9 deletions test/integration/parachain/staging/sequential/issue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
import { BitcoinCoreClient } from "../../../../../src/utils/bitcoin-core-client";
import { issueSingle } from "../../../../../src/utils/issueRedeem";
import { newVaultId, WrappedCurrency } from "../../../../../src";
import { getCorrespondingCollateralCurrenciesForTests, runWhileMiningBTCBlocks, sudo } from "../../../../utils/helpers";
import { getCorrespondingCollateralCurrenciesForTests, getIssuableAmounts, runWhileMiningBTCBlocks, sudo } from "../../../../utils/helpers";

describe("issue", () => {
let api: ApiPromise;
Expand Down Expand Up @@ -156,7 +156,6 @@ describe("issue", () => {
}
}).timeout(1000000);

// TODO: maybe add this to issue API
it("should get issueBtcDustValue", async () => {
const dust = await userInterBtcAPI.api.query.issue.issueBtcDustValue();
assert.equal(dust.toString(), "1000");
Expand All @@ -182,19 +181,25 @@ describe("issue", () => {
assert.equal(feePercentage.toString(), "0.0015");
});

// FIXME: don't use magic numbers for these tests
it("should getRequestLimits", async () => {
const requestLimits = await userInterBtcAPI.issue.getRequestLimits();
const singleMaxIssuable = requestLimits.singleVaultMaxIssuable;
const totalMaxIssuable = requestLimits.singleVaultMaxIssuable;
const expected = newMonetaryAmount(0.0005, wrappedCurrency, true);
const totalMaxIssuable = requestLimits.totalMaxIssuable;

const issuableAmounts = await getIssuableAmounts(userInterBtcAPI);
const singleIssueable = issuableAmounts.reduce(
(prev, curr) => (prev > curr) ? prev : curr,
newMonetaryAmount(0, wrappedCurrency)
);
const totalIssuable = issuableAmounts.reduce((prev, curr) => prev.add(curr));

assert.isTrue(
singleMaxIssuable.gt(expected),
`singleVaultMaxIssuable is ${singleMaxIssuable.toHuman()}, expected greater than ${expected.toHuman()}`
singleMaxIssuable.toBig().sub(singleIssueable.toBig()).abs().lte(1),
`${singleMaxIssuable.toHuman()} != ${singleIssueable.toHuman()}`
);
assert.isTrue(
totalMaxIssuable.gte(singleMaxIssuable),
`totalMaxIssuable is ${totalMaxIssuable.toHuman()}, expected greater than or equal to ${singleMaxIssuable.toHuman()}`
totalMaxIssuable.toBig().sub(totalIssuable.toBig()).abs().lte(1),
`${totalMaxIssuable.toHuman()} != ${totalIssuable.toHuman()}`
);
});

Expand Down
8 changes: 4 additions & 4 deletions test/integration/parachain/staging/sequential/loans.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,13 @@ describe("Loans", () => {

describe("getAccruedRewardsOfAccount", () => {
it("should return correct amount of reward", () => {
//TODO
// TODO
});
});

describe("claimAllSubsidyRewards", () => {
it("should claim all subsidy rewards", () => {
//TODO
// TODO
});
});

Expand Down Expand Up @@ -445,11 +445,11 @@ describe("Loans", () => {

describe("getBorrowPositionsOfAccount", () => {
before(async function () {
// TODO:borrow
// TODO
});

it("should get borrow positions in correct format", async function () {
//TODO
// TODO
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ describe.skip("NominationAPI", () => {
const transactionAPI = new DefaultTransactionAPI(api);
sudoAccount = keyring.addFromUri(SUDO_URI);
userAccount = keyring.addFromUri(USER_1_URI);
// TODO: remove all uses of config currencies and query the chain instead
userInterBtcAPI = new DefaultInterBtcApi(api, "regtest", userAccount, ESPLORA_BASE_PATH);
sudoInterBtcAPI = new DefaultInterBtcApi(api, "regtest", sudoAccount, ESPLORA_BASE_PATH);
assetRegistry = new DefaultAssetRegistryAPI(api);
Expand Down
4 changes: 0 additions & 4 deletions test/integration/parachain/staging/sequential/redeem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,6 @@ describe("redeem", () => {
// get the actual values on the BTC transaction
const btcTx = await interBtcAPI.electrsAPI.getTx(txId);

// TODO: remove at later stage. print debug info while we seem to have occasional failures
console.log(`BTC transaction details:\n${JSON.stringify(btcTx)} `);

const actualTxFeeSatoshi = new Big(btcTx.fee || 0);
const actualTxVsize = calculateBtcTxVsize(btcTx);
if (actualTxVsize.eq(0)) {
Expand Down Expand Up @@ -249,7 +246,6 @@ describe("redeem", () => {
);
}).timeout(10 * 60000);

// TODO: maybe add this to redeem API
it("should get redeemBtcDustValue", async () => {
const dust = await interBtcAPI.api.query.redeem.redeemBtcDustValue();
assert.equal(dust.toString(), "1000");
Expand Down
10 changes: 7 additions & 3 deletions test/integration/parachain/staging/sequential/vaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { getSS58Prefix, newMonetaryAmount } from "../../../../../src/utils";
import {
getAUSDForeignAsset,
getCorrespondingCollateralCurrenciesForTests,
getIssuableAmounts,
vaultStatusToLabel,
} from "../../../../utils/helpers";
import sinon from "sinon";
Expand Down Expand Up @@ -94,11 +95,14 @@ describe("vaultsAPI", () => {
);
}

// FIXME: this should be tested in a way that in doesn't use magic numbers
it("should get issuable", async () => {
const issuableInterBTC = await interBtcAPI.vaults.getTotalIssuableAmount();
const minExpectedIssuableInterBTC = newMonetaryAmount(0.002, wrappedCurrency, true);
assert.isTrue(issuableInterBTC.gte(minExpectedIssuableInterBTC), `Issuable ${issuableInterBTC.toHuman()}`);
const issuableAmounts = await getIssuableAmounts(interBtcAPI);
const totalIssuable = issuableAmounts.reduce((prev, curr) => prev.add(curr));
assert.isTrue(
issuableInterBTC.toBig().sub(totalIssuable.toBig()).abs().lte(1),
`${issuableInterBTC.toHuman()} != ${totalIssuable.toHuman()}`
);
});

it("should get the required collateral for the vault", async () => {
Expand Down
14 changes: 8 additions & 6 deletions test/integration/parachain/staging/setup/initialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getCollateralCurrencies,
DefaultTransactionAPI,
newCurrencyId,
intoAccountTruncating,
} from "../../../../../src";
import {
initializeVaultNomination,
Expand Down Expand Up @@ -56,12 +57,6 @@ import { DefaultAssetRegistryAPI } from "../../../../../src/parachain/asset-regi
import { BN } from "bn.js";

describe("Initialize parachain state", () => {
// TODO: compute from constants
const vaultAnnuityAddress = Buffer.concat([
Buffer.from("modl"), // 4 bytes
Buffer.from("vlt/annu"), // 8 bytes
], 32);

let api: ApiPromise;
let userInterBtcAPI: InterBtcApi;
let sudoInterBtcAPI: InterBtcApi;
Expand All @@ -76,6 +71,8 @@ describe("Initialize parachain state", () => {
let vault_2: KeyringPair;
let vault_3: KeyringPair;

let vaultAnnuityAddress: AccountId;

let collateralCurrency: CollateralCurrencyExt;

let aUSD: ForeignAsset | undefined;
Expand Down Expand Up @@ -106,6 +103,11 @@ describe("Initialize parachain state", () => {
vault_2 = keyring.addFromUri(VAULT_2_URI);
vault_3 = keyring.addFromUri(VAULT_3_URI);

vaultAnnuityAddress = intoAccountTruncating(
api,
api.consts.vaultAnnuity.annuityPalletId
);

bitcoinCoreClient = new BitcoinCoreClient(
BITCOIN_CORE_NETWORK,
BITCOIN_CORE_HOST,
Expand Down
19 changes: 18 additions & 1 deletion test/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Transaction } from "@interlay/esplora-btc-api";
import { Kintsugi, Kusama, Polkadot } from "@interlay/monetary-js";
import { Kintsugi, Kusama, MonetaryAmount, Polkadot } from "@interlay/monetary-js";
import { Keyring } from "@polkadot/api";
import { KeyringPair } from "@polkadot/keyring/types";
import { mnemonicGenerate } from "@polkadot/util-crypto";
Expand Down Expand Up @@ -277,3 +277,20 @@ export function includesStringified<T extends ImplementsToString>(arr: Array<T>,
}
return arr.map((x) => x.toString() == element.toString()).reduce((acc, value) => acc || value);
}

export async function getIssuableAmounts(interBtcApi: InterBtcApi): Promise<Array<MonetaryAmount<CurrencyExt>>> {
const allVaults = await interBtcApi.vaults.list();
const activeVaults = await Promise.all(allVaults.filter(vault => vault.isBanned()));
return Promise.all(activeVaults.map(async (vault): Promise<MonetaryAmount<CurrencyExt>> => {
const [usedCollateral, secureThreshold] = await Promise.all([
interBtcApi.oracle.convertWrappedToCurrency(
vault.issuedTokens.add(vault.toBeIssuedTokens),
vault.backingCollateral.currency
),
interBtcApi.vaults.getSecureCollateralThreshold(vault.backingCollateral.currency),
]);
const freeCollateral = vault.backingCollateral.sub(usedCollateral.mul(secureThreshold));
const wrappedAmount = (await interBtcApi.oracle.convertCollateralToWrapped(freeCollateral)).div(secureThreshold);
return wrappedAmount;
}));
}

0 comments on commit 676fb75

Please sign in to comment.