Skip to content

Commit

Permalink
Merge branch 'master' into add-sol-tbtc
Browse files Browse the repository at this point in the history
  • Loading branch information
nohaapav authored Feb 3, 2025
2 parents abe1323 + 304c9a8 commit 684027d
Show file tree
Hide file tree
Showing 102 changed files with 2,604 additions and 3,811 deletions.
4 changes: 2 additions & 2 deletions examples/sdk-cjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"dependencies": {
"@galacticcouncil/sdk": "^5.3.0",
"@galacticcouncil/xcm-sdk": "^8.0.1",
"@galacticcouncil/xcm-cfg": "^7.0.0"
"@galacticcouncil/xcm-sdk": "^8.1.0",
"@galacticcouncil/xcm-cfg": "^7.1.1"
}
}
3 changes: 3 additions & 0 deletions examples/xcm-transfer/.papi/descriptors/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!.gitignore
!package.json
24 changes: 24 additions & 0 deletions examples/xcm-transfer/.papi/descriptors/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": "0.1.0-autogenerated.1636418960291530843",
"name": "@polkadot-api/descriptors",
"files": [
"dist"
],
"exports": {
".": {
"types": "./dist/index.d.ts",
"module": "./dist/index.mjs",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"browser": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"sideEffects": false,
"peerDependencies": {
"polkadot-api": "*"
}
}
Binary file not shown.
Binary file not shown.
14 changes: 14 additions & 0 deletions examples/xcm-transfer/.papi/polkadot-api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": 0,
"descriptorPath": ".papi/descriptors",
"entries": {
"assethub": {
"wsUrl": "wss://polkadot-asset-hub-rpc.polkadot.io",
"metadata": ".papi/metadata/assethub.scale"
},
"hydration": {
"wsUrl": "wss://hydradx-rpc.dwellir.com",
"metadata": ".papi/metadata/hydration.scale"
}
}
}
8 changes: 5 additions & 3 deletions examples/xcm-transfer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
},
"dependencies": {
"@galacticcouncil/sdk": "^5.3.0",
"@galacticcouncil/xcm-cfg": "^7.0.0",
"@galacticcouncil/xcm-sdk": "^8.0.1",
"@talismn/connect-wallets": "^1.2.5"
"@galacticcouncil/xcm-cfg": "^7.1.1",
"@galacticcouncil/xcm-sdk": "^8.1.0",
"@polkadot-api/descriptors": "file:.papi/descriptors",
"@talismn/connect-wallets": "^1.2.8",
"polkadot-api": "^1.8.2"
}
}
37 changes: 23 additions & 14 deletions examples/xcm-transfer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { AssetAmount, ConfigBuilder } from '@galacticcouncil/xcm-core';
import { Call } from '@galacticcouncil/xcm-sdk';
import { Call, SubstrateCall } from '@galacticcouncil/xcm-sdk';

import {
getWormholeChainById,
logAssets,
logSrcChains,
logDestChains,
} from './utils';
import { evm, solana, substrate } from './signers';
import { evm, solana, substrate, substrateV2 } from './signers';
import { configService, wallet, whClient, whScan } from './setup';

// Define transfer constraints
Expand Down Expand Up @@ -74,22 +74,31 @@ balanceSubscription.unsubscribe();
/***************************/

/**
* Sign transaction
* Sign substrate transaction
*
* @param address - signer address
*/
async function sign(address: string) {
substrate.signAndSend(
address,
call,
srcChain,
({ status }) => {
console.log(status.toHuman());
},
(error) => {
console.error(error);
}
);
const { txOptions } = call as SubstrateCall;

// Using papi signer as txOptions not working in 14.x pjs
if (txOptions) {
substrateV2.signAndSend(address, call, srcChain, (event) => {
console.log(event);
});
} else {
substrate.signAndSend(
address,
call,
srcChain,
({ status }) => {
console.log(status.toHuman());
},
(error) => {
console.error(error);
}
);
}
}

/**
Expand Down
12 changes: 7 additions & 5 deletions examples/xcm-transfer/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
assetsMap,
chainsMap,
routesMap,
swaps,
dex,
validations,
HydrationConfigService,
} from '@galacticcouncil/xcm-cfg';
Expand Down Expand Up @@ -31,11 +31,13 @@ export const whClient = new WormholeClient();
// Register external assets
configService.registerExternal(externals);

// Register chain swaps
// Register dex-es
const hydration = configService.getChain('hydration');
const assethub = configService.getChain('assethub');
const assethubCex = configService.getChain('assethub_cex');

wallet.registerSwaps(
new swaps.HydrationSwap(hydration),
new swaps.AssethubSwap(assethub)
wallet.registerDex(
new dex.HydrationDex(hydration),
new dex.AssethubDex(assethub),
new dex.AssethubDex(assethubCex)
);
1 change: 1 addition & 0 deletions examples/xcm-transfer/src/signers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * as evm from './evm';
export * as solana from './solana';
export * as substrate from './substrate';
export * as substrateV2 from './substrateV2';
13 changes: 13 additions & 0 deletions examples/xcm-transfer/src/signers/substrate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { AnyChain, AnyParachain } from '@galacticcouncil/xcm-core';
import type { Call } from '@galacticcouncil/xcm-sdk';

import type { ApiPromise } from '@polkadot/api';
import type { ISubmittableResult } from '@polkadot/types/types';
import { getWalletBySource } from '@talismn/connect-wallets';

/**
* Doesn't work in 14.x pjs !!!
*/
const feeAsset = (api: ApiPromise, assetId: number) =>
api.createType('MultiLocation', {
parents: 0,
interior: {
x2: [{ palletInstance: 50 }, { generalIndex: assetId }],
},
});

