Last active
March 6, 2024 14:05
-
-
Save PraetorP/4bc323ff85401abe253897ba990ec29d to your computer and use it in GitHub Desktop.
XCM payment API example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import '@polkadot/api-augment'; | |
import '@polkadot/api-augment/substrate/runtime'; | |
import { ApiPromise, WsProvider } from '@polkadot/api'; | |
async function main() { | |
const api = await ApiPromise.create({ | |
provider: new WsProvider('ws://localhost:9944/'), | |
runtime: { | |
XcmPaymentApi: [ | |
{ | |
methods: { | |
query_acceptable_payment_assets: { | |
description: 'The API to query acceptable payment assets', | |
params: [ | |
{ | |
name: 'version', | |
type: 'u32' | |
} | |
], | |
type: 'Result<Vec<XcmVersionedAssetId>, XcmPaymentApiError>' | |
}, | |
query_weight_to_asset_fee: { | |
description: '', | |
params: [ | |
{ | |
name: 'weight', | |
type: 'WeightV2' | |
}, | |
{ | |
name: 'asset', | |
type: 'XcmVersionedAssetId' | |
} | |
], | |
type: 'Result<u128, XcmPaymentApiError>' | |
}, | |
query_xcm_weight: { | |
description: '', | |
params: [ | |
{ | |
name: 'message', | |
type: 'XcmVersionedXcm' | |
}, | |
], | |
type: 'Result<WeightV2, XcmPaymentApiError>' | |
}, | |
}, | |
version: 1, | |
} | |
], | |
}, | |
types: { | |
XcmPaymentApiError: { | |
_enum: { | |
Unimplemented: 'Null', | |
VersionedConversionFailed: 'Null', | |
WeightNotComputable: 'Null', | |
UnhandledXcmVersion: 'Null', | |
AssetNotFound: 'Null', | |
} | |
} | |
} | |
}); | |
const chainInfo = api.registry.getChainProperties(); | |
console.log(chainInfo.toHuman()); | |
const assets = await api.call.xcmPaymentApi.queryAcceptablePaymentAssets(3); | |
const weightToNativeAssets = await api.call.xcmPaymentApi.queryWeightToAssetFee( | |
{ | |
refTime: 10_000_000_000n, | |
profSize: 0n, | |
}, | |
{ | |
V3: { | |
Concrete: { | |
parents: 0, | |
interior: 'Here', | |
}, | |
} | |
} | |
); | |
const weightToForeingAssets = await api.call.xcmPaymentApi.queryWeightToAssetFee( | |
{ | |
refTime: 10_000_000_000n, | |
profSize: 0n, | |
}, | |
{ | |
V3: { | |
Concrete: { | |
parents: 1, | |
interior: { | |
x1: { | |
parachain: 2040 | |
} | |
}, | |
}, | |
} | |
} | |
); | |
const xcmToWeight = await api.call.xcmPaymentApi.queryXcmWeight( | |
{ | |
V3: | |
[ | |
{ | |
Transact: { | |
originKind: 'Superuser', | |
requireWeightAtMost: { | |
refTime: 200000000n, | |
proofSize: 3000n, | |
}, | |
call: { | |
encoded: '0x0408001cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c0284d717', | |
}, | |
} | |
}, | |
], | |
}, | |
); | |
console.log( | |
'assets:', assets.toJSON(), | |
'\nweightToNativeAsset: ', weightToNativeAssets.toHuman(), | |
'\nweightToForeingAsset: ', weightToForeingAssets.toHuman(), | |
"\nxcmToWeight: ", xcmToWeight.toHuman(), | |
); | |
}; | |
main().then(() => process.exit()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment