Skip to content

Commit b598a60

Browse files
committed
Updates based on feedback
1 parent 5bb552d commit b598a60

19 files changed

+1260
-883
lines changed

_code-samples/modular-tutorials/account-support.js

Lines changed: 83 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ function getNet() {
66
let net
77
if (document.getElementById("tn").checked) net = "wss://s.altnet.rippletest.net:51233/"
88
if (document.getElementById("dn").checked) net = "wss://s.devnet.rippletest.net:51233/"
9-
const client = new xrpl.Client(net)
109
return net
1110
} // End of getNet()
1211

@@ -18,23 +17,36 @@ async function getAccount() {
1817
let net = getNet()
1918
const client = new xrpl.Client(net)
2019
await client.connect()
21-
let results = `\nConnected to ${net}.`
22-
let faucetHost = null
23-
const my_wallet = (await client.fundWallet(null, { faucetHost})).wallet
24-
const newAccount = [my_wallet.address, my_wallet.seed]
25-
return (newAccount)
26-
client.disconnect()
20+
resultField.value = `===Getting Account===\n\nConnected to ${net}.`
21+
try {
22+
let faucetHost = null
23+
const my_wallet = (await client.fundWallet(null, { faucetHost})).wallet
24+
const newAccount = [my_wallet.address, my_wallet.seed]
25+
return (newAccount)
26+
}
27+
catch (error) {
28+
console.error('===Error getting account:', error);
29+
results += `\nError: ${error.message}\n`
30+
resultField.value = results
31+
throw error; // Re-throw the error to be handled by the caller
32+
}
33+
finally {
34+
// Disconnect from the client
35+
await client.disconnect();
36+
}
2737
} // End of getAccount()
2838

2939
async function getNewAccount1() {
30-
account1address.value = "Getting new account."
40+
account1address.value = "=== Getting new account. ===\n\n"
41+
account1seed.value = ""
3142
const accountInfo= await getAccount()
3243
account1address.value = accountInfo[0]
3344
account1seed.value = accountInfo[1]
3445
}
3546

