Skip to content

Commit

Permalink
fix(TxBuilder): Fix fee calculation for Oracles (#924)
Browse files Browse the repository at this point in the history
* fix(TxBuilder): Fix fee calculation for Oracles

* fix(Oracle): Fix tests

* chore(travis): trigger
  • Loading branch information
nduchak authored Feb 27, 2020
1 parent 8c0fb99 commit a9d784f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Promise.all([
Ae({
nodes: [
{ name: 'someNode', instance: nodes[0] },
// node2, node3..
// node2, node3, ...
],
compilerUrl: 'COMPILER_URL',
accounts: [
Expand Down
4 changes: 2 additions & 2 deletions es/ae/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ export async function pollForQueryResponse (oracleId, queryId, { attempts = 20,
* @param {String|Number} [options.queryFee] queryFee Oracle query Fee
* @param {Object} [options.oracleTtl] oracleTtl OracleTtl object {type: 'delta|block', value: 'number'}
* @param {Number} [options.abiVersion] abiVersion Always 0 (do not use virtual machine)
* @param {Number} [options.fee] fee Transaction fee
* @param {Number} [options.ttl] Transaction time to leave
* @param {Number|String} [options.fee] fee Transaction fee
* @param {Number|String} [options.ttl] Transaction time to leave
* @return {Promise<Object>} Oracle object
*/
async function registerOracle (queryFormat, responseFormat, options = {}) {
Expand Down
31 changes: 17 additions & 14 deletions es/tx/builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
TX_FEE_BASE_GAS,
TX_FEE_OTHER_GAS,
TX_SERIALIZATION_SCHEMA,
TX_TYPE,
VALIDATION_MESSAGE,
VSN
} from './schema'
Expand Down Expand Up @@ -174,16 +175,15 @@ function validateField (value, key, type, prefix) {
}

function transformParams (params, schema, { denomination } = {}) {
// console.log(schema.filter(([_, t]) => t === FIELD_TYPES.amount))
// console.log(params)
params = schema
.filter(([_, t]) => t === FIELD_TYPES.amount)
.reduce((acc, [key]) => ({ ...params, [key]: formatAmount(params[key], { denomination }) }), params)
const schemaKeys = schema.map(([k]) => k)
return Object
.entries(params)
.reduce(
(acc, [key, value]) => {
acc[key] = value
if (schemaKeys.includes(key)) acc[key] = value
if (['oracleTtl', 'queryTtl', 'responseTtl'].includes(key)) {
acc[`${key}Type`] = value.type === ORACLE_TTL_TYPES.delta ? 0 : 1
acc[`${key}Value`] = value.value
Expand All @@ -196,15 +196,18 @@ function transformParams (params, schema, { denomination } = {}) {

// INTERFACE

function getOracleRelativeTtl (params) {
// const ORACLE_TTL_KEYS = ['oracleTtl', 'queryTtl', 'responseTtl']
// return Object.entries(params).reduce((acc, [key, value]) => {
// if (ORACLE_TTL_KEYS.includes(key)) acc = value.value
// if (ORACLE_TTL_KEYS.map(k => `${k}Value`).includes(key)) acc = value
// return acc
// }, 500)
// TODO Investigate this
return 500
function getOracleRelativeTtl (params, txType) {
const ttlKey = {
[TX_TYPE.oracleRegister]: 'oracleTtl',
[TX_TYPE.oracleExtend]: 'oracleTtl',
[TX_TYPE.oracleQuery]: 'queryTtl',
[TX_TYPE.oracleResponse]: 'responseTtl'
}[txType]

if (params[ttlKey] || params[`${ttlKey}Value`]) {
return params[`${ttlKey}Value`] || params[ttlKey].value
}
return 1
}

/**
Expand Down Expand Up @@ -246,7 +249,7 @@ function buildFee (txType, { params, gas = 0, multiplier, vsn }) {
const { rlpEncoded: txWithOutFee } = buildTx({ ...params }, txType, { vsn })
const txSize = txWithOutFee.length
return TX_FEE_BASE_GAS(txType, { backend: getContractBackendFromTx(params) })
.plus(TX_FEE_OTHER_GAS(txType)({ txSize, relativeTtl: getOracleRelativeTtl(params) }))
.plus(TX_FEE_OTHER_GAS(txType)({ txSize, relativeTtl: getOracleRelativeTtl(params, txType) }))
.times(multiplier)
}

Expand Down Expand Up @@ -297,7 +300,7 @@ export function validateParams (params, schema, { excludeKeys = [] }) {
* @param {Object} params Object with tx params
* @param {Array} schema Transaction schema
* @param {Object} [options={}] options
* @param {Object} [options.excludeKeys] excludeKeys Array of keys to exclude for validation and build
* @param {Array} [options.excludeKeys] excludeKeys Array of keys to exclude for validation and build
* @throws {Error} Validation error
* @return {Array} Array with binary fields of transaction
*/
Expand Down
6 changes: 3 additions & 3 deletions test/integration/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ describe('Oracle', function () {
client = await ready(this)
})

it('Register Oracle', async () => {
it('Register Oracle with 5000 TTL', async () => {
const expectedOracleId = `ok_${(await client.address()).slice(3)}`
oracle = await client.registerOracle("{'city': str}", "{'tmp': num}")
oracle = await client.registerOracle("{'city': str}", "{'tmp': num}", { oracleTtl: { type: 'delta', value: 5000 } })
oracle.id.should.be.equal(expectedOracleId)
})

it('Extend Oracle', async () => {
const ttlToExtend = { type: 'delta', value: 123 }
const ttlToExtend = { type: 'delta', value: 7450 }
const extendedOracle = await oracle.extendOracle(ttlToExtend)
const isExtended = extendedOracle.ttl > oracle.ttl
isExtended.should.be.equal(true)
Expand Down

0 comments on commit a9d784f

Please sign in to comment.