Skip to content

Commit

Permalink
Merge pull request #415 from bvotteler/brendon/test-add-multi-currenc…
Browse files Browse the repository at this point in the history
…y-vaults

Add multi-collateral vaults
  • Loading branch information
bvotteler authored Jun 6, 2022
2 parents 770123d + af50cb2 commit 2935ca2
Show file tree
Hide file tree
Showing 21 changed files with 925 additions and 661 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ jobs:
node-version: "16"
- run: corepack enable
- run: yarn install
- name: Check versions
run: |
yarn --version
npx node --version
npx ts-node --version
- name: Run and set up the parachain, oracle, staked relayer and vault
run: yarn docker-parachain-start -- -d
- run: yarn ci:test
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@interlay/interbtc-api",
"version": "1.10.4",
"version": "1.11.0",
"description": "JavaScript library to interact with interBTC and kBTC",
"main": "build/src/index.js",
"typings": "build/src/index.d.ts",
Expand Down Expand Up @@ -54,7 +54,7 @@
"@interlay/esplora-btc-api": "0.4.0",
"@interlay/interbtc-types": "1.6.0",
"@interlay/monetary-js": "0.6.0",
"@polkadot/api": "7.7.1",
"@polkadot/api": "8.6.2",
"@types/big.js": "6.1.2",
"big.js": "6.1.1",
"bitcoin-core": "^3.0.0",
Expand All @@ -65,11 +65,12 @@
"regtest-client": "^0.2.0"
},
"devDependencies": {
"@polkadot/typegen": "7.7.1",
"@polkadot/typegen": "8.6.2",
"@types/chai": "^4.2.12",
"@types/chai-as-promised": "^7.1.3",
"@types/mocha": "^8.0.3",
"@types/node": "^14.6.4",
"@types/shelljs": "0.8.8",
"@types/sinon": "^9.0.5",
"@typescript-eslint/eslint-plugin": "^4.1.0",
"@typescript-eslint/parser": "^4.1.0",
Expand All @@ -83,14 +84,13 @@
"npm-run-all": "^4.1.5",
"nyc": "^15.1.0",
"prettier": "^2.0.5",
"shelljs": "0.8.4",
"sinon": "^9.0.3",
"ts-mock-imports": "^1.3.0",
"ts-node": "10.4.0",
"typedoc": "^0.22.9",
"typedoc-plugin-markdown": "^3.11.6",
"typescript": "^4.3.2",
"shelljs": "^0.8.4",
"@types/shelljs": "^0.8.8"
"typescript": "4.3.2"
},
"resolutions": {
"bn.js": "4.12.0"
Expand Down
2 changes: 1 addition & 1 deletion src/json/parachain.json

Large diffs are not rendered by default.

10 changes: 1 addition & 9 deletions src/parachain/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BalanceOf, BlockNumber, Moment, RuntimeDbWeight } from "@polkadot/types/interfaces/runtime";
import { BlockNumber, Moment, RuntimeDbWeight } from "@polkadot/types/interfaces/runtime";
import { ApiPromise } from "@polkadot/api";
import { Vec } from "@polkadot/types/codec";
import { WeightToFeeCoefficient } from "@polkadot/types/interfaces/support";
Expand All @@ -24,10 +24,6 @@ export interface ConstantsAPI {
* period on default settings.
*/
getTimestampMinimumPeriod(): Moment;
/**
* @returns The fee to be paid for making a transaction; the per-byte portion.
*/
getTransactionByteFee(): BalanceOf;
/**
* @returns The polynomial that is applied in order to derive fee from weight.
*/
Expand All @@ -49,10 +45,6 @@ export class DefaultConstantsAPI implements ConstantsAPI {
return this.api.consts.timestamp.minimumPeriod;
}

getTransactionByteFee(): BalanceOf {
return this.api.consts.transactionPayment.transactionByteFee;
}

getTransactionWeightToFee(): Vec<WeightToFeeCoefficient> {
return this.api.consts.transactionPayment.weightToFee;
}
Expand Down
2 changes: 1 addition & 1 deletion src/parachain/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export class DefaultOracleAPI implements OracleAPI {

async isOnline(): Promise<boolean> {
const errors = await this.api.query.security.errors();
return !this.hasOracleError(errors.toArray());
return !this.hasOracleError(Array.from(errors));
}

async getRawValuesUpdated(key: InterbtcPrimitivesOracleKey): Promise<boolean> {
Expand Down
28 changes: 10 additions & 18 deletions src/parachain/vaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
newCurrencyId,
newVaultId,
newVaultCurrencyPair,
addHexPrefix,
} from "../utils";
import { TokensAPI } from "./tokens";
import { OracleAPI } from "./oracle";
Expand Down Expand Up @@ -158,14 +159,12 @@ export interface VaultsAPI {
Map<InterbtcPrimitivesVaultId, MonetaryAmount<WrappedCurrency, BitcoinUnit>>
>;
/**
* @param vaultAccountId The vault account ID
* @param collateralCurrency The currency specification, a `Monetary.js` object
* @param vaultId The vault ID
* @param btcTxId ID of the Bitcoin transaction to check
* @returns A bollean value
*/
isVaultFlaggedForTheft(
vaultAccountId: AccountId,
collateralCurrencyIdLiteral: CollateralIdLiteral,
vaultId: InterbtcPrimitivesVaultId,
btcTxId: string
): Promise<boolean>;
/**
Expand Down Expand Up @@ -270,10 +269,11 @@ export interface VaultsAPI {
* (malicious Bitcoin transaction and transaction inclusion proof).
* @remarks If `txId` is not set, the `merkleProof` and `rawTx` must both be set.
*
* @param vaultId The account of the vault to check.
* @param vaultId The vault ID of the vault to be reported.
* @param btcTxId Bitcoin transaction ID
*/
reportVaultTheft(vaultAccountId: AccountId, btcTxId: string): Promise<void>;
reportVaultTheft(vaultId: InterbtcPrimitivesVaultId, btcTxId: string): Promise<void>;

/**
* @returns The wrapped currency issued by the vaults
*/
Expand Down Expand Up @@ -848,18 +848,10 @@ export class DefaultVaultsAPI implements VaultsAPI {
}

async isVaultFlaggedForTheft(
vaultAccountId: AccountId,
collateralCurrencyIdLiteral: CollateralIdLiteral,
vaultId: InterbtcPrimitivesVaultId,
btcTxId: string
): Promise<boolean> {
const collateralCurrencyId = newCurrencyId(this.api, collateralCurrencyIdLiteral);
const vaultId = newVaultId(
this.api,
vaultAccountId.toString(),
currencyIdToMonetaryCurrency(collateralCurrencyId) as CollateralCurrency,
this.wrappedCurrency
);
const theftReports = await this.api.query.relay.theftReports(vaultId, { content: btcTxId });
const theftReports = await this.api.query.relay.theftReports(vaultId, { content: addHexPrefix(btcTxId) });
return theftReports.isEmpty;
}

Expand Down Expand Up @@ -961,10 +953,10 @@ export class DefaultVaultsAPI implements VaultsAPI {
);
}

async reportVaultTheft(vaultAccountId: AccountId, btcTxId: string): Promise<void> {
async reportVaultTheft(vaultId: InterbtcPrimitivesVaultId, btcTxId: string): Promise<void> {
const txInclusionDetails = await getTxProof(this.electrsAPI, btcTxId);
const tx = this.api.tx.relay.reportVaultTheft(
vaultAccountId,
vaultId,
txInclusionDetails.merkleProof,
txInclusionDetails.rawTx
);
Expand Down
7 changes: 4 additions & 3 deletions src/types/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
VoteInterlay,
VoteKintsugi,
MonetaryAmount,
KintsugiAmount,
} from "@interlay/monetary-js";
import { ApiPromise } from "@polkadot/api";
import {
Expand Down Expand Up @@ -47,13 +48,13 @@ export type CollateralIdLiteral =
| CurrencyIdLiteral.KINT
| CurrencyIdLiteral.INTR;

export const CollateralAmount = [PolkadotAmount, KusamaAmount];
export const CollateralAmount = [PolkadotAmount, KusamaAmount, KintsugiAmount];
export type CollateralAmount = typeof CollateralAmount[number];

export const CollateralCurrency = [Polkadot, Kusama] as const;
export const CollateralCurrency = [Polkadot, Kusama, Kintsugi] as const;
export type CollateralCurrency = typeof CollateralCurrency[number];

export const CollateralUnit = [PolkadotUnit, KusamaUnit];
export const CollateralUnit = [PolkadotUnit, KusamaUnit, KintsugiUnit];
export type CollateralUnit = typeof CollateralUnit[number];

export const CurrencyUnit = [BitcoinUnit, PolkadotUnit, KusamaUnit, KintsugiUnit, InterlayUnit];
Expand Down
6 changes: 3 additions & 3 deletions src/utils/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ export function toVoting(governanceCurrency: GovernanceCurrency): Currency<Gover
}
}

export function getCorrespondingCollateralCurrency(governanceCurrency: Currency<GovernanceUnit>): CollateralCurrency {
export function getCorrespondingCollateralCurrencies(governanceCurrency: Currency<GovernanceUnit>): Array<CollateralCurrency> {
switch (governanceCurrency.ticker) {
case "KINT":
return Kusama;
return [Kusama, Kintsugi];
case "INTR":
return Polkadot;
return [Polkadot];
default:
throw new Error("Provided currency is not a governance currency");
}
Expand Down
9 changes: 6 additions & 3 deletions src/utils/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from "../../test/config";
import { CollateralCurrency, CollateralUnit, CurrencyUnit, WrappedCurrency } from "../types";
import { newVaultId } from "./encoding";
import { InterBtcApi, DefaultInterBtcApi, newMonetaryAmount, getCorrespondingCollateralCurrency } from "..";
import { InterBtcApi, DefaultInterBtcApi, newMonetaryAmount, getCorrespondingCollateralCurrencies } from "..";
import { AddressOrPair } from "@polkadot/api/types";

// Command line arguments of the initialization script
Expand Down Expand Up @@ -213,9 +213,12 @@ async function main<U extends CurrencyUnit>(params: InitializationParams): Promi
const sudoAccountInterBtcApi = new DefaultInterBtcApi(api, "regtest", sudoAccount, ESPLORA_BASE_PATH);

const wrappedCurrency = sudoAccountInterBtcApi.getWrappedCurrency();
const collateralCurrency = getCorrespondingCollateralCurrency(
const collateralCurrencies = getCorrespondingCollateralCurrencies(
sudoAccountInterBtcApi.getGovernanceCurrency()
) as unknown as Currency<U>;
) as unknown as Array<Currency<U>>;
// TODO: figure out if we want to initialize alternative collateral currencies (like KINT in additon to KSM), too?
const collateralCurrency = collateralCurrencies[0];

const defaultInitializationParams = getDefaultInitializationParams<U>(
keyring,
vault_1.address,
Expand Down
Loading

0 comments on commit 2935ca2

Please sign in to comment.