Skip to content

Commit

Permalink
feat: update moonbeam transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
qwer951123 committed Nov 25, 2024
1 parent bc520fe commit 5c1614b
Show file tree
Hide file tree
Showing 7 changed files with 587 additions and 531 deletions.
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polkawallet/bridge",
"version": "0.1.7-12",
"version": "0.1.7-13",
"description": "polkawallet bridge sdk",
"main": "index.js",
"types": "index.d.ts",
Expand All @@ -25,23 +25,24 @@
},
"peerDependencies": {
"@acala-network/api": "^5",
"@polkadot/api": "^12",
"@polkadot/api": "^14",
"ethers": "^5"
},
"resolutions": {
"@acala-network/api": "^5.1.1",
"@acala-network/sdk": "^4.1.9-13",
"@acala-network/sdk-core": "^4.1.9-13",
"@polkadot/api": "^12.0.2",
"@polkadot/types": "^12.0.2"
"@polkadot/api": "^14.0.1",
"@polkadot/types": "^14.0.1",
"@polkadot/util": "^13.2.3"
},
"dependencies": {
"@acala-network/api": "^5",
"@acala-network/sdk": "^4.1.9-13",
"@acala-network/sdk-core": "^4.1.9-13",
"@polkadot/api": "^12.0.2",
"@polkadot/api": "^14.0.1",
"@polkadot/apps-config": "^0.133.1",
"@polkadot/types": "^12.0.2",
"@polkadot/types": "^14.0.1",
"axios": "^0.27.2",
"ethers": "^5",
"lodash": "^4.17.20"
Expand Down
42 changes: 36 additions & 6 deletions src/adapters/acala/acala-configs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createRouteConfigs } from "../../utils";
import { BasicToken } from "../../types";
import { BasicToken, ExtendedToken } from "../../types";

