Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit d765fc3

Browse files
author
Brad Hart
authored
Merge pull request #813 from EOSIO/api_shorthand_docs
Documentation for new transaction shorthand design
2 parents 3664985 + 7f8561c commit d765fc3

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

docs/how-to-guides/01_how-to-submit-a-transaction.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,62 @@ Alternatively, the transaction could be submitted without the optional configura
7474
})();
7575
```
7676

77+
#### Concise Actions
78+
To construct transactions and actions in a more concise way, you can also utilize this format instead:
79+
```javascript
80+
(async () => {
81+
await api.transact({
82+
actions: [
83+
api.with('eosio').as('useraaaaaaaa').buyrambytes('useraaaaaaaa', 'useraaaaaaaa', 8192)
84+
]
85+
}, {
86+
blocksBehind: 3,
87+
expireSeconds: 30,
88+
});
89+
})();
90+
```
91+
With this concise format of an action, the `with()` function has the account, `as()` contains the actor, and the name of the action is the third function. The arguments within the action function are listed in the same order as the arguments from the smart contract. You can also send a longer authentication within the `as()` function, such as `[{ actor: ‘useraaaaaaaa’, permission: ‘active’}]`.
92+
93+
Before using this structure, you need to cache the JSON Abi:
94+
```javascript
95+
(async () => {
96+
await api.getAbi('eosio');
97+
...
98+
})();
99+
```
100+
101+
Additionally, utilizing this structure, a stateful transaction object can be created and passed through your application before sending when ready. The transaction object can also be created as a callback method.
102+
103+
```javascript
104+
(async () => {
105+
const tx = api.buildTransaction();
106+
tx.with('eosio').as('useraaaaaaaa').buyrambytes('useraaaaaaaa', 'useraaaaaaaa', 8192)
107+
await tx.send({ blocksBehind: 3, expireSeconds: 30 });
108+
109+
// ...or...
110+
111+
api.buildTransaction(async (tx) => {
112+
tx.with('eosio').as('useraaaaaaaa').buyrambytes('useraaaaaaaa', 'useraaaaaaaa', 8192)
113+
await tx.send({ blocksBehind: 3, expireSeconds: 30 });
114+
});
115+
})();
116+
```
117+
118+
By using this object and passing it around your application, it might be more difficult for your application to keep the correct references and indexes for context free actions. The transaction object has a function for mapping actions, context free actions, and context free data together.
119+
120+
```javascript
121+
(async () => {
122+
const tx = api.buildTransaction();
123+
tx.associateContextFree((index) => ({
124+
contextFreeData: cfdata,
125+
contextFreeAction: tx.with('account').as().cfaName(index.cfd, 'context free example'),
126+
action: tx.with('account').as('actor').actionName('example', index.cfa)
127+
}));
128+
await tx.send({ blocksBehind: 3, expireSeconds: 30 });
129+
})();
130+
```
131+
132+
By providing that function inside `tx.associateContextFree()`, the transaction object will provide the correct indexes for the context free action and context free data. You can input the `index.cfa` or `index.cfd` arguments where your smart contract requires that index in the list of arguments. Additionally, all three object keys are not necessary in the function, in case for example, the action is not necessary for your context free action.
133+
77134
#### Return Values
78-
From nodeos version 2.1, the ability to receive return values from smart contracts to eosjs has been introduced. In the above examples, the `transaction` object will include the values `transaction_id` and the `processed` object. If your smart contract returns values, you will be able to find the values within the `transaction.processed.action_traces` array. The order of the `action_traces` array matches the order of actions in your transaction and within those `action_trace` objects, you can find your deserialized return value for your action in the `return_value` field.
135+
From nodeos version 2.1, the ability to receive return values from smart contracts to eosjs has been introduced. In the above examples, the `transaction` object will include the values `transaction_id` and the `processed` object. If your smart contract returns values, you will be able to find the values within the `transaction.processed.action_traces` array. The order of the `action_traces` array matches the order of actions in your transaction and within those `action_trace` objects, you can find your deserialized return value for your action in the `return_value` field.

0 commit comments

Comments
 (0)