This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
reserve_transfer.eg.ts
222 lines (201 loc) · 6.87 KB
/
reserve_transfer.eg.ts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* @title XCM Reserve Asset Transfer
* @description Perform an XCM reserve asset transfer, in which two chains, which
* do not trust one another, rely on a third chain to store assets and facilitate
* the exchange.
* @test_skip
*/
import { DoubleEncoded, Instruction, rococoDevXcm } from "@capi/rococo-dev-xcm"
import {
$assetDetails,
AssetId,
Fungibility,
Junctions,
NetworkId,
rococoDevXcmStatemine,
VersionedMultiAssets,
VersionedMultiLocation,
VersionedXcm,
WeightLimit,
XcmV1Junction,
XcmV1MultiAsset,
XcmV1MultiLocation,
} from "@capi/rococo-dev-xcm-statemine"
import { rococoDevXcmTrappist } from "@capi/rococo-dev-xcm-trappist"
import { assert, assertNotEquals } from "asserts"
import { $, alice as root, createDevUsers, is, Rune, ValueRune } from "capi"
import { $siblId } from "capi/patterns/para_id"
import { signature } from "capi/patterns/signature/statemint"
import { retry } from "../../deps/std/async.ts"
const { alexa, billy } = await createDevUsers()
/// Define some constants for later use.
const RESERVE_ASSET_ID = 1
const RESERVE_CHAIN_ID = 1000 // Statemine
const TRAPPIST_ASSET_ID = RESERVE_ASSET_ID
const TRAPPIST_CHAIN_ID = 2000
/// Define some common options to be used along with `retry`,
/// which will poll for XCM-resulting changes.
const retryOptions = {
multiplier: 1,
maxAttempts: Infinity,
maxTimeout: 2 * 60 * 1000,
}
/// Create a sufficient asset with Sudo. When targeting a common good
/// parachain, access root instead through the relay chain.
await rococoDevXcm.Sudo
// ae8aa6c (clean up reserve transfer example)
.sudo({
call: rococoDevXcm.ParasSudoWrapper.sudoQueueDownwardXcm({
id: RESERVE_CHAIN_ID,
xcm: VersionedXcm.V2(
Rune.array([
Instruction.Transact({
originType: "Superuser",
requireWeightAtMost: 1000000000n,
call: DoubleEncoded({
encoded: rococoDevXcmStatemine.Assets.forceCreate({
id: RESERVE_ASSET_ID,
isSufficient: true,
minBalance: 1n,
owner: alexa.address,
}).callData,
}),
}),
]),
),
}),
})
.signed(signature({ sender: root }))
.sent()
.dbgStatus("Rococo(root) > Statemine(root): Create asset")
.finalized()
.run()
/// Wait for the asset to be recorded in storage.
const assetDetails = await retry(
() => rococoDevXcmStatemine.Assets.Asset.value(RESERVE_ASSET_ID).unhandle(is(undefined)).run(),
retryOptions,
)
/// Ensure the reserve asset was created.
console.log("Statemine: Asset created", assetDetails)
$.assert($assetDetails, assetDetails)
/// Mint assets on reserve parachain.
await rococoDevXcmStatemine.Assets
.mint({
id: RESERVE_ASSET_ID,
amount: 100000000000000n,
beneficiary: billy.address,
})
.signed(signature({ sender: alexa }))
.sent()
.dbgStatus("Statemine(Alexa): Mint reserve asset to Billy")
.finalized()
.run()
const billyStatemintBalance = rococoDevXcmStatemine.Assets.Account
.value([RESERVE_ASSET_ID, billy.publicKey])
.unhandle(is(undefined))
.access("balance")
const billyStatemintBalanceInitial = await billyStatemintBalance.run()
console.log("Statemine(Billy): asset balance", billyStatemintBalanceInitial)
$.assert($.u128, billyStatemintBalanceInitial)
/// Create the asset on the Trappist parachain.
await rococoDevXcmTrappist.Sudo
.sudo({
call: rococoDevXcmTrappist.Assets.forceCreate({
id: TRAPPIST_ASSET_ID,
isSufficient: false,
minBalance: 1n,
owner: alexa.address,
}),
})
.signed(signature({ sender: root }))
.sent()
.dbgStatus("Trappist(root): Create derived asset")
.finalized()
.run()
const assetsPalletId = rococoDevXcmStatemine.Assets.into(ValueRune).access("id")
/// Register Trappist parachain asset id to reserve asset id.
await rococoDevXcmTrappist.Sudo
.sudo({
call: rococoDevXcmTrappist.AssetRegistry.registerReserveAsset({
assetId: TRAPPIST_ASSET_ID,
assetMultiLocation: XcmV1MultiLocation({
parents: 1,
interior: Junctions.X3(
XcmV1Junction.Parachain(RESERVE_CHAIN_ID),
XcmV1Junction.PalletInstance(assetsPalletId),
XcmV1Junction.GeneralIndex(BigInt(RESERVE_ASSET_ID)),
),
}),
}),
})
.signed(signature({ sender: root }))
.sent()
.dbgStatus("Trappist(root): Register AssetId to Reserve AssetId")
.finalized()
.run()
/// Reserve transfer asset id on reserve parachain to Trappist parachain.
const messageHash = await rococoDevXcmStatemine.PolkadotXcm
.limitedReserveTransferAssets({
dest: VersionedMultiLocation.V1(XcmV1MultiLocation({
parents: 1,
interior: Junctions.X1(
XcmV1Junction.Parachain(TRAPPIST_CHAIN_ID),
),
})),
beneficiary: VersionedMultiLocation.V1(XcmV1MultiLocation({
parents: 0,
interior: Junctions.X1(
XcmV1Junction.AccountId32({
network: NetworkId.Any(),
id: billy.publicKey,
}),
),
})),
assets: VersionedMultiAssets.V1(Rune.array([XcmV1MultiAsset({
id: AssetId.Concrete(XcmV1MultiLocation({
parents: 0,
interior: Junctions.X2(
XcmV1Junction.PalletInstance(assetsPalletId),
XcmV1Junction.GeneralIndex(BigInt(RESERVE_ASSET_ID)),
),
})),
fun: Fungibility.Fungible(10000000000000n),
})])),
feeAssetItem: 0,
weightLimit: WeightLimit.Unlimited(),
})
.signed(signature({ sender: billy }))
.sent()
.dbgStatus("Statemine(Billy): Reserve transfer to Trappist")
.finalizedEvents("XcmpQueue", "XcmpMessageSent")
.access(0, "event", "value", "messageHash")
.run()
/// Ensure that `messageHash` is of type `Uint8Array`
console.log("XcmpMessageSent.messageHash:", messageHash)
$.assert($.str, messageHash)
/// Retrieve billy's balance on Trappist.
const { balance: billyTrappistBalance } = await retry(
() =>
rococoDevXcmTrappist.Assets.Account
.value([TRAPPIST_ASSET_ID, billy.publicKey])
.unhandle(is(undefined))
.run(),
retryOptions,
)
/// Ensure the balance is greater than zero.
console.log("Trappist(Billy): asset balance:", billyTrappistBalance)
assert(billyTrappistBalance > 0)
/// Retrieve Billy's balance on statemint.
const billyStatemintBalanceFinal = await billyStatemintBalance.run()
/// Ensure the balance is different from the initial.
console.log("Statemine(Billy): asset balance:", billyStatemintBalanceFinal)
assertNotEquals(billyStatemintBalanceInitial, billyStatemintBalanceFinal)
/// Retrieve the statemint sovereign account balance.
const statemintSovereignAccountBalance = await rococoDevXcmStatemine.Assets.Account
.value([RESERVE_ASSET_ID, $siblId.encode(TRAPPIST_CHAIN_ID)])
.unhandle(is(undefined))
.access("balance")
.run()
/// Ensure the balance is greater than zero.
console.log("Statemine(TrappistSovereignAccount): asset balance", statemintSovereignAccountBalance)
assert(statemintSovereignAccountBalance > 0)