Skip to content

Commit

Permalink
Merge pull request #284 from interlay/dan/parachain-upgrade
Browse files Browse the repository at this point in the history
Parachain upgrade
  • Loading branch information
daniel-savu authored May 20, 2021
2 parents 7723ab0 + c833ba6 commit 54d4ba9
Show file tree
Hide file tree
Showing 46 changed files with 723 additions and 964 deletions.
36 changes: 21 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/standalone:0.6.1"
image: "registry.gitlab.com/interlay/btc-parachain:master-3585aaed-1621264843"
command:
- btc-parachain
- --rpc-external
Expand Down Expand Up @@ -58,92 +58,98 @@ 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:0.6.2"
image: "registry.gitlab.com/interlay/polkabtc-clients/staked-relayer:acfcc599"
command:
- /bin/sh
- -c
- |
echo '{"alice_stash": "0x3c881bc4d45926680c64a7f9315eeda3dd287f8d598f3653d7c107799c5422b3"}' > keyfile.json
echo "Sleeping..."
sleep 10
staked-relayer --keyring=eve --polka-btc-url 'ws://polkabtc:9944' --auto-register-with-stake 100
staked-relayer --keyfile="keyfile.json" --keyname=alice_stash --polka-btc-url 'ws://polkabtc:9944' --auto-register-with-stake 100
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:0.6.2"
image: "registry.gitlab.com/interlay/polkabtc-clients/oracle:acfcc599"
command:
- /bin/sh
- -c
- |
echo "Sleeping..."
sleep 5
oracle --keyring=bob --polka-btc-url 'ws://polkabtc:9944'
oracle --keyring=charlie --polka-btc-url 'ws://polkabtc:9944'
environment:
RUST_LOG: info
faucet:
image: "registry.gitlab.com/interlay/polkabtc-clients/faucet:0.6.2"
image: "registry.gitlab.com/interlay/polkabtc-clients/faucet:acfcc599"
command:
- /bin/sh
- -c
- |
echo '{"bob_stash": "0x1a7d114100653850c65edecda8a9b2b4dd65d900edef8e70b1a6ecdcda967056"}' > keyfile.json
echo "Sleeping..."
sleep 5
faucet --keyring=ferdie --polka-btc-url 'ws://polkabtc:9944' --user-allowance 1 --vault-allowance 500 --http-addr '[::0]:3036'
faucet --keyfile="keyfile.json" --keyname=bob_stash --polka-btc-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:0.6.2"
image: "registry.gitlab.com/interlay/polkabtc-clients/vault:acfcc599"
command:
- /bin/sh
- -c
- |
echo '{"charlie_stash": "0x66256c4e2f90e273bf387923a9a7860f2e9f47a1848d6263de512f7fb110fc08"}' > keyfile.json
echo "Sleeping..."
# 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 --keyring=charlie --auto-register-with-collateral 1000000000000000000 --polka-btc-url 'ws://polkabtc:9944'
vault --keyfile="keyfile.json" --keyname=charlie_stash --auto-register-with-collateral 1000000000000000000 --polka-btc-url 'ws://polkabtc:9944'
environment:
<<: *client-env
vault_2:
image: "registry.gitlab.com/interlay/polkabtc-clients/vault:0.6.2"
image: "registry.gitlab.com/interlay/polkabtc-clients/vault:master"
command:
- /bin/sh
- -c
- |
echo '{"dave_stash": "0x8766312742c2ebdc78713dab36f8eb328d30d053d702da8167e4672d615f421e"}' > keyfile.json
echo "Sleeping..."
# 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 --keyring=dave --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 --polka-btc-url 'ws://polkabtc:9944'
environment:
<<: *client-env
vault_3:
image: "registry.gitlab.com/interlay/polkabtc-clients/vault:0.6.2"
image: "registry.gitlab.com/interlay/polkabtc-clients/vault:acfcc599"
command:
- /bin/sh
- -c
- |
echo '{"eve_stash": "0x26eda5da160bf8e1810336f97a31d3271fe8b386c4e5b7b4367e55ea33f297d0"}' > keyfile.json
echo "Sleeping..."
# 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 --keyring=eve --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 --polka-btc-url 'ws://polkabtc:9944'
environment:
<<: *client-env
vault_to_liquidate:
image: "registry.gitlab.com/interlay/polkabtc-clients/vault:0.6.2"
image: "registry.gitlab.com/interlay/polkabtc-clients/vault:acfcc599"
command:
- /bin/sh
- -c
- |
echo '{"ferdie_stash": "0x32a1ce8b76ea5820c59663458a0f5dd90b70d9c69beff048ddcdb6daa451afd9"}' > keyfile.json
echo "Sleeping..."
# 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 --keyring=bob --auto-register-with-collateral 1000000000000 --polka-btc-url 'ws://polkabtc:9944'
vault --keyfile="keyfile.json" --keyname=ferdie_stash --auto-register-with-collateral 1000000000000 --polka-btc-url 'ws://polkabtc:9944'
environment:
<<: *client-env
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@interlay/polkabtc",
"version": "0.14.4",
"version": "0.15.0",
"description": "JavaScript library to interact with PolkaBTC",
"main": "build/index.js",
"typings": "build/index.d.ts",
Expand Down Expand Up @@ -39,19 +39,20 @@
"engineStrict": true,
"dependencies": {
"@interlay/esplora-btc-api": "0.4.0",
"@interlay/polkabtc-types": "0.6.2",
"@interlay/polkabtc-types": "0.7.0",
"@polkadot/api": "4.8.1",
"@polkadot/typegen": "4.8.1",
"@types/big.js": "4.0.5",
"big.js": "6.0.3",
"bitcoin-core": "^3.0.0",
"bitcoinjs-lib": "^5.2.0",
"cross-fetch": "^3.0.6",
"regtest-client": "^0.2.0",
"sinon": "^9.0.3",
"ts-mock-imports": "^1.3.0",
"@polkadot/api": "4.8.1",
"@polkadot/typegen": "4.8.1",
"bitcoin-core": "^3.0.0"
"ts-mock-imports": "^1.3.0"
},
"devDependencies": {
"@polkadot/util": "^6.3.1",
"@types/chai": "^4.2.12",
"@types/chai-as-promised": "^7.1.3",
"@types/mocha": "^8.0.3",
Expand Down Expand Up @@ -96,4 +97,4 @@
],
"recursive": true
}
}
}
2 changes: 1 addition & 1 deletion src/json/parachain.json

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion src/parachain/btc-relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ export const DEFAULT_STABLE_CONFIRMATIONS = 6;
export interface BTCRelayAPI {
/**
* @returns A global security parameter: the required block confirmations
* for a transaction to be considered stable
* for a transaction to be considered stable on Bitcoin
*/
getStableBitcoinConfirmations(): Promise<number>;
/**
* @returns A global security parameter: the required block confirmations
* for a transaction to be considered stable on the parachain
*/
getStableParachainConfirmations(): Promise<number>;
/**
* @returns The raw transaction data, represented as a Buffer object
*/
Expand All @@ -40,6 +45,11 @@ export class DefaultBTCRelayAPI implements BTCRelayAPI {
return this.api.query.btcRelay.stableBitcoinConfirmations.at(head).then((param) => param.toNumber());
}

async getStableParachainConfirmations(): Promise<number> {
const head = await this.api.rpc.chain.getFinalizedHead();
return this.api.query.btcRelay.stableParachainConfirmations.at(head).then((param) => param.toNumber());
}

async getLatestBlock(): Promise<H256Le> {
const head = await this.api.rpc.chain.getFinalizedHead();
return await this.api.query.btcRelay.bestBlock.at(head);
Expand Down
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.totalCollateral.at(head);
const totalLockedBN = await this.api.query.collateral.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.dot.account.at(head, id);
const account = await this.api.query.backing.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.dot.account.at(head, id);
const account = await this.api.query.backing.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.dot.account(accountId, (balance) => {
const unsubscribe = await this.api.query.backing.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.dot.transfer(address, amountSmallDenomination);
await this.sendLogged(transferTx, this.api.events.dot.Transfer);
const transferTx = this.api.tx.backing.transfer(address, amountSmallDenomination);
await this.sendLogged(transferTx, this.api.events.backing.Transfer);
}
}
31 changes: 3 additions & 28 deletions src/parachain/constants.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Balance, BalanceOf, BlockNumber, Moment, RuntimeDbWeight } from "@polkadot/types/interfaces/runtime";
import { DOT } from "../interfaces/default";
import { ApiPromise } from "@polkadot/api";
import { Vec } from "@polkadot/types/codec";
import { WeightToFeeCoefficient } from "@polkadot/types/interfaces/support";

/**
* @category PolkaBTC Bridge
* The type Big represents DOT or PolkaBTC denominations,
* The type Big represents Backing or Issuing token denominations,
* while the type BN represents Planck or Satoshi denominations.
*/
export interface ConstantsAPI {
Expand All @@ -18,18 +17,6 @@ export interface ConstantsAPI {
* @returns The minimum amount of PolkaBTC required to keep an account open.
*/
getPolkaBtcExistentialDeposit(): Balance;
/**
* @returns The minimum amount of deposit required to propose an update
*/
getStakedRelayersMinimumDeposit(): DOT;
/**
* @returns The minimum amount of stake required to participate
*/
getStakedRelayersMinimumStake(): DOT;
/**
* @returns How often (in blocks) to check for new votes
*/
getStakedRelayersVotingPeriod(): BlockNumber;
/**
* @returns Maximum number of block number to block hash mappings to keep (oldest pruned first).
*/
Expand Down Expand Up @@ -59,23 +46,11 @@ export class DefaultConstantsAPI implements ConstantsAPI {
constructor(private api: ApiPromise) {}

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

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

getStakedRelayersMinimumDeposit(): DOT {
return this.api.consts.stakedRelayers.minimumDeposit;
}

getStakedRelayersMinimumStake(): DOT {
return this.api.consts.stakedRelayers.minimumStake;
}

getStakedRelayersVotingPeriod(): BlockNumber {
return this.api.consts.stakedRelayers.votingPeriod;
return this.api.consts.issuing.existentialDeposit;
}

getSystemBlockHashCount(): BlockNumber {
Expand Down
25 changes: 9 additions & 16 deletions src/parachain/fee.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { decodeFixedPointType } from "..";
import { PolkaBTC } from "../interfaces";
import { DefaultOracleAPI, OracleAPI } from "./oracle";
import Big from "big.js";
import { ApiPromise } from "@polkadot/api";
Expand All @@ -11,14 +10,14 @@ import { ApiPromise } from "@polkadot/api";
*/
export interface FeeAPI {
/**
* @param amountSat Amount, in Satoshi, for which to compute the required
* griefing collateral, in Planck
* @param amount Amount, in BTC, for which to compute the required
* griefing collateral
* @param griefingCollateralRate
* @param oracleAPI
* @returns The griefing collateral, in Planck
* @returns The griefing collateral, in BTC
*/
getGriefingCollateralInPlanck(
amountSat: PolkaBTC,
getGriefingCollateral(
amount: Big,
griefingCollateralRate: Big
): Promise<Big>;
/**
Expand Down Expand Up @@ -46,18 +45,12 @@ export class DefaultFeeAPI implements FeeAPI {
this.oracleAPI = new DefaultOracleAPI(api);
}

async getGriefingCollateralInPlanck(
amountSat: PolkaBTC,
async getGriefingCollateral(
amount: Big,
griefingCollateralRate: Big
): Promise<Big> {
const amountInPlanck = await this.oracleAPI.convertSatoshiToPlanck(amountSat);
const griefingCollateralPlanck = amountInPlanck.mul(griefingCollateralRate).toString();

// Compute the ceiling of the griefing collateral, because the parachain
// ignores the decimal place (123.456 -> 123456), because there is nothing
// smaller than 1 Planck
const griefingCollateralPlanckRoundedUp = new Big(griefingCollateralPlanck).round(0, 3);
return griefingCollateralPlanckRoundedUp;
const dotAmount = await this.oracleAPI.convertBitcoinToDot(amount);
return dotAmount.mul(griefingCollateralRate).round(10, 3);
}

async getIssueGriefingCollateralRate(): Promise<Big> {
Expand Down
Loading

0 comments on commit 54d4ba9

Please sign in to comment.