Skip to content

Commit

Permalink
feat(account): override fee, gasPrice of GaMetaTx in authData
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Mar 1, 2023
1 parent bd656c2 commit 177a100
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
7 changes: 5 additions & 2 deletions examples/node/account-generalized.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

// ## 1. Create SDK instance and generate an account
import {
AeSdk, Node, MemoryAccount, AccountGeneralized, CompilerHttp,
AeSdk, Node, MemoryAccount, AccountGeneralized, CompilerHttp, MIN_GAS_PRICE,
} from '@aeternity/aepp-sdk';

const aeSdk = new AeSdk({
Expand Down Expand Up @@ -74,9 +74,12 @@ console.log('balance after', await aeSdk.getBalance(address));

await aeSdk.spend(2e18, recipient, {
async authData(transaction) {
const authTxHash = await aeSdk.buildAuthTxHash(transaction);
const fee = 10n ** 14n;
const gasPrice = MIN_GAS_PRICE;
const authTxHash = await aeSdk.buildAuthTxHash(transaction, { fee, gasPrice });
console.log('Auth.tx_hash', authTxHash.toString());
authData.args[1] += 1;
Object.assign(authData, { fee, gasPrice });
return authData;
},
});
Expand Down
3 changes: 3 additions & 0 deletions src/account/Base.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Encoded } from '../utils/encoder';
import Node from '../Node';
import CompilerBase from '../contract/compiler/Base';
import { Int } from '../tx/builder/constants';

interface AuthData {
fee?: Int;
gasLimit?: number;
gasPrice?: Int;
callData?: Encoded.ContractBytearray;
sourceCode?: string;
args?: any[];
Expand Down
4 changes: 3 additions & 1 deletion src/account/Generalized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class AccountGeneralized extends AccountBase {
throw new ArgumentError('authData, onCompiler, onNode', 'provided', null);
}
const {
callData, sourceCode, args, gasLimit,
callData, sourceCode, args, fee, gasLimit, gasPrice,
} = typeof authData === 'function' ? await authData(tx) : authData;

const authCallData = callData ?? await (async () => {
Expand All @@ -73,7 +73,9 @@ export default class AccountGeneralized extends AccountBase {
tx: { tag: Tag.SignedTx, encodedTx: decode(tx), signatures: [] },
gaId: this.address,
authData: authCallData,
fee,
gasLimit,
gasPrice,
onNode,
});
return buildTx({ tag: Tag.SignedTx, encodedTx: decode(gaMetaTx), signatures: [] });
Expand Down
9 changes: 8 additions & 1 deletion test/integration/account-generalized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
unpackTx,
buildTx,
Contract,
MIN_GAS_PRICE,
} from '../../src';
import { Encoded } from '../../src/utils/encoder';
import { ContractMethodsBase } from '../../src/contract/Contract';
Expand Down Expand Up @@ -95,15 +96,21 @@ describe('Generalized Account', () => {

it('accepts a function in authData', async () => {
let spendTx;
const fee = 1e16;
const gasPrice = MIN_GAS_PRICE + 1;
const { rawTx } = await aeSdk.spend(10000, publicKey, {
authData: async (tx) => {
spendTx = tx;
return { sourceCode, args: [genSalt()] };
return {
sourceCode, args: [genSalt()], fee, gasPrice,
};
},
});
const txParams = unpackTx(rawTx, Tag.SignedTx);
ensureEqual<Tag.GaMetaTx>(txParams.encodedTx.tag, Tag.GaMetaTx);
expect(buildTx(txParams.encodedTx.tx.encodedTx)).to.be.equal(spendTx);
expect(txParams.encodedTx.fee).to.be.equal(fee.toString());
expect(txParams.encodedTx.gasPrice).to.be.equal(gasPrice.toString());
});

it('fails trying to send SignedTx using generalized account', async () => {
Expand Down

0 comments on commit 177a100

Please sign in to comment.