Skip to content

Commit

Permalink
feat: update crab bridge tx creator
Browse files Browse the repository at this point in the history
  • Loading branch information
qwer951123 committed Dec 12, 2024
1 parent ab2395b commit 01836fe
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polkawallet/bridge",
"version": "0.1.7-13",
"version": "0.1.7-14",
"description": "polkawallet bridge sdk",
"main": "index.js",
"types": "index.d.ts",
Expand Down Expand Up @@ -58,5 +58,5 @@
"ts-node": "^10.9.2",
"typescript": "^4.7.4"
},
"stableVersion": "0.1.7-12"
"stableVersion": "0.1.7-13"
}
11 changes: 9 additions & 2 deletions src/adapters/darwinia.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ApiProvider } from "../api-provider";
import { firstValueFrom } from "rxjs";
import { CrabAdapter } from "./darwinia";

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

const address = "5GREeQcGHt7na341Py6Y6Grr38KUYRvVoiFSiDB52Gt7VZiN";
Expand All @@ -20,6 +20,8 @@ describe.skip("crab adapter should work", () => {

await crab.init(provider.getApi("crab"));

console.log("init crab adapter done");

bridge = new Bridge({ adapters: [crab] });
});

Expand Down Expand Up @@ -63,7 +65,12 @@ describe.skip("crab adapter should work", () => {

expect(tx).toBeDefined();

const logRoute = formateRouteLogLine(e.token, e.from.display, e.to.display, "createTx");
const logRoute = formateRouteLogLine(
e.token,
e.from.display,
e.to.display,
"createTx"
);
logFormatedRoute("", [logRoute]);
outputSummary.push(logRoute);
});
Expand Down
6 changes: 3 additions & 3 deletions src/adapters/darwinia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ class BaseDarwiniaAdapter extends BaseCrossChainAdapter {
const rawAmount = amount.toChainData();

return this.api?.tx.polkadotXcm.limitedReserveTransferAssets(
createPolkadotXCMDest(this.api, parachainId),
createPolkadotXCMAccount(this.api, accountId, accountType),
createPolkadotXCMAsset(this.api, rawAmount, "NATIVE"),
createPolkadotXCMDest(this.api, parachainId, 1, "V4"),
createPolkadotXCMAccount(this.api, accountId, accountType, "V4"),
createPolkadotXCMAsset(this.api, rawAmount, "NATIVE", "V4"),
0,
this.getDestWeight(token, to)?.toString() as any
);
Expand Down
65 changes: 58 additions & 7 deletions src/utils/polkadot-xcm-params.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { AnyApi } from "@acala-network/sdk-core";
import { checkMessageVersion } from "./check-message-version";

export type XCMType = "V0" | "V1" | "V2" | "V3" | "V4";

export function createPolkadotXCMDest(
api: AnyApi,
parachainId: number,
parents = 1
parents = 1,
targetVersion: XCMType | undefined = undefined
): any {
const version = checkMessageVersion(api);
const version = targetVersion ?? checkMessageVersion(api);

if (version === "V4") {
return {
V4: {
parents,
interior: { X1: [{ Parachain: parachainId }] },
},
};
}

return {
[version]: {
Expand All @@ -19,9 +31,28 @@ export function createPolkadotXCMDest(
export function createPolkadotXCMAccount(
api: AnyApi,
accountId: string,
accountType = "AccountId32"
accountType = "AccountId32",
targetVersion: XCMType | undefined = undefined
): any {
const version = checkMessageVersion(api);
const version = targetVersion ?? checkMessageVersion(api);

if (version === "V4") {
return {
V4: {
parents: 0,
interior: {
X1: [
{
[accountType]: {
[accountType === "AccountId32" ? "id" : "key"]: accountId,
network: undefined,
},
},
],
},
},
};
}

return {
[version]: {
Expand All @@ -41,10 +72,11 @@ export function createPolkadotXCMAccount(
export function createPolkadotXCMAsset(
api: AnyApi,
amount: string,
position: "NATIVE" | any[]
position: "NATIVE" | any[],
targetVersion: XCMType | undefined = undefined
): any {
const version = checkMessageVersion(api);
const tokenPosition =
const version = targetVersion ?? checkMessageVersion(api);
let tokenPosition: any =
position === "NATIVE"
? {
id: { Concrete: { parents: 0, interior: "Here" } },
Expand All @@ -60,6 +92,25 @@ export function createPolkadotXCMAsset(
},
};

if (version === "V4") {
tokenPosition =
position === "NATIVE"
? {
id: {
parents: 0,
interior: "Here",
},
}
: {
id: {
paraents: 1,
interior: {
X2: position,
},
},
};
}

return {
[version]: [
{
Expand Down

0 comments on commit 01836fe

Please sign in to comment.