-
Notifications
You must be signed in to change notification settings - Fork 4
/
jest.setup.js
106 lines (91 loc) · 3.23 KB
/
jest.setup.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
jest.setTimeout(60000)
const algosdk = require('algosdk')
const shell = require('shelljs')
const waitOn = require('wait-on')
shell.env["ALGOSMALLLAMBDAMSEC"]=1
const privTestNet = 'devnet'
const server = "http://127.0.0.1"
const port = 8080
async function initAlgod() {
let token = await shell.cat(`${privTestNet}/Primary/algod.token`).stdout
return new algosdk.Algodv2(token, server, port)
}
async function initAlgodV1() {
let token = await shell.cat(`${privTestNet}/Primary/algod.token`).stdout
return new algosdk.Algod(token, server, port)
}
async function privateTestNetSetup(appCounter=0) {
// TODO: this could be smarter by checking current appId instead of passing it in somewhat inconsistently
if(appCounter === undefined) appCounter = 0
console.log(`App id is ${appCounter}`)
let status = await shell.exec(`goal node status -d ${privTestNet}/Primary/`, {
async: false,
silent: true
}).stderr
let isNodeRunning = !(/Cannot contact Algorand node/.test(status))
if(isNodeRunning && appCounter < 10) {
} else{
console.log('restarting the devnet')
await shell.exec(`ALGOSMALLLAMBDAMSEC=200 bin/start-devnet.sh`, {
async: false,
silent: true
})
}
await waitOn({
resources: [
`${privTestNet}/Primary/algod.token`
]
})
let addresses = shell.exec(`goal account list -d ${privTestNet}/Primary/ | awk '{print $2}'`, {
silent: true
}).stdout.split("\n").slice(0, -1)
global.accounts = addresses.map((address) => {
var mnemonic = shell.exec(`goal account export -d ${privTestNet}/Primary -a ${address}`, {
silent: true
}).stdout.match(/"(.*)"/)[0].replace(/"/g, "")
return algosdk.mnemonicToSecretKey(mnemonic)
})
}
async function delay(n) {
n = n || 2000
return new Promise(done => {
setTimeout(() => {
done(true)
}, n)
})
}
async function waitForConfirmation(txId) {
let algodclient = await initAlgod()
while (await delay(1000)) {
let pendingTxnInfo = await algodclient.pendingTransactionInformation(txId)
let round = pendingTxnInfo.round
// console.log("PendingTxnInfo: " + pendingTxnInfo.round)
if (round != null && round > 0) {
// console.log("Transaction " + pendingTxnInfo.tx + " confirmed in round " + round)
break
}
}
}
// Utility function to get the latest parameters from the blockchain
async function getChangingParams() {
let algod = await initAlgod()
let params = await algod.getTransactionParams().do()
params.fee = 1000
params.flatFee = true
// params.genesisHash = params.genHash
return params
}
async function signAndSend(signerKey, txn) {
let client = await initAlgod()
let signedTxn = txn.signTxn(signerKey) // this is account.sk
let sentTxn = await client.sendRawTransaction(signedTxn.blob)
await waitForConfirmation(sentTxn.txId)
return sentTxn
}
global.privateTestNetSetup = privateTestNetSetup
global.getChangingParams = getChangingParams
global.waitForConfirmation = waitForConfirmation
global.addresses
global.signAndSend = signAndSend
global.initAlgod = initAlgod
global.initAlgodV1 = initAlgodV1