-
Notifications
You must be signed in to change notification settings - Fork 1
/
send-xrp.js
148 lines (129 loc) · 5.06 KB
/
send-xrp.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
// Dependencies for Node.js.
// In browsers, use a <script> tag instead.
if (typeof module !== "undefined") {
// Use var here because const/let are block-scoped to the if statement.
require('dotenv').config()
require('log-timestamp')
var xrpl = require('xrpl')
var fs = require('fs')
var { parse } = require('csv-parse');
}
var network = process.argv[2]
var mode = process.argv[3]
var client
var wallet
var timestamp
addressesToPay = []
function logToFile(data) {
const now = new Date();
timestamp = `${now.toISOString()}`;
// Append to file
fs.appendFile(`log/${mode}-${network}-payxrp.csv`, `${timestamp}${data}\n`, function (err) {
if (err) throw err;
})
// Output to console
//console.log(`${timestamp} ${data}`)
// Output to file
//logToFile('Hello, world!')
}
// Console log to console and file
// credentials
console.log(network)
const createArray = new Promise((resolve, reject) => {
fs.createReadStream("data/addresses.csv")
.pipe(parse({ delimiter: ",", from_line: 1 }))
.on("data", function (row) {
//console.log([row[0]])
addressesToPay.push([row[0]])
})
.on("end", function() {
resolve(addressesToPay)
})
.on("error", function(err) {
reject(err)
})
})
async function main() {
try {
// Connect -------------------------------------------------------------------
if (network == "testnet") {
try {
wallet = xrpl.Wallet.fromSeed(process.env.SEED_TESTNET);
}catch(err) {
console.log("Family seed likely incorrect, please check. Error:", err.message)
process.exit(1)
}
console.log(wallet.address);
console.log("Connecting to Testnet...");
client = new xrpl.Client(process.env.WS_TESTNET);
}
if (network == "mainnet") {
try {
wallet = xrpl.Wallet.fromSeed(process.env.SEED_MAINNET);
}catch(err) {
console.log("Family seed likely incorrect, please check. Error:", err.message)
process.exit(1)
}
console.log(wallet.address);
console.log("Connecting to Mainnet...");
client = new xrpl.Client(process.env.WS_MAINNET);
}
await client.connect();
fs.appendFile(`log/${mode}-${network}-payxrp.csv`, 'Timestamp,AmountSent,DestinationAddress,Fee,Hash\n', (err) => {
if (err) throw err;
})
// Prepare transaction -------------------------------------------------------
for (const arrayAddress of addressesToPay) {
var prepared = await client.autofill({
TransactionType: "Payment",
Account: wallet.address,
Amount: xrpl.xrpToDrops(process.env.XRP_AMOUNT),
Destination: arrayAddress.toString(),
Memos: [
{
Memo: {
MemoData: Buffer.from(process.env.PAY_MEMO, 'utf8').toString('hex').toUpperCase()
}
}
]
});
const max_ledger = prepared.LastLedgerSequence;
console.log("Prepared transaction instructions:", prepared);
console.log(`Sending ${xrpl.dropsToXrp(prepared.Amount)} XRP to ${prepared.Destination} at a fee of ${xrpl.dropsToXrp(prepared.Fee)} XRP\n`);
// Sign prepared instructions ------------------------------------------------
var signed = await wallet.sign(prepared);
//console.log("Identifying hash:", signed.hash)
//console.log("Signed blob:", signed.tx_blob)
// Submit signed blob --------------------------------------------------------
if ( mode == 'payment' ) {
var tx = await client.submitAndWait(signed.tx_blob);
// Check transaction results -------------------------------------------------
let txResult = tx.result.meta.TransactionResult
console.log("Transaction result:", txResult);
if ( tx.result.meta.TransactionResult !== 'tesSUCCESS') {
console.log(`Something wrong, non-success message of ${txResult} returned from TX`)
await client.disconnect()
process.exit(1)
} else {
logToFile(`,${xrpl.dropsToXrp(prepared.Amount)},${prepared.Destination},${xrpl.dropsToXrp(prepared.Fee)},${signed.hash}`)
}
} else {
console.log("Simulating the submitAndWait function")
console.log(`Simulated sending ${xrpl.dropsToXrp(prepared.Amount)} XRP to ${prepared.Destination} at a fee of ${xrpl.dropsToXrp(prepared.Fee)} XRP`)
logToFile(`,${xrpl.dropsToXrp(prepared.Amount)},${prepared.Destination},${xrpl.dropsToXrp(prepared.Fee)},null`)
}
}
// Disconnect ----------------------------------------------------------------
fs.rename(`log/${mode}-${network}-payxrp.csv`, `log/${mode}-${network}-payxrp-${timestamp}.csv`, (err) => {
if (err) throw err;
})
await client.disconnect();
} catch (error) {
console.error(error);
}
}
createArray.then(() => {
main()
}).catch((err) => {
console.error(err)
})