export async function signAndSend(
address: string,
call: Call,
Expand Down
40 changes: 40 additions & 0 deletions examples/xcm-transfer/src/signers/substrateV2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Binary, TxEvent } from 'polkadot-api';

import { assethub } from '@polkadot-api/descriptors';

import type { AnyChain, AnyParachain } from '@galacticcouncil/xcm-core';
import type { Call, SubstrateCall } from '@galacticcouncil/xcm-sdk';

import { getSignerBySource, getWs, getFeeAsset } from './v2';

export async function signAndSend(
address: string,
call: Call,
chain: AnyChain,
observer: (value: TxEvent) => void
) {
const ctx = chain as AnyParachain;
const signer = await getSignerBySource('polkadot-js', address);

const { data, txOptions } = call as SubstrateCall;

const apiPjs = await ctx.api;
const extrinsic = apiPjs.tx(data);

const client = await getWs(ctx.ws);
const api = client.getTypedApi(assethub);

const callData = Binary.fromHex(extrinsic.inner.toHex());

let opts = {};
if (txOptions && txOptions.asset) {
const feeAsset = getFeeAsset(ctx, txOptions.asset);
opts = {
...opts,
asset: feeAsset,
};
}

const tx = await api.txFromCallData(callData);
tx.signSubmitAndWatch(signer, opts).subscribe(observer);
}
31 changes: 31 additions & 0 deletions examples/xcm-transfer/src/signers/v2/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { PolkadotSigner } from 'polkadot-api';

import {
getInjectedExtensions,
connectInjectedExtension,
InjectedExtension,
InjectedPolkadotAccount,
} from 'polkadot-api/pjs-signer';

export const getSignerBySource = async (
source: string,
address: string
): Promise<PolkadotSigner> => {
const extensions: string[] = getInjectedExtensions();
const extension = extensions.find((e) => e === source);

if (!extension) {
throw new Error('No wallet extension forund for ' + source);
}

const selectedExtension: InjectedExtension =
await connectInjectedExtension(extension);

const accounts: InjectedPolkadotAccount[] = selectedExtension.getAccounts();
const account = accounts.find((a) => a.address === address);

if (!account) {
throw new Error('Account is not connected: ' + address);
}
return account.polkadotSigner;
};
4 changes: 4 additions & 0 deletions examples/xcm-transfer/src/signers/v2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './extension';
export * from './provider';
export * from './utils';
export * from './types';
15 changes: 15 additions & 0 deletions examples/xcm-transfer/src/signers/v2/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createClient, PolkadotClient } from 'polkadot-api';
import { withPolkadotSdkCompat } from 'polkadot-api/polkadot-sdk-compat';

export const getWs = async (
wsUrl: string | string[]
): Promise<PolkadotClient> => {
const endpoints = typeof wsUrl === 'string' ? wsUrl.split(',') : wsUrl;
const isNodeJs = typeof window === 'undefined';
const getWsProvider = isNodeJs
? (await import('polkadot-api/ws-provider/node')).getWsProvider
: (await import('polkadot-api/ws-provider/web')).getWsProvider;

const wsProvider = getWsProvider(endpoints);
return createClient(withPolkadotSdkCompat(wsProvider));
};
6 changes: 6 additions & 0 deletions examples/xcm-transfer/src/signers/v2/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { XcmV3Junctions } from '@polkadot-api/descriptors';

export type XcmV3Multilocation = {
parents: number;
interior: XcmV3Junctions;
};
22 changes: 22 additions & 0 deletions examples/xcm-transfer/src/signers/v2/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { XcmV3Junctions, XcmV3Junction } from '@polkadot-api/descriptors';

import { AnyParachain, Asset, multiloc } from '@galacticcouncil/xcm-core';

import { XcmV3Multilocation } from './types';

