Skip to content

Commit

Permalink
update transfer example (papi fallback with tx opts)
Browse files Browse the repository at this point in the history
  • Loading branch information
nohaapav committed Feb 1, 2025
1 parent 7071ff1 commit d2d6c5c
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 50 deletions.
46 changes: 22 additions & 24 deletions examples/xcm-transfer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AssetAmount, ConfigBuilder } from '@galacticcouncil/xcm-core';
import { Call } from '@galacticcouncil/xcm-sdk';
import { Call, SubstrateCall } from '@galacticcouncil/xcm-sdk';

import {
getWormholeChainById,
Expand Down Expand Up @@ -74,33 +74,31 @@ balanceSubscription.unsubscribe();
/***************************/

/**
* Sign transaction with Papi
*
* @param address - signer address
*/
async function signV2(address: string) {
substrateV2.signAndSend(address, call, srcChain, (event) => {
console.log(event);
});
}

/**
* Sign transaction with Pjs
* 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
4 changes: 3 additions & 1 deletion examples/xcm-transfer/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ configService.registerExternal(externals);
// Register dex-es
const hydration = configService.getChain('hydration');
const assethub = configService.getChain('assethub');
const assethubCex = configService.getChain('assethub_cex');

wallet.registerDex(
new dex.HydrationDex(hydration),
new dex.AssethubDex(assethub)
new dex.AssethubDex(assethub),
new dex.AssethubDex(assethubCex)
);
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
17 changes: 11 additions & 6 deletions examples/xcm-transfer/src/signers/substrateV2.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Binary, TxEvent } from 'polkadot-api';

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

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

import { getSignerBySource, getWs, asset } from './v2';
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,
Expand All @@ -16,16 +16,21 @@ export async function signAndSend(
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(call.data);
const extrinsic = apiPjs.tx(data);

console.log(extrinsic.toHuman());

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

const callData = Binary.fromHex(extrinsic.inner.toHex());
const asset = getFeeAsset(txOptions?.asset);

const tx = await api.txFromCallData(callData);
tx.signSubmitAndWatch(signer, {
asset: asset.usdc,
asset: asset,
}).subscribe(observer);
}
2 changes: 1 addition & 1 deletion examples/xcm-transfer/src/signers/v2/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './extension';
export * from './provider';
export * as asset from './types';
export * from './utils';
20 changes: 2 additions & 18 deletions examples/xcm-transfer/src/signers/v2/types.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
import { XcmV3Junctions, XcmV3Junction } from '@polkadot-api/descriptors';
import { XcmV3Junctions } from '@polkadot-api/descriptors';

type XcmV3Multilocation = {
export type XcmV3Multilocation = {
parents: number;
interior: XcmV3Junctions;
};

export const usdt = {
parents: 0,
interior: XcmV3Junctions.X2([
XcmV3Junction.PalletInstance(50),
XcmV3Junction.GeneralIndex(BigInt(1984)),
]),
} as XcmV3Multilocation;

export const usdc = {
parents: 0,
interior: XcmV3Junctions.X2([
XcmV3Junction.PalletInstance(50),
XcmV3Junction.GeneralIndex(BigInt(1337)),
]),
} as XcmV3Multilocation;
18 changes: 18 additions & 0 deletions examples/xcm-transfer/src/signers/v2/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { XcmV3Junctions, XcmV3Junction } from '@polkadot-api/descriptors';

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

import { XcmV3Multilocation } from './types';

export const getFeeAsset = (location: any) => {
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;
};

0 comments on commit d2d6c5c

Please sign in to comment.