Skip to content

Commit 39eaff8

Browse files
authored
Merge pull request #403 from alephium/add-doc-for-call-tx-script
Add doc for call TxScript
2 parents 3004820 + e1ea487 commit 39eaff8

File tree

3 files changed

+127
-40
lines changed

3 files changed

+127
-40
lines changed

docs/ralph/built-in-functions.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,20 @@ Returns the group of the input address.
859859
860860
---
861861

862+
### len
863+
864+
```Rust
865+
fn len!(array) -> (U256)
866+
```
867+
868+
Get the length of an array
869+
870+
> @param **an** *array*
871+
>
872+
> @returns *the length of an array*
873+
874+
---
875+
862876
### nullContractAddress
863877

864878
```Rust

docs/ralph/contracts.md

Lines changed: 21 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,25 @@ TxScript Main(fooId: ByteVec) {
337337
}
338338
```
339339

340+
### Implicit and Explicit Main Function
341+
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:
346+
347+
```rust
348+
TxScript Main(foo: Foo) {
349+
@using(preapprovedAssets = false)
350+
pub fn main() -> () {
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+
340359
## Gasless Transaction
341360

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

docs/sdk/interact-with-contracts.md

Lines changed: 92 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ the contract states: `getTotalSupply`, `getSymbol`, `getName`,
105105
`getDecimals` and `balance`. It also has a function called `withdraw`,
106106
which not only updates the contract state, but also transfers assets.
107107

108+
## Deploying Contracts
109+
108110
After `TokenFaucet` contract is
109111
[compiled](/dapps/tutorials/quick-start#compile-your-contract), a
110112
corresponding Typescript class is generated. We can get an instance of
@@ -142,44 +144,11 @@ You can use `<contractInstance>.fetchState()` to get the current fields of the t
142144
const state = await tokenFaucet.fetchState()
143145
```
144146

145-
## Contract Views
146-
147-
Inside of the `TokenFaucet` typescript class, [Typescript
148-
SDK](/sdk/getting-started) generates the read-only `view` methods for
149-
all functions in the `TokenFaucet` contract. `view` methods do not
150-
update the blockchan state, they can be called just like regular
151-
typescript functions:
152-
153-
```typescript
154-
const getNameResult = await tokenFaucet.view.getName()
155-
console.log(`name: ${hexToString(getNameResult.returns)}`) // name: TokenFaucet
156-
157-
const getDecimalsResult = await tokenFaucet.view.getDecimals()
158-
console.log(`decimals: ${getDecimalsResult.returns)}`) // decimals: 18
159-
```
160-
161-
Note that results of the `view` methods are returned immediately and
162-
there is no gas or signatures required.
163-
164-
[Typescript SDK](/sdk/getting-started) also generates code for calling
165-
multiple functions at the same time, reducing the number of network
166-
requests:
167-
168-
```typescript
169-
const result = await tokenFaucet.multicall({
170-
getSymbol: {},
171-
getName: {},
172-
getDecimals: {},
173-
getTotalSupply: {}
174-
})
147+
## Interact With Contracts
175148

176-
console.log(`name: ${hexToString(result.getName.returns)}`) // name: TokenFaucet
177-
console.log(`symbol: ${hexToString(result.getSymbol.returns)}`) // symbol: TF
178-
console.log(`decimals: ${result.getDecimals.returns}`) // decimals: 18
179-
console.log(`total supply: ${result.getTotalSupply.returns}`) // total supply: 10
180-
```
149+
We can interact with contracts either by sending transactions or by calling contract methods locally without publishing anything onchain.
181150

182-
## TxScript Transactions
151+
### TxScript Transactions
183152

184153
`TokenFaucet` contract also has a function called `withdraw`, which
185154
transfers the token from the faucet to the caller and updates the
@@ -226,9 +195,10 @@ non-deterministic depending on the future state of the blockchain when
226195
the transaction is mined. `Events` are instead often used to gain insights
227196
into the contract activities.
228197

229-
## Contract Transact Methods
198+
### Transact Methods
230199

231-
[Typescript SDK](/sdk/getting-started) also generates the `transact`
200+
Inside of the `TokenFaucet` typescript class, [Typescript
201+
SDK](/sdk/getting-started) generates the `transact`
232202
methods for all functions in the `TokenFaucet` contract to simplify
233203
the scenario where user wants to perform a `TxScript` transaction by
234204
only calling a function in the contract. For example, the following
@@ -248,6 +218,90 @@ generates bytecode for the corresponding `TxScript` and executes
248218
it. Note that `TxScript` is still required for more complicated
249219
scenarios.
250220

221+
### View Methods
222+
223+
[Typescript SDK](/sdk/getting-started) also generates the read-only `view` methods for
224+
all functions in the `TokenFaucet` contract. `view` methods do not
225+
update the blockchain state, they can be called just like regular
226+
typescript functions:
227+
228+
```typescript
229+
const getNameResult = await tokenFaucet.view.getName()
230+
console.log(`name: ${hexToString(getNameResult.returns)}`) // name: TokenFaucet
231+
232+
const getDecimalsResult = await tokenFaucet.view.getDecimals()
233+
console.log(`decimals: ${getDecimalsResult.returns}`) // decimals: 18
234+
```
235+
236+
Note that results of the `view` methods are returned immediately and
237+
there is no gas or signatures required.
238+
239+
[Typescript SDK](/sdk/getting-started) also generates code for calling
240+
multiple functions at the same time, reducing the number of network
241+
requests:
242+
243+
```typescript
244+
const result = await tokenFaucet.multicall({
245+
getSymbol: {},
246+
getName: {},
247+
getDecimals: {},
248+
getTotalSupply: {}
249+
})
250+
251+
console.log(`name: ${hexToString(result.getName.returns)}`) // name: TokenFaucet
252+
console.log(`symbol: ${hexToString(result.getSymbol.returns)}`) // symbol: TF
253+
console.log(`decimals: ${result.getDecimals.returns}`) // decimals: 18
254+
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+
Contract Foo(mut value: U256) {
263+
pub fn foo() -> U256 {
264+
value = value + 1
265+
return value
266+
}
267+
}
268+
269+
Contract Bar(value: ByteVec) {
270+
pub fn bar() -> ByteVec {
271+
return value
272+
}
273+
}
274+
275+
// This TxScript uses explicit main function. In most cases, the main functions are implicit
276+
TxScript Main(foo: Foo, bar: Bar) {
277+
pub fn main() -> (U256, ByteVec) {
278+
return foo.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:
287+
288+
```typescript
289+
import { getSigner } from '@alephium/web3-test'
290+
291+
const signer = await getSigner()
292+
const deployFooResult = await Foo.deploy(signer, { initialFields: { value: 0n } })
293+
const deployBarResult = await Bar.deploy(signer, { initialFields: { value: '0011' } })
294+
const fooInstance = deployFooResult.contractInstance
295+
const barInstance = deployBarResult.contractInstance
296+
const callResult = await MainForCall.call({
297+
initialFields: { foo: fooInstance.contractId, bar: barInstance.contractId },
298+
interestedContracts: [ fooInstance.address, barInstance.address ]
299+
})
300+
expect(callResult.contracts[0].fields.value).toEqual(1n)
301+
expect(callResult.contracts[1].fields.value).toEqual('0011')
302+
expect(callResult.returns).toEqual([0n, '0011'])
303+
```
304+
251305
## Events Subscription
252306

253307
The `withdraw` function for `TokenFaucet` contract emits a `Withdraw`

0 commit comments

Comments
 (0)