Removes all prepare methods (#1605)

* deprecate and alias prepareTransaction

* delete prepareTransaction and replace methods in check-cancel

* WIP update check-cash

* remove all prepares

* remove prepares from client

* fix ts issues

* remove tests

* fix tests

* additional cleanup

* fix integration tests

* remove console statement

* re-add helper function

* fix imports

* fix more issues with integration tests

Co-authored-by: Omar Khan <khancodegt@gmail.com>
This commit is contained in:
Mayukha Vadari
2021-09-09 14:31:54 -04:00
parent 1115f17a3e
commit af7b187dc7
44 changed files with 429 additions and 6153 deletions

View File

@@ -4,10 +4,20 @@ import _ from 'lodash'
import { isValidXAddress } from 'ripple-address-codec'
import { Client } from 'xrpl-local'
import { isValidSecret } from 'xrpl-local/utils'
import {
AccountSet,
OfferCreate,
SignerListSet,
TrustSet,
} from 'xrpl-local/models/transactions'
import {
isValidSecret,
generateXAddress,
xrpToDrops,
convertStringToHex,
} from 'xrpl-local/utils'
import { generateXAddress } from '../../src/utils/generateAddress'
import requests from '../fixtures/requests'
// import requests from '../fixtures/requests'
import { payTo, ledgerAccept } from './utils'
import wallet from './wallet'
@@ -116,16 +126,20 @@ const masterSecret = 'snoPBrXtMeMyMHUVTgbuqAfg1SUTb'
function makeTrustLine(testcase, address, secret) {
const client = testcase.client
const specification = {
currency: 'USD',
counterparty: masterAccount,
limit: '1341.1',
ripplingDisabled: true,
const trustSet: TrustSet = {
TransactionType: 'TrustSet',
Account: address,
LimitAmount: {
value: '1341.1',
issuer: masterAccount,
currency: 'USD',
},
Flags: 0x00020000,
}
const trust = client
.prepareTrustline(address, specification, {})
.then((data) => {
const signed = client.sign(data.txJSON, secret)
.autofill(trustSet)
.then(async (tx) => {
const signed = client.sign(JSON.stringify(tx), secret)
if (address === wallet.getAddress()) {
testcase.transactions.push(signed.id)
}
@@ -134,32 +148,65 @@ function makeTrustLine(testcase, address, secret) {
tx_blob: signed.signedTransaction,
})
})
.then(() => ledgerAccept(client))
.then((response) => {
if (
response.result.engine_result !== 'tesSUCCESS' &&
response.result.engine_result !== 'tecPATH_PARTIAL'
) {
console.log(response)
assert.fail(`Response not successful, ${response.result.engine_result}`)
}
ledgerAccept(client)
})
return trust
}
function makeOrder(client, address, specification, secret) {
function makeOrder(client, offerCreate, secret) {
return client
.prepareOrder(address, specification)
.then((data) => client.sign(data.txJSON, secret))
.autofill(offerCreate)
.then((tx) => client.sign(JSON.stringify(tx), secret))
.then((signed) =>
client.request({ command: 'submit', tx_blob: signed.signedTransaction }),
)
.then(() => ledgerAccept(client))
.then((response) => {
if (
response.result.engine_result !== 'tesSUCCESS' &&
response.result.engine_result !== 'tecPATH_PARTIAL'
) {
console.log(response)
assert.fail(`Response not successful, ${response.result.engine_result}`)
}
ledgerAccept(client)
})
}
function setupAccounts(testcase) {
const client = testcase.client
let fundAmount = '20'
const promise = payTo(client, 'rMH4UxPrbuMa1spCBR98hLLyNJp4d8p4tM')
.then(() => payTo(client, wallet.getAddress()))
.then(() => payTo(client, testcase.newWallet.xAddress))
.then(() => payTo(client, 'rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc'))
.then(() => payTo(client, 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q'))
const promise = client
.request({ command: 'server_info' })
.then(
(response) =>
(fundAmount = xrpToDrops(
Number(response.result.info.validated_ledger.reserve_base_xrp) * 2,
)),
)
.then(() => payTo(client, 'rMH4UxPrbuMa1spCBR98hLLyNJp4d8p4tM', fundAmount))
.then(() => payTo(client, wallet.getAddress(), fundAmount))
.then(() => payTo(client, testcase.newWallet.classicAddress, fundAmount))
.then(() => payTo(client, 'rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc', fundAmount))
.then(() => payTo(client, 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q', fundAmount))
.then(() => {
const accountSet: AccountSet = {
TransactionType: 'AccountSet',
Account: masterAccount,
// default ripple
SetFlag: 8,
}
return client
.prepareSettings(masterAccount, { defaultRipple: true })
.then((data) => client.sign(data.txJSON, masterSecret))
.autofill(accountSet)
.then((tx) => client.sign(JSON.stringify(tx), masterSecret))
.then((signed) =>
client.request({
command: 'submit',
@@ -181,44 +228,30 @@ function setupAccounts(testcase) {
.then(() => payTo(client, wallet.getAddress(), '123', 'USD', masterAccount))
.then(() => payTo(client, 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q'))
.then(() => {
const orderSpecification = {
direction: 'buy',
quantity: {
const offerCreate: OfferCreate = {
TransactionType: 'OfferCreate',
Account: testcase.newWallet.xAddress,
TakerPays: {
currency: 'USD',
value: '432',
counterparty: masterAccount,
},
totalPrice: {
currency: 'XRP',
value: '432',
issuer: masterAccount,
},
TakerGets: xrpToDrops('432'),
}
return makeOrder(
testcase.client,
testcase.newWallet.xAddress,
orderSpecification,
testcase.newWallet.secret,
)
return makeOrder(testcase.client, offerCreate, testcase.newWallet.secret)
})
.then(() => {
const orderSpecification = {
direction: 'buy',
quantity: {
currency: 'XRP',
value: '1741',
},
totalPrice: {
const offerCreate: OfferCreate = {
TransactionType: 'OfferCreate',
Account: masterAccount,
TakerPays: xrpToDrops('1741'),
TakerGets: {
currency: 'USD',
value: '171',
counterparty: masterAccount,
issuer: masterAccount,
},
}
return makeOrder(
testcase.client,
masterAccount,
orderSpecification,
masterSecret,
)
return makeOrder(testcase.client, offerCreate, masterSecret)
})
return promise
}
@@ -234,7 +267,10 @@ function suiteSetup(this: any) {
setup
.bind(this)(serverUrl)
.then(() => ledgerAccept(this.client))
.then(() => (this.newWallet = generateXAddress()))
.then(
() =>
(this.newWallet = generateXAddress({ includeClassicAddress: true })),
)
// two times to give time to server to send `ledgerClosed` event
// so getLedgerVersion will return right value
.then(() => ledgerAccept(this.client))
@@ -256,128 +292,12 @@ function suiteSetup(this: any) {
describe('integration tests', function () {
const address = wallet.getAddress()
const instructions = { maxLedgerVersionOffset: 10 }
this.timeout(TIMEOUT)
before(suiteSetup)
beforeEach(_.partial(setup, serverUrl))
afterEach(teardown)
it('trustline', function () {
return this.client
.request({
command: 'ledger',
ledger_index: 'validated',
})
.then((response) => response.result.ledger_index)
.then((ledgerVersion) => {
return this.client
.prepareTrustline(
address,
requests.prepareTrustline.simple,
instructions,
)
.then((prepared) =>
testTransaction(this, 'TrustSet', ledgerVersion, prepared),
)
})
})
it('payment', function () {
const amount = { currency: 'XRP', value: '0.000001' }
const paymentSpecification = {
source: {
address,
maxAmount: amount,
},
destination: {
address: 'rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc',
amount,
},
}
return this.client
.request({
command: 'ledger',
ledger_index: 'validated',
})
.then((response) => response.result.ledger_index)
.then((ledgerVersion) => {
return this.client
.preparePayment(address, paymentSpecification, instructions)
.then((prepared) =>
testTransaction(this, 'Payment', ledgerVersion, prepared),
)
})
})
it('order', function () {
const orderSpecification = {
direction: 'buy',
quantity: {
currency: 'USD',
value: '237',
counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
},
totalPrice: {
currency: 'XRP',
value: '0.0002',
},
}
const expectedOrder = {
flags: 0,
quality: '1.185',
taker_gets: '200',
taker_pays: {
currency: 'USD',
value: '237',
issuer: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
},
}
return this.client
.request({
command: 'ledger',
ledger_index: 'validated',
})
.then((response) => response.result.ledger_index)
.then((ledgerVersion) => {
return this.client
.prepareOrder(address, orderSpecification, instructions)
.then((prepared) =>
testTransaction(this, 'OfferCreate', ledgerVersion, prepared),
)
.then((result) => {
const txData = JSON.parse(result.txJSON)
return this.client
.request({
command: 'account_offers',
account: address,
})
.then((response) => response.result.offers)
.then((orders) => {
assert(orders && orders.length > 0)
const createdOrder = orders.filter((order) => {
return order.seq === txData.Sequence
})[0]
assert(createdOrder)
delete createdOrder.seq
assert.deepEqual(createdOrder, expectedOrder)
return txData
})
})
.then((txData) =>
this.client
.prepareOrderCancellation(
address,
{ orderSequence: txData.Sequence },
instructions,
)
.then((prepared) =>
testTransaction(this, 'OfferCancel', ledgerVersion, prepared),
),
)
})
})
it('isConnected', function () {
assert(this.client.isConnected())
})
@@ -390,29 +310,29 @@ describe('integration tests', function () {
})
})
it('getTrustlines', function () {
const fixture = requests.prepareTrustline.simple
const { currency, counterparty } = fixture
const options = { currency, counterparty }
return this.client.getTrustlines(address, options).then((data) => {
assert(data && data.length > 0 && data[0] && data[0].specification)
const specification = data[0].specification
assert.strictEqual(Number(specification.limit), Number(fixture.limit))
assert.strictEqual(specification.currency, fixture.currency)
assert.strictEqual(specification.counterparty, fixture.counterparty)
})
})
// it('getTrustlines', function () {
// const fixture = requests.prepareTrustline.simple
// const { currency, counterparty } = fixture
// const options = { currency, counterparty }
// return this.client.getTrustlines(address, options).then((data) => {
// assert(data && data.length > 0 && data[0] && data[0].specification)
// const specification = data[0].specification
// assert.strictEqual(Number(specification.limit), Number(fixture.limit))
// assert.strictEqual(specification.currency, fixture.currency)
// assert.strictEqual(specification.counterparty, fixture.counterparty)
// })
// })
it('getBalances', function () {
const fixture = requests.prepareTrustline.simple
const { currency, counterparty } = fixture
const options = { currency, counterparty }
return this.client.getBalances(address, options).then((data) => {
assert(data && data.length > 0 && data[0])
assert.strictEqual(data[0].currency, fixture.currency)
assert.strictEqual(data[0].counterparty, fixture.counterparty)
})
})
// it('getBalances', function () {
// const fixture = requests.prepareTrustline.simple
// const { currency, counterparty } = fixture
// const options = { currency, counterparty }
// return this.client.getBalances(address, options).then((data) => {
// assert(data && data.length > 0 && data[0])
// assert.strictEqual(data[0].currency, fixture.currency)
// assert.strictEqual(data[0].counterparty, fixture.counterparty)
// })
// })
it('getOrderbook', function () {
const orderbook = {
@@ -505,98 +425,122 @@ describe('integration tests', function () {
assert(isValidXAddress(newWallet.xAddress))
assert(isValidSecret(newWallet.secret))
})
})
describe('integration tests - standalone rippled', function () {
const instructions = { maxLedgerVersionOffset: 10 }
this.timeout(TIMEOUT)
beforeEach(_.partial(setup, serverUrl))
afterEach(teardown)
const address = 'r5nx8ZkwEbFztnc8Qyi22DE9JYjRzNmvs'
const secret = 'ss6F8381Br6wwpy9p582H8sBt19J3'
const multisignAccount = 'r5nx8ZkwEbFztnc8Qyi22DE9JYjRzNmvs'
const multisignSecret = 'ss6F8381Br6wwpy9p582H8sBt19J3'
const signer1address = 'rQDhz2ZNXmhxzCYwxU6qAbdxsHA4HV45Y2'
const signer1secret = 'shK6YXzwYfnFVn3YZSaMh5zuAddKx'
const signer2address = 'r3RtUvGw9nMoJ5FuHxuoVJvcENhKtuF9ud'
const signer2secret = 'shUHQnL4EH27V4EiBrj6EfhWvZngF'
it('submit multisigned transaction', function () {
const signers = {
threshold: 2,
weights: [
{ address: signer1address, weight: 1 },
{ address: signer2address, weight: 1 },
const signerListSet: SignerListSet = {
TransactionType: 'SignerListSet',
Account: multisignAccount,
SignerEntries: [
{
SignerEntry: {
Account: signer1address,
SignerWeight: 1,
},
},
{
SignerEntry: {
Account: signer2address,
SignerWeight: 1,
},
},
],
SignerQuorum: 2,
}
let minLedgerVersion = null
return payTo(this.client, address)
.then(() => {
return this.client
.request({
command: 'ledger',
ledger_index: 'validated',
})
.then((response) => response.result.ledger_index)
.then((ledgerVersion) => {
minLedgerVersion = ledgerVersion
let fundAmount = '20'
return this.client
.request({ command: 'server_info' })
.then(
(response) =>
(fundAmount = xrpToDrops(
Number(response.result.info.validated_ledger.reserve_base_xrp) * 2,
)),
)
.then(() =>
payTo(this.client, multisignAccount, fundAmount)
.then(() => {
return this.client
.prepareSettings(address, { signers }, instructions)
.then((prepared) => {
.request({
command: 'ledger',
ledger_index: 'validated',
})
.then((response) => response.result.ledger_index)
.then((ledgerVersion) => {
minLedgerVersion = ledgerVersion
})
.then(() => this.client.autofill(signerListSet, 2))
.then((tx) => {
return testTransaction(
this,
'SignerListSet',
ledgerVersion,
prepared,
address,
secret,
minLedgerVersion,
{ txJSON: JSON.stringify(tx) },
multisignAccount,
multisignSecret,
)
})
})
})
.then(() => {
const multisignInstructions = { ...instructions, signersCount: 2 }
return this.client
.prepareSettings(
address,
{ domain: 'example.com' },
multisignInstructions,
)
.then((prepared) => {
const signed1 = this.client.sign(prepared.txJSON, signer1secret, {
signAs: signer1address,
})
const signed2 = this.client.sign(prepared.txJSON, signer2secret, {
signAs: signer2address,
})
const combined = this.client.combine([
signed1.signedTransaction,
signed2.signedTransaction,
])
return this.client
.request({
command: 'submit',
tx_blob: combined.signedTransaction,
})
.then((response) =>
acceptLedger(this.client).then(() => response),
.then(() => {
const accountSet: AccountSet = {
TransactionType: 'AccountSet',
Account: multisignAccount,
Domain: convertStringToHex('example.com'),
}
return this.client.autofill(accountSet, 2).then((tx) => {
const signed1 = this.client.sign(
JSON.stringify(tx),
signer1secret,
{
signAs: signer1address,
},
)
.then((response) => {
assert.strictEqual(response.result.engine_result, 'tesSUCCESS')
const options = { minLedgerVersion }
return verifyTransaction(
this,
combined.id,
'AccountSet',
options,
{},
address,
const signed2 = this.client.sign(
JSON.stringify(tx),
signer2secret,
{
signAs: signer2address,
},
)
const combined = this.client.combine([
signed1.signedTransaction,
signed2.signedTransaction,
])
return this.client
.request({
command: 'submit',
tx_blob: combined.signedTransaction,
})
.then((response) =>
acceptLedger(this.client).then(() => response),
)
})
.catch((error) => {
console.log(error.message)
throw error
})
})
})
.then((response) => {
assert.strictEqual(
response.result.engine_result,
'tesSUCCESS',
)
const options = { minLedgerVersion }
return verifyTransaction(
this,
combined.id,
'AccountSet',
options,
{},
multisignAccount,
)
})
.catch((error) => {
console.log(error.message)
throw error
})
})
}),
)
})
})

View File

@@ -1,57 +1,80 @@
'use strict';
const assert = require('chai').assert
const masterAccount = 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh';
const masterSecret = 'snoPBrXtMeMyMHUVTgbuqAfg1SUTb';
const models = require('xrpl-local/models/transactions')
const utils = require('xrpl-local/utils')
const masterAccount = 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh'
const masterSecret = 'snoPBrXtMeMyMHUVTgbuqAfg1SUTb'
function ledgerAccept(client) {
const request = {command: 'ledger_accept'};
return client.connection.request(request);
const request = { command: 'ledger_accept' }
return client.connection.request(request)
}
function pay(client, from, to, amount, secret, currency = 'XRP', counterparty) {
const paymentSpecification = {
source: {
address: from,
maxAmount: {
value: amount,
currency: currency
}
},
destination: {
address: to,
amount: {
value: amount,
currency: currency
}
}
};
function pay(client, from, to, amount, secret, currency = 'XRP', issuer) {
const paymentAmount =
currency === 'XRP' ? amount : { value: amount, currency, issuer }
if (counterparty != null) {
paymentSpecification.source.maxAmount.counterparty = counterparty;
paymentSpecification.destination.amount.counterparty = counterparty;
const payment = {
TransactionType: 'Payment',
Account: from,
Destination: to,
Amount: paymentAmount,
}
let id = null;
return client.preparePayment(from, paymentSpecification, {})
.then(data => client.sign(data.txJSON, secret))
.then(signed => {
id = signed.id;
return client.request({command: 'submit', tx_blob: signed.signedTransaction});
})
// TODO: add better error handling here
.then(() => ledgerAccept(client))
.then(() => id);
let id = null
return (
client
.autofill(payment, 1)
.then((tx) => {
models.verifyPayment(payment)
return client.sign(JSON.stringify(tx), secret)
})
.then((signed) => {
id = signed.id
return client.request({
command: 'submit',
tx_blob: signed.signedTransaction,
})
})
// TODO: add better error handling here
// TODO: fix path issues
.then((response) => {
if (
response.result.engine_result !== 'tesSUCCESS' &&
response.result.engine_result !== 'tecPATH_PARTIAL'
) {
console.log(response)
assert.fail(
`Response not successful, ${response.result.engine_result}`,
)
}
ledgerAccept(client)
})
.then(() => id)
)
}
function payTo(client, to, amount = '4003218', currency = 'XRP', counterparty) {
return pay(client, masterAccount, to, amount, masterSecret, currency,
counterparty);
function payTo(
client,
to,
amount = '40000000',
currency = 'XRP',
counterparty,
) {
return pay(
client,
masterAccount,
to,
amount,
masterSecret,
currency,
counterparty,
)
}
module.exports = {
pay,
payTo,
ledgerAccept
};
ledgerAccept,
}