From 4b6a406b42f0afd1c3376092545e54b982a01101 Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Mon, 13 Nov 2023 13:15:51 +0200 Subject: [PATCH 1/5] chore: replace ExpandToken type with ExtendedToken --- src/adapters/hydradx.ts | 18 +++++++++--------- src/types.ts | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/adapters/hydradx.ts b/src/adapters/hydradx.ts index 5ea12c4d..18dfe07f 100644 --- a/src/adapters/hydradx.ts +++ b/src/adapters/hydradx.ts @@ -12,7 +12,7 @@ import { ChainName, chains } from "../configs"; import { ApiNotFound, CurrencyNotFound } from "../errors"; import { BalanceData, - ExpandToken, + ExtendedToken, CrossChainRouterConfigs, CrossChainTransferParams, } from "../types"; @@ -41,27 +41,27 @@ export const hydraRoutersConfig: Omit[] = [ }, ]; -export const hydraTokensConfig: Record = { +export const hydraTokensConfig: Record = { HDX: { name: "HDX", symbol: "HDX", decimals: 12, ed: "1000000000000", - toChainData: () => 0, + toRaw: () => 0, }, IBTC: { name: "IBTC", symbol: "IBTC", decimals: 8, ed: "36", - toChainData: () => 11, + toRaw: () => 11, }, INTR: { name: "INTR", symbol: "INTR", decimals: 10, ed: "6164274209", - toChainData: () => 17, + toRaw: () => 17, }, }; @@ -114,9 +114,9 @@ class HydradxBalanceAdapter extends BalanceAdapter { ); } - const token = this.getToken(tokenName); + const token = this.getToken(tokenName); - return this.storages.assets(token.toChainData(), address).observable.pipe( + return this.storages.assets(token.toRaw(), address).observable.pipe( map((balance) => { const amount = FN.fromInner( balance.free?.toString() || "0", @@ -210,13 +210,13 @@ class BaseHydradxAdapter extends BaseCrossChainAdapter { const { address, amount, to, token: tokenName } = params; - const token = this.getToken(tokenName); + const token = this.getToken(tokenName); if (!token) { throw new CurrencyNotFound(token); } - const tokenId = token.toChainData(); + const tokenId = token.toRaw(); const toChain = chains[to]; const accountId = this.api.createType("AccountId32", address).toHex(); diff --git a/src/types.ts b/src/types.ts index 50bb2d51..780c899e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -29,8 +29,8 @@ export interface BasicToken { ed: string; } -export interface ExpandToken extends BasicToken { - toChainData: () => any; +export interface ExtendedToken extends BasicToken { + toRaw: () => any; } export interface CrossChainRouterConfigs { From fe381e78d4475a121c9de4e6cb29954b6e4d919a Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Mon, 13 Nov 2023 14:40:00 +0200 Subject: [PATCH 2/5] chore: change astar adapter to use xtokens.transferMultiasset --- src/adapters/astar.ts | 216 +++++++++++++++++------------------------- 1 file changed, 85 insertions(+), 131 deletions(-) diff --git a/src/adapters/astar.ts b/src/adapters/astar.ts index fb79c0a7..4506d81b 100644 --- a/src/adapters/astar.ts +++ b/src/adapters/astar.ts @@ -12,14 +12,12 @@ import { ChainName, chains } from "../configs"; import { ApiNotFound, CurrencyNotFound } from "../errors"; import { BalanceData, - BasicToken, CrossChainRouterConfigs, CrossChainTransferParams, + ExtendedToken, } from "../types"; -import { - XCM_V3_GENERAL_KEY_DATA_BYTES, - supportsV0V1Multilocation, -} from "../utils/xcm-versioned-multilocation-check"; + +type TokenData = ExtendedToken & { toQuery: () => string }; export const astarRoutersConfig: Omit[] = [ { @@ -42,20 +40,35 @@ export const astarRoutersConfig: Omit[] = [ }, ]; -export const astarTokensConfig: Record> = { +export const astarTokensConfig: Record> = { astar: { - ASTR: { name: "ASTR", symbol: "ASTR", decimals: 18, ed: "1000000" }, - IBTC: { name: "IBTC", symbol: "IBTC", decimals: 8, ed: "1" }, - INTR: { name: "INTR", symbol: "INTR", decimals: 10, ed: "1" }, + ASTR: { + name: "ASTR", + symbol: "ASTR", + decimals: 18, + ed: "1000000", + } as TokenData, + IBTC: { + name: "IBTC", + symbol: "IBTC", + decimals: 8, + ed: "1", + toRaw: () => + "0x0001000000000000000000000000000000000000000000000000000000000000", + toQuery: () => "18446744073709551620", + }, + INTR: { + name: "INTR", + symbol: "INTR", + decimals: 10, + ed: "1", + toRaw: () => + "0x0002000000000000000000000000000000000000000000000000000000000000", + toQuery: () => "18446744073709551621", + }, }, }; -const SUPPORTED_TOKENS: Record = { - // to interlay - IBTC: "18446744073709551620", - INTR: "18446744073709551621", -}; - // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types const createBalanceStorages = (api: AnyApi) => { return { @@ -105,13 +118,13 @@ class AstarBalanceAdapter extends BalanceAdapter { ); } - const tokenId = SUPPORTED_TOKENS[token]; + const tokenData: TokenData = this.getToken(token); - if (tokenId === undefined) { + if (!tokenData) { throw new CurrencyNotFound(token); } - return this.storages.assets(tokenId, address).observable.pipe( + return this.storages.assets(tokenData.toQuery(), address).observable.pipe( map((balance) => { const amount = FN.fromInner( balance.unwrapOrDefault()?.balance?.toString() || "0", @@ -201,135 +214,76 @@ class BaseAstarAdapter extends BaseCrossChainAdapter { ): | SubmittableExtrinsic<"promise", ISubmittableResult> | SubmittableExtrinsic<"rxjs", ISubmittableResult> { - if (this.api === undefined) { - throw new ApiNotFound(this.chain.id); - } + if (!this.api) throw new ApiNotFound(this.chain.id); const { address, amount, to, token } = params; - const toChain = chains[to]; + const toChain = chains[to]; const accountId = this.api?.createType("AccountId32", address).toHex(); - const supportsV1 = supportsV0V1Multilocation(this.api); - - let [dst, acc, ass] = supportsV1 - ? [ - { - V1: { - parents: 1, - interior: { X1: { Parachain: toChain.paraChainId } }, + if (token === this.balanceAdapter?.nativeToken) { + return this.api.tx.xtokens.transferMultiasset( + { + V3: { + id: { Concrete: { parents: 0, interior: "Here" } }, + fun: { Fungible: amount.toChainData() }, + }, + }, + { + V3: { + parents: 1, + interior: { + X2: [ + { Parachain: toChain.paraChainId }, + { + AccountId32: { + id: accountId, + }, + }, + ], }, }, - { - V1: { - parents: 0, + }, + "Unlimited" + ); + } + + const tokenData: TokenData = this.getToken(params.token); + + return this.api.tx.xtokens.transferMultiasset( + { + V3: { + fun: { Fungible: amount.toChainData() }, + id: { + Concrete: { + parents: 1, interior: { - X1: { AccountId32: { id: accountId, network: "Any" } }, + X2: [ + { Parachain: toChain.paraChainId }, + { GeneralKey: { length: 2, data: tokenData.toRaw() } }, + ], }, }, }, - { - V1: [ + }, + }, + { + V3: { + parents: 1, + interior: { + X2: [ + { Parachain: toChain.paraChainId }, { - id: { Concrete: { parents: 0, interior: "Here" } }, - fun: { Fungible: amount.toChainData() }, + AccountId32: { + id: accountId, + }, }, ], }, - ] - : [ - { - V3: { - parents: 1, - interior: { X1: { Parachain: toChain.paraChainId } }, - }, - } as any, - { - V3: { - parents: 0, - interior: { X1: { AccountId32: { id: accountId } } }, - }, - } as any, - { - V3: [ - { - id: { Concrete: { parents: 0, interior: "Here" } }, - fun: { Fungible: amount.toChainData() }, - }, - ], - } as any, - ]; - - if (token === this.balanceAdapter?.nativeToken) { - return this.api?.tx.polkadotXcm.reserveTransferAssets(dst, acc, ass, 0); - } - - const tokenIds: Record = { - // ids taken from https://github.com/colorfulnotion/xcm-global-registry/blob/main/assets/polkadot/polkadot_2000_assets.json - // to interlay - IBTC: "0x0001", - INTR: "0x0002", - }; - - const tokenId = tokenIds[token]; - - if (tokenId === undefined) { - throw new CurrencyNotFound(token); - } - - // count bytes without the leading '0x' - const tokenIdByteCount = tokenId.replace("0x", "").length / 2; - const paddedTokenId = tokenId.padEnd( - // pad for a total length of required bytes plus '0x' - 2 * XCM_V3_GENERAL_KEY_DATA_BYTES + 2, - "00" + }, + }, + "Unlimited" ); - - // not native token: reconstruct asset argument - ass = supportsV1 - ? { - V1: [ - { - id: { - Concrete: { - parents: 1, - interior: { - X2: [ - { Parachain: toChain.paraChainId }, - { GeneralKey: tokenId }, - ], - }, - }, - }, - fun: { Fungible: amount.toChainData() }, - }, - ], - } - : { - V3: [ - { - id: { - Concrete: { - parents: 1, - interior: { - X2: [ - { Parachain: toChain.paraChainId }, - { - GeneralKey: { - length: tokenIdByteCount, - data: paddedTokenId, - }, - }, - ], - }, - }, - }, - fun: { Fungible: amount.toChainData() }, - }, - ], - }; - - return this.api?.tx.polkadotXcm.reserveWithdrawAssets(dst, acc, ass, 0); } } From 8dc81e372286e8ca8f4511652e33ab335b5cb05a Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Thu, 4 Jan 2024 12:11:27 +0200 Subject: [PATCH 3/5] fix: typo in call to xTokens.transferMultiasset --- src/adapters/astar.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/adapters/astar.ts b/src/adapters/astar.ts index 4506d81b..8218b1cf 100644 --- a/src/adapters/astar.ts +++ b/src/adapters/astar.ts @@ -222,7 +222,7 @@ class BaseAstarAdapter extends BaseCrossChainAdapter { const accountId = this.api?.createType("AccountId32", address).toHex(); if (token === this.balanceAdapter?.nativeToken) { - return this.api.tx.xtokens.transferMultiasset( + return this.api.tx.xTokens.transferMultiasset( { V3: { id: { Concrete: { parents: 0, interior: "Here" } }, @@ -243,14 +243,14 @@ class BaseAstarAdapter extends BaseCrossChainAdapter { ], }, }, - }, + } as any, "Unlimited" ); } const tokenData: TokenData = this.getToken(params.token); - return this.api.tx.xtokens.transferMultiasset( + return this.api.tx.xTokens.transferMultiasset( { V3: { fun: { Fungible: amount.toChainData() }, @@ -281,7 +281,7 @@ class BaseAstarAdapter extends BaseCrossChainAdapter { ], }, }, - }, + } as any, "Unlimited" ); } From f6a2062445baa3769cc20e4eee25e0bcc67aec55 Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Thu, 4 Jan 2024 12:11:42 +0200 Subject: [PATCH 4/5] test: reenable chopsticks tests for Astar --- .github/workflows/xcm-tests.yml | 1 + scripts/interlay-chopsticks-test.ts | 12 +++++------- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/xcm-tests.yml b/.github/workflows/xcm-tests.yml index 31941958..72ff8fc3 100644 --- a/.github/workflows/xcm-tests.yml +++ b/.github/workflows/xcm-tests.yml @@ -71,6 +71,7 @@ jobs: -p scripts/configs/statemint.yml \ -p scripts/configs/hydradx.yml \ -p scripts/configs/acala.yml \ + -p scripts/configs/astar.yml \ -p scripts/configs/parallel.yml \ -p scripts/configs/bifrost-polkadot.yml \ &> log.txt & diff --git a/scripts/interlay-chopsticks-test.ts b/scripts/interlay-chopsticks-test.ts index e40f9a2c..69a45577 100644 --- a/scripts/interlay-chopsticks-test.ts +++ b/scripts/interlay-chopsticks-test.ts @@ -6,6 +6,7 @@ import { InterlayAdapter } from "../src/adapters/interlay"; import { StatemintAdapter } from "../src/adapters/statemint"; 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 { BaseCrossChainAdapter } from "../src/base-chain-adapter"; @@ -27,16 +28,13 @@ async function main(): Promise { statemint: { adapter: new StatemintAdapter(), endpoints: ['ws://127.0.0.1:8001'] }, hydra: { adapter: new HydraAdapter(), endpoints: ['ws://127.0.0.1:8002'] }, acala: { adapter: new AcalaAdapter(), endpoints: ['ws://127.0.0.1:8003'] }, - // disable astar - rpc currently is too fragile for use in recurring tests - // astar: { adapter: new AstarAdapter(), endpoints: ['ws://127.0.0.1:8004'] }, - parallel: { adapter: new ParallelAdapter(), endpoints: ['ws://127.0.0.1:8004'] }, - bifrost_polkadot: { adapter: new BifrostPolkadotAdapter(), endpoints: ['ws://127.0.0.1:8005']}, - polkadot: { adapter: new PolkadotAdapter(), endpoints: ['ws://127.0.0.1:8006'] }, + astar: { adapter: new AstarAdapter(), endpoints: ['ws://127.0.0.1:8004'] }, + parallel: { adapter: new ParallelAdapter(), endpoints: ['ws://127.0.0.1:8005'] }, + bifrost_polkadot: { adapter: new BifrostPolkadotAdapter(), endpoints: ['ws://127.0.0.1:8006']}, + polkadot: { adapter: new PolkadotAdapter(), endpoints: ['ws://127.0.0.1:8007'] }, }; const filterCases: Partial[] = [ - {from: "astar"}, - {to: "astar"}, ]; await runTestCasesAndExit(adaptersEndpoints, filterCases); From d82f6595b30598a9a23155f9a8144b556c3d1265 Mon Sep 17 00:00:00 2001 From: Brendon Votteler Date: Thu, 4 Jan 2024 12:12:04 +0200 Subject: [PATCH 5/5] chore: bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 86857cb0..b927e2c3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@interlay/bridge", - "version": "0.4.1", + "version": "0.4.2", "description": "polkawallet bridge sdk", "main": "build/index.js", "typings": "build/index.d.ts",