-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex3_des_as_source.js
66 lines (56 loc) · 2.23 KB
/
ex3_des_as_source.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
const { Keypair, Operation, Server, TransactionBuilder, Network, Asset, TimeoutInfinite, Memo, MemoText } = 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')
const transferFunc = async (master, destinationPublic, asset, amount) => {
console.log('transfer', asset.code)
let account = await server.loadAccount(destinationPublic)
let transaction = new TransactionBuilder(account, { fee: 100 })
.addOperation(
Operation.payment({
source: master.publicKey(),
amount: `${amount}`,
asset: asset,
destination: destinationPublic,
})
)
.addOperation(
Operation.payment({
source: master.publicKey(),
amount: `${2*0.00001}`,
asset: asset,
destination: destinationPublic,
})
)
.addMemo(Memo.text(`${Date.now()}`))
.setTimeout(TimeoutInfinite)
.build()
transaction.sign(master)
return server.submitTransaction(transaction)
}
txs = walletAccounts.map(w => transferFunc(masterAccount, w.publicKey(), Asset.native(), amount))
await Promise.all(txs)
}
const start = async () => {
console.log('example 3 - receiver as source')
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)
await engine.transferNative(godAccount, masterAccount.publicKey(), 20)
await engine.createAccountsWithSigner(masterAccount, 2, ...wallets)
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)