mirror of
https://github.com/Xahau/xahau.js.git
synced 2026-06-04 09:16:41 +00:00
test: Add Integration tests for Escrow Transactions (#1680)
* test: Add Integration tests for Escrow Transactions
This commit is contained in:
74
test/integration/transactions/escrowCancel.ts
Normal file
74
test/integration/transactions/escrowCancel.ts
Normal file
@@ -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,
|
||||
)
|
||||
})
|
||||
})
|
||||
51
test/integration/transactions/escrowCreate.ts
Normal file
51
test/integration/transactions/escrowCreate.ts
Normal file
@@ -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,
|
||||
)
|
||||
})
|
||||
})
|
||||
73
test/integration/transactions/escrowFinish.ts
Normal file
73
test/integration/transactions/escrowFinish.ts
Normal file
@@ -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)
|
||||
})
|
||||
})
|
||||
@@ -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<string> {
|
||||
const request: AccountInfoRequest = {
|
||||
command: 'account_info',
|
||||
account: wallet.getClassicAddress(),
|
||||
}
|
||||
return (await client.request(request)).result.account_data.Balance
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user