Skip to content

Commit

Permalink
feat(tx-builder): pick queryFee from the node if not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Dec 6, 2023
1 parent 4a88f76 commit ca495c8
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ export async function postQueryToOracle(
): Promise<
Awaited<ReturnType<typeof sendTransaction>> & Awaited<ReturnType<typeof getQueryObject>>
> {
options.queryFee ??= (await options.onNode.getOracleByPubkey(oracleId)).queryFee.toString();
const senderId = options.onAccount.address;

const oracleQueryTx = await buildTxAsync({
Expand Down
2 changes: 2 additions & 0 deletions src/tx/builder/field-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import nameFee from './name-fee';
import nameId from './name-id';
import nonce from './nonce';
import pointers from './pointers';
import queryFee from './query-fee';
import raw from './raw';
import shortUInt from './short-u-int';
import shortUIntConst from './short-u-int-const';
Expand Down Expand Up @@ -58,6 +59,7 @@ export {
nameId,
nonce,
pointers,
queryFee,
raw,
shortUInt,
shortUIntConst,
Expand Down
2 changes: 1 addition & 1 deletion src/tx/builder/field-types/name-fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default {
},

/**
* @param value - AENS name fee Buffer
* @param value - AENS name fee
* @param txFields - Transaction fields
* @param txFields.name - AENS Name in transaction
*/
Expand Down
22 changes: 22 additions & 0 deletions src/tx/builder/field-types/query-fee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import coinAmount from './coin-amount';
import { Int } from '../constants';
import Node from '../../../Node';
import { Encoded } from '../../../utils/encoder';
import { ArgumentError } from '../../../utils/errors';

export default {
...coinAmount,

async prepare(
value: Int | undefined,
params: {},
options: { oracleId?: Encoded.OracleAddress; onNode?: Node },
) {
if (value != null) return value;
const { onNode, oracleId } = options;
const requirement = 'provided (or provide `queryFee` instead)';
if (onNode == null) throw new ArgumentError('onNode', requirement, onNode);
if (oracleId == null) throw new ArgumentError('oracleId', requirement, oracleId);
return (await onNode.getOracleByPubkey(oracleId)).queryFee.toString();
},
};
4 changes: 2 additions & 2 deletions src/tx/builder/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Tag } from './constants';
import SchemaTypes from './SchemaTypes';
import {
uInt, shortUInt, coinAmount, name, nameId, nameFee, deposit, gasLimit, gasPrice, fee,
address, pointers, entry, enumeration, mptree, shortUIntConst, string, encoded, raw,
address, pointers, queryFee, entry, enumeration, mptree, shortUIntConst, string, encoded, raw,
array, boolean, ctVersion, abiVersion, ttl, nonce, map, wrapped,
} from './field-types';
import { Encoded, Encoding } from '../../utils/encoder';
Expand Down Expand Up @@ -307,7 +307,7 @@ export const txSchema = [{
nonce: nonce('senderId'),
oracleId: address(Encoding.OracleAddress, Encoding.Name),
query: string,
queryFee: coinAmount,
queryFee,
queryTtlType: enumeration(ORACLE_TTL_TYPES),
queryTtlValue: shortUInt,
responseTtlType: enumeration(ORACLE_TTL_TYPES),
Expand Down
26 changes: 26 additions & 0 deletions test/integration/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, before } from 'mocha';
import { expect } from 'chai';
import { createSandbox } from 'sinon';
import { getSdk } from './index';
import {
AeSdk, Contract,
Expand Down Expand Up @@ -156,6 +157,31 @@ describe('Transaction', () => {
async () => aeSdk.buildTx({
tag: Tag.OracleExtendTx, nonce, oracleId, ...oracleTtl,
}),
], [
'oracle post query simple',
'tx_+GkXAaEBhAyXS5cWR3ZFS6EZ2E7cTWBYqN7JK27cV4qy0wtMQgABoQSEDJdLlxZHdkVLoRnYTtxNYFio3skrbtxXirLTC0xCAJJ7J2NpdHknOiAnQmVybGluJ32CMDkACgAKhg+XLtIcAAA6bZb1',
async () => {
const sandbox = createSandbox();
sandbox.replace(aeSdk.api, 'getOracleByPubkey', async () => Promise.resolve({
id: oracleId,
responseFormat,
queryFormat,
queryFee: 12345n,
abiVersion: '0',
ttl: 42,
}));
const tx = await aeSdk.buildTx({
tag: Tag.OracleQueryTx,
nonce,
oracleId,
...responseTtl,
query,
...queryTtl,
senderId: address,
});
sandbox.restore();
return tx;
},
], [
'oracle post query',
'tx_+GkXAaEBhAyXS5cWR3ZFS6EZ2E7cTWBYqN7JK27cV4qy0wtMQgABoQSEDJdLlxZHdkVLoRnYTtxNYFio3skrbtxXirLTC0xCAJJ7J2NpdHknOiAnQmVybGluJ32CdTAAZABkhg+bJBmGAABcZrGe',
Expand Down

0 comments on commit ca495c8

Please sign in to comment.