-
Notifications
You must be signed in to change notification settings - Fork 3
Testing meta transactions with Aion4j
AVM 2.0 provides support for meta-transaction(which is called Invokable). It provides an api Blockchain.invokeTransaction(bytes, energy)
to execute an encoded meta-transactions.
How it works ?
- Sign and encode a transaction with sender's private key.
- Send the encoded transaction to a smart contract method in a separate transaction.
- Smart contract executes the meta-transaction using
Blockchain.invokeTransaction
api.
Aion4j provides support to test meta-transaction both in local embedded AVM environment and on a remote kernel.
Pre-requisites
- Aion4j Maven Plugin 1.0.0 and above
To encode a meta-transaction using a private key, Aion4j maven plugin provides a goal aion4j:create-invokable
.
Usage:
$> mvn aion4j:create-invokable [-Dpk=<private_key>] [-Dto=<destination_address>]-Dexecutor=<executor_address> [-Dnonce=<sender_nonce>] [-DfetchNonce] [-Dvalue=<value>] [-Ddata=<data>] [-Dmethod=<method_name>] [-Dargs=<args>]
Notes:
- pk - Sender's private key can be passed as -Dpk or can be set as an environment variable "pk".
- executor - It's an optional parameter. It's the contract which can execute the invokable transaction. If executor address is not specified, any contract can execute this invokable transaction.
- nonce - It's an optional parameter. It's not required if -DfetchNonce option is there.
- fetchNonce - If this option is there, plugin automatically fetches the nonce value from the kernel. (Both local & remote)
- data - It's an optional parameter. Any data in hex.
- method - Optional parameter. Specify method name if it's a contract call.
- args - Optional parameter. Specify args if it's a contract call.
Example:
- Get nonce from embedded AVM and encode transaction
$> mvn aion4j:create-invokable -Dto=0x0bce2c2ad85356556a60455a8896892803606eb662d83b89b49eaf4a17481bbf -Dexecutor=0x0bce2c2ad85356556a60455a8896892803606eb662d83b89b49eaf4a17481bbf -Dmethod=setCounter -Dargs="-I 965" -DfetchNonce
- For remote kernel, as usual just provide -Premote. Also make sure web3rpc.url is set.
$> mvn aion4j:create-invokable -Dto=0x0bce2c2ad85356556a60455a8896892803606eb662d83b89b49eaf4a17481bbf -Dexecutor=0x0bce2c2ad85356556a60455a8896892803606eb662d83b89b49eaf4a17481bbf -Dmethod=setCounter -Dargs="-I 965" -DfetchNonce -Premote
Sample Output
[INFO] Sender Nonce fetched from kernel: 162
[INFO] Invokable Transaction (Hex) : 0x00f8ba81a2a00bce2c2ad85356556a60455a8896892803606eb662d83b89b49eaf4a17481bbf809221000a736574436f756e74657205000003c5a00bce2c2ad85356556a60455a8896892803606eb662d83b89b49eaf4a17481bbfb86034458d527f3981728cff45e75b53dafa23a5fb8a85ddef65e051332e4980acfc6196eeb912b078c87ce600108d4a1143cb3946ffac64ce1c4297f3166314a5983fa14dc7782088467b120e58a4652a3526e8788cbc04a3564e5ebd8901094c0d
Using IntelliJ IDE
- To test inside IntelliJ IDE, just paste the hex value of encoded meta-transaction in the input field of method call dialog.
Using Maven command line
The encoded meta-transaction can be passed as a normal argument in a smart contract method call. As the contract method usually takes a byte[] as parameter for meta-transaction, you can pass the hex-value of the meta-transaction with -B option. Just make sure the hex-value starts with 0x. The plugin automatically converts that to byte[].
Examples:
For embedded AVM call
$> mvn aion4j:call -Dcontract=0bce2c2ad85356556a60455a8896892803606eb662d83b89b49eaf4a17481bbf -Dmethod=invoke -Dargs="-B 0x00f8b907a00bce2c2ad85356556a60455a8896892803606eb662d83b89b49eaf4a17481bbf809221000a736574436f756e74657205000003c5a00bce2c2ad85356556a60455a8896892803606eb662d83b89b49eaf4a17481bbfb86034458d527f3981728cff45e75b53dafa23a5fb8a85ddef65e051332e4980acfcc14d2ce53260d7ec0820932bd933b76369648fc3deb5dbd5d6cd692f66b836c258c9bfa4c126a96749e8051a07697e0f2fc7d51b895b9a323912e7fce6f66705"
For remote kernel call
$> mvnw aion4j:contract-txn -Dcontract=0bce2c2ad85356556a60455a8896892803606eb662d83b89b49eaf4a17481bbf -Dmethod=invoke -Dargs="-B 0x00f8b907a00bce2c2ad85356556a60455a8896892803606eb662d83b89b49eaf4a17481bbf809221000a736574436f756e74657205000003c5a00bce2c2ad85356556a60455a8896892803606eb662d83b89b49eaf4a17481bbfb86034458d527f3981728cff45e75b53dafa23a5fb8a85ddef65e051332e4980acfcc14d2ce53260d7ec0820932bd933b76369648fc3deb5dbd5d6cd692f66b836c258c9bfa4c126a96749e8051a07697e0f2fc7d51b895b9a323912e7fce6f66705" -Premote
Note: If you need to pass multiple meta-transactions hex values, you can use -B[][] <hexvalue1> <hexvalue2> ...
.