fix: fix error handling in generateFaucetWallet (#1671)

* fix: handle error correctly in generateFaucetWallet
* test: Adds generateFaucetWallet integration + browser tests
This commit is contained in:
Nathan Nichols
2021-09-27 13:05:15 -07:00
committed by Mayukha Vadari
parent edcdd3a0fc
commit 9e3654d7d6
6 changed files with 98 additions and 15 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "xrpl",
"version": "2.0.0-beta.0",
"version": "2.0.0-beta.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "xrpl",
"version": "2.0.0-beta.0",
"version": "2.0.0-beta.1",
"license": "ISC",
"dependencies": {
"@types/lodash": "^4.14.136",

View File

@@ -54,10 +54,15 @@ async function generateFaucetWallet(
}),
)
// Retrieve the existing account balance
const addressToFundBalance = await getAddressXrpBalance(
this,
fundWallet.classicAddress,
)
let addressToFundBalance: undefined | string
try {
addressToFundBalance = await getAddressXrpBalance(
this,
fundWallet.classicAddress,
)
} catch {
/* addressToFundBalance remains undefined */
}
// Check the address balance is not undefined and is a number
const startingBalance =
@@ -212,13 +217,12 @@ async function getAddressXrpBalance(
): Promise<string> {
// Get all the account balances
try {
const balances = await client.getBalances(address)
const balances = await client.request({
command: 'account_info',
account: address,
})
// Retrieve the XRP balance
const xrpBalance = balances.filter(
(balance) => balance.currency.toUpperCase() === 'XRP',
)
return xrpBalance[0].value
return balances.result.account_data.Balance
} catch (err) {
if (err instanceof Error) {
throw new XRPLFaucetError(
@@ -254,7 +258,13 @@ async function hasAddressBalanceIncreased(
}
try {
const newBalance = Number(await getAddressXrpBalance(client, address))
let newBalance
try {
newBalance = Number(await getAddressXrpBalance(client, address))
} catch {
/* newBalance remains undefined */
}
if (newBalance > originalBalance) {
clearInterval(interval)
resolve(true)
@@ -279,6 +289,7 @@ async function hasAddressBalanceIncreased(
*
* @param client - Client.
* @returns A {@link FaucetNetwork}.
* @throws When the client url is not on altnet or devnet.
*/
function getFaucetUrl(client: Client): FaucetNetwork | undefined {
const connectionUrl = client.connection.getUrl()
@@ -292,7 +303,7 @@ function getFaucetUrl(client: Client): FaucetNetwork | undefined {
return FaucetNetwork.Devnet
}
return undefined
throw new XRPLFaucetError('Faucet URL is not defined or inferrable.')
}
export default generateFaucetWallet

View File

@@ -3,17 +3,23 @@ import path from 'path'
import { expect, assert } from 'chai'
import puppeteer from 'puppeteer'
const TIMEOUT = 60000
describe('Browser Tests', function () {
this.timeout(TIMEOUT)
it('Integration Tests', async function () {
const browser = await puppeteer.launch({ headless: true })
try {
const page = await browser.newPage().catch()
page.setDefaultNavigationTimeout(0)
await page.goto(
path.join('file:///', __dirname, '../localIntegrationRunner.html'),
)
await page.waitForFunction(
'document.querySelector("body").innerText.includes("submit multisigned transaction")',
{ timeout: TIMEOUT },
)
const fails = await page.evaluate(() => {

View File

@@ -0,0 +1,64 @@
import assert from 'assert'
import _ from 'lodash'
import { Client, isValidClassicAddress, isValidXAddress } from 'xrpl-local'
// how long before each test case times out
const TIMEOUT = 60000
// This test is reliant on external networks, and as such may be flaky.
describe('generateFaucetWallet', function () {
this.timeout(TIMEOUT)
it('submit generates a testnet wallet', async function () {
const api = new Client('wss://s.altnet.rippletest.net:51233')
await api.connect()
const wallet = await api.generateFaucetWallet()
assert.notEqual(wallet, undefined)
assert(isValidClassicAddress(wallet?.classicAddress ?? ''))
assert(isValidXAddress(wallet?.getXAddress() ?? ''))
const info = await api.request({
command: 'account_info',
account: wallet?.classicAddress ?? '',
})
assert.equal(info.result.account_data.Balance, '1000000000')
await api.generateFaucetWallet(wallet)
const afterSent = await api.request({
command: 'account_info',
account: wallet?.classicAddress ?? '',
})
assert.equal(afterSent.result.account_data.Balance, '2000000000')
await api.disconnect()
})
it('submit generates a devnet wallet', async function () {
const api = new Client('wss://s.devnet.rippletest.net:51233')
await api.connect()
const wallet = await api.generateFaucetWallet()
assert.notEqual(wallet, undefined)
assert(isValidClassicAddress(wallet?.classicAddress ?? ''))
assert(isValidXAddress(wallet?.getXAddress() ?? ''))
const info = await api.request({
command: 'account_info',
account: wallet?.classicAddress ?? '',
})
assert.equal(info.result.account_data.Balance, '1000000000')
await api.generateFaucetWallet(wallet)
const afterSent = await api.request({
command: 'account_info',
account: wallet?.classicAddress ?? '',
})
assert.equal(afterSent.result.account_data.Balance, '2000000000')
await api.disconnect()
})
})

View File

@@ -36,4 +36,6 @@ export * from './requests/ripplePathFind'
export * from './requests/submit'
export * from './requests/tx'
export * from './requests/utility'
export * from './generateFaucetWallet'
export * from './integration'

View File

@@ -32,6 +32,6 @@ describe('Get Faucet URL', function () {
it('returns undefined if not a Testnet or Devnet server URL', function () {
// Info: setupClient.setup creates a connection to 'localhost'
assert.strictEqual(getFaucetUrl(this.client), undefined)
assert.throws(() => getFaucetUrl(this.client))
})
})