export const acalaRouteConfigs = createRouteConfigs("acala", [
{
Expand Down Expand Up @@ -186,17 +186,40 @@ export const acalaRouteConfigs = createRouteConfigs("acala", [
},
]);

export const acalaTokensConfig: Record<string, BasicToken> = {
ACA: { name: "ACA", symbol: "ACA", decimals: 12, ed: "100000000000" },
AUSD: { name: "AUSD", symbol: "AUSD", decimals: 12, ed: "100000000000" },
LDOT: { name: "LDOT", symbol: "LDOT", decimals: 10, ed: "500000000" },
export const acalaTokensConfig: Record<string, ExtendedToken> = {
ACA: {
name: "ACA",
symbol: "ACA",
decimals: 12,
ed: "100000000000",
toRaw: () =>
"0x0000000000000000000000000000000000000000000000000000000000000000",
},
AUSD: {
name: "AUSD",
symbol: "AUSD",
decimals: 12,
ed: "100000000000",
toRaw: () =>
"0x0001000000000000000000000000000000000000000000000000000000000000",
},
LDOT: {
name: "LDOT",
symbol: "LDOT",
decimals: 10,
ed: "500000000",
toRaw: () =>
"0x0003000000000000000000000000000000000000000000000000000000000000",
},
INTR: { name: "INTR", symbol: "INTR", decimals: 10, ed: "1000000000" },
IBTC: { name: "IBTC", symbol: "IBTC", decimals: 8, ed: "100" },
GLMR: {
name: "GLMR",
symbol: "GLMR",
decimals: 18,
ed: "100000000000000000",
toRaw: () =>
"0x0500000000000000000000000000000000000000000000000000000000000000",
},
PARA: { name: "PARA", symbol: "PARA", decimals: 12, ed: "100000000000" },
ASTR: {
Expand All @@ -205,7 +228,14 @@ export const acalaTokensConfig: Record<string, BasicToken> = {
decimals: 18,
ed: "100000000000000000",
},
DOT: { name: "DOT", symbol: "DOT", decimals: 10, ed: "100000000" },
DOT: {
name: "DOT",
symbol: "DOT",
decimals: 10,
ed: "100000000",
toRaw: () =>
"0x0002000000000000000000000000000000000000000000000000000000000000",
},
DAI: {
name: "DAI",
symbol: "DAI",
Expand Down
82 changes: 82 additions & 0 deletions src/adapters/moonbeam.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Bridge } from "../bridge";
import { logFormatedRoute, formateRouteLogLine } from "../utils/unit-test";
import { FixedPointNumber } from "@acala-network/sdk-core";
import { ApiPromise, WsProvider } from "@polkadot/api";
import { MoonbeamAdapter } from "./moonbeam";
import { AcalaAdapter } from "./acala/acala";

describe.skip("moonbeam adapter should work", () => {
jest.setTimeout(300000);

let bridge: Bridge;
const outputSummary: string[] = [];

beforeAll(async () => {
const moonbeam = new MoonbeamAdapter();

const moonbeamApi = new ApiPromise({ provider: new WsProvider("wss://moonbeam-rpc.dwellir.com") });

await moonbeam.init(moonbeamApi);

const acala = new AcalaAdapter();

const acalaApi = new ApiPromise({ provider: new WsProvider("wss://acala-rpc.dwellir.com") });

await acala.init(acalaApi);

bridge = new Bridge({
adapters: [moonbeam, acala],
});
});

afterAll(async () => {
for (const adapter of bridge.adapters) {
const api = adapter.getApi();

if (api) {
await api.disconnect();
}
}

await new Promise((resolve) => setTimeout(() => resolve(undefined), 5000));
logFormatedRoute("Moonbeam summary:\n", outputSummary || []);
});

test("bridge sdk init should work", (done) => {
expect(bridge).toBeDefined();

done();
});

test("transfer tokens from moonbeam should work", (done) => {
try {
const adapter = bridge.findAdapter("moonbeam");
expect(adapter).toBeDefined();

if (!adapter) return;

const allRoutes = bridge.router.getAvailableRouters();
allRoutes.forEach((e) => {
const token = adapter.getToken(e.token);

const tx = adapter.createTx({
to: e.to.id,
token: token.name,
amount: new FixedPointNumber(1, token.decimals),
address: "5GREeQcGHt7na341Py6Y6Grr38KUYRvVoiFSiDB52Gt7VZiN",
});

expect(tx).toBeDefined();

const logRoute = formateRouteLogLine(e.token, e.from.display, e.to.display, "createTx");
logFormatedRoute("", [logRoute]);
outputSummary.push(logRoute);
});

done();
} catch (e) {
// ignore error
console.log(e);
}
});
});
88 changes: 64 additions & 24 deletions src/adapters/moonbeam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { BaseCrossChainAdapter } from "../base-chain-adapter";
import { ChainId, chains } from "../configs";
import { ApiNotFound, InvalidAddress, TokenNotFound } from "../errors";
import {
createPolkadotXCMAccount,
createPolkadotXCMAsset,
createPolkadotXCMDest,
createRouteConfigs,
getDestAccountInfo,
validateAddress,
Expand All @@ -23,13 +26,13 @@ import {
} from "src/types";

export const moonbeamRouteConfigs = createRouteConfigs("moonbeam", [
{
to: "subsocial",
token: "xcSUB",
xcm: {
fee: { token: "SUB", amount: "1000000000" },
},
},
// {
// to: "subsocial",
// token: "xcSUB",
// xcm: {
// fee: { token: "SUB", amount: "1000000000" },
// },
// },
{
to: "acala",
token: "GLMR",
Expand Down Expand Up @@ -58,13 +61,13 @@ export const moonbeamRouteConfigs = createRouteConfigs("moonbeam", [
fee: { token: "DOT", amount: "1000000000" },
},
},
{
to: "assetHubPolkadot",
token: "xcUSDT",
xcm: {
fee: { token: "USDT", amount: "1000000000" },
},
},
// {
// to: "assetHubPolkadot",
// token: "xcUSDT",
// xcm: {
// fee: { token: "USDT", amount: "1000000000" },
// },
// },
]);

const moonbeamTokensConfig: Record<string, ExtendedToken> = {
Expand Down Expand Up @@ -264,26 +267,63 @@ class MoonbeamBaseAdapter extends BaseCrossChainAdapter {
to
);

const tokenData = moonbeamTokensConfig[token.replace("xc", "")];

// FIXME: just for acala
if (!validateAddress(address, addrType)) throw new InvalidAddress(address);

const toChain = chains[to];

return this.api.tx.xTokens.transfer(
tokenData.toRaw(),
amount.toChainData(),
const destToken = (
this.getToken(token, to) as unknown as ExtendedToken
).toRaw?.();

if (!destToken) throw new Error("destToken not found");

console.log(accountId, accountType);

return this.api.tx.polkadotXcm.transferAssets(
// dest,
{
V3: {
V4: {
parents: 1,
interior: { X1: [{ Parachain: toChain.paraChainId }] },
},
},
// beneficiary,
{
V4: {
parents: 0,
interior: {
X2: [
{ Parachain: toChain.paraChainId },
{ [accountType]: { id: accountId, network: undefined } },
X1: [
{
[accountType]: {
[accountType === "AccountId32" ? "id" : "key"]: accountId,
network: undefined,
},
},
],
},
},
} as any,
},
// assets,
{
V4: [
{
id: {
parents: 1,
interior: {
X2: [
{ Parachain: toChain.paraChainId },
{ GeneralKey: { length: 2, data: destToken } },
],
},
},
fun: { Fungible: amount },
},
],
},
// feeAssetItem,
0,
// weightLimit
"Unlimited"
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/base-chain-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export abstract class BaseCrossChainAdapter {
protected api?: AnyApi;
readonly chain: Chain;
// @ts-ignore
private findAdapter!: (chain: Chain | ChainId) => BaseCrossChainAdapter;
protected findAdapter!: (chain: Chain | ChainId) => BaseCrossChainAdapter;

constructor(
chain: Chain,
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface BasicToken {
}

export interface ExtendedToken extends BasicToken {
toRaw: () => any;
toRaw?: () => any;
}

export interface RouteConfigs {
Expand Down
Loading

0 comments on commit 5c1614b

Please sign in to comment.