git.delta.rocks / unique-network / refs/commits / 50becf1c3138

difftreelog

feat check required balance before sending the msg

Daniel Shiposha2023-12-25parent: #99f6558.patch.diff
in: master

1 file changed

modifiedjs-packages/scripts/hrmpchannel.tsdiffbeforeafterboth
1import {ApiPromise, WsProvider} from '@polkadot/api';1import {ApiPromise, WsProvider} from '@polkadot/api';
2import {blake2AsHex} from '@polkadot/util-crypto';2import {blake2AsHex, encodeAddress} from '@polkadot/util-crypto';
3
4export 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") == 0x70617261
7 const childPrefix = '0x70617261';
8
9 const encodedParaId = relayApi.createType('u32', paraid).toHex(true).substring(2);
10 const suffix = '000000000000000000000000000000000000000000000000';
11
12 return childPrefix + encodedParaId + suffix;
13};
314
4const 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;
20
21 const senderDeposit = BigInt(conf.hrmpSenderDeposit);
22
23 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);
27
28 if(balance < requiredBalance) {
29 throw Error(`Not enough balance on the sender's sovereign account: balance(${balance}) < requiredBalance(${requiredBalance})`);
30 }
931
10 return relayApi.tx.hrmp.hrmpInitOpenChannel(32 return relayApi.tx.hrmp.hrmpInitOpenChannel(
11 receiverParaId,33 receiverParaId,
14 ).method.toHex();36 ).method.toHex();
15};37};
1638
17const 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);
43
44 const requiredBalance = relayFee + recipientDeposit;
45
46 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);
49
50 if(balance < requiredBalance) {
51 throw Error(`Not enough balance on the recipient's sovereign account: balance(${balance}) < requiredBalance(${requiredBalance})`);
52 }
53
18 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);
41
42 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 }
5077
51 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());
5380
54 const relayFee = 1 * (10 ** relayDecimals);81 const relayFee = 2n * BigInt(10 ** relayDecimals);
82
83 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 }
5591
56 const proposal = uniqueApi.tx.polkadotXcm.send(92 const proposal = uniqueApi.tx.polkadotXcm.send(
57 {93 {