-
Notifications
You must be signed in to change notification settings - Fork 0
/
ripplex5-broker-nfts.js
331 lines (287 loc) · 12.1 KB
/
ripplex5-broker-nfts.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// *******************************************************
// *******************************************************
// ************** Broker Transactions ********************
// *******************************************************
// *******************************************************
// *******************************************************
// *************** Broker Get Offers *********************
// *******************************************************
async function brGetOffers() {
let net = getNet()
const client = new xrpl.Client(net)
results = 'Connecting to ' + getNet() + '...'
brokerResultField.value = results
await client.connect()
results += '\nConnected. Getting offers...'
brokerResultField.value = results
results += '\n\n***Sell Offers***\n'
let nftSellOffers
try {
nftSellOffers = await client.request({
method: "nft_sell_offers",
nft_id: brokerTokenIdField.value
})
} catch (err) {
nftSellOffers = 'No sell offers.'
}
results += JSON.stringify(nftSellOffers,null,2)
brokerResultField.value = results
results += '\n\n***Buy Offers***\n'
let nftBuyOffers
try {
nftBuyOffers = await client.request({
method: "nft_buy_offers",
nft_id: brokerTokenIdField.value
})
} catch (err) {
nftBuyOffers = 'No buy offers.'
}
results += JSON.stringify(nftBuyOffers,null,2)
brokerResultField.value = results
client.disconnect()
}// End of brGetOffers()
// *******************************************************
// ******************* Broker Sale ***********************
// *******************************************************
async function brokerSale() {
const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value)
const operational_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value)
const broker_wallet = xrpl.Wallet.fromSeed (brokerSeedField.value)
let net = getNet()
const client = new xrpl.Client(net)
results = 'Connecting to ' + getNet() + '...'
brokerResultField.value = results
await client.connect()
results += '\nConnected. Brokering sale...'
brokerResultField.value = results
// Prepare transaction -------------------------------------------------------
const transactionBlob = {
"TransactionType": "NFTokenAcceptOffer",
"Account": broker_wallet.classicAddress,
"NFTokenSellOffer": brokerTokenSellOfferIndexField.value,
"NFTokenBuyOffer": brokerTokenBuyOfferIndexField.value,
"NFTokenBrokerFee": brokerBrokerFeeField.value
}
// Submit transaction --------------------------------------------------------
const tx = await client.submitAndWait(transactionBlob,{wallet: broker_wallet})
// Check transaction results -------------------------------------------------
results += "\n\nTransaction result:\n" +
JSON.stringify(tx.result.meta.TransactionResult, null, 2)
results += "\nBalance changes:\n" +
JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2)
operationalBalanceField.value = (await client.getXrpBalance(operational_wallet.address))
standbyBalanceField.value = (await client.getXrpBalance(standby_wallet.address))
brokerBalanceField.value = (await client.getXrpBalance(broker_wallet.address))
brokerResultField.value = results
client.disconnect()
}// End of brokerSale()
// *******************************************************
// ************* Broker Cancel Offer ****************
// *******************************************************
async function brCancelOffer() {
const wallet = xrpl.Wallet.fromSeed(brokerSeedField.value)
let net = getNet()
const client = new xrpl.Client(net)
results = 'Connecting to ' + getNet() + '...'
brokerResultField.value = results
await client.connect()
results += "\nConnected. Cancelling offer..."
brokerResultField.value = results
const tokenOfferIDs = [brokerTokenBuyOfferIndexField.value]
// Prepare transaction -------------------------------------------------------
const transactionBlob = {
"TransactionType": "NFTokenCancelOffer",
"Account": wallet.classicAddress,
"NFTokenOffers": tokenOfferIDs
}
// Submit transaction --------------------------------------------------------
const tx = await client.submitAndWait(transactionBlob,{wallet})
results += "\n\n***Sell Offers***\n"
let nftSellOffers
try {
nftSellOffers = await client.request({
method: "nft_sell_offers",
nft_id: brokerTokenBuyOfferIndexField.value
})
} catch (err) {
nftSellOffers = "No sell offers."
}
results += JSON.stringify(nftSellOffers,null,2)
results += "\n\n***Buy Offers***\n"
let nftBuyOffers
try {
nftBuyOffers = await client.request({
method: "nft_buy_offers",
nft_id: brokerTokenBuyOfferIndexField.value })
} catch (err) {
nftBuyOffers = "No buy offers."
}
results += JSON.stringify(nftBuyOffers,null,2)
// Check transaction results -------------------------------------------------
results += "\nTransaction result:\n" +
JSON.stringify(tx.result.meta.TransactionResult, null, 2)
results += "\nBalance changes:\n" +
JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2)
brokerResultField.value = results
client.disconnect()
}// End of brCancelOffer()
// ***************************************************************************
// ************** Revised Functions ******************************************
// ***************************************************************************
// *******************************************************
// ************* Get Account *****************************
// *******************************************************
async function getAccount(type) {
let net = getNet()
const client = new xrpl.Client(net)
results = 'Connecting to ' + net + '....'
// This uses the default faucet for Testnet/Devnet
let faucetHost = null
if (type == 'standby') {
standbyResultField.value = results
}
if (type == 'operational') {
operationalResultField.value = results
}
if (type == 'broker') {
brokerResultField.value = results
}
await client.connect()
results += '\nConnected, funding wallet.'
if (type == 'standby') {
standbyResultField.value = results
}
if (type == 'operational') {
operationalResultField.value = results
}
if (type == 'broker') {
brokerResultField.value = results
}
// -----------------------------------Create and fund a test account wallet
const my_wallet = (await client.fundWallet(null, { faucetHost })).wallet
results += '\nGot a wallet.'
if (type == 'standby') {
standbyResultField.value = results
}
if (type == 'operational') {
operationalResultField.value = results
}
if (type == 'broker') {
brokerResultField.value = results
}
// -----------------------------------Get the current balance.
const my_balance = (await client.getXrpBalance(my_wallet.address))
if (type == 'standby') {
standbyAccountField.value = my_wallet.address
standbyPubKeyField.value = my_wallet.publicKey
standbyPrivKeyField.value = my_wallet.privateKey
standbyBalanceField.value = (await client.getXrpBalance(my_wallet.address))
standbySeedField.value = my_wallet.seed
results += '\nStandby account created.'
standbyResultField.value = results
}
if (type == 'operational') {
operationalAccountField.value = my_wallet.address
operationalPubKeyField.value = my_wallet.publicKey
operationalPrivKeyField.value = my_wallet.privateKey
operationalSeedField.value = my_wallet.seed
operationalBalanceField.value = (await client.getXrpBalance(my_wallet.address))
results += '\nOperational account created.'
operationalResultField.value = results
}
if (type == 'broker') {
brokerAccountField.value = my_wallet.address
brokerPubKeyField.value = my_wallet.publicKey
brokerPrivKeyField.value = my_wallet.privateKey
brokerSeedField.value = my_wallet.seed
brokerBalanceField.value = (await client.getXrpBalance(my_wallet.address))
results += '\nBroker account created.'
brokerResultField.value = results
}
// --------------- Capture the seeds for accounts for ease of reload.
seeds.value = standbySeedField.value + '\n' + operationalSeedField.value + "\n" +
brokerSeedField.value
client.disconnect()
} // End of getAccount()
// *******************************************************
// ********** Get Accounts from Seeds ********************
// *******************************************************
async function getAccountsFromSeeds() {
let net = getNet()
const client = new xrpl.Client(net)
results = 'Connecting to ' + getNet() + '....'
standbyResultField.value = results
await client.connect()
results += '\nConnected, finding wallets.\n'
standbyResultField.value = results
// -----------------------------------Find the test account wallets
var lines = seeds.value.split('\n');
const standby_wallet = xrpl.Wallet.fromSeed(lines[0])
const operational_wallet = xrpl.Wallet.fromSeed(lines[1])
const broker_wallet = xrpl.Wallet.fromSeed(lines[2])
// -----------------------------------Get the current balance.
const standby_balance = (await client.getXrpBalance(standby_wallet.address))
const operational_balance = (await client.getXrpBalance(operational_wallet.address))
const broker_balance = (await client.getXrpBalance(broker_wallet.address))
standbyAccountField.value = standby_wallet.address
standbyPubKeyField.value = standby_wallet.publicKey
standbyPrivKeyField.value = standby_wallet.privateKey
standbySeedField.value = standby_wallet.seed
standbyBalanceField.value = (await client.getXrpBalance(standby_wallet.address))
operationalAccountField.value = operational_wallet.address
operationalPubKeyField.value = operational_wallet.publicKey
operationalPrivKeyField.value = operational_wallet.privateKey
operationalSeedField.value = operational_wallet.seed
operationalBalanceField.value = (await client.getXrpBalance(operational_wallet.address))
brokerAccountField.value = broker_wallet.address
brokerPubKeyField.value = broker_wallet.publicKey
brokerPrivKeyField.value = broker_wallet.privateKey
brokerSeedField.value = broker_wallet.seed
brokerBalanceField.value = (await client.getXrpBalance(broker_wallet.address))
client.disconnect()
getBalances()
} // End of getAccountsFromSeeds()
// *******************************************************
// ****************** Get Balances ***********************
// *******************************************************
async function getBalances() {
let net = getNet()
const client = new xrpl.Client(net)
results = 'Connecting to ' + getNet() + '....'
standbyResultField.value = results
await client.connect()
results += '\nConnected.'
standbyResultField.value = results
const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value)
const operational_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value)
const broker_wallet = xrpl.Wallet.fromSeed(brokerSeedField.value)
results = "\nGetting standby account balances...\n"
const standby_balances = await client.request({
command: "gateway_balances",
account: standby_wallet.address,
ledger_index: "validated",
hotwallet: [operational_wallet.address]
})
results += JSON.stringify(standby_balances.result, null, 2)
standbyResultField.value = results
results= "\nGetting operational account balances...\n"
const operational_balances = await client.request({
command: "account_lines",
account: operational_wallet.address,
ledger_index: "validated"
})
results += JSON.stringify(operational_balances.result, null, 2)
operationalResultField.value = results
results= "\nGetting broker account balances...\n"
const broker_balances = await client.request({
command: "account_lines",
account: broker_wallet.address,
ledger_index: "validated"
})
results += JSON.stringify(broker_balances.result, null, 2)
brokerResultField.value = results
operationalBalanceField.value = (await client.getXrpBalance(operational_wallet.address))
standbyBalanceField.value = (await client.getXrpBalance(standby_wallet.address))
brokerBalanceField.value = (await client.getXrpBalance(broker_wallet.address))
client.disconnect()
} // End of getBalances()