3647
async function getNewAccount2() {
37-
account2address.value = "Getting new account."
48+
account2address.value = "=== Getting new account. ===\n\n"
49+
account2seed.value = ""
3850
const accountInfo= await getAccount()
3951
account2address.value = accountInfo[0]
4052
account2seed.value = accountInfo[1]
@@ -48,13 +60,26 @@ async function getAccountFromSeed(my_seed) {
4860
const net = getNet()
4961
const client = new xrpl.Client(net)
5062
await client.connect()
51-
let results = '\nConnected, finding wallet.\n'
63+
let results = '===Finding wallet.===\n\n'
5264
resultField.value = results
53-
const wallet = xrpl.Wallet.fromSeed(my_seed)
54-
// ----------------------Populate the fields for left and right accounts.
55-
const address = wallet.address
56-
client.disconnect()
57-
return (address)
65+
try {
66+
const wallet = xrpl.Wallet.fromSeed(my_seed)
67+
const address = wallet.address
68+
results += "===Wallet found.===\n\n"
69+
results += "Account address: " + address + "\n\n"
70+
resultField.value = results
71+
return (address)
72+
}
73+
catch (error) {
74+
console.error('===Error getting account from seed:', error);
75+
results += `\nError: ${error.message}\n`
76+
resultField.value = results
77+
throw error; // Re-throw the error to be handled by the caller
78+
}
79+
finally {
80+
// Disconnect from the client
81+
await client.disconnect();
82+
}
5883
} // End of getAccountFromSeed()
5984

6085
// *****************************************************
@@ -120,15 +145,32 @@ function populate2() {
120145
}
121146

122147
// *******************************************************
123-
// **************** Get Xrp Balance *********************
148+
// **************** Get XRP Balance *********************
124149
// *******************************************************
125150

126151
async function getXrpBalance() {
127152
const net = getNet()
128153
const client = new xrpl.Client(net)
129154
await client.connect()
130-
xrpBalanceField.value = await client.getXrpBalance(accountAddressField.value)
131-
client.disconnect()
155+
let results = `\n===Getting XRP balance...===\n\n`
156+
resultField.value = results
157+
try {
158+
const wallet = xrpl.Wallet.fromSeed(accountSeedField.value)
159+
const balance = await client.getXrpBalance(wallet.address)
160+
results += accountNameField.value + " current XRP balance: " + balance + "\n\n"
161+
xrpBalanceField.value = await client.getXrpBalance(accountAddressField.value)
162+
resultField.value = results
163+
}
164+
catch (error) {
165+
console.error('Error getting XRP balance:', error);
166+
results += `\nError: ${error.message}\n`
167+
resultField.value = results
168+
throw error; // Re-throw the error to be handled by the caller
169+
}
170+
finally {
171+
// Disconnect from the client
172+
await client.disconnect();
173+
}
132174
} // End of getXrpBalance()
133175

134176
// *******************************************************
@@ -138,21 +180,29 @@ async function getXrpBalance() {
138180
async function getTokenBalance() {
139181
let net = getNet()
140182
const client = new xrpl.Client(net)
141-
results = 'Connecting to ' + getNet() + '....'
142-
resultField.value = results
143183
await client.connect()
144-
results += '\nConnected.'
145-
resultField.value = results
146-
const wallet = xrpl.Wallet.fromSeed(accountSeedField.value)
147-
results= "\nGetting account balance...\n"
148-
const balance = await client.request({
149-
command: "gateway_balances",
150-
account: wallet.address,
151-
ledger_index: "validated",
152-
})
153-
results += JSON.stringify(balance.result, null, 2)
154-
resultField.value = results
155-
xrpBalanceField.value = (await client.getXrpBalance(wallet.address))
156-
client.disconnect()
184+
let results = `===Connected to ${net}.===\n===Getting account token balance...===\n\n`
185+
resultField.value += results
186+
try {
187+
const wallet = xrpl.Wallet.fromSeed(accountSeedField.value)
188+
const balance = await client.request({
189+
command: "gateway_balances",
190+
account: wallet.address,
191+
ledger_index: "validated",
192+
})
193+
results = accountNameField.value + "\'s token balance(s): " + JSON.stringify(balance.result, null, 2) + "\n"
194+
resultField.value += results
195+
xrpBalanceField.value = (await client.getXrpBalance(wallet.address))
196+
}
197+
catch (error) {
198+
console.error('Error getting token balance:', error);
199+
results = `\nError: ${error.message}\n`
200+
resultField.value += results
201+
throw error; // Re-throw the error to be handled by the caller
202+
}
203+
finally {
204+
// Disconnect from the client
205+
await client.disconnect();
206+
}
157207
} // End of getTokenBalance()
158208

_code-samples/modular-tutorials/create-conditional-escrow.js

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,43 @@ async function createConditionalEscrow() {
88
let net = getNet()
99
const client = new xrpl.Client(net)
1010
await client.connect()
11-
let results = `Connected to ${net}, creating conditional escrow.\n`
12-
resultField.value = results
13-
1411
const wallet = xrpl.Wallet.fromSeed(accountSeedField.value)
1512
const sendAmount = amountField.value
16-
17-
wallet.address
18-
13+
let results = `===Connected to ${net}===\n===Creating conditional escrow.===\n\n`
14+
resultField.value = results
1915
let escrow_cancel_date = new Date()
2016
escrow_cancel_date = addSeconds(parseInt(escrowCancelDateField.value))
2117

2218
// ------------------------------------------------------- Prepare transaction
23-
24-
const escrowTx = await client.autofill({
25-
"TransactionType": "EscrowCreate",
26-
"Account": wallet.address,
27-
"Amount": xrpl.xrpToDrops(sendAmount),
28-
"Destination": destinationField.value,
29-
"CancelAfter": escrow_cancel_date,
30-
"Condition": escrowConditionField.value
31-
})
32-
33-
// ------------------------------------------------ Sign prepared instructions
34-
const signed = wallet.sign(escrowTx)
35-
36-
// -------------------------------------------------------- Submit signed blob
37-
const tx = await client.submitAndWait(signed.tx_blob)
38-
results += "\nSequence Number (Save!): " + tx.result.tx_json.Sequence
39-
results += "\n\nBalance changes: " +
40-
JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2)
41-
xrpBalanceField.value = (await client.getXrpBalance(wallet.address))
42-
resultField.value = results
43-
44-
// ----------------------------------------------Disconnect from the XRP Ledger
45-
client.disconnect()
46-
19+
try {
20+
const escrowTx = await client.autofill({
21+
"TransactionType": "EscrowCreate",
22+
"Account": wallet.address,
23+
"Amount": xrpl.xrpToDrops(sendAmount),
24+
"Destination": destinationField.value,
25+
"CancelAfter": escrow_cancel_date,
26+
"Condition": escrowConditionField.value
27+
})
28+
29+
// ------------------------------------------------ Sign prepared instructions
30+
const signed = wallet.sign(escrowTx)
31+
32+
// -------------------------------------------------------- Submit signed blob
33+
const tx = await client.submitAndWait(signed.tx_blob)
34+
results = "\n=== *** Sequence Number (Save!): " + tx.result.tx_json.Sequence
35+
results += "\n\n===Balance changes===\n" +
36+
JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2)
37+
xrpBalanceField.value = (await client.getXrpBalance(wallet.address))
38+
resultField.value += results
39+
}
40+
catch (error) {
41+
results += "\n===Error: " + error.message
42+
resultField.value = results
43+
}
44+
finally {
45+
// -------------------------------------------------------- Disconnect
46+
client.disconnect()
47+
}
4748
} // End of createTimeEscrow()
4849

