Skip to content

Commit

Permalink
feat(contract): accept amount in contract and tx builder commands
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 11, 2024
1 parent fb8cac1 commit 1dbd195
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/actions/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export async function deploy(walletPath, args, options) {
// ## Function which `call` contract
export async function call(fn, args, walletPath, options) {
const {
callStatic, json, top, ttl, gas, nonce,
callStatic, json, top, ttl, gas, nonce, amount,
} = options;
if (callStatic !== true && walletPath == null) {
throw new CliError('wallet_path is required for on-chain calls');
Expand All @@ -110,6 +110,7 @@ export async function call(fn, args, walletPath, options) {
nonce: nonce && +nonce,
callStatic,
top,
amount,
});
if (json) print(callResult);
else {
Expand Down
4 changes: 4 additions & 0 deletions src/arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export const coinAmountParser = (amount) => {
return new BigNumber(amount);
};

export const amountOption = new Option('-a, --amount [amount]', 'Amount of coins to send')
.default(0, '0ae')
.argParser(coinAmountParser);

export const feeOption = new Option('-F, --fee [fee]', 'Override the transaction fee')
.argParser(coinAmountParser);

Expand Down
3 changes: 3 additions & 0 deletions src/commands/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
forceOption,
passwordOption,
ttlOption,
amountOption,
} from '../arguments.js';

const callArgs = new Argument('[args]', 'JSON-encoded arguments array of contract call')
Expand Down Expand Up @@ -108,6 +109,7 @@ addCommonOptions(program
.addOption(gasOption)
.option('-s, --callStatic', 'Call static')
.option('-t, --topHash', 'Hash of block to make call')
.addOption(amountOption)
.addOption(feeOption)
.addOption(ttlOption(true))
.option('-N, --nonce [nonce]', 'Override the nonce that the transaction is going to be sent with')
Expand Down Expand Up @@ -135,6 +137,7 @@ addCommonOptions(program
.addOption(passwordOption)
.addOption(gasOption)
.addOption(gasPriceOption(true))
.addOption(amountOption)
.addOption(feeOption)
.addOption(ttlOption(true))
.option('-N, --nonce [nonce]', 'Override the nonce that the transaction is going to be sent with')
Expand Down
5 changes: 3 additions & 2 deletions src/commands/tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
forceOption,
ttlOption,
networkIdOption,
amountOption,
} from '../arguments.js';

const program = new Command().name('aecli tx');
Expand Down Expand Up @@ -113,7 +114,7 @@ addCommonOptions(program
.addArgument(nonceArgument)
.addOption(gasOption)
.addOption(gasPriceOption(false))
.option('--amount [amount]', 'Amount', 0)
.addOption(amountOption)
.description('Build contract create transaction.')
.action(Transaction.contractDeploy));

Expand All @@ -127,7 +128,7 @@ addCommonOptions(program
.addArgument(nonceArgument)
.addOption(gasOption)
.addOption(gasPriceOption(false))
.option('--amount [amount]', 'Amount', 0)
.addOption(amountOption)
.description('Build contract create transaction.')
.action(Transaction.contractCall));

Expand Down
32 changes: 31 additions & 1 deletion test/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('Contract Module', function contractTests() {
expect(descriptor).to.eql({
version: 1,
address,
bytecode: 'cb_+L5GA6Ar1lCAsdVHFnIFRGVmOW8j4LcSXxgJgqPlwvI2Zeak28C4kbhX/kTWRB8ANwEHNwAaBoIAAQM//pKLIDYANwIHBwcMAoIMAQICAxHQ4oJSDAEABAMR0OKCUv7Q4oJSAjcCBwcHFBQAAgD+6YyQGwA3AGcHBwEDLwICBAYItC8EEUTWRB8RaW5pdBGSiyA2EXRlc3QR0OKCUjEuVGVzdExpYi5zdW0R6YyQGxlnZXRNYXCCLwCFNy40LjEAF0MstQ==',
bytecode: 'cb_+NRGA6CmFq9nCCwTbFoTpKtDko1jYl6CdmFWd0+re1zVAPUVJMC4p7hj/kTWRB8ANwEHNwAaBoIAAQM//pKLIDYANwIHBwcMAoIMAQICAxHQ4oJSDAEABAMR0OKCUv7Q4oJSAjcCBwcHFBQAAgD+6YyQGwA3AGcHBwEDLwICBAYI/viMoQQENwAHAQMAuD0vBRFE1kQfEWluaXQRkosgNhF0ZXN0EdDiglIxLlRlc3RMaWIuc3VtEemMkBsZZ2V0TWFwEfiMoQQNcGF5gi8AhTcuNC4xAK21f/c=',
aci: [{
namespace: { name: 'TestLib', typedefs: [] },
}, {
Expand All @@ -100,6 +100,12 @@ describe('Contract Module', function contractTests() {
payable: false,
returns: { map: ['int', 'int'] },
stateful: false,
}, {
arguments: [],
name: 'pay',
payable: true,
returns: 'int',
stateful: false,
}],
kind: 'contract_main',
name: 'Identity',
Expand Down Expand Up @@ -137,6 +143,18 @@ describe('Contract Module', function contractTests() {
'--json',
])).to.be.rejectedWith(CliError, expectedError);
});

it('deploys contract with coins', async () => {
const { address } = await executeContract([
'deploy',
WALLET_NAME, '--password', 'test',
'--contractSource', contractSourceFile,
'[3]',
'--json',
'--amount', '1',
]);
expect(await sdk.getBalance(address)).to.be.equal('1');
});
});

describe('Call', () => {
Expand Down Expand Up @@ -242,6 +260,18 @@ describe('Contract Module', function contractTests() {
]);
callResponse.decodedResult.should.equal('6');
});

it('calls contract with coins', async () => {
await executeContract([
'call',
'--json',
'--descrPath', deployDescriptorFile,
'pay', '[]',
WALLET_NAME, '--password', 'test',
'--amount', '0.000000001ae',
]);
expect(await sdk.getBalance(contractAddress)).to.be.equal('1000000000');
});
});

describe('Calldata', () => {
Expand Down
1 change: 1 addition & 0 deletions test/contracts/contract.aes
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ contract Identity =
entrypoint init(_z: int) = { z = _z }
entrypoint test(x : int, y: int) = TestLib.sum(x, TestLib.sum(y, state.z))
entrypoint getMap(): map(int, int) = {[1] = 2, [3] = 4}
payable entrypoint pay() = 0
10 changes: 5 additions & 5 deletions test/tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,18 +360,18 @@ ABI Version _____________________________ 3
nonce += 1;
// eslint-disable-next-line no-underscore-dangle
const callData = contract._calldata.encode(contract._name, 'test', ['1', '2']);
const { tx } = await executeTx(['contract-call', TX_KEYS.publicKey, contractId, callData, nonce, '--json']);
const { tx } = await executeTx(['contract-call', TX_KEYS.publicKey, contractId, callData, nonce, '--json', '--amount', '0.00000042ae']);

const [detailsJson, details] = await signAndPostAndInspect(tx);
expect(detailsJson.fee).to.be.a('string');
expect(detailsJson).to.eql({
abiVersion: '3',
amount: '0',
amount: '420000000000',
callData,
callerId: TX_KEYS.publicKey,
contractId,
fee: detailsJson.fee,
gas: 5817960,
gas: 5817860,
gasPrice: '1000000000',
nonce,
type: 'ContractCallTx',
Expand All @@ -381,8 +381,8 @@ ABI Version _____________________________ 3
Tx Type _________________________________ ContractCallTx
Caller Account __________________________ ${TX_KEYS.publicKey}
Contract Hash ___________________________ ${contractId}
Amount __________________________________ 0
Gas _____________________________________ 5817960
Amount __________________________________ 420000000000
Gas _____________________________________ 5817860
Gas Price _______________________________ 1000000000
Call data _______________________________ ${callData}
Fee _____________________________________ ${detailsJson.fee}
Expand Down

0 comments on commit 1dbd195

Please sign in to comment.