difftreelog
feat check required balance before sending the msg
in: master
1 file changed
js-packages/scripts/hrmpchannel.tsdiffbeforeafterboth1import {ApiPromise, WsProvider} from '@polkadot/api';1import {ApiPromise, WsProvider} from '@polkadot/api';2import {blake2AsHex} from '@polkadot/util-crypto';2import {blake2AsHex, encodeAddress} from '@polkadot/util-crypto';34export const paraChildSovereignAccount = (relayApi: ApiPromise, paraid: number) => {5 // We are getting a *child* parachain sovereign account,6 // so we need a child prefix: encoded(b"para") == 0x706172617 const childPrefix = '0x70617261';89 const encodedParaId = relayApi.createType('u32', paraid).toHex(true).substring(2);10 const suffix = '000000000000000000000000000000000000000000000000';1112 return childPrefix + encodedParaId + suffix;13};3144const proposeOpenChannel = async (relayApi: ApiPromise, receiverParaId: number) => {15const proposeOpenChannel = async (relayApi: ApiPromise, relayFee: bigint, senderParaId: number, receiverParaId: number) => {5 const conf: any = await relayApi.query.configuration.activeConfig()16 const conf: any = await relayApi.query.configuration.activeConfig()6 .then(data => data.toJSON());17 .then(data => data.toJSON());7 const maxCapacity = conf.hrmpChannelMaxCapacity;18 const maxCapacity = conf.hrmpChannelMaxCapacity;8 const maxSize = conf.hrmpChannelMaxMessageSize;19 const maxSize = conf.hrmpChannelMaxMessageSize;2021 const senderDeposit = BigInt(conf.hrmpSenderDeposit);2223 const requiredBalance = relayFee + senderDeposit;24 const sovereignAccount = await paraChildSovereignAccount(relayApi, senderParaId);25 const balance = await relayApi.query.system.account(sovereignAccount)26 .then(accountInfo => (accountInfo.toJSON() as any).data.free as bigint);2728 if(balance < requiredBalance) {29 throw Error(`Not enough balance on the sender's sovereign account: balance(${balance}) < requiredBalance(${requiredBalance})`);30 }93110 return relayApi.tx.hrmp.hrmpInitOpenChannel(32 return relayApi.tx.hrmp.hrmpInitOpenChannel(11 receiverParaId,33 receiverParaId,14 ).method.toHex();36 ).method.toHex();15};37};163817const proposeAcceptChannel = (relayApi: ApiPromise, senderParaId: number) => {39const proposeAcceptChannel = async (relayApi: ApiPromise, relayFee: bigint, senderParaId: number, recipientParaId: number) => {40 const conf: any = await relayApi.query.configuration.activeConfig()41 .then(data => data.toJSON());42 const recipientDeposit = BigInt(conf.hrmpRecipientDeposit);4344 const requiredBalance = relayFee + recipientDeposit;4546 const sovereignAccount = await paraChildSovereignAccount(relayApi, recipientParaId);47 const balance = await relayApi.query.system.account(sovereignAccount)48 .then(accountInfo => (accountInfo.toJSON() as any).data.free as bigint);4950 if(balance < requiredBalance) {51 throw Error(`Not enough balance on the recipient's sovereign account: balance(${balance}) < requiredBalance(${requiredBalance})`);52 }5318 return relayApi.tx.hrmp.hrmpAcceptOpenChannel(senderParaId).method.toHex();54 return relayApi.tx.hrmp.hrmpAcceptOpenChannel(senderParaId).method.toHex();19};55};39 const otherParaId = await otherApi.query.parachainInfo.parachainId()75 const otherParaId = await otherApi.query.parachainInfo.parachainId()40 .then(data => data.toJSON() as number);76 .then(data => data.toJSON() as number);4142 let encodedRelayCall: string;43 if(op == 'open') {44 encodedRelayCall = await proposeOpenChannel(relayApi, otherParaId);45 } else if(op == 'accept') {46 encodedRelayCall = proposeAcceptChannel(relayApi, otherParaId);47 } else {48 throw Error(`Unknown hrmp channel operation: ${op}`);49 }507751 const relayDecimals = await relayApi.rpc.system.properties()78 const relayDecimals = await relayApi.rpc.system.properties()52 .then(data => data.tokenDecimals.unwrap()[0].toNumber());79 .then(data => data.tokenDecimals.unwrap()[0].toNumber());538054 const relayFee = 1 * (10 ** relayDecimals);81 const relayFee = 2n * BigInt(10 ** relayDecimals);8283 let encodedRelayCall: string;84 if(op == 'open') {85 encodedRelayCall = await proposeOpenChannel(relayApi, relayFee, uniqueParaId, otherParaId);86 } else if(op == 'accept') {87 encodedRelayCall = await proposeAcceptChannel(relayApi, relayFee, otherParaId, uniqueParaId);88 } else {89 throw Error(`Unknown hrmp channel operation: ${op}`);90 }559156 const proposal = uniqueApi.tx.polkadotXcm.send(92 const proposal = uniqueApi.tx.polkadotXcm.send(57 {93 {