4950
// *******************************************************
@@ -54,31 +55,32 @@ async function finishConditionalEscrow() {
5455
let net = getNet()
5556
const client = new xrpl.Client(net)
5657
await client.connect()
57-
let results = `Connected to ${net}, fulfilling conditional escrow.\n`
58+
let results = `===Connected to ${net}===\n===Fulfilling conditional escrow.===\n`
5859
resultField.value = results
59-
6060
const wallet = xrpl.Wallet.fromSeed(accountSeedField.value)
61-
62-
// ------------------------------------------------------- Prepare transaction
63-
64-
const prepared = await client.autofill({
65-
"TransactionType": "EscrowFinish",
66-
"Account": accountAddressField.value,
67-
"Owner": escrowOwnerField.value,
68-
"OfferSequence": parseInt(escrowSequenceNumberField.value),
69-
"Condition": escrowConditionField.value,
70-
"Fulfillment": escrowFulfillmentField.value
71-
})
72-
73-
// ------------------------------------------------ Sign prepared instructions
74-
const signed = wallet.sign(prepared)
75-
76-
// -------------------------------------------------------- Submit signed blob
77-
const tx = await client.submitAndWait(signed.tx_blob)
78-
results += "\nBalance changes: " +
79-
JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2)
80-
resultField.value = results
81-
xrpBalanceField.value = (await client.getXrpBalance(wallet.address))
82-
client.disconnect()
83-
84-
} // End of finishEscrow()
61+
try {
62+
// ------------------------------------------------------- Prepare transaction
63+
const prepared = await client.autofill({
64+
"TransactionType": "EscrowFinish",
65+
"Account": accountAddressField.value,
66+
"Owner": escrowOwnerField.value,
67+
"OfferSequence": parseInt(escrowSequenceNumberField.value),
68+
"Condition": escrowConditionField.value,
69+
"Fulfillment": escrowFulfillmentField.value
70+
})
71+
const signed = wallet.sign(prepared)
72+
const tx = await client.submitAndWait(signed.tx_blob)
73+
results += "\n===Balance changes===" +
74+
JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2)
75+
resultField.value = results
76+
xrpBalanceField.value = (await client.getXrpBalance(wallet.address))
77+
}
78+
catch (error) {
79+
results += "\n===Error: " + error.message + ".===\n"
80+
resultField.value = results
81+
}
82+
finally {
83+
// -------------------------------------------------------- Disconnect
84+
client.disconnect()
85+
}
86+
} // End of finisConditionalEscrow()

0 commit comments

Comments
 (0)