Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Improve a guide about how to use executeTx and ExecParams #772

Merged
merged 7 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 51 additions & 4 deletions docs/guide/execute-transaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ layout: splash

# Execute Transaction

`executeTx` is a high level method of Deployer which can be used to perform transactions on Algorand Network. It supports every transaction (atomic or single) which is possible in network. Ex: Deploy ASA/App, Opt-In, Transfers, Delete, Destroy etc. `executeTx` takes `ExecParams[]` as parameter.
If the length of `ExecParams` array is greater than one, it will be considered as `atomic transaction`.
`executeTx` is a high level method of Deployer which can be used to perform transactions on Algorand Network. It supports every transaction (atomic or single) which is possible in network. Ex: Deploy ASA/App, Opt-In, Transfers, Delete, Destroy etc. `executeTx` takes `ExecParams[]` as parameter. If the length of `ExecParams` array is greater than one, it will be considered as `atomic transaction` otherwise `single transaction`.
In below sections we will demonstrate how to pass these parameters.

Note: For each parameter `type` and `sign` attributes are mandatory.
Expand All @@ -15,6 +14,35 @@ Note: For each parameter `type` and `sign` attributes are mandatory.

Depending on the transaction `type`, other attributes will specify a signer of the transaction. For example, for `TransactionType.TransferAlgo` the `fromAccount` must be a full account (with secret key) and will sign the transaction.

`deployer.executeTx` returns list of `TxnReceipt`, which extends `ConfirmedTxInfo`.

```js
const receipts = deployer.executeTx([txn0, txn1]);
console.log("txn0 information: ", receipts[0]);
console.log("txn1 information: ", receipts[2]);
```

Below examples demonstrates, how to perform application call using `ExecParam` and `WebMode`.

```js
appCall = () => {
const webMode: WebMode = new WebMode(AlgoSigner, networkType);
const tx: types.ExecParams[] = [
{
type: types.TransactionType.CallApp,
sign: types.SignType.SecretKey,
fromAccount: {
addr: addr,
sk: new Uint8Array(0),
},
appID: 100,
payFlags: {},
},
];
webMode.executeTx(tx);
};
```

## Examples [`ExecParams`](https://algobuilder.dev/api/algob/modules/runtime.types.html#ExecParams) usage:

#### [Transfer Algo using secret key](https://algobuilder.dev/api/algob/modules/runtime.types.html#AlgoTransferParam)
Expand All @@ -26,7 +54,8 @@ Depending on the transaction `type`, other attributes will specify a signer of t
fromAccount: john,
toAccountAddr: alice.address,
amountMicroAlgos: 100,
payFlags: { totalFee: fee } }
payFlags: { totalFee: fee }
}
```

- payFlags: [TxParams](https://algobuilder.dev/api/algob/interfaces/runtime.types.TxParams.html)
Expand Down Expand Up @@ -261,7 +290,12 @@ Even though fee paid by alice is `0`, this transaction will pass because total f

## Sign and Send SDK Transaction object using `executeTx` method

`deployer.executeTx` method supports signing and sending sdk transaction objects. To do this you will have to pass an [`TransactionAndSign`](https://algobuilder.dev/api/web/interfaces/types.TransactionAndSign.html) object which has `transaction` and `sign`. Ex:
`deployer.executeTx` method supports signing and sending sdk transaction objects. To do this you will have to pass an [`TransactionAndSign`](https://algobuilder.dev/api/web/interfaces/types.TransactionAndSign.html) object which has following properties:

- `type`: type of transaction.
- `sign`: signature [`types`](https://github.com/scale-it/algo-builder/blob/2bcef8f611b349dfb8dc3542ed2f0a129a0c405c/packages/web/src/types.ts#L117).

Ex:

```js
const tx = makeAssetCreateTxn(
Expand All @@ -270,6 +304,7 @@ const tx = makeAssetCreateTxn(
undefined, mockSuggestedParam.genesisHash, mockSuggestedParam.genesisID,
1e6, 0, false, undefined, undefined, undefined, undefined, "UM", "ASM", undefined
);

const transaction: wtypes.TransactionAndSign = {
transaction: tx,
sign: {sign: wtypes.SignType.SecretKey, fromAccount: bobAcc}
Expand All @@ -280,6 +315,18 @@ const res = await deployer.executeTx([transaction]);

You can check the implementation in [asa](https://github.com/scale-it/algo-builder/blob/master/examples/asa/scripts/2-gold-asc.js) example.

There is also a function to check if given object implements `Transaction` class and has `Sign`.

```js
export function isSDKTransactionAndSign(object: unknown): object is TransactionAndSign {
if (object === undefined || object === null) {
return false;
}
const res = isSDKTransaction((object as TransactionAndSign).transaction);
return Object.prototype.hasOwnProperty.call(object, "sign") && res;
}
```

### SignTransactions function

This function takes array of [`TransactionAndSign`](https://algobuilder.dev/api/web/interfaces/types.TransactionAndSign.html) objects and returns raw signed transaction
Expand Down
4 changes: 3 additions & 1 deletion packages/algob/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,9 @@ export interface Deployer {
* executes `ExecParams` or `Transaction` Object, SDK Transaction object passed to this function
* will be signed and sent to network. User can use SDK functions to create transactions.
* Note: If passing transaction object a signer/s must be provided.
* @param transactionParam transaction parameters or atomic transaction parameters
* Check out {@link https://algobuilder.dev/guide/execute-transaction.html#execute-transaction|execute-transaction}
* for more info.
* @param transactions transaction parameters or atomic transaction parameters
* https://github.com/scale-it/algo-builder/blob/docs/docs/guide/execute-transaction.md
* or TransactionAndSign object(SDK transaction object and signer parameters)
*/
Expand Down
5 changes: 3 additions & 2 deletions packages/runtime/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,9 @@ export class Runtime {
}

/**
* This function executes a transaction based on a smart
* contract logic and updates state afterwards
* This function executes a transaction based on a smart contract logic and updates state afterwards
* Check out {@link https://algobuilder.dev/guide/execute-transaction.html#execute-transaction|execute-transaction}
* for more info.
* @param txnParams : Transaction parameters
* @param debugStack: if passed then TEAL Stack is logged to console after
* each opcode execution (upto depth = debugStack)
Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/lib/web-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ export class WebMode {

/**
* Execute single transaction or group of transactions (atomic transaction)
* Check out {@link https://algobuilder.dev/guide/execute-transaction.html#execute-transaction|execute-transaction}
* for more info.
* @param transactions transaction parameters, atomic transaction parameters
* or TransactionAndSign object(SDK transaction object and signer parameters).
* When list of ExecParams is used, the function will request wallet to sign transactions.
Expand Down