-
Notifications
You must be signed in to change notification settings - Fork 0
/
ripplex8-escrow.js
240 lines (188 loc) · 8.51 KB
/
ripplex8-escrow.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
// *******************************************************
// ************* Add Seconds to Current Date *************
// *******************************************************
function addSeconds(numOfSeconds, date = new Date()) {
date.setSeconds(date.getSeconds() + numOfSeconds);
date = Math.floor(date / 1000)
date = date - 946684800
return date;
}
// *******************************************************
// ***************** Create Time Escrow ******************
// *******************************************************
async function createTimeEscrow() {
//-------------------------------------------- Prepare Finish and Cancel Dates
let escrow_finish_date = new Date()
let escrow_cancel_date = new Date()
escrow_finish_date = addSeconds(parseInt(standbyEscrowFinishDateField.value))
escrow_cancel_date = addSeconds(parseInt(standbyEscrowCancelDateField.value))
//------------------------------------------------------Connect to the Ledger
results = "Connecting to the selected ledger.\n"
standbyResultField.value = results
let net = getNet()
results = "Connecting to " + net + "....\n"
const client = new xrpl.Client(net)
await client.connect()
results += "Connected. Creating time-based escrow.\n"
standbyResultField.value = results
const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value)
const operational_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value)
const sendAmount = standbyAmountField.value
results += "\nstandby_wallet.address: = " + standby_wallet.address
standbyResultField.value = results
// ------------------------------------------------------- Prepare transaction
const escrowTx = await client.autofill({
"TransactionType": "EscrowCreate",
"Account": standby_wallet.address,
"Amount": xrpl.xrpToDrops(sendAmount),
"Destination": standbyDestinationField.value,
"FinishAfter": escrow_finish_date,
"CancelAfter": escrow_cancel_date
})
// ------------------------------------------------ Sign prepared instructions
const signed = standby_wallet.sign(escrowTx)
// -------------------------------------------------------- Submit signed blob
const tx = await client.submitAndWait(signed.tx_blob)
results += "\nSequence Number (Save!): " + JSON.stringify(tx.result.Sequence)
results += "\n\nBalance changes: " +
JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2)
standbyBalanceField.value = (await client.getXrpBalance(standby_wallet.address))
operationalBalanceField.value = (await client.getXrpBalance(operational_wallet.address))
standbyResultField.value = results
// ----------------------------------------------Disconnect from the XRP Ledger
client.disconnect()
} // End of createTimeEscrow()
// *******************************************************
// ***************** Finish Time Escrow ******************
// *******************************************************
async function finishEscrow() {
results = "Connecting to the selected ledger.\n"
operationalResultField.value = results
let net = getNet()
results = 'Connecting to ' + getNet() + '....'
const client = new xrpl.Client(net)
await client.connect()
results += "\nConnected. Finishing escrow.\n"
operationalResultField.value = results
const operational_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value)
const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value)
const sendAmount = operationalAmountField.value
results += "\noperational_wallet.address: = " + operational_wallet.address
operationalResultField.value = results
// ------------------------------------------------------- Prepare transaction
// Note that the destination is hard coded.
const prepared = await client.autofill({
"TransactionType": "EscrowFinish",
"Account": operationalAccountField.value,
"Owner": standbyAccountField.value,
"OfferSequence": parseInt(operationalEscrowSequenceField.value)
})
// ------------------------------------------------ Sign prepared instructions
const signed = operational_wallet.sign(prepared)
// -------------------------------------------------------- Submit signed blob
const tx = await client.submitAndWait(signed.tx_blob)
results += "\nBalance changes: " +
JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2)
operationalResultField.value = results
standbyBalanceField.value = (await client.getXrpBalance(standby_wallet.address))
operationalBalanceField.value = (await client.getXrpBalance(operational_wallet.address))
client.disconnect()
} // End of finishEscrow()
// *******************************************************
// ************** Get Standby Escrows ********************
// *******************************************************
async function getStandbyEscrows() {
let net = getNet()
const client = new xrpl.Client(net)
results = 'Connecting to ' + getNet() + '....'
standbyResultField.value = results
await client.connect()
results += '\nConnected.'
standbyResultField.value = results
results= "\nGetting standby account escrows...\n"
const escrow_objects = await client.request({
"id": 5,
"command": "account_objects",
"account": standbyAccountField.value,
"ledger_index": "validated",
"type": "escrow"
})
results += JSON.stringify(escrow_objects.result, null, 2)
standbyResultField.value = results
client.disconnect()
} // End of getStandbyEscrows()
// *******************************************************
// ***************** Get Op Escrows **********************
// *******************************************************
async function getOperationalEscrows() {
let net = getNet()
const client = new xrpl.Client(net)
results = 'Connecting to ' + getNet() + '....'
operationalResultField.value = results
await client.connect()
results += '\nConnected.'
operationalResultField.value = results
results= "\nGetting operational account escrows...\n"
const escrow_objects = await client.request({
"id": 5,
"command": "account_objects",
"account": operationalAccountField.value,
"ledger_index": "validated",
"type": "escrow"
})
results += JSON.stringify(escrow_objects.result, null, 2)
operationalResultField.value = results
client.disconnect()
} // End of getOperationalEscrows()
// *******************************************************
// ************** Get Transaction Info *******************
// *******************************************************
async function getTransaction() {
let net = getNet()
const client = new xrpl.Client(net)
results = 'Connecting to ' + getNet() + '....'
operationalResultField.value = results
await client.connect()
results += '\nConnected.'
operationalResultField.value = results
results= "\nGetting transaction information...\n"
const tx_info = await client.request({
"id": 1,
"command": "tx",
"transaction": operationalTransactionField.value,
})
results += JSON.stringify(tx_info.result, null, 2)
operationalResultField.value = results
client.disconnect()
} // End of getTransaction()
// *******************************************************
// ****************** Cancel Escrow **********************
// *******************************************************
async function cancelEscrow() {
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)
// ------------------------------------------------------- Prepare transaction
const prepared = await client.autofill({
"TransactionType": "EscrowCancel",
"Account": standby_wallet.address,
"Owner": standbyAccountField.value,
"OfferSequence": parseInt(standbyEscrowSequenceNumberField.value)
})
// ------------------------------------------------ Sign prepared instructions
const signed = standby_wallet.sign(prepared)
// -------------------------------------------------------- Submit signed blob
const tx = await client.submitAndWait(signed.tx_blob)
results += "\nBalance changes: " +
JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2)
standbyResultField.value = results
standbyBalanceField.value = (await client.getXrpBalance(standby_wallet.address))
operationalBalanceField.value = (await client.getXrpBalance(operational_wallet.address))
client.disconnect()
}