You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: docs/ralph/contracts.md
+6-4Lines changed: 6 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -337,10 +337,12 @@ TxScript Main(foo: Foo) {
337
337
}
338
338
```
339
339
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:
340
+
### Implicit and Explicit Main Function
341
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:
342
+
The `main` function in `TxScript` serves as the entry point for contract code execution. TxScript supports both implicit and explicit definitions of the `main` function:
343
+
344
+
1. Implicit definition: When Ralph statements are present in the script body,, the compiler automatically generates a `main` function for the `TxScript`.
345
+
2. Explicit definition:
344
346
345
347
```rust
346
348
TxScriptMain(foo:Foo) {
@@ -352,7 +354,7 @@ TxScript Main(foo: Foo) {
352
354
}
353
355
```
354
356
355
-
The explicitly defined `main` function cannot have parameters. If you need to specify parameters, you can pass them as parameters of the `TxScript`.
357
+
In an explicit definition, the `main` function cannot accept parameters directly. If parameters are needed, they should be passed as fields of the `TxScript`.
console.log(`total supply: ${result.getTotalSupply.returns}`) // total supply: 10
255
+
```
256
+
257
+
### Call TxScript Locally
258
+
259
+
The `Call TxScript` feature 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 and the return values of the `TxScript` entry function.
260
+
261
+
```rust
262
+
ContractFoo(mutvalue:U256) {
263
+
pubfnfoo() ->U256 {
264
+
value=value+1
265
+
returnvalue
266
+
}
267
+
}
268
+
269
+
ContractBar(value:ByteVec) {
270
+
pubfnbar() ->ByteVec {
271
+
returnvalue
272
+
}
273
+
}
274
+
275
+
// This TxScript uses explicit main function. In most cases, the main functions are implicit
276
+
TxScriptMain(foo:Foo, bar:Bar) {
277
+
pubfnmain() -> (U256, ByteVec) {
278
+
returnfoo.foo(), bar.bar()
279
+
}
280
+
}
281
+
```
282
+
283
+
Implicit `main` functions do not allow for return values, so an explicit definition of the `main` function is required here.
284
+
More info about `main` function can be found [here](/ralph/contracts#implicit-and-explicit-main-function).
285
+
286
+
You can use the generated `TypeScript` code to call `TxScript` locally:
0 commit comments