export const getFeeAsset = (chain: AnyParachain, asset: Asset) => {
const location = chain.getAssetXcmLocation(asset);
if (location) {
const pallet = multiloc.findPalletInstance(location);
const index = multiloc.findGeneralIndex(location);

return {
parents: 0,
interior: XcmV3Junctions.X2([
XcmV3Junction.PalletInstance(Number(pallet)),
XcmV3Junction.GeneralIndex(BigInt(index)),
]),
} as XcmV3Multilocation;
}
throw new Error('Asset locations not found');
};
5 changes: 5 additions & 0 deletions integration-tests/xcm-test/jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ process.env.LOG_COMPACT = 'false';
*/
process.env.VERBOSE_LOG = 'false';

process.env.ASSETHUBPOLKADOT_BLOCK_NUMBER = 8112196;
process.env.HYDRATION_BLOCK_NUMBER = 6875392;
process.env.MOONBEAM_BLOCK_NUMBER = 9418127;
process.env.POLKADOT_BLOCK_NUMBER = 24527644;

export default config;
4 changes: 2 additions & 2 deletions integration-tests/xcm-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@acala-network/chopsticks-testing": "1.0.1"
},
"dependencies": {
"@galacticcouncil/xcm-cfg": "^7.0.0",
"@galacticcouncil/xcm-sdk": "^8.0.1"
"@galacticcouncil/xcm-cfg": "^7.1.1",
"@galacticcouncil/xcm-sdk": "^8.1.0"
}
}
24 changes: 24 additions & 0 deletions integration-tests/xcm-test/src/__db__/metadata.db.json
Original file line number Diff line number Diff line change
Expand Up @@ -910,5 +910,29 @@
"feeAsset": "PHA",
"feeNative": "64296000000"
}
},
"hydration-assethub_cex-usdt": {
"updated": 1738523661393,
"destination": {
"fee": "1.055453",
"feeAsset": "USDT",
"feeNative": "1055453"
}
},
"hydration-assethub_cex-usdc": {
"updated": 1738493249475,
"destination": {
"fee": "1.184201",
"feeAsset": "USDC",
"feeNative": "1184201"
}
},
"hydration-polkadot_cex-dot": {
"updated": 1738579585913,
"destination": {
"fee": "10",
"feeAsset": "DOT",
"feeNative": "100000000000"
}
}
}
21 changes: 21 additions & 0 deletions integration-tests/xcm-test/src/__snapshots__/call.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,20 @@ exports[`Wallet with XCM config should return valid Polkadot calldata for Hydrat
]
`;

exports[`Wallet with XCM config should return valid Polkadot calldata for Hydration -> AssetHub (CEX) USDC [usdc] transfer 1`] = `
[
"hydration-assethub_cex-usdc",
"0x5503040d02086b0d04010100a10f0404010300a10f043205e5140002093d000204010300a10f043205e5140204040d010000010100f959d131179b0264f98ac96d2c9c13c403129f5de93e52f3a611ccd4294d1741006b0004010100a10f041400040002043205e5140082380100130002043205e51400823801000004040002043205e5140022cf040000010100d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d140d01020400010100f959d131179b0264f98ac96d2c9c13c403129f5de93e52f3a611ccd4294d1741",
]
`;

exports[`Wallet with XCM config should return valid Polkadot calldata for Hydration -> AssetHub (CEX) USDT [usdt] transfer 1`] = `
[
"hydration-assethub_cex-usdt",
"0x5503040d02086b0d04010100a10f0404010300a10f043205011f0002093d000204010300a10f043205011f0204040d010000010100f959d131179b0264f98ac96d2c9c13c403129f5de93e52f3a611ccd4294d1741006b0004010100a10f041400040002043205011f0082380100130002043205011f0082380100000601c2e6e0810161a43208011f00d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27dca1c0500140d01020400010100f959d131179b0264f98ac96d2c9c13c403129f5de93e52f3a611ccd4294d1741",
]
`;

exports[`Wallet with XCM config should return valid Polkadot calldata for Hydration -> AssetHub DED [ded] transfer 1`] = `
[
"hydration-assethub-ded",
Expand Down Expand Up @@ -903,6 +917,13 @@ exports[`Wallet with XCM config should return valid Polkadot calldata for Hydrat
]
`;

exports[`Wallet with XCM config should return valid Polkadot calldata for Hydration -> Polkadot (CEX) DOT [dot] transfer 1`] = `
[
"hydration-polkadot_cex-dot",
"0xc502040d020889000500000000e40b5402000000000000000000000004010200000100f959d131179b0264f98ac96d2c9c13c403129f5de93e52f3a611ccd4294d1741006b00040100041400040000000208af2f130000000208af2f000601027c3125253890050400d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d01140d01020400010100f959d131179b0264f98ac96d2c9c13c403129f5de93e52f3a611ccd4294d1741",
]
`;

exports[`Wallet with XCM config should return valid Polkadot calldata for Hydration -> Polkadot DOT [dot] transfer 1`] = `
[
"hydration-polkadot-dot",
Expand Down
Loading

0 comments on commit 684027d

Please sign in to comment.