-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex4_pool.js
107 lines (89 loc) · 3.1 KB
/
ex4_pool.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
const { Keypair, Operation, Server, TransactionBuilder, Network, Asset, TimeoutInfinite, Memo } = 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 (agentPool, masterAccount, amount, ...walletAccounts) => {
console.log('distribute xlm')
walletAccounts.map((w, i) => {
let operations = [Operation.payment({
source: masterAccount.publicKey(),
amount: `${amount}`,
asset: Asset.native(),
destination: w.publicKey(),
})]
let memo = Memo.text(`payment - wallet ${i}`)
agentPool.queueTx(
operations,
[masterAccount],
memo,
(ret) => {
console.log(ret.hash)
})
})
}
const initAgentPool = (poolAccounts) => {
let running = false
agents = poolAccounts.map(a => ({
account: a,
}))
txQueue = []
const executeTx = async (agent, { operations, signers, memo, callback }) => {
let account = await server.loadAccount(agent.account.publicKey())
const txBuilder = new TransactionBuilder(account, { fee: 100 })
.setTimeout(TimeoutInfinite)
operations.forEach(operation => txBuilder.addOperation(operation))
memo && txBuilder.addMemo(memo)
let transaction = txBuilder.build()
transaction.sign(agent.account, ...signers)
result = await server.submitTransaction(transaction)
callback && callback(result)
}
const startPool = () => {
if (running) return
while (txQueue.length && agents.length) {
let agent = agents.shift()
let tx = txQueue.shift()
executeTx(agent, tx)
.catch(engine.printError)
.then(() => {
agents.push(agent)
setImmediate(() => startPool())
})
}
running = false
}
const queueTx = (operations, signers, memo = null, callback = _ => { }) => {
txQueue.push({ operations, signers, memo, callback })
startPool()
}
return {
queueTx
}
}
const start = async () => {
console.log('example 4 - agent pool')
const godAccount = Keypair.random()
const masterAccount = Keypair.random()
const walletCount = 10
const poolCount = 5
const wallets = Array.from(Array(walletCount)).map(a => Keypair.random())
const agentAccounts = Array.from(Array(poolCount)).map(a => Keypair.random())
engine.printAccount(godAccount, 'G O D')
engine.printAccount(masterAccount, 'master')
wallets.forEach((a, i) => engine.printAccount(a, `wallet ${i}`))
agentAccounts.forEach((a, i) => engine.printAccount(a, `agent ${i}`))
const agentPool = initAgentPool(agentAccounts)
await engine.initAccountWithFriendBot(godAccount.publicKey())
await engine.createAccounts(godAccount, 2, masterAccount, ...wallets, ...agentAccounts)
await engine.transferNative(godAccount, masterAccount.publicKey(), 20)
await engine.showBalance(masterAccount.publicKey())
await distributeXlm(agentPool, masterAccount, 2, ...wallets)
setTimeout(
() => engine.showBalance(masterAccount.publicKey()),
15000
)
}
start()
.then(_ => console.log('D O N E'))
.catch(engine.printError)