Skip to content

Commit

Permalink
txDryRun: Simplify arguments, support txEvents option
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Mar 1, 2021
1 parent bdf76e2 commit 401c53d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
17 changes: 4 additions & 13 deletions es/ae/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,27 +176,18 @@ async function contractCallStatic (source, address, name, args = [], { top, opti
}
}

async function dryRunContractTx (tx, callerId, source, name, opt = {}) {
const { top } = opt
// Resolve Account for Dry-run
const dryRunAmount = BigNumber(opt.dryRunAccount.amount).gt(BigNumber(opt.amount || 0)) ? opt.dryRunAccount.amount : opt.amount
const dryRunAccount = {
amount: dryRunAmount,
pubKey: callerId
}
// Dry-run
const [{ result: status, callObj, reason }] = (await this.txDryRun([tx], [dryRunAccount], top)).results
async function dryRunContractTx (tx, callerId, source, name, options) {
const { callObj, ...dryRunOther } = await this.txDryRun(tx, callerId, options)

// Process response
if (status !== 'ok') throw Object.assign(new Error('Dry run error, ' + reason), { tx: TxObject({ tx }), dryRunParams: { accounts: [dryRunAccount], top } })
const { returnType, returnValue } = callObj
if (returnType !== 'ok') {
await this._handleCallError(callObj, tx)
}
return {
...dryRunOther,
tx: TxObject({ tx }),
result: callObj,
decode: () => this.contractDecodeData(source, name, returnValue, returnType, opt)
decode: () => this.contractDecodeData(source, name, returnValue, returnType, options)
}
}

Expand Down
9 changes: 5 additions & 4 deletions es/chain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,11 @@ const Chain = Oracle.compose({
* @instance
* @abstract
* @category async
* @rtype (txs, accounts, hashOrHeight) => result: Object
* @param {Array} txs - Array of transaction's
* @param {Array} accounts - Array of account's
* @param {String|Number} hashOrHeight - hash or height of block on which to make dry-run
* @rtype (tx, accountAddress, options) => result: Object
* @param {String} tx - transaction to execute
* @param {String} accountAddress - address that will be used to execute transaction
* @param {String|Number} [options.top] - hash of block on which to make dry-run
* @param {Boolean} [options.txEvents] - collect and return on-chain tx events that would result from the call
* @return {Object} Result
*/

Expand Down
21 changes: 18 additions & 3 deletions es/chain/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import NodePool from '../node-pool'
import { assertedType } from '../utils/crypto'
import { pause } from '../utils/other'
import { isNameValid, produceNameId } from '../tx/builder/helpers'
import { NAME_ID_KEY } from '../tx/builder/schema'
import { DRY_RUN_ACCOUNT, NAME_ID_KEY } from '../tx/builder/schema'

/**
* ChainNode module
Expand Down Expand Up @@ -174,8 +174,23 @@ async function getMicroBlockHeader (hash) {
return this.api.getMicroBlockHeaderByHash(hash)
}

async function txDryRun (txs, accounts, top) {
return this.api.dryRunTxs({ txs: txs.map(tx => ({ tx })), accounts, top })
async function txDryRun (tx, accountAddress, options) {
const { results: [{ result, reason, ...resultPayload }], ...other } =
await this.api.dryRunTxs({
...options,
txs: [{ tx }],
accounts: [{
pubKey: accountAddress,
amount: DRY_RUN_ACCOUNT.amount
}]
})

if (result === 'ok') return { ...resultPayload, ...other }

throw Object.assign(
new Error('Dry run error, ' + reason),
{ tx, accountAddress, options }
)
}

async function getContractByteCode (contractId) {
Expand Down
4 changes: 4 additions & 0 deletions es/utils/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ const conformTypes = {
throw TypeError('Not a string', spec, value)
}
},
boolean (value, spec) {
if (typeof value === 'boolean') return value
throw TypeError('Not a boolean', spec, value)
},
object (value, spec, types) {
if (R.type(value) === 'Object') {
const required = (spec.required || []).map(snakeToPascal)
Expand Down

0 comments on commit 401c53d

Please sign in to comment.