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
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:
346
+
347
+
```rust
348
+
TxScriptMain(foo:Foo) {
349
+
@using(preapprovedAssets =false)
350
+
pubfnmain() -> () {
351
+
bar()
352
+
foo.foo(0)
353
+
}
354
+
}
355
+
```
356
+
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`.
358
+
340
359
## Gasless Transaction
341
360
342
361
In Ralph, you can use the built-in `payGasFee` to pay transaction gas fees on behalf of the user, for example:
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