Skip to content

Commit

Permalink
Merge pull request #289 from interlay/dom/parachain-upgrade
Browse files Browse the repository at this point in the history
chore: update client and parachain tags
  • Loading branch information
nud3l authored May 26, 2021
2 parents 015bbd1 + 414944f commit 8d613da
Show file tree
Hide file tree
Showing 20 changed files with 113 additions and 193 deletions.
30 changes: 15 additions & 15 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.8"
services:
polkabtc:
image: "registry.gitlab.com/interlay/btc-parachain:master-3585aaed-1621264843"
image: "registry.gitlab.com/interlay/btc-parachain:0-7-2-a06ada7d-1622023912"
command:
- btc-parachain
- --rpc-external
Expand Down Expand Up @@ -58,47 +58,47 @@ services:
- "3002:3002"
staked-relayer:
# only start staked relayer after mining to prevent long catch-up
image: "registry.gitlab.com/interlay/polkabtc-clients/staked-relayer:acfcc599"
image: "registry.gitlab.com/interlay/polkabtc-clients/staked-relayer:0-7-2"
command:
- /bin/sh
- -c
- |
echo '{"alice_stash": "0x3c881bc4d45926680c64a7f9315eeda3dd287f8d598f3653d7c107799c5422b3"}' > keyfile.json
echo "Sleeping..."
sleep 10
staked-relayer --keyfile="keyfile.json" --keyname=alice_stash --polka-btc-url 'ws://polkabtc:9944' --auto-register-with-stake 100
staked-relayer --keyfile="keyfile.json" --keyname=alice_stash --btc-parachain-url 'ws://polkabtc:9944' --auto-fund-with-faucet-url '[::0]:3036'
environment: &client-env
BITCOIN_RPC_URL: http://bitcoind:18443
BITCOIN_RPC_USER: rpcuser
BITCOIN_RPC_PASS: rpcpassword
RUST_LOG: info
oracle:
image: "registry.gitlab.com/interlay/polkabtc-clients/oracle:acfcc599"
image: "registry.gitlab.com/interlay/polkabtc-clients/oracle:0-7-2"
command:
- /bin/sh
- -c
- |
echo "Sleeping..."
sleep 5
oracle --keyring=charlie --polka-btc-url 'ws://polkabtc:9944'
oracle --keyring=charlie --btc-parachain-url 'ws://polkabtc:9944'
environment:
RUST_LOG: info
faucet:
image: "registry.gitlab.com/interlay/polkabtc-clients/faucet:acfcc599"
image: "registry.gitlab.com/interlay/polkabtc-clients/faucet:0-7-2"
command:
- /bin/sh
- -c
- |
echo '{"bob_stash": "0x1a7d114100653850c65edecda8a9b2b4dd65d900edef8e70b1a6ecdcda967056"}' > keyfile.json
echo "Sleeping..."
sleep 5
faucet --keyfile="keyfile.json" --keyname=bob_stash --polka-btc-url 'ws://polkabtc:9944' --user-allowance 1 --vault-allowance 500 --http-addr '[::0]:3036'
faucet --keyfile="keyfile.json" --keyname=bob_stash --btc-parachain-url 'ws://polkabtc:9944' --user-allowance 1 --vault-allowance 500 --http-addr '[::0]:3036'
environment:
RUST_LOG: info
ports:
- "3036:3036"
vault_1:
image: "registry.gitlab.com/interlay/polkabtc-clients/vault:acfcc599"
image: "registry.gitlab.com/interlay/polkabtc-clients/vault:0-7-2"
command:
- /bin/sh
- -c
Expand All @@ -108,11 +108,11 @@ services:
# sleep for 30s to wait for bitcoin to create the Charlie wallet
# and also to ensure that the issue period and redeem period are set
sleep 30
vault --keyfile="keyfile.json" --keyname=charlie_stash --auto-register-with-collateral 1000000000000000000 --polka-btc-url 'ws://polkabtc:9944'
vault --keyfile="keyfile.json" --keyname=charlie_stash --auto-register-with-collateral 1000000000000000000 --btc-parachain-url 'ws://polkabtc:9944'
environment:
<<: *client-env
vault_2:
image: "registry.gitlab.com/interlay/polkabtc-clients/vault:master"
image: "registry.gitlab.com/interlay/polkabtc-clients/vault:0-7-2"
command:
- /bin/sh
- -c
Expand All @@ -122,11 +122,11 @@ services:
# sleep for 30s to wait for bitcoin to create the Dave wallet
# and also to ensure that the issue period and redeem period are set
sleep 30
vault --keyfile="keyfile.json" --keyname=dave_stash --auto-register-with-collateral 1000000000000000000 --no-issue-execution --polka-btc-url 'ws://polkabtc:9944'
vault --keyfile="keyfile.json" --keyname=dave_stash --auto-register-with-collateral 1000000000000000000 --no-issue-execution --btc-parachain-url 'ws://polkabtc:9944'
environment:
<<: *client-env
vault_3:
image: "registry.gitlab.com/interlay/polkabtc-clients/vault:acfcc599"
image: "registry.gitlab.com/interlay/polkabtc-clients/vault:0-7-2"
command:
- /bin/sh
- -c
Expand All @@ -136,11 +136,11 @@ services:
# sleep for 30s to wait for bitcoin to create the Eve wallet
# and also to ensure that the issue period and redeem period are set
sleep 30
vault --keyfile="keyfile.json" --keyname=eve_stash --auto-register-with-collateral 1000000000000 --no-issue-execution --polka-btc-url 'ws://polkabtc:9944'
vault --keyfile="keyfile.json" --keyname=eve_stash --auto-register-with-collateral 1000000000000 --no-issue-execution --btc-parachain-url 'ws://polkabtc:9944'
environment:
<<: *client-env
vault_to_liquidate:
image: "registry.gitlab.com/interlay/polkabtc-clients/vault:acfcc599"
image: "registry.gitlab.com/interlay/polkabtc-clients/vault:0-7-2"
command:
- /bin/sh
- -c
Expand All @@ -150,6 +150,6 @@ services:
# sleep for 30s to wait for bitcoin to create the Ferdie wallet
# and also to ensure that the issue period and redeem period are set
sleep 30
vault --keyfile="keyfile.json" --keyname=ferdie_stash --auto-register-with-collateral 1000000000000 --polka-btc-url 'ws://polkabtc:9944'
vault --keyfile="keyfile.json" --keyname=ferdie_stash --auto-register-with-collateral 1000000000000 --btc-parachain-url 'ws://polkabtc:9944'
environment:
<<: *client-env
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@interlay/polkabtc",
"version": "0.15.3",
"version": "0.15.4",
"description": "JavaScript library to interact with PolkaBTC",
"main": "build/index.js",
"typings": "build/index.d.ts",
Expand Down Expand Up @@ -39,7 +39,7 @@
"engineStrict": true,
"dependencies": {
"@interlay/esplora-btc-api": "0.4.0",
"@interlay/polkabtc-types": "0.7.0",
"@interlay/polkabtc-types": "0.7.2",
"@polkadot/api": "4.8.1",
"@polkadot/typegen": "4.8.1",
"@types/big.js": "4.0.5",
Expand Down
2 changes: 1 addition & 1 deletion src/json/parachain.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions src/parachain/collateral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,26 @@ export class DefaultCollateralAPI extends DefaultTransactionAPI implements Colla

async totalLocked(): Promise<Big> {
const head = await this.api.rpc.chain.getFinalizedHead();
const totalLockedBN = await this.api.query.collateral.totalLocked.at(head);
const totalLockedBN = await this.api.query.collateralCurrency.totalLocked.at(head);
return new Big(planckToDOT(totalLockedBN.toString()));
}

async balanceLocked(id: AccountId): Promise<Big> {
const head = await this.api.rpc.chain.getFinalizedHead();
const account = await this.api.query.backing.account.at(head, id);
const account = await this.api.query.collateral.account.at(head, id);
return new Big(planckToDOT(account.reserved.toString()));
}

async balance(id: AccountId): Promise<Big> {
const head = await this.api.rpc.chain.getFinalizedHead();
const account = await this.api.query.backing.account.at(head, id);
const account = await this.api.query.collateral.account.at(head, id);
return new Big(planckToDOT(account.free.toString()));
}

async subscribeToBalance(account: string, callback: (account: string, balance: Big) => void): Promise<() => void> {
try {
const accountId = this.api.createType("AccountId", account);
const unsubscribe = await this.api.query.backing.account(accountId, (balance) => {
const unsubscribe = await this.api.query.collateral.account(accountId, (balance) => {
callback(account, new Big(planckToDOT(balance.free.toString())));
});
return unsubscribe;
Expand All @@ -83,7 +83,7 @@ export class DefaultCollateralAPI extends DefaultTransactionAPI implements Colla

async transfer(address: string, amount: Big): Promise<void> {
const amountSmallDenomination = this.api.createType("Balance", dotToPlanck(amount.toString()));
const transferTx = this.api.tx.backing.transfer(address, amountSmallDenomination);
await this.sendLogged(transferTx, this.api.events.backing.Transfer);
const transferTx = this.api.tx.collateral.transfer(address, amountSmallDenomination);
await this.sendLogged(transferTx, this.api.events.collateral.Transfer);
}
}
4 changes: 2 additions & 2 deletions src/parachain/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ export class DefaultConstantsAPI implements ConstantsAPI {
constructor(private api: ApiPromise) {}

getDotExistentialDeposit(): Balance {
return this.api.consts.backing.existentialDeposit;
return this.api.consts.collateral.existentialDeposit;
}

getPolkaBtcExistentialDeposit(): Balance {
return this.api.consts.issuing.existentialDeposit;
return this.api.consts.wrapped.existentialDeposit;
}

getSystemBlockHashCount(): BlockNumber {
Expand Down
2 changes: 1 addition & 1 deletion src/parachain/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class DefaultIssueAPI extends DefaultTransactionAPI implements IssueAPI
if (!vaultId) {
vaultId = await this.vaultsAPI.selectRandomVaultIssue(amount);
}
const amountSat = this.api.createType("Issuing", btcToSat(amount.toString()));
const amountSat = this.api.createType("Wrapped", btcToSat(amount.toString()));
const griefingCollateral = await this.getGriefingCollateral(amount);
const requestIssueTx = this.api.tx.issue.requestIssue(amountSat, vaultId, dotToPlanck(griefingCollateral.toString()) as string);
const result = await this.sendLogged(requestIssueTx, this.api.events.issue.RequestIssue);
Expand Down
2 changes: 1 addition & 1 deletion src/parachain/redeem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class DefaultRedeemAPI extends DefaultTransactionAPI implements RedeemAPI
if (!vaultId) {
vaultId = await this.vaultsAPI.selectRandomVaultIssue(amount);
}
const amountSat = this.api.createType("Issuing", btcToSat(amount.toString()));
const amountSat = this.api.createType("Wrapped", btcToSat(amount.toString()));
const btcAddress = this.api.createType("BtcAddress", decodeBtcAddress(btcAddressEnc, this.btcNetwork));
const requestRedeemTx = this.api.tx.redeem.requestRedeem(amountSat, btcAddress, vaultId);
const result = await this.sendLogged(requestRedeemTx, this.api.events.redeem.RequestRedeem);
Expand Down
8 changes: 4 additions & 4 deletions src/parachain/replace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class DefaultReplaceAPI extends DefaultTransactionAPI implements ReplaceA
}

async request(amount: Big): Promise<string> {
const amountSat = this.api.createType("Issuing", btcToSat(amount.toString()));
const amountSat = this.api.createType("Wrapped", btcToSat(amount.toString()));
const griefingCollateral = await this.getGriefingCollateral(amount);
const requestTx = this.api.tx.replace.requestReplace(amountSat, btcToSat(griefingCollateral.toString()));
const result = await this.sendLogged(requestTx, this.api.events.replace.RequestReplace);
Expand All @@ -131,15 +131,15 @@ export class DefaultReplaceAPI extends DefaultTransactionAPI implements ReplaceA
}

async withdraw(amount: Big): Promise<void> {
const amountSat = this.api.createType("Issuing", btcToSat(amount.toString()));
const amountSat = this.api.createType("Wrapped", btcToSat(amount.toString()));
const requestTx = this.api.tx.replace.withdrawReplace(amountSat);
await this.sendLogged(requestTx, this.api.events.replace.WithdrawReplace);
}

async accept(oldVault: AccountId, amount: Big, collateral: Big, btcAddress: string): Promise<void> {
const parsedBtcAddress = this.api.createType("BtcAddress", btcAddress);
const amountSat = this.api.createType("Issuing", btcToSat(amount.toString()));
const collateralPlanck = this.api.createType("Backing", dotToPlanck(collateral.toString()));
const amountSat = this.api.createType("Wrapped", btcToSat(amount.toString()));
const collateralPlanck = this.api.createType("Collateral", dotToPlanck(collateral.toString()));
const requestTx = this.api.tx.replace.acceptReplace(oldVault, amountSat, collateralPlanck, parsedBtcAddress);
await this.sendLogged(requestTx, this.api.events.replace.AcceptReplace);
}
Expand Down
90 changes: 24 additions & 66 deletions src/parachain/staked-relayer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StakedRelayer, StatusCode } from "../interfaces/default";
import { StatusCode } from "../interfaces/default";
import { u64, u128 } from "@polkadot/types/primitive";
import { AccountId, BlockNumber, Moment } from "@polkadot/types/interfaces/runtime";
import { ApiPromise } from "@polkadot/api";
Expand All @@ -9,11 +9,11 @@ import { Network } from "bitcoinjs-lib";
import { AddressOrPair } from "@polkadot/api/types";

import { VaultsAPI, DefaultVaultsAPI } from "./vaults";
import { pagedIterator, decodeFixedPointType, satToBTC, planckToDOT, storageKeyToFirstInner } from "../utils";
import { decodeFixedPointType, satToBTC, planckToDOT, storageKeyToFirstInner } from "../utils";
import { DefaultTransactionAPI, TransactionAPI } from "./transaction";
import { CollateralAPI, DefaultCollateralAPI } from "./collateral";
import { DefaultFeeAPI, FeeAPI } from "./fee";
import { ElectrsAPI, getTxProof } from "..";
import { computeReward, ElectrsAPI, getTxProof } from "..";

/**
* @category PolkaBTC Bridge
Expand All @@ -24,25 +24,7 @@ export interface StakedRelayerAPI extends TransactionAPI {
/**
* @returns An array containing tuples of type [stakedRelayerId, backingCollateral]
*/
list(): Promise<[AccountId, Big][]>;
/**
* @returns A mapping from the staked relayer AccountId to the backing collateral
*/
map(): Promise<Map<AccountId, Big>>;
/**
* @param perPage Number of staked relayers to iterate through at a time
* @returns An AsyncGenerator to be used as an iterator
*/
getPagedIterator(perPage: number): AsyncGenerator<StakedRelayer[]>;
/**
* @param stakedRelayerId The ID of the relayer for which to fetch the staked Backing token amount
* @returns The staked Backing token amount, denoted in Planck
*/
getStakedCollateral(stakedRelayerId: AccountId): Promise<Big>;
/**
* @returns The total staked Backing token amount, denoted in Planck
*/
getTotalStakedCollateral(): Promise<Big>;
list(): Promise<AccountId[]>;
/**
* @returns A mapping from vault IDs to their collateralization
*/
Expand All @@ -62,12 +44,12 @@ export interface StakedRelayerAPI extends TransactionAPI {
getWrappingFees(stakedRelayerId: AccountId): Promise<Big>;
/**
* @param stakedRelayerId The ID of a staked relayer
* @returns Total rewards in Backing tokens for the given staked relayer
* @returns Total rewards in Collateral tokens for the given staked relayer
*/
getCollateralFees(stakedRelayerId: AccountId): Promise<Big>;
/**
* Get the total APY for a staked relayer based on the income in Issuing and Backing tokens
* divided by the locked Backing tokens.
* Get the total APY for a staked relayer based on the income in Wrapped and Collateral tokens
* divided by the locked Collateral tokens.
*
* @note this does not account for interest compounding
*
Expand Down Expand Up @@ -141,50 +123,20 @@ export class DefaultStakedRelayerAPI extends DefaultTransactionAPI implements St
await this.sendLogged(tx, this.api.events.stakedRelayers.VaultTheft);
}

async list(): Promise<[AccountId, Big][]> {
const head = await this.api.rpc.chain.getFinalizedHead();
const stakedRelayersMap = await this.api.query.stakedRelayers.stakes.entriesAt(head);
return stakedRelayersMap.map(
(v) => [storageKeyToFirstInner(v[0]), new Big(planckToDOT(v[1].toString()))]
);
}

async map(): Promise<Map<AccountId, Big>> {
const list = await this.list();
const stakedRelayersMap = new Map<AccountId, Big>();
list.forEach((stakedRelayer) =>
stakedRelayersMap.set(stakedRelayer[0], new Big(planckToDOT(stakedRelayer[1].toString())))
);
return stakedRelayersMap;
}

getPagedIterator(perPage: number): AsyncGenerator<StakedRelayer[]> {
return pagedIterator<StakedRelayer>(this.api.query.issue.issueRequests, perPage);
}

async getStakedCollateral(stakedRelayerId: AccountId): Promise<Big> {
async list(): Promise<AccountId[]> {
const head = await this.api.rpc.chain.getFinalizedHead();
const stake = await this.api.query.stakedRelayers.stakes.at(head, stakedRelayerId);
return new Big(planckToDOT(stake.toString()));
}

private async getStakedCollateralAmounts(): Promise<Big[]> {
const list = await this.list();
return list.map(([_, stake]) => stake);
}

async getTotalStakedCollateral(): Promise<Big> {
const stakedCollateralAmounts: Big[] = await this.getStakedCollateralAmounts();
if (stakedCollateralAmounts.length) {
const sumReducer = (accumulator: Big, currentValue: Big) => accumulator.add(currentValue);
return stakedCollateralAmounts.reduce(sumReducer);
}
return new Big(0);
const stakedRelayersMap = await this.api.query.wrappedRelayerRewards.stake.entriesAt(head);
return stakedRelayersMap
.filter(
(v) => (new Big(v[1].toString()) > new Big(0))
)
.map(
(v) => storageKeyToFirstInner(v[0])
);
}

async getMonitoredVaultsCollateralizationRate(): Promise<Map<AccountId, Big>> {
const vaults = await this.vaultsAPI.list();

const collateralizationRates = await Promise.all(
vaults.filter(vault => vault.status.isActive).map<Promise<[AccountId, Big | undefined]>>(async (vault) => [
vault.id,
Expand Down Expand Up @@ -217,13 +169,19 @@ export class DefaultStakedRelayerAPI extends DefaultTransactionAPI implements St

async getWrappingFees(stakedRelayerId: AccountId): Promise<Big> {
const head = await this.api.rpc.chain.getFinalizedHead();
const fees = await this.api.query.fee.totalRewardsIssuing.at(head, stakedRelayerId);
const stake = decodeFixedPointType(await this.api.query.wrappedRelayerRewards.stake.at(head, stakedRelayerId));
const rewardPerToken = decodeFixedPointType(await this.api.query.wrappedRelayerRewards.rewardPerToken.at(head));
const rewardTally = decodeFixedPointType(await this.api.query.wrappedRelayerRewards.rewardTally.at(head, stakedRelayerId));
const fees = computeReward(new Big(stake), new Big(rewardPerToken), new Big(rewardTally));
return new Big(satToBTC(fees.toString()));
}

async getCollateralFees(stakedRelayerId: AccountId): Promise<Big> {
const head = await this.api.rpc.chain.getFinalizedHead();
const fees = await this.api.query.fee.totalRewardsBacking.at(head, stakedRelayerId);
const stake = decodeFixedPointType(await this.api.query.collateralRelayerRewards.stake.at(head, stakedRelayerId));
const rewardPerToken = decodeFixedPointType(await this.api.query.collateralRelayerRewards.rewardPerToken.at(head));
const rewardTally = decodeFixedPointType(await this.api.query.collateralRelayerRewards.rewardTally.at(head, stakedRelayerId));
const fees = computeReward(new Big(stake), new Big(rewardPerToken), new Big(rewardTally));
return new Big(planckToDOT(fees.toString()));
}

Expand Down
Loading

0 comments on commit 8d613da

Please sign in to comment.