Skip to content

Commit

Permalink
add extrinsic builders for foreign assets
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaurello committed Nov 22, 2024
1 parent 721ee47 commit 28503d1
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 31 deletions.
35 changes: 35 additions & 0 deletions packages/builder/src/asset-min/AssetMinBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

import { Option } from '@polkadot/types';
import { PalletAssetsAssetDetails } from '@polkadot/types/lookup';
import { getExtrinsicAccount } from '../extrinsic/ExtrinsicBuilder.utils';
import { SubstrateQueryConfig } from '../types/substrate/SubstrateQueryConfig';
import { AssetMinConfigBuilder } from './AssetMinBuilder.interfaces';

export function AssetMinBuilder() {
return {
assetRegistry,
assets,
foreignAssets,
};
}

Expand Down Expand Up @@ -57,3 +59,36 @@ function assets() {
}),
};
}

function foreignAssets() {
return {
asset: (): AssetMinConfigBuilder => ({
build: ({ asset }) => {
const multilocation = {
parents: 2,
interior: {
X2: [
{
globalconsensus: {
ethereum: {
chainId: 1,
},
},
},
getExtrinsicAccount(asset as string),
],
},
};
return new SubstrateQueryConfig({
module: 'foreignAssets',
func: 'asset',
args: [multilocation],
transform: async (
response: Option<PalletAssetsAssetDetails>,
): Promise<bigint> =>
response.unwrapOrDefault().minBalance.toBigInt(),
});
},
}),
};
}
1 change: 0 additions & 1 deletion packages/builder/src/balance/BalanceBuilder.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export interface BalanceConfigBuilder {
export interface BalanceConfigBuilderPrams {
address: string;
asset: ChainAssetId;
globalConcensusId?: ChainAssetId;
}

export interface PalletBalancesAccountDataOld extends Struct {
Expand Down
19 changes: 8 additions & 11 deletions packages/builder/src/balance/BalanceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { isString } from '@polkadot/util';
import { evmToAddress } from '@polkadot/util-crypto';
import { ContractConfig } from '../contract';
import { getExtrinsicAccount } from '../extrinsic/ExtrinsicBuilder.utils';
import { SubstrateQueryConfig } from '../types/substrate/SubstrateQueryConfig';
import {
BalanceConfigBuilder,
Expand Down Expand Up @@ -76,19 +77,15 @@ function assets() {
function foreignAssets() {
return {
account: (): BalanceConfigBuilder => ({
build: ({ address, asset, globalConcensusId }) => {
// TODO verify
build: ({ address, asset }) => {
const multilocation = {
parents: 2,
interior: [
{ globalConsensus: globalConcensusId },
{
accountKey20: {
network: null,
key: asset,
},
},
],
interior: {
X2: [
{ GlobalConsensus: { ethereum: { chainId: 1 } } },
getExtrinsicAccount(asset as string),
],
},
};

return new SubstrateQueryConfig({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/* eslint-disable sort-keys */
import { ExtrinsicConfigBuilder } from '../../ExtrinsicBuilder.interfaces';
import {
ExtrinsicConfigBuilder,
XcmVersion,
} from '../../ExtrinsicBuilder.interfaces';
import { getExtrinsicAccount } from '../../ExtrinsicBuilder.utils';
import { ExtrinsicConfig } from '../../ExtrinsicConfig';
import {
getPolkadotXcmExtrinsicArgs,
Expand Down Expand Up @@ -213,5 +217,83 @@ export function polkadotXcm() {
}),
};
},
transferAssetsUsingTypeAndThen: () => {
const func = 'transferAssetsUsingTypeAndThen';
return {
// TODO we could pass a parameter globalConcensus in the chain asset
// but we can do it in V3
globalConcensusEthereum: (): ExtrinsicConfigBuilder => ({
build: ({ destination, address, amount, asset }) => {
const version = XcmVersion.v4;
return new ExtrinsicConfig({
module: pallet,
func,

getArgs: () => [
{
[version]: {
parents: 1,
interior: {
X1: [
{
Parachain: destination.parachainId,
},
],
},
},
},
{
[version]: [
{
id: {
parents: 2,
interior: {
X2: [
{ globalConsensus: { Ethereum: { ChainId: 1 } } },
getExtrinsicAccount(asset as string),
],
},
},
fun: { fungible: amount },
},
],
},
'LocalReserve',
{
[version]: {
parents: 2,
interior: {
X2: [
{ globalConsensus: { Ethereum: { ChainId: 1 } } },
getExtrinsicAccount(asset as string),
],
},
},
},
'LocalReserve',
{
[version]: [
{
DepositAsset: {
assets: {
Wild: { AllCounted: 1 },
},
beneficiary: {
parents: 0,
interior: {
X1: [getExtrinsicAccount(address)],
},
},
},
},
],
},
'Unlimited',
],
});
},
}),
};
},
};
}
9 changes: 6 additions & 3 deletions packages/config/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1737,17 +1737,20 @@ export const polkadotAssetHub = new Parachain({
{
address: '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599',
asset: wbtce,
globalConcensusId: { ethereum: { chainId: 1 } },
decimals: 8,
id: '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599',
},
{
address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
asset: wethe,
globalConcensusId: { ethereum: { chainId: 1 } },
decimals: 18,
id: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
},
{
address: '0x7f39c581f595b53c5cb19bd0b3f8da6c935e2ca0',
asset: wstethe,
globalConcensusId: { ethereum: { chainId: 1 } },
decimals: 18,
id: '0x7f39c581f595b53c5cb19bd0b3f8da6c935e2ca0',
},
],
checkSovereignAccountBalances: true,
Expand Down
12 changes: 8 additions & 4 deletions packages/config/src/configs/moonbeam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import {
vglmr,
vmanta,
wbtc,
wbtce,
weth,
wethe,
wifd,
ztg,
} from '../assets';
Expand Down Expand Up @@ -953,15 +953,19 @@ export const moonbeamConfig = new ChainConfig({
},
}),
new AssetConfig({
asset: wbtce,
asset: wethe,
balance: BalanceBuilder().substrate().assets().account(),
contract: ContractBuilder().Xtokens().transfer(),
destination: polkadotAssetHub,
destinationFee: {
amount: 0.02, // TODO
asset: usdt, // TODO
amount: 0.0000035,
asset: wethe,
balance: BalanceBuilder().substrate().assets().account(),
},
fee: {
asset: glmr,
balance: BalanceBuilder().substrate().system().account(),
},
}),
],
chain: moonbeam,
Expand Down
14 changes: 6 additions & 8 deletions packages/config/src/configs/polkadotAssetHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
stink,
usdc,
usdt,
wbtce,
wethe,
wifd,
} from '../assets';
import { moonbeam, polkadotAssetHub } from '../chains';
Expand Down Expand Up @@ -200,28 +200,26 @@ export const polkadotAssetHubConfig = new ChainConfig({
min: AssetMinBuilder().assets().asset(),
}),
new AssetConfig({
asset: wbtce,
asset: wethe,
balance: BalanceBuilder().substrate().foreignAssets().account(),
destination: moonbeam,
destinationFee: {
amount: FeeBuilder().xcmPaymentApi().xcmPaymentFee({
isAssetReserveChain: false,
shouldTransferAssetPrecedeAsset: true,
}),
asset: usdt,
asset: wethe,
balance: BalanceBuilder().substrate().assets().account(),
},
// TODO
extrinsic: ExtrinsicBuilder()
.polkadotXcm()
.limitedReserveTransferAssets()
.X2(),
.transferAssetsUsingTypeAndThen()
.globalConcensusEthereum(),
fee: {
asset: dot,
balance: BalanceBuilder().substrate().system().account(),
xcmDeliveryFeeAmount,
},
min: AssetMinBuilder().assets().asset(),
min: AssetMinBuilder().foreignAssets().asset(),
}),
],
chain: polkadotAssetHub,
Expand Down
3 changes: 0 additions & 3 deletions packages/sdk/src/getTransferData/getTransferData.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ export async function getBalance({
const cfg = balanceBuilder.build({
address,
asset: polkadot.chain.getBalanceAssetId(asset),
// TODO temporary low effort solution because this will not be needed in V3, with the concept of ChainAsset
globalConcensusId: polkadot.chain.assetsData.get(asset.key)
?.globalConcensusId,
});
if (cfg.type === CallType.Substrate) {
const balance = await polkadot.query(cfg as SubstrateQueryConfig);
Expand Down

0 comments on commit 28503d1

Please sign in to comment.