Skip to content

Commit

Permalink
Merge pull request #127 from bvotteler/chore-update-astar-adapter
Browse files Browse the repository at this point in the history
Chore: update Astar adapter
  • Loading branch information
sander2 authored Jan 10, 2024
2 parents 562ea33 + d82f659 commit 342cbb9
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 150 deletions.
1 change: 1 addition & 0 deletions .github/workflows/xcm-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 &
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
12 changes: 5 additions & 7 deletions scripts/interlay-chopsticks-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -27,16 +28,13 @@ async function main(): Promise<void> {
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<RouterTestCase>[] = [
{from: "astar"},
{to: "astar"},
];

await runTestCasesAndExit(adaptersEndpoints, filterCases);
Expand Down
216 changes: 85 additions & 131 deletions src/adapters/astar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CrossChainRouterConfigs, "from">[] = [
{
Expand All @@ -42,20 +40,35 @@ export const astarRoutersConfig: Omit<CrossChainRouterConfigs, "from">[] = [
},
];

export const astarTokensConfig: Record<string, Record<string, BasicToken>> = {
export const astarTokensConfig: Record<string, Record<string, TokenData>> = {
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<string, string> = {
// to interlay
IBTC: "18446744073709551620",
INTR: "18446744073709551621",
};

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
const createBalanceStorages = (api: AnyApi) => {
return {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
} as any,
"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<string, string> = {
// 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"
},
} as any,
"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);
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/adapters/hydradx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ChainName, chains } from "../configs";
import { ApiNotFound, CurrencyNotFound } from "../errors";
import {
BalanceData,
ExpandToken,
ExtendedToken,
CrossChainRouterConfigs,
CrossChainTransferParams,
} from "../types";
Expand Down Expand Up @@ -41,27 +41,27 @@ export const hydraRoutersConfig: Omit<CrossChainRouterConfigs, "from">[] = [
},
];

export const hydraTokensConfig: Record<string, ExpandToken> = {
export const hydraTokensConfig: Record<string, ExtendedToken> = {
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,
},
};

Expand Down Expand Up @@ -114,9 +114,9 @@ class HydradxBalanceAdapter extends BalanceAdapter {
);
}

const token = this.getToken<ExpandToken>(tokenName);
const token = this.getToken<ExtendedToken>(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",
Expand Down Expand Up @@ -210,13 +210,13 @@ class BaseHydradxAdapter extends BaseCrossChainAdapter {

const { address, amount, to, token: tokenName } = params;

const token = this.getToken<ExpandToken>(tokenName);
const token = this.getToken<ExtendedToken>(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();

Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 342cbb9

Please sign in to comment.