Files
bch-js/test/e2e/send-token/send-token.js
T

101 lines
3.2 KiB
JavaScript
Raw Normal View History

2020-07-27 21:32:51 -07:00
/*
This is an end-to-end test which verified the happy-path of sending an SLP
token. It's really a test of the speed of SLPDB to process new token
transactions.
This program expects two wallets. Wallet 1 must have a small amount of BCH
and an inventory of SLP test tokens. Wallet 2 is the reieving wallet.
*/
// Inspect utility used for debugging.
2021-01-02 15:41:12 -08:00
const util = require('util')
2020-07-27 21:32:51 -07:00
util.inspect.defaultOptions = {
showHidden: true,
colors: true,
depth: 1
}
// const SLPSDK = require("../../../lib/SLP")
// const slpsdk = new SLPSDK()
2021-01-02 15:41:12 -08:00
const WALLET1 = '../wallet1.json'
const WALLET2 = '../wallet2.json'
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
const lib = require('../util/e2e-util')
2020-07-27 21:32:51 -07:00
// The main test function.
// Sends a token and reports on how long it takes to show up in SLPDB production.
2021-01-02 15:41:12 -08:00
async function sendTokenTest () {
2020-07-27 21:32:51 -07:00
try {
// Open the sending wallet.
const sendWallet = await lib.openWallet(WALLET1)
2021-01-02 15:41:12 -08:00
// console.log(`sendWallet: ${JSON.stringify(walletInfo, null, 2)}`)
2020-07-27 21:32:51 -07:00
// Open the recieving wallet.
const recvWallet = await lib.openWallet(WALLET2)
2021-01-02 15:41:12 -08:00
// console.log(`recvWallet: ${JSON.stringify(walletInfo, null, 2)}`)
2020-07-27 21:32:51 -07:00
// Get the balance of the recieving wallet.
// const testTokens = recvWallet.tokenBalance.filter(
// x => testTokenId === x.tokenId
// )
// const startBalance = testTokens[0].balance
const startBalance = await lib.getTestTokenBalance(recvWallet)
console.log(`Starting balance: ${startBalance} test tokens.`)
let newBalance = startBalance
// Send a token to the recieving wallet.
await lib.sendToken(sendWallet, recvWallet)
2021-01-02 15:41:12 -08:00
console.log('Sent test token.')
2020-07-27 21:32:51 -07:00
// Track the time until the balance for the recieving wallet has been updated.
const startTime = new Date()
const waitTime = 10000 // time in milliseconds
// Loop with a definite exit point, so we don't loop forever.
for (let i = 0; i < 50; i++) {
await sleep(waitTime) // Wait for a while before checking
2021-01-02 15:41:12 -08:00
console.log('Checking token balance...')
2020-07-27 21:32:51 -07:00
newBalance = await lib.getTestTokenBalance(recvWallet)
// Break out of the loop once a new balance is detected.
if (newBalance > startBalance) break
// Provide high-level warnings.
const secondsPassed = (i * waitTime) / 1000
if (secondsPassed > 60 * 10) {
2021-01-02 15:41:12 -08:00
console.log('More than 10 minutes passed.')
2020-07-27 21:32:51 -07:00
return false // Fail the test.
} else if (secondsPassed > 60 * 5) {
2021-01-02 15:41:12 -08:00
console.log('More than 5 minutes passed.')
2020-07-27 21:32:51 -07:00
} else if (secondsPassed > 60) {
2021-01-02 15:41:12 -08:00
console.log('More than 1 minute passed.')
2020-07-27 21:32:51 -07:00
}
}
// Calculate the amount of time that has passed.
const endTime = new Date()
let deltaTime = (endTime.getTime() - startTime.getTime()) / 60000
deltaTime = lib.threeDecimals(deltaTime)
console.log(`SLPDB updated token balance in ${deltaTime} minutes.`)
// Consolidate the SLP UTXOs on the recieve wallet.
await lib.sendToken(recvWallet, recvWallet)
return deltaTime // Return the time in minutes it took for SLPDB to update.
} catch (err) {
2021-01-02 15:41:12 -08:00
console.log('Error in e2e/send-token.js/sendTokenTest(): ', err)
2020-07-27 21:32:51 -07:00
return false
}
}
// Promise based sleep function.
2021-01-02 15:41:12 -08:00
function sleep (ms) {
2020-07-27 21:32:51 -07:00
return new Promise(resolve => setTimeout(resolve, ms))
}
module.exports = {
sendTokenTest
}