Files
xahau.js/test/integration/utils.ts
Mayukha Vadari 98c9b9bc14 ci: run lint tests (#1603)
* turn on lint tests

* remove tsc

* fix errors in src/utils/hashes

* fix linter errors in src/utils

* fix lint issues in test/

* resolve lint issues in src/client

* resolve dependency cycle

* resolve other linting issues in src/models

* resolve rest of linting issues

* fix tests

* fix linting errors in test/integration

* fix rest of linting issues

* fix test name
2021-10-04 14:10:13 -04:00

94 lines
2.7 KiB
TypeScript

import { assert } from 'chai'
import _ from 'lodash'
import { decode } from 'ripple-binary-codec'
import { Client, Wallet } from 'xrpl-local'
import { Payment, Transaction } from 'xrpl-local/models/transactions'
import { computeSignedTransactionHash } from 'xrpl-local/utils/hashes'
const masterAccount = 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh'
const masterSecret = 'snoPBrXtMeMyMHUVTgbuqAfg1SUTb'
export async function ledgerAccept(client: Client): Promise<void> {
const request = { command: 'ledger_accept' }
await client.connection.request(request)
}
export async function fundAccount(
client: Client,
wallet: Wallet,
): Promise<void> {
const payment: Payment = {
TransactionType: 'Payment',
Account: masterAccount,
Destination: wallet.getClassicAddress(),
// 2 times the amount needed for a new account (20 XRP)
Amount: '400000000',
}
const response = await client.submitTransaction(
Wallet.fromSeed(masterSecret),
payment,
)
if (response.result.engine_result !== 'tesSUCCESS') {
// eslint-disable-next-line no-console -- happens only when something goes wrong
console.log(response)
assert.fail(`Response not successful, ${response.result.engine_result}`)
}
await ledgerAccept(client)
}
export async function generateFundedWallet(client: Client): Promise<Wallet> {
const wallet = Wallet.generate()
await fundAccount(client, wallet)
return wallet
}
export async function verifySubmittedTransaction(
client: Client,
tx: Transaction | string,
): Promise<void> {
const hash = computeSignedTransactionHash(tx)
const data = await client.request({
command: 'tx',
transaction: hash,
})
assert(data.result)
assert.deepEqual(
_.omit(data.result, [
'date',
'hash',
'inLedger',
'ledger_index',
'meta',
'validated',
]),
typeof tx === 'string' ? decode(tx) : tx,
)
if (typeof data.result.meta === 'object') {
assert.strictEqual(data.result.meta.TransactionResult, 'tesSUCCESS')
} else {
assert.strictEqual(data.result.meta, 'tesSUCCESS')
}
}
export async function testTransaction(
client: Client,
transaction: Transaction,
wallet: Wallet,
): Promise<void> {
// sign/submit the transaction
const response = await client.submitTransaction(wallet, transaction)
// check that the transaction was successful
assert.equal(response.status, 'success')
assert.equal(response.type, 'response')
assert.equal(response.result.engine_result, 'tesSUCCESS')
// check that the transaction is on the ledger
const signedTx = _.omit(response.result.tx_json, 'hash')
await ledgerAccept(client)
await verifySubmittedTransaction(client, signedTx as Transaction)
}