From 90dae91fb95dbefe51d549d8185bc74a05f100f0 Mon Sep 17 00:00:00 2001 From: Mukul Jangid <49061120+mukulljangid@users.noreply.github.com> Date: Tue, 5 Oct 2021 13:04:34 -0400 Subject: [PATCH] test: Add Integration tests for Escrow Transactions (#1680) * test: Add Integration tests for Escrow Transactions --- test/integration/transactions/escrowCancel.ts | 74 +++++++++++++++++++ test/integration/transactions/escrowCreate.ts | 51 +++++++++++++ test/integration/transactions/escrowFinish.ts | 73 ++++++++++++++++++ test/integration/utils.ts | 13 +++- 4 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 test/integration/transactions/escrowCancel.ts create mode 100644 test/integration/transactions/escrowCreate.ts create mode 100644 test/integration/transactions/escrowFinish.ts diff --git a/test/integration/transactions/escrowCancel.ts b/test/integration/transactions/escrowCancel.ts new file mode 100644 index 00000000..0a89dfe6 --- /dev/null +++ b/test/integration/transactions/escrowCancel.ts @@ -0,0 +1,74 @@ +import { assert } from 'chai' +import _ from 'lodash' + +import { EscrowCancel, EscrowCreate } from 'xrpl-local' + +import serverUrl from '../serverUrl' +import { setupClient, suiteClientSetup, teardownClient } from '../setup' +import { generateFundedWallet, getXRPBalance, testTransaction } from '../utils' + +// how long before each test case times out +const TIMEOUT = 20000 + +describe('EscrowCancel', function () { + this.timeout(TIMEOUT) + + before(suiteClientSetup) + beforeEach(_.partial(setupClient, serverUrl)) + afterEach(teardownClient) + + it('base', async function () { + // get the most recent close_time from the standalone container for cancel & finish after. + const CLOSE_TIME: number = ( + await this.client.request({ + command: 'ledger', + ledger_index: 'validated', + }) + ).result.ledger.close_time + const wallet1 = await generateFundedWallet(this.client) + + const createTx: EscrowCreate = { + Account: this.wallet.getClassicAddress(), + TransactionType: 'EscrowCreate', + Amount: '10000', + Destination: wallet1.getClassicAddress(), + CancelAfter: CLOSE_TIME + 3, + FinishAfter: CLOSE_TIME + 2, + } + + await testTransaction(this.client, createTx, this.wallet) + + const initialBalanceWallet1 = await getXRPBalance(this.client, wallet1) + + // check that the object was actually created + const accountObjects = ( + await this.client.request({ + command: 'account_objects', + account: this.wallet.getClassicAddress(), + }) + ).result.account_objects + + assert.equal(accountObjects.length, 1) + + const sequence = ( + await this.client.request({ + command: 'tx', + transaction: accountObjects[0].PreviousTxnID, + }) + ).result.Sequence + + const cancelTx: EscrowCancel = { + TransactionType: 'EscrowCancel', + Account: this.wallet.getClassicAddress(), + Owner: this.wallet.getClassicAddress(), + OfferSequence: sequence, + } + + await testTransaction(this.client, cancelTx, this.wallet) + + assert.equal( + await getXRPBalance(this.client, wallet1), + initialBalanceWallet1, + ) + }) +}) diff --git a/test/integration/transactions/escrowCreate.ts b/test/integration/transactions/escrowCreate.ts new file mode 100644 index 00000000..39a9edd7 --- /dev/null +++ b/test/integration/transactions/escrowCreate.ts @@ -0,0 +1,51 @@ +import { assert } from 'chai' +import _ from 'lodash' + +import { EscrowCreate } from 'xrpl-local' + +import serverUrl from '../serverUrl' +import { setupClient, suiteClientSetup, teardownClient } from '../setup' +import { generateFundedWallet, testTransaction } from '../utils' + +// how long before each test case times out +const TIMEOUT = 20000 + +describe('EscrowCreate', function () { + this.timeout(TIMEOUT) + + before(suiteClientSetup) + beforeEach(_.partial(setupClient, serverUrl)) + afterEach(teardownClient) + + it('base', async function () { + // get the most recent close_time from the standalone container for finish after. + const CLOSE_TIME: number = ( + await this.client.request({ + command: 'ledger', + ledger_index: 'validated', + }) + ).result.ledger.close_time + + const wallet1 = await generateFundedWallet(this.client) + const tx: EscrowCreate = { + Account: this.wallet.getClassicAddress(), + TransactionType: 'EscrowCreate', + Amount: '10000', + Destination: wallet1.getClassicAddress(), + FinishAfter: CLOSE_TIME + 2, + } + + await testTransaction(this.client, tx, this.wallet) + + // check that the object was actually created + assert.equal( + ( + await this.client.request({ + command: 'account_objects', + account: this.wallet.getClassicAddress(), + }) + ).result.account_objects.length, + 1, + ) + }) +}) diff --git a/test/integration/transactions/escrowFinish.ts b/test/integration/transactions/escrowFinish.ts new file mode 100644 index 00000000..bf1e6714 --- /dev/null +++ b/test/integration/transactions/escrowFinish.ts @@ -0,0 +1,73 @@ +import { assert } from 'chai' +import _ from 'lodash' + +import { EscrowFinish, EscrowCreate } from 'xrpl-local' + +import serverUrl from '../serverUrl' +import { setupClient, suiteClientSetup, teardownClient } from '../setup' +import { generateFundedWallet, getXRPBalance, testTransaction } from '../utils' + +// how long before each test case times out +const TIMEOUT = 20000 + +describe('EscrowFinish', function () { + this.timeout(TIMEOUT) + + before(suiteClientSetup) + beforeEach(_.partial(setupClient, serverUrl)) + afterEach(teardownClient) + + it('base', async function () { + // get the most recent close_time from the standalone container for cancel & finish after. + const CLOSE_TIME: number = ( + await this.client.request({ + command: 'ledger', + ledger_index: 'validated', + }) + ).result.ledger.close_time + const wallet1 = await generateFundedWallet(this.client) + + const AMOUNT = 10000 + + const createTx: EscrowCreate = { + Account: this.wallet.getClassicAddress(), + TransactionType: 'EscrowCreate', + Amount: '10000', + Destination: wallet1.getClassicAddress(), + FinishAfter: CLOSE_TIME + 2, + } + + await testTransaction(this.client, createTx, this.wallet) + + const initialBalance = await getXRPBalance(this.client, wallet1) + + // check that the object was actually created + const accountObjects = ( + await this.client.request({ + command: 'account_objects', + account: this.wallet.getClassicAddress(), + }) + ).result.account_objects + + assert.equal(accountObjects.length, 1) + + const sequence = ( + await this.client.request({ + command: 'tx', + transaction: accountObjects[0].PreviousTxnID, + }) + ).result.Sequence + + const finishTx: EscrowFinish = { + TransactionType: 'EscrowFinish', + Account: this.wallet.getClassicAddress(), + Owner: this.wallet.getClassicAddress(), + OfferSequence: sequence, + } + + await testTransaction(this.client, finishTx, this.wallet) + + const expectedBalance = String(Number(initialBalance) + Number(AMOUNT)) + assert.equal(await getXRPBalance(this.client, wallet1), expectedBalance) + }) +}) diff --git a/test/integration/utils.ts b/test/integration/utils.ts index e611e4ad..09c50ac0 100644 --- a/test/integration/utils.ts +++ b/test/integration/utils.ts @@ -2,7 +2,7 @@ import { assert } from 'chai' import _ from 'lodash' import { decode } from 'ripple-binary-codec' -import { Client, Wallet, Response } from 'xrpl-local' +import { Client, Wallet, Response, AccountInfoRequest } from 'xrpl-local' import { Payment, Transaction } from 'xrpl-local/models/transactions' import { computeSignedTransactionHash } from 'xrpl-local/utils/hashes' @@ -103,3 +103,14 @@ export async function testTransaction( await ledgerAccept(client) await verifySubmittedTransaction(client, signedTx as Transaction) } + +export async function getXRPBalance( + client: Client, + wallet: Wallet, +): Promise { + const request: AccountInfoRequest = { + command: 'account_info', + account: wallet.getClassicAddress(), + } + return (await client.request(request)).result.account_data.Balance +}