Skip to content

Commit

Permalink
feat: update moonbeam (#140)
Browse files Browse the repository at this point in the history
* update version

* feat: update moonbeam transfer

* fix: fix lint
  • Loading branch information
qwer951123 authored Nov 25, 2024
1 parent 8ed6e56 commit 4dc68ad
Show file tree
Hide file tree
Showing 6 changed files with 583 additions and 530 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-11",
"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 | BasicToken> = {
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);
}
});
});
85 changes: 61 additions & 24 deletions src/adapters/moonbeam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,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 +58,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 +264,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
Loading

0 comments on commit 4dc68ad

Please sign in to comment.