-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex2_batch.js
51 lines (42 loc) · 1.69 KB
/
ex2_batch.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
const { Keypair, Operation, Server, TransactionBuilder, Network, Asset, TimeoutInfinite } = require('stellar-sdk')
const horizonUrl = 'https://horizon-testnet.stellar.org'
Network.useTestNetwork()
const server = new Server(horizonUrl)
const engine = require('./engine')(server)
const distributeXlm = async (masterAccount, amount, ...walletAccounts) => {
console.log('distribute xlm')
let account = await server.loadAccount(masterAccount.publicKey())
let txBuilder = new TransactionBuilder(account, { fee: 100 })
.setTimeout(TimeoutInfinite)
walletAccounts.forEach(w => {
txBuilder.addOperation(
Operation.payment({
amount: `${amount}`,
asset: Asset.native(),
destination: w.publicKey(),
})
)
})
const transaction = txBuilder.build()
transaction.sign(masterAccount)
return server.submitTransaction(transaction)
}
const start = async () => {
console.log('example 2 - batch')
const godAccount = Keypair.random()
const masterAccount = Keypair.random()
const walletCount = 5
const wallets = Array.from(Array(walletCount)).map(a => Keypair.random())
engine.printAccount(godAccount, 'G O D')
engine.printAccount(masterAccount, 'master')
wallets.forEach((a, i) => engine.printAccount(a, `wallet${i}`))
await engine.initAccountWithFriendBot(godAccount.publicKey())
await engine.createAccounts(godAccount, 2, masterAccount, ...wallets)
await engine.transferNative(godAccount, masterAccount.publicKey(), 10)
await engine.showBalance(masterAccount.publicKey())
await distributeXlm(masterAccount, 2, ...wallets)
await engine.showBalance(masterAccount.publicKey())
}
start()
.then(_ => console.log('D O N E'))
.catch(engine.printError)