Skip to content

Commit

Permalink
Feat update xcm (#128)
Browse files Browse the repository at this point in the history
* feat: support xcm version4

* feat: support xcm v4

* fix: disable test
  • Loading branch information
qwer951123 authored May 8, 2024
1 parent 99cfcb6 commit c7e0d40
Show file tree
Hide file tree
Showing 6 changed files with 526 additions and 446 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polkawallet/bridge",
"version": "0.1.6-3",
"version": "0.1.6",
"description": "polkawallet bridge sdk",
"main": "index.js",
"types": "index.d.ts",
Expand All @@ -25,23 +25,23 @@
},
"peerDependencies": {
"@acala-network/api": "^5",
"@polkadot/api": "^10",
"@polkadot/api": "^11",
"ethers": "^5"
},
"resolutions": {
"@acala-network/api": "^5.1.1",
"@acala-network/sdk": "^4.1.9-7",
"@acala-network/sdk-core": "^4.1.9-7",
"@polkadot/api": "^10.9.1",
"@polkadot/types": "^10.9.1"
"@polkadot/api": "^11.0.2",
"@polkadot/types": "^11.0.2"
},
"dependencies": {
"@acala-network/api": "^5",
"@acala-network/sdk": "^4.1.9-7",
"@acala-network/sdk-core": "^4.1.9-7",
"@polkadot/api": "^10.9.1",
"@polkadot/api": "^11.0.2",
"@polkadot/apps-config": "^0.133.1",
"@polkadot/types": "^10.9.1",
"@polkadot/types": "^11.0.2",
"axios": "^0.27.2",
"ethers": "^5",
"lodash": "^4.17.20"
Expand All @@ -55,5 +55,5 @@
"jest": "^28.1.1",
"typescript": "^4.7.4"
},
"stableVersion": "0.1.6-2"
"stableVersion": "0.1.5"
}
16 changes: 8 additions & 8 deletions src/adapters/acala/acala.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ describe.skip("acala-adapter", () => {
const moonriver = new MoonriverAdapter();
const assetHubKusama = new AssetHubKusamaAdapter();

const karuraApi = new ApiPromise({ provider: new WsProvider('wss://karura.api.onfinality.io/public-ws') });
const kusmaApi = new ApiPromise({ provider: new WsProvider('wss://kusama.api.onfinality.io/public-ws') });
const karuraApi = new ApiPromise({ provider: new WsProvider('wss://karura-rpc-1.aca-api.network') });
const kusmaApi = new ApiPromise({ provider: new WsProvider('wss://kusama-public-rpc.blockops.network/ws') });
const assetHubApi = new ApiPromise({ provider: new WsProvider('wss://statemine-rpc.dwellir.com') });

await karura.init(karuraApi);
Expand Down Expand Up @@ -86,10 +86,10 @@ describe.skip("acala-adapter", () => {
});

// DONT MODIFY THIS, THE OBJECT IS VALID, UNLESS YOU KNOW WHAT YOU ARE DOING
const location = api.createType('XcmVersionedMultiLocation', {
V3: {
const location = api.createType('XcmVersionedLocation', {
V4: {
parents: '1',
interior: { X1: { AccountId32: { id: addressId, network: null } } }
interior: { X1: [{ AccountId32: { id: addressId, network: null } }] }
}
});

Expand Down Expand Up @@ -123,7 +123,7 @@ describe.skip("acala-adapter", () => {
});

// DONT MODIFY THIS, THE OBJECT IS VALID, UNLESS YOU KNOW WHAT YOU ARE DOING
const location = api.createType('XcmVersionedMultiLocation', {
const location = api.createType('XcmVersionedLocation', {
V3: {
parents: "1",
interior: {
Expand Down Expand Up @@ -171,7 +171,7 @@ describe.skip("acala-adapter", () => {
});

// DONT MODIFY THIS, THE OBJECT IS VALID, UNLESS YOU KNOW WHAT YOU ARE DOING
const location = api.createType('XcmVersionedMultiLocation', {
const location = api.createType('XcmVersionedLocation', {
V3: {
parents: "1",
interior: {
Expand All @@ -184,7 +184,7 @@ describe.skip("acala-adapter", () => {
});

// DONT MODIFY THIS, THE OBJECT IS VALID, UNLESS YOU KNOW WHAT YOU ARE DOING
const assets = api.createType('XcmVersionedMultiAsset', {
const assets = api.createType('XcmVersionedAsset', {
V3: {
fun: {
Fungible: amount.toChainData(),
Expand Down
39 changes: 29 additions & 10 deletions src/utils/check-message-version.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
import { AnyApi } from "@acala-network/sdk-core";

export function checkMessageVersionIsV3(api: AnyApi) {
export type XCMVersion = "V1" | "V3" | "V4";

export function checkMessageVersion(api: AnyApi) {
return "V4";

let version: XCMVersion = "V1";

try {
const keys = (api?.createType("XcmVersionedMultiLocation") as any)
.defKeys as string[];

return keys.includes("V3");
if (keys.includes("V3")) {
version = "V3";
}
} catch (e) {
try {
const keys = (api?.createType("StagingXcmVersionedMultiLocation") as any)
.defKeys as string[];
// ignore
}

try {
const keys = (api?.createType("StagingXcmVersionedMultiLocation") as any)
.defKeys as string[];

return keys.includes("V3");
} catch (e) {
// ignore
if (keys.includes("V3")) {
version = "V3";
}
// ignore error
} catch (e) {
// ignore
}

try {
api?.createType("XcmVersionedLocation") as any;

version = "V4";
} catch (e) {
// ignore
}

return false;
return version;
}
19 changes: 8 additions & 11 deletions src/utils/polkadot-xcm-params.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { AnyApi } from "@acala-network/sdk-core";
import { checkMessageVersionIsV3 } from "./check-message-version";
import { checkMessageVersion } from "./check-message-version";

export function createPolkadotXCMDest(
api: AnyApi,
parachainId: number,
parents = 1
): any {
const isV3 = checkMessageVersionIsV3(api);
const versionTag = isV3 ? "V3" : "V1";
const version = checkMessageVersion(api);

return {
[versionTag]: {
[version]: {
parents,
interior: { X1: { Parachain: parachainId } },
},
Expand All @@ -22,17 +21,16 @@ export function createPolkadotXCMAccount(
accountId: string,
accountType = "AccountId32"
): any {
const isV3 = checkMessageVersionIsV3(api);
const versionTag = isV3 ? "V3" : "V1";
const version = checkMessageVersion(api);

return {
[versionTag]: {
[version]: {
parents: 0,
interior: {
X1: {
[accountType]: {
[accountType === "AccountId32" ? "id" : "key"]: accountId,
network: isV3 ? undefined : "Any",
network: version === "V1" ? "Any" : undefined,
},
},
},
Expand All @@ -45,7 +43,7 @@ export function createPolkadotXCMAsset(
amount: string,
position: "NATIVE" | any[]
): any {
const isV3 = checkMessageVersionIsV3(api);
const version = checkMessageVersion(api);
const tokenPosition =
position === "NATIVE"
? {
Expand All @@ -61,10 +59,9 @@ export function createPolkadotXCMAsset(
},
},
};
const versionTag = isV3 ? "V3" : "V1";

return {
[versionTag]: [
[version]: [
{
...tokenPosition,
fun: { Fungible: amount },
Expand Down
33 changes: 21 additions & 12 deletions src/utils/xtokens-params.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { AnyApi } from "@acala-network/sdk-core";
import { checkMessageVersionIsV3 } from "./check-message-version";
import { XCMVersion, checkMessageVersion } from "./check-message-version";

interface DestConfigs {
useAccountKey20?: boolean;
isToRelayChain?: boolean;
}

function createToRelayChainDestParam(isV3: boolean, accountId: string) {
if (!isV3) {
function createToRelayChainDestParam(version: XCMVersion, accountId: string) {
if (version === "V1") {
return {
V1: {
parents: 1,
Expand All @@ -16,8 +16,17 @@ function createToRelayChainDestParam(isV3: boolean, accountId: string) {
};
}

if (version === "V4") {
return {
[version]: {
parents: 1,
interior: { X1: [{ AccountId32: { id: accountId } }] },
},
};
}

return {
V3: {
[version]: {
parents: 1,
interior: { X1: { AccountId32: { id: accountId } } },
},
Expand All @@ -30,16 +39,16 @@ export function createXTokensDestParam(
accountId: string,
configs?: DestConfigs
) {
const isV3 = checkMessageVersionIsV3(api);
const version = checkMessageVersion(api);
const { useAccountKey20, isToRelayChain } = configs || {};

const accountKeyName = useAccountKey20 ? "AccountKey20" : "AccountId32";

if (isToRelayChain) {
return createToRelayChainDestParam(isV3, accountId);
return createToRelayChainDestParam(version, accountId);
}

if (!isV3) {
if (version === "V1") {
return {
V1: {
parents: 1,
Expand All @@ -58,9 +67,9 @@ export function createXTokensDestParam(
};
}

// for message version v3
// for message version v3 & v4
return {
V3: {
[version]: {
parents: 1,
interior: {
X2: [
Expand All @@ -82,9 +91,9 @@ export function createXTokensAssetsParam(
assetId: any,
amount: string
) {
const isV3 = checkMessageVersionIsV3(api);
const version = checkMessageVersion(api);

if (!isV3) {
if (version === "V1") {
return {
V1: {
fun: {
Expand All @@ -107,7 +116,7 @@ export function createXTokensAssetsParam(
}

return {
V3: {
[version]: {
fun: {
Fungible: amount,
},
Expand Down
Loading

0 comments on commit c7e0d40

Please sign in to comment.