From 793591b4a02d0cd472e900b48d903ef51a8c4c2e Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Thu, 4 Jul 2024 12:05:48 +1000 Subject: [PATCH 01/16] chore: update dwellir asset hub urls --- scripts/configs/statemine.yml | 2 +- scripts/configs/statemint.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/configs/statemine.yml b/scripts/configs/statemine.yml index c590691..cd0e31d 100644 --- a/scripts/configs/statemine.yml +++ b/scripts/configs/statemine.yml @@ -1,5 +1,5 @@ endpoint: - - wss://statemine-rpc.dwellir.com + - wss://asset-hub-kusama-rpc.dwellir.com - wss://rpc-asset-hub-kusama.luckyfriday.io - wss://statemine-rpc-tn.dwellir.com - wss://kusama-asset-hub-rpc.polkadot.io diff --git a/scripts/configs/statemint.yml b/scripts/configs/statemint.yml index d690dc7..cb48600 100644 --- a/scripts/configs/statemint.yml +++ b/scripts/configs/statemint.yml @@ -1,5 +1,5 @@ endpoint: - - wss://statemint-rpc.dwellir.com + - wss://asset-hub-polkadot-rpc.dwellir.com - wss://rpc-asset-hub-polkadot.luckyfriday.io - wss://statemint-rpc-tn.dwellir.com - wss://polkadot-asset-hub-rpc.polkadot.io From c4d3a25c58677685d44477c1e21d626db7d01f5c Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Thu, 4 Jul 2024 13:09:04 +1000 Subject: [PATCH 02/16] feat: add phala adapter and starter configs --- .github/workflows/xcm-tests.yml | 1 + scripts/configs/phala.yml | 26 +++ scripts/interlay-chopsticks-test.ts | 6 +- src/adapters/interlay.ts | 15 ++ src/adapters/phala.ts | 281 ++++++++++++++++++++++++++ src/configs/chains/polkadot-chains.ts | 8 + 6 files changed, 334 insertions(+), 3 deletions(-) create mode 100644 scripts/configs/phala.yml create mode 100644 src/adapters/phala.ts diff --git a/.github/workflows/xcm-tests.yml b/.github/workflows/xcm-tests.yml index b831c39..2acfbf9 100644 --- a/.github/workflows/xcm-tests.yml +++ b/.github/workflows/xcm-tests.yml @@ -72,6 +72,7 @@ jobs: -p scripts/configs/acala.yml \ -p scripts/configs/astar.yml \ -p scripts/configs/bifrost-polkadot.yml \ + -p scripts/configs/phala.yml \ &> log.txt & echo "Waiting for log to show chopsticks is ready..." tail -f log.txt | grep -q "Connected parachains" diff --git a/scripts/configs/phala.yml b/scripts/configs/phala.yml new file mode 100644 index 0000000..c5970bd --- /dev/null +++ b/scripts/configs/phala.yml @@ -0,0 +1,26 @@ +endpoint: + - wss://api.phala.network/ws + - wss://phala-rpc.dwellir.com +mock-signature-host: true + +import-storage: + System: + Account: + - + - + - 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY # Alice + - providers: 1 + data: + free: '1000000000000000' + Assets: + Account: + - + - + - 14 # IBTC + - 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY # Alice + - balance: '1000000000000000' + - + - + - 13 # INTR + - 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY # Alice + - balance: '1000000000000000' \ No newline at end of file diff --git a/scripts/interlay-chopsticks-test.ts b/scripts/interlay-chopsticks-test.ts index 8420765..0b554e5 100644 --- a/scripts/interlay-chopsticks-test.ts +++ b/scripts/interlay-chopsticks-test.ts @@ -6,8 +6,8 @@ import { InterlayAdapter } from "../src/adapters/interlay"; import { HydraAdapter } from "../src/adapters/hydradx"; import { AcalaAdapter } from "../src/adapters/acala"; import { AstarAdapter } from "../src/adapters/astar"; -// import { ParallelAdapter } from "../src/adapters/parallel"; import { BifrostPolkadotAdapter } from "../src/adapters/bifrost"; +import { PhalaAdapter } from "../src/adapters/phala"; import { BaseCrossChainAdapter } from "../src/base-chain-adapter"; import { RouterTestCase, runTestCasesAndExit } from "./chopsticks-test"; @@ -27,9 +27,9 @@ async function main(): Promise { hydra: { adapter: new HydraAdapter(), endpoints: ['ws://127.0.0.1:8001'] }, acala: { adapter: new AcalaAdapter(), endpoints: ['ws://127.0.0.1:8002'] }, astar: { adapter: new AstarAdapter(), endpoints: ['ws://127.0.0.1:8003'] }, - // parallel: { adapter: new ParallelAdapter(), endpoints: ['ws://127.0.0.1:8004'] }, bifrost_polkadot: { adapter: new BifrostPolkadotAdapter(), endpoints: ['ws://127.0.0.1:8004']}, - polkadot: { adapter: new PolkadotAdapter(), endpoints: ['ws://127.0.0.1:8005'] }, + phala: { adapter: new PhalaAdapter(), endpoints: ['ws://127.0.0.1:8005']}, + polkadot: { adapter: new PolkadotAdapter(), endpoints: ['ws://127.0.0.1:8006'] }, }; const skipCases: Partial[] = [ diff --git a/src/adapters/interlay.ts b/src/adapters/interlay.ts index 8367d78..86864a1 100644 --- a/src/adapters/interlay.ts +++ b/src/adapters/interlay.ts @@ -65,6 +65,21 @@ export const interlayRoutersConfig: Omit[] = [ // during chopsticks test: fee = 103 Add 10x margin xcm: { fee: { token: "IBTC", amount: "1030" }, weightLimit: DEST_WEIGHT }, }, + { + to: "phala", + token: "INTR", + xcm: { + // TODO: get chopsticks test data: fee = 6_535_947_712 Add 10x margin + fee: { token: "INTR", amount: "65359477120" }, + weightLimit: DEST_WEIGHT, + }, + }, + { + to: "phala", + token: "IBTC", + // TODO: get chopsticks test: fee = 103 Add 10x margin + xcm: { fee: { token: "IBTC", amount: "1030" }, weightLimit: DEST_WEIGHT }, + }, { to: "polkadot", token: "DOT", diff --git a/src/adapters/phala.ts b/src/adapters/phala.ts new file mode 100644 index 0000000..98facad --- /dev/null +++ b/src/adapters/phala.ts @@ -0,0 +1,281 @@ +import { Storage } from "@acala-network/sdk/utils/storage"; +import { AnyApi, FixedPointNumber as FN } from "@acala-network/sdk-core"; +import { combineLatest, map, Observable } from "rxjs"; + +import { SubmittableExtrinsic } from "@polkadot/api/types"; +import { DeriveBalancesAll } from "@polkadot/api-derive/balances/types"; +import { ISubmittableResult } from "@polkadot/types/types"; + +import { BalanceAdapter, BalanceAdapterConfigs } from "../balance-adapter"; +import { BaseCrossChainAdapter } from "../base-chain-adapter"; +import { ChainName, chains } from "../configs"; +import { ApiNotFound, CurrencyNotFound } from "../errors"; +import { + BalanceData, + CrossChainRouterConfigs, + CrossChainTransferParams, + ExtendedToken, +} from "../types"; + +const DEST_WEIGHT = "Unlimited"; +type TokenData = ExtendedToken & { toQuery: () => string }; + +export const phalaRoutersConfig: Omit[] = [ + { + to: "interlay", + token: "IBTC", + xcm: { + // during chopsticks test: fee = 71 Add 10x margin + fee: { token: "IBTC", amount: "710" }, + weightLimit: DEST_WEIGHT, + }, + }, + { + to: "interlay", + token: "INTR", + xcm: { + // during chopsticks test: fee = 21_660_472 Add 10x margin + fee: { token: "INTR", amount: "216604720" }, + weightLimit: DEST_WEIGHT, + }, + }, +]; + +export const phalaTokensConfig: Record< + string, + Record +> = { + phala: { + PHA: { + name: "PHA", + symbol: "PHA", + decimals: 12, + ed: "10000000000", + } as TokenData, + // ed confirmed via assets.asset() + IBTC: { + name: "IBTC", + symbol: "IBTC", + decimals: 8, + ed: "1,000,000", + toRaw: () => + "0x0001000000000000000000000000000000000000000000000000000000000000", + toQuery: () => "14", + }, + INTR: { + name: "INTR", + symbol: "INTR", + decimals: 10, + ed: "100,000,000", + toRaw: () => + "0x0002000000000000000000000000000000000000000000000000000000000000", + toQuery: () => "13", + }, + }, +}; + +// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types +const createBalanceStorages = (api: AnyApi) => { + return { + balances: (address: string) => + Storage.create({ + api, + path: "derive.balances.all", + params: [address], + }), + assets: (tokenId: string, address: string) => + Storage.create({ + api, + path: "query.assets.account", + params: [tokenId, address], + }), + }; +}; + +class PhalaBalanceAdapter extends BalanceAdapter { + private storages: ReturnType; + + constructor({ api, chain, tokens }: BalanceAdapterConfigs) { + super({ api, chain, tokens }); + this.storages = createBalanceStorages(api); + } + + public subscribeBalance( + token: string, + address: string + ): Observable { + const storage = this.storages.balances(address); + + if (token === this.nativeToken) { + return storage.observable.pipe( + map((data) => ({ + free: FN.fromInner(data.freeBalance.toString(), this.decimals), + locked: FN.fromInner(data.lockedBalance.toString(), this.decimals), + reserved: FN.fromInner( + data.reservedBalance.toString(), + this.decimals + ), + available: FN.fromInner( + data.availableBalance.toString(), + this.decimals + ), + })) + ); + } + + const tokenData: TokenData = this.getToken(token); + + if (!tokenData) { + throw new CurrencyNotFound(token); + } + + return this.storages.assets(tokenData.toQuery(), address).observable.pipe( + map((balance) => { + const amount = FN.fromInner( + balance.unwrapOrDefault()?.balance?.toString() || "0", + this.getToken(token).decimals + ); + + return { + free: amount, + locked: new FN(0), + reserved: new FN(0), + available: amount, + }; + }) + ); + } +} + +class BasePhalaAdapter extends BaseCrossChainAdapter { + private balanceAdapter?: PhalaBalanceAdapter; + + public override async setApi(api: AnyApi) { + this.api = api; + + await api.isReady; + + const chain = this.chain.id as ChainName; + + this.balanceAdapter = new PhalaBalanceAdapter({ + chain, + api, + tokens: phalaTokensConfig[chain], + }); + } + + public subscribeTokenBalance( + token: string, + address: string + ): Observable { + if (!this.balanceAdapter) { + throw new ApiNotFound(this.chain.id); + } + + return this.balanceAdapter.subscribeBalance(token, address); + } + + public subscribeMaxInput( + token: string, + address: string, + to: ChainName + ): Observable { + if (!this.balanceAdapter) { + throw new ApiNotFound(this.chain.id); + } + + return combineLatest({ + txFee: + token === this.balanceAdapter?.nativeToken + ? this.estimateTxFee({ + amount: FN.ZERO, + to, + token, + address, + signer: address, + }) + : "0", + balance: this.balanceAdapter + .subscribeBalance(token, address) + .pipe(map((i) => i.available)), + }).pipe( + map(({ balance, txFee }) => { + const tokenMeta = this.balanceAdapter?.getToken(token); + const feeFactor = 1.2; + const fee = FN.fromInner(txFee, tokenMeta?.decimals).mul( + new FN(feeFactor) + ); + + // always minus ed + return balance + .minus(fee) + .minus(FN.fromInner(tokenMeta?.ed || "0", tokenMeta?.decimals)); + }) + ); + } + + public createTx( + params: CrossChainTransferParams + ): + | SubmittableExtrinsic<"promise", ISubmittableResult> + | SubmittableExtrinsic<"rxjs", ISubmittableResult> { + if (!this.api) { + throw new ApiNotFound(this.chain.id); + } + + const { address, amount, to, token } = params; + + const accountId = this.api?.createType("AccountId32", address).toHex(); + const toChain = chains[to]; + + const dst = { + parents: 1, + interior: { + X2: [ + { Parachain: toChain.paraChainId }, + { AccountId32: { id: accountId } }, + ], + }, + }; + + let asset: any = { + id: { Concrete: { parents: 0, interior: "Here" } }, + fun: { Fungible: amount.toChainData() }, + }; + + const tokenData: TokenData = this.getToken(token); + + if (!tokenData) { + throw new CurrencyNotFound(token); + } + + if (token !== this.balanceAdapter?.nativeToken) { + asset = { + id: { + Concrete: { + parents: 1, + interior: { + X2: [ + { Parachain: toChain.paraChainId }, + { GeneralKey: { length: 2, data: tokenData.toRaw() } }, + ], + }, + }, + }, + fun: { Fungible: amount.toChainData() }, + }; + } + + return this.api.tx.xTransfer.transfer(asset, dst, undefined); + } +} + +export class PhalaAdapter extends BasePhalaAdapter { + constructor() { + super( + chains.phala, + phalaRoutersConfig, + phalaTokensConfig.parallel + ); + } +} diff --git a/src/configs/chains/polkadot-chains.ts b/src/configs/chains/polkadot-chains.ts index 3644774..08d412e 100644 --- a/src/configs/chains/polkadot-chains.ts +++ b/src/configs/chains/polkadot-chains.ts @@ -77,4 +77,12 @@ export const polkadotChains = { paraChainId: 2034, ss58Prefix: 63, }, + phala: { + id: "phala", + display: "Phala", + type: typeSubstrate, + icon: "", + paraChainId: 2035, + ss58Prefix: 30, + }, }; From f2c0ed95a4bfa28c73ac57f82b3b9d1722223776 Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Thu, 4 Jul 2024 13:17:19 +1000 Subject: [PATCH 03/16] fix: linting and typo --- src/adapters/phala.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/adapters/phala.ts b/src/adapters/phala.ts index 98facad..633aa01 100644 --- a/src/adapters/phala.ts +++ b/src/adapters/phala.ts @@ -41,10 +41,7 @@ export const phalaRoutersConfig: Omit[] = [ }, ]; -export const phalaTokensConfig: Record< - string, - Record -> = { +export const phalaTokensConfig: Record> = { phala: { PHA: { name: "PHA", @@ -272,10 +269,6 @@ class BasePhalaAdapter extends BaseCrossChainAdapter { export class PhalaAdapter extends BasePhalaAdapter { constructor() { - super( - chains.phala, - phalaRoutersConfig, - phalaTokensConfig.parallel - ); + super(chains.phala, phalaRoutersConfig, phalaTokensConfig.phala); } } From 5d8aa2f4e0af5bde3c38d76c297505cd9239910d Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Wed, 31 Jul 2024 10:04:15 +0200 Subject: [PATCH 04/16] feat: add PHA token --- src/adapters/interlay.ts | 11 +++++++++++ src/adapters/phala.ts | 13 +++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/adapters/interlay.ts b/src/adapters/interlay.ts index 86864a1..62bf42e 100644 --- a/src/adapters/interlay.ts +++ b/src/adapters/interlay.ts @@ -65,6 +65,15 @@ export const interlayRoutersConfig: Omit[] = [ // during chopsticks test: fee = 103 Add 10x margin xcm: { fee: { token: "IBTC", amount: "1030" }, weightLimit: DEST_WEIGHT }, }, + { + to: "phala", + token: "PHA", + xcm: { + // TODO: get chopsticks test data + fee: { token: "PHA", amount: "51200000000" }, + weightLimit: DEST_WEIGHT, + }, + }, { to: "phala", token: "INTR", @@ -247,6 +256,7 @@ export const interlayTokensConfig: Record< VDOT: { name: "VDOT", symbol: "VDOT", decimals: 10, ed: "0" }, BNC: { name: "BNC", symbol: "BNC", decimals: 12, ed: "0" }, HDX: { name: "HDX", symbol: "HDX", decimals: 12, ed: "0" }, + PHA: { name: "PHA", symbol: "PHA", decimals: 12, ed: "10000000000" }, }, kintsugi: { KBTC: { name: "KBTC", symbol: "KBTC", decimals: 8, ed: "0" }, @@ -276,6 +286,7 @@ const INTERLAY_SUPPORTED_TOKENS: Record = { BNC: { ForeignAsset: 11 }, USDC: { ForeignAsset: 12 }, HDX: { ForeignAsset: 13 }, + PHA: { ForeignAsset: 14 }, }; const getSupportedTokens = (chainname: string): Record => { diff --git a/src/adapters/phala.ts b/src/adapters/phala.ts index 633aa01..0ad9c7c 100644 --- a/src/adapters/phala.ts +++ b/src/adapters/phala.ts @@ -21,6 +21,15 @@ const DEST_WEIGHT = "Unlimited"; type TokenData = ExtendedToken & { toQuery: () => string }; export const phalaRoutersConfig: Omit[] = [ + { + to: "interlay", + token: "PHA", + xcm: { + // TODO: confirm via chopsticks test + fee: { token: "PHA", amount: "216604720" }, + weightLimit: DEST_WEIGHT, + }, + }, { to: "interlay", token: "IBTC", @@ -54,7 +63,7 @@ export const phalaTokensConfig: Record> = { name: "IBTC", symbol: "IBTC", decimals: 8, - ed: "1,000,000", + ed: "1000000", toRaw: () => "0x0001000000000000000000000000000000000000000000000000000000000000", toQuery: () => "14", @@ -63,7 +72,7 @@ export const phalaTokensConfig: Record> = { name: "INTR", symbol: "INTR", decimals: 10, - ed: "100,000,000", + ed: "100000000", toRaw: () => "0x0002000000000000000000000000000000000000000000000000000000000000", toQuery: () => "13", From fd14a685f3cb8b5377496bb4e2b56cba0c00efda Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Wed, 31 Jul 2024 10:23:33 +0200 Subject: [PATCH 05/16] chore: bump phala <-> interlay fees --- src/adapters/interlay.ts | 6 +++--- src/adapters/phala.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/adapters/interlay.ts b/src/adapters/interlay.ts index 62bf42e..6c61360 100644 --- a/src/adapters/interlay.ts +++ b/src/adapters/interlay.ts @@ -70,7 +70,7 @@ export const interlayRoutersConfig: Omit[] = [ token: "PHA", xcm: { // TODO: get chopsticks test data - fee: { token: "PHA", amount: "51200000000" }, + fee: { token: "PHA", amount: "101200000000" }, weightLimit: DEST_WEIGHT, }, }, @@ -86,8 +86,8 @@ export const interlayRoutersConfig: Omit[] = [ { to: "phala", token: "IBTC", - // TODO: get chopsticks test: fee = 103 Add 10x margin - xcm: { fee: { token: "IBTC", amount: "1030" }, weightLimit: DEST_WEIGHT }, + // chopsticks test: fee = 6429600. Add ~3x margin + xcm: { fee: { token: "IBTC", amount: "20000000" }, weightLimit: DEST_WEIGHT }, }, { to: "polkadot", diff --git a/src/adapters/phala.ts b/src/adapters/phala.ts index 0ad9c7c..4032f7c 100644 --- a/src/adapters/phala.ts +++ b/src/adapters/phala.ts @@ -26,7 +26,7 @@ export const phalaRoutersConfig: Omit[] = [ token: "PHA", xcm: { // TODO: confirm via chopsticks test - fee: { token: "PHA", amount: "216604720" }, + fee: { token: "PHA", amount: "1016604720" }, weightLimit: DEST_WEIGHT, }, }, From e22835d6f7793ad36d2c53d0675ce2eafe87a952 Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Wed, 31 Jul 2024 10:26:45 +0200 Subject: [PATCH 06/16] chore: linting --- src/adapters/interlay.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/adapters/interlay.ts b/src/adapters/interlay.ts index 6c61360..113a4fd 100644 --- a/src/adapters/interlay.ts +++ b/src/adapters/interlay.ts @@ -86,8 +86,11 @@ export const interlayRoutersConfig: Omit[] = [ { to: "phala", token: "IBTC", - // chopsticks test: fee = 6429600. Add ~3x margin - xcm: { fee: { token: "IBTC", amount: "20000000" }, weightLimit: DEST_WEIGHT }, + xcm: { + // chopsticks test: fee = 6429600. Add ~3x margin + fee: { token: "IBTC", amount: "20000000" }, + weightLimit: DEST_WEIGHT + }, }, { to: "polkadot", From d0ac65617b5778b700c88339fdc9bcbb8ea96c36 Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Wed, 31 Jul 2024 11:21:43 +0200 Subject: [PATCH 07/16] chore: adjust phala fees and chopsticks configs --- scripts/configs/interlay.yml | 5 +++++ src/adapters/interlay.ts | 10 +++++----- src/adapters/phala.ts | 12 ++++++------ src/bridge.spec.ts | 10 ++++++++-- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/scripts/configs/interlay.yml b/scripts/configs/interlay.yml index e77801a..98e486d 100644 --- a/scripts/configs/interlay.yml +++ b/scripts/configs/interlay.yml @@ -49,3 +49,8 @@ import-storage: - 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY # Alice, hdx - foreignAsset: 13 - free: '1000000000000000' + - + - + - 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY # Alice, pha + - foreignAsset: 14 + - free: '1000000000000000' diff --git a/src/adapters/interlay.ts b/src/adapters/interlay.ts index 113a4fd..361d57d 100644 --- a/src/adapters/interlay.ts +++ b/src/adapters/interlay.ts @@ -69,8 +69,8 @@ export const interlayRoutersConfig: Omit[] = [ to: "phala", token: "PHA", xcm: { - // TODO: get chopsticks test data - fee: { token: "PHA", amount: "101200000000" }, + // chopsticks test data: 2_810_000_095, use ~10x buffer + fee: { token: "PHA", amount: "28000000000" }, weightLimit: DEST_WEIGHT, }, }, @@ -78,8 +78,8 @@ export const interlayRoutersConfig: Omit[] = [ to: "phala", token: "INTR", xcm: { - // TODO: get chopsticks test data: fee = 6_535_947_712 Add 10x margin - fee: { token: "INTR", amount: "65359477120" }, + // chopsticks test data: fee = 642_960_000, use ~10x buffer + fee: { token: "INTR", amount: "6400000000" }, weightLimit: DEST_WEIGHT, }, }, @@ -87,7 +87,7 @@ export const interlayRoutersConfig: Omit[] = [ to: "phala", token: "IBTC", xcm: { - // chopsticks test: fee = 6429600. Add ~3x margin + // chopsticks test: fee = 6429600. Use ~3x buffer fee: { token: "IBTC", amount: "20000000" }, weightLimit: DEST_WEIGHT }, diff --git a/src/adapters/phala.ts b/src/adapters/phala.ts index 4032f7c..31cc0b4 100644 --- a/src/adapters/phala.ts +++ b/src/adapters/phala.ts @@ -25,8 +25,8 @@ export const phalaRoutersConfig: Omit[] = [ to: "interlay", token: "PHA", xcm: { - // TODO: confirm via chopsticks test - fee: { token: "PHA", amount: "1016604720" }, + // TODO: confirm via chopsticks test, best guess: 8_000_0000_000_000 + fee: { token: "PHA", amount: "5000000000000000" }, weightLimit: DEST_WEIGHT, }, }, @@ -34,8 +34,8 @@ export const phalaRoutersConfig: Omit[] = [ to: "interlay", token: "IBTC", xcm: { - // during chopsticks test: fee = 71 Add 10x margin - fee: { token: "IBTC", amount: "710" }, + // during chopsticks test: fee = 63, use ~10x buffer + fee: { token: "IBTC", amount: "600" }, weightLimit: DEST_WEIGHT, }, }, @@ -43,8 +43,8 @@ export const phalaRoutersConfig: Omit[] = [ to: "interlay", token: "INTR", xcm: { - // during chopsticks test: fee = 21_660_472 Add 10x margin - fee: { token: "INTR", amount: "216604720" }, + // during chopsticks test: fee = 19_213_457, use ~10x buffer + fee: { token: "INTR", amount: "200000000" }, weightLimit: DEST_WEIGHT, }, }, diff --git a/src/bridge.spec.ts b/src/bridge.spec.ts index 5e95738..a30ac44 100644 --- a/src/bridge.spec.ts +++ b/src/bridge.spec.ts @@ -13,6 +13,7 @@ import { AcalaAdapter, KaruraAdapter } from "./adapters/acala"; import { BifrostKusamaAdapter, BifrostPolkadotAdapter } from "./adapters/bifrost"; import { HydraAdapter } from "./adapters/hydradx"; import { AstarAdapter } from "./adapters/astar"; +import { PhalaAdapter } from "./adapters/phala"; describe.skip("Bridge sdk usage", () => { jest.setTimeout(30000); @@ -34,6 +35,7 @@ describe.skip("Bridge sdk usage", () => { hydra: new HydraAdapter(), parallel: new ParallelAdapter(), astar: new AstarAdapter(), + phala: new PhalaAdapter(), }; const bridge = new Bridge({ @@ -183,6 +185,9 @@ describe.skip("Bridge sdk usage", () => { // interlay // printBidirectionalTxs("interlay", "polkadot", "DOT"); + printBidirectionalTxs("interlay", "phala", "PHA"); + printBidirectionalTxs("interlay", "phala", "INTR"); + printBidirectionalTxs("interlay", "phala", "IBTC"); // printBidirectionalTxs("interlay", "statemint", "USDT"); // printBidirectionalTxs("interlay", "hydra", "IBTC"); // printBidirectionalTxs("interlay", "hydra", "INTR"); @@ -192,11 +197,11 @@ describe.skip("Bridge sdk usage", () => { // printBidirectionalTxs("interlay", "parallel", "IBTC"); // printBidirectionalTxs("interlay", "astar", "INTR"); // printBidirectionalTxs("interlay", "astar", "IBTC"); - printBidirectionalTxs("interlay", "bifrost_polkadot", "VDOT"); + // printBidirectionalTxs("interlay", "bifrost_polkadot", "VDOT"); // printBidirectionalTxs("polkadot", "statemint", "DOT"); }); - test("5. getNativeToken should work", () => { + test.skip("5. getNativeToken should work", () => { const testCases: [ChainName, String][] = [ // kusama network ["kusama", "KSM"], @@ -213,6 +218,7 @@ describe.skip("Bridge sdk usage", () => { ["parallel", "PARA"], ["bifrost_polkadot", "BNC"], ["statemint", "DOT"], + ["phala", "PHA"], ]; for (const [chainName, expectedNativeToken] of testCases) { From 2aec10670b7620a55f34ddb8d0ad2c1a547d8ba8 Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Wed, 31 Jul 2024 17:17:40 +0200 Subject: [PATCH 08/16] fix: adjust pha fee estimate --- src/adapters/phala.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/adapters/phala.ts b/src/adapters/phala.ts index 31cc0b4..7a57c87 100644 --- a/src/adapters/phala.ts +++ b/src/adapters/phala.ts @@ -25,8 +25,8 @@ export const phalaRoutersConfig: Omit[] = [ to: "interlay", token: "PHA", xcm: { - // TODO: confirm via chopsticks test, best guess: 8_000_0000_000_000 - fee: { token: "PHA", amount: "5000000000000000" }, + // chopsticks test: fees 78_600_800_000_000, use a small buffer (too expensive to use a standard buffer) + fee: { token: "PHA", amount: "100000000000000" }, weightLimit: DEST_WEIGHT, }, }, From de8b81c5dc341dfbfed032f128ab90891c8eb060 Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Wed, 31 Jul 2024 17:20:51 +0200 Subject: [PATCH 09/16] chore: lint --- src/adapters/interlay.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adapters/interlay.ts b/src/adapters/interlay.ts index 361d57d..3ee440f 100644 --- a/src/adapters/interlay.ts +++ b/src/adapters/interlay.ts @@ -89,7 +89,7 @@ export const interlayRoutersConfig: Omit[] = [ xcm: { // chopsticks test: fee = 6429600. Use ~3x buffer fee: { token: "IBTC", amount: "20000000" }, - weightLimit: DEST_WEIGHT + weightLimit: DEST_WEIGHT, }, }, { From da35462ec4ada5fd4a2fb2c5f1adaee740932dfc Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Wed, 31 Jul 2024 17:39:58 +0200 Subject: [PATCH 10/16] tests: fund sibl:2032 in phala chopsticks config --- scripts/configs/phala.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scripts/configs/phala.yml b/scripts/configs/phala.yml index c5970bd..17fa61e 100644 --- a/scripts/configs/phala.yml +++ b/scripts/configs/phala.yml @@ -12,6 +12,12 @@ import-storage: - providers: 1 data: free: '1000000000000000' + - + - + - 5Eg2fntQS1bgCgPtXQ9Ysip6RUQkQJEMZqZ9u9qX6fcnhB4H # sibl 2032 (interlay sov) + - providers: 1 + data: + free: '1000000000000000' Assets: Account: - From 2cfc67baafcef7f69d301362a54200ee842309c1 Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Thu, 1 Aug 2024 06:59:41 +0200 Subject: [PATCH 11/16] chore: adjust pha fee estimate --- src/adapters/interlay.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/adapters/interlay.ts b/src/adapters/interlay.ts index 3ee440f..81aa2bb 100644 --- a/src/adapters/interlay.ts +++ b/src/adapters/interlay.ts @@ -69,8 +69,8 @@ export const interlayRoutersConfig: Omit[] = [ to: "phala", token: "PHA", xcm: { - // chopsticks test data: 2_810_000_095, use ~10x buffer - fee: { token: "PHA", amount: "28000000000" }, + // chopsticks test data: 64_296_000_000, use ~2.5x buffer + fee: { token: "PHA", amount: "150000000000" }, weightLimit: DEST_WEIGHT, }, }, From 4f21d68807dbf4424fc8c6f852d50d3c206cd483 Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Thu, 1 Aug 2024 09:15:24 +0200 Subject: [PATCH 12/16] chore: tweak fee estimate, adjust test setup config --- scripts/configs/phala.yml | 4 ++-- src/adapters/phala.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/configs/phala.yml b/scripts/configs/phala.yml index 17fa61e..982c800 100644 --- a/scripts/configs/phala.yml +++ b/scripts/configs/phala.yml @@ -11,13 +11,13 @@ import-storage: - 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY # Alice - providers: 1 data: - free: '1000000000000000' + free: '100000000000000000' - - - 5Eg2fntQS1bgCgPtXQ9Ysip6RUQkQJEMZqZ9u9qX6fcnhB4H # sibl 2032 (interlay sov) - providers: 1 data: - free: '1000000000000000' + free: '100000000000000000' Assets: Account: - diff --git a/src/adapters/phala.ts b/src/adapters/phala.ts index 7a57c87..a87ad1d 100644 --- a/src/adapters/phala.ts +++ b/src/adapters/phala.ts @@ -26,7 +26,7 @@ export const phalaRoutersConfig: Omit[] = [ token: "PHA", xcm: { // chopsticks test: fees 78_600_800_000_000, use a small buffer (too expensive to use a standard buffer) - fee: { token: "PHA", amount: "100000000000000" }, + fee: { token: "PHA", amount: "120000000000000" }, weightLimit: DEST_WEIGHT, }, }, From a3120b9d222e5025bbbc85181484d4e3c6a7ff54 Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Thu, 1 Aug 2024 09:32:03 +0200 Subject: [PATCH 13/16] tests: warning is not test failure, adjust check for pha token --- scripts/chopsticks-test.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/chopsticks-test.ts b/scripts/chopsticks-test.ts index c3d1dd1..ec258b2 100644 --- a/scripts/chopsticks-test.ts +++ b/scripts/chopsticks-test.ts @@ -115,8 +115,12 @@ async function checkTransfer(fromChain: ChainName, toChain: ChainName, token: st let feeOverestimationFactor = feeBudget.div(actualFee); let actualFeePlancks = actualFee._getInner(); // console.log(`Fee budget: ${feeBudget}, actual fee: ${actualFee} (= ${actualFeePlancks} plank), marginFactor: ${feeOverestimationFactor}`); - if (feeOverestimationFactor.toNumber() <= 2) { - let message = `Fees need to be increased in config. The actual fees are ${actualFee} (= ${actualFeePlancks} plank). Fee overestimation factor was ${feeOverestimationFactor} - we want at least 2.0`; + + // expect 2x actual fees, except for PHA to interlay where fees are very high. + const minimumFeeOverestimationFactor = (token === "PHA" && toChain === "interlay") ? 1.1 : 2; + + if (feeOverestimationFactor.toNumber() < minimumFeeOverestimationFactor) { + let message = `Fees need to be increased in config. The actual fees are ${actualFee} (= ${actualFeePlancks} plank). Fee overestimation factor was ${feeOverestimationFactor} - we want at least ${minimumFeeOverestimationFactor}`; // if below 1, this is an error. if (feeOverestimationFactor.toNumber() < 1) { @@ -171,7 +175,7 @@ async function retryCheckTransfer( ): Promise>> { const result = await checkTransfer(fromChain, toChain, token, bridge); - if (result.result === ResultCode.OK) { + if (result.result !== ResultCode.FAIL) { return result; } @@ -301,10 +305,10 @@ export async function runTestCasesAndExit( case ResultCode.WARN: console.log(icon, 'action required'); problematicTestStrings.forEach((logMessage) => console.log(logMessage)); - process.exit(-1); + process.exit(0); case ResultCode.FAIL: console.log(icon, 'some channels FAILED'); problematicTestStrings.forEach((logMessage) => console.log(logMessage)); - process.exit(-2); + process.exit(-1); } } From 9557aa0db95015f80db8b734a62e8e833cbed385 Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Thu, 1 Aug 2024 09:42:03 +0200 Subject: [PATCH 14/16] test: disable parallel heiko tests - wss endpoint not responding anymore --- .github/workflows/xcm-tests.yml | 1 - scripts/kintsugi-chopsticks-test.ts | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/xcm-tests.yml b/.github/workflows/xcm-tests.yml index 2acfbf9..193a3df 100644 --- a/.github/workflows/xcm-tests.yml +++ b/.github/workflows/xcm-tests.yml @@ -26,7 +26,6 @@ jobs: -r scripts/configs/kusama.yml \ -p scripts/configs/kintsugi.yml \ -p scripts/configs/karura.yml \ - -p scripts/configs/parallel-heiko.yml \ -p scripts/configs/bifrost.yml \ &> log.txt & echo "Waiting for log to show chopsticks is ready..." diff --git a/scripts/kintsugi-chopsticks-test.ts b/scripts/kintsugi-chopsticks-test.ts index 85f7af8..a6537eb 100644 --- a/scripts/kintsugi-chopsticks-test.ts +++ b/scripts/kintsugi-chopsticks-test.ts @@ -23,9 +23,9 @@ async function main(): Promise { // relaychain gets its port last after all parachains. kintsugi: { adapter: new KintsugiAdapter(), endpoints: ['ws://127.0.0.1:8000'] }, karura: { adapter: new KaruraAdapter(), endpoints: ['ws://127.0.0.1:8001'] }, - heiko: { adapter: new HeikoAdapter(), endpoints: ['ws://127.0.0.1:8002'] }, - bifrost: { adapter: new BifrostKusamaAdapter(), endpoints: ['ws://127.0.0.1:8003'] }, - kusama: { adapter: new KusamaAdapter(), endpoints: ['ws://127.0.0.1:8004'] }, + // heiko: { adapter: new HeikoAdapter(), endpoints: ['ws://127.0.0.1:8002'] }, + bifrost: { adapter: new BifrostKusamaAdapter(), endpoints: ['ws://127.0.0.1:8002'] }, + kusama: { adapter: new KusamaAdapter(), endpoints: ['ws://127.0.0.1:8003'] }, }; const skipCases: Partial[] = [ From 1caf2ee9ad9d995c3f1afc8700528522079c5586 Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Thu, 1 Aug 2024 09:44:25 +0200 Subject: [PATCH 15/16] chore: comment unused import --- scripts/kintsugi-chopsticks-test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/kintsugi-chopsticks-test.ts b/scripts/kintsugi-chopsticks-test.ts index a6537eb..c928915 100644 --- a/scripts/kintsugi-chopsticks-test.ts +++ b/scripts/kintsugi-chopsticks-test.ts @@ -4,7 +4,7 @@ import { KaruraAdapter } from "../src/adapters/acala"; import { BifrostKusamaAdapter } from "../src/adapters/bifrost"; import { KintsugiAdapter } from "../src/adapters/interlay"; -import { HeikoAdapter } from "../src/adapters/parallel"; +// import { HeikoAdapter } from "../src/adapters/parallel"; import { KusamaAdapter } from "../src/adapters/polkadot"; import { BaseCrossChainAdapter } from "../src/base-chain-adapter"; import { RouterTestCase, runTestCasesAndExit } from "./chopsticks-test"; From a14fbf07fa1f89a13dcf146d8074b1f91272f21c Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Thu, 1 Aug 2024 09:56:01 +0200 Subject: [PATCH 16/16] chore: bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 07eb971..358529b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@interlay/bridge", - "version": "0.6.0", + "version": "0.6.1", "description": "polkawallet bridge sdk", "main": "build/index.js", "typings": "build/index.d.ts",