-
Notifications
You must be signed in to change notification settings - Fork 149
/
sendXcm.js
86 lines (79 loc) · 3.54 KB
/
sendXcm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require('dotenv').config();
const { ApiPromise, Keyring, WsProvider } = require('@polkadot/api');
const BN = require('bn.js');
const typedefs = require('@phala/typedefs').latest;
const bn1e12 = new BN(10).pow(new BN(12));
function createTransferXcm(
api, fromLocation='PHA30', toParaId=100, amount=100,
toAddr='45R2pfjQUW2s9PQRHU48HQKLKHVMaDja7N3wpBtmF28UYDs2'
) {
const bnAmount = bn1e12.muln(amount);
return api.createType('Xcm', {
WithdrawAsset: api.createType('WithdrawAsset', {
assets: [
api.createType('MultiAsset', {
ConcreteFungible: api.createType('ConcreteFungible', {
id: api.createType('MultiLocation', {
X1: api.createType('Junction', {
'GeneralKey': fromLocation
})
}),
amount: api.createType('Compact<U128>', bnAmount),
})
}),
],
effects: [
api.createType('Order', {
DepositReserveAsset: api.createType('DepositReserveAsset', {
assets: [api.createType('MultiAsset', 'All')],
dest: api.createType('MultiLocation', {
X2: [
api.createType('Junction', 'Parent'),
api.createType('Junction', {
Parachain: api.createType('Compact<U32>', toParaId),
}),
]
}),
effects: [
api.createType('Order', {
DepositAsset: api.createType('DepositAsset', {
assets: [
api.createType('MultiAsset', 'All'),
],
dest: api.createType('MultiLocation', {
X1: api.createType('Junction', {
AccountId32: api.createType('AccountId32Junction', {
network: api.createType('NetworkId', 'Any'),
id: toAddr,
})
})
})
})
}),
]
})
})
],
})
});
}
async function main () {
const wsProvider = new WsProvider(process.env.ENDPOINT);
const api = await ApiPromise.create({ provider: wsProvider, types: typedefs });
const keyring = new Keyring({ type: 'sr25519' });
const sender = keyring.addFromUri('//Alice');
await new Promise(async resolve => {
const unsub = await api.tx.xcmHandler.executeXcm(createTransferXcm(api))
.signAndSend(sender, (result) => {
console.log(`Current status is ${result.status}`);
if (result.status.isInBlock) {
console.log(`Transaction included at blockHash ${result.status.asInBlock}`);
} else if (result.status.isFinalized) {
console.log(`Transaction finalized at blockHash ${result.status.asFinalized}`);
unsub();
resolve();
}
});
});
}
main().catch(console.error).finally(() => process.exit());