Skip to content

Commit 47d09be

Browse files
committed
Add doc for call TxScript
1 parent bb818d5 commit 47d09be

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

docs/dapps/tutorials/dapp-recipes.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,80 @@ const testResult1 = await TokenFaucet.tests.withdraw({
258258
const contractState1 = testResult1.contracts[0] as TokenFaucetTypes.State
259259
expect(contractState1.fields.balance).toEqual(8n)
260260
```
261+
262+
## Call TxScript
263+
264+
Call `TxScript` is a feature that allows interaction with smart contracts on Alephium without consuming gas and modifying the on-chain state. Instead, it executes scripts and returns updated contract states.
265+
266+
### Call contract functions
267+
268+
```rust
269+
Contract Foo(mut value: U256) {
270+
@using(updateFields = true, checkExternalCaller = false)
271+
pub fn foo() -> () {
272+
value = value + 1
273+
}
274+
}
275+
276+
@using(preapprovedAssets = false)
277+
TxScript Main(foo: Foo) {
278+
foo.foo()
279+
}
280+
```
281+
282+
You can use the generated `TypeScript` code to call `TxScript`:
283+
284+
```typescript
285+
import { getSigner } from '@alephium/web3-test'
286+
287+
const signer = await getSigner()
288+
const deployFooResult = await Foo.deploy(signer, { initialFields: { value: 0n } })
289+
const fooInstance = deployFooResult.contractInstance
290+
const callResult = await Main.call({
291+
initialFields: { foo: fooInstance.contractId },
292+
interestedContracts: [fooInstance.address]
293+
})
294+
expect(callResult.contracts[0].fields.value).toEqual(1n)
295+
```
296+
297+
### Query and return contract states
298+
299+
`TxScript` also allows you to query and return the states of multiple contracts in a single request:
300+
301+
```rust
302+
Contract Foo(value: U256) {
303+
pub fn foo() -> U256 {
304+
return value
305+
}
306+
}
307+
308+
Contract Bar(value: ByteVec) {
309+
pub fn bar() -> ByteVec {
310+
return value
311+
}
312+
}
313+
314+
TxScript Main(foo: Foo, bar: Bar) {
315+
pub fn main() -> (U256, ByteVec) {
316+
return foo.foo(), bar.bar()
317+
}
318+
}
319+
```
320+
321+
Implicit definition of the `main` function does not allow for return values, so an explicit definition of the `main` function is required here.
322+
323+
You can use the generated `TypeScript` code to call `TxScript`:
324+
325+
```typescript
326+
import { getSigner } from '@alephium/web3-test'
327+
328+
const signer = await getSigner()
329+
const deployFooResult = await Foo.deploy(signer, { initialFields: { value: 0n } })
330+
const deployBarResult = await Bar.deploy(signer, { initialFields: { value: '0011' } })
331+
const fooInstance = deployFooResult.contractInstance
332+
const barInstance = deployBarResult.contractInstance
333+
const callResult = await MainForCall.call({
334+
initialFields: { foo: fooInstance.contractId, bar: barInstance.contractId }
335+
})
336+
expect(callResult.returns).toEqual([0n, '0011'])
337+
```

docs/ralph/contracts.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,10 @@ Contract Foo() {
325325
@using(preapprovedAssets = false)
326326
// `TxScript` fields are more like function parameters, and these
327327
// fields need to be specified every time the script is executed.
328-
TxScript Main(fooId: ByteVec) {
328+
TxScript Main(foo: Foo) {
329329
// The body of `TxScript` consists of statements
330330
bar()
331-
Foo(fooId).foo(0)
331+
foo.foo(0)
332332

333333
// You can also define functions in `TxScript`
334334
fn bar() -> () {
@@ -337,6 +337,23 @@ TxScript Main(fooId: ByteVec) {
337337
}
338338
```
339339

340+
The `main` function of `TxScript` serves as the entry point for contract code execution. It supports both explicit and implicit definitions of the `main` function:
341+
342+
1. Implicit definition of the `main` function, in the example above, the compiler automatically generates a `main` function for the `TxScript`.
343+
2. Explicit definition of the `main` function:
344+
345+
```rust
346+
TxScript Main(foo: Foo) {
347+
@using(preapprovedAssets = false)
348+
pub fn main() -> () {
349+
bar()
350+
foo.foo(0)
351+
}
352+
}
353+
```
354+
355+
The explicitly defined `main` function cannot have parameters. If you need to specify parameters, you can pass them as parameters of the `TxScript`.
356+
340357
## Gasless Transaction
341358

342359
In Ralph, you can use the built-in `payGasFee` to pay transaction gas fees on behalf of the user, for example:

0 commit comments

Comments
 (0)