Skip to content

Commit aa501bc

Browse files
Akashneelesh“AkashBalasubramani-Router
andauthored
refactor: call_contract_syscall example (#423)
* Added extensive example for call_contract_syscall * Updated example contract for contract_sys_call * Updated the info * Updated the info and contract * Updated example and information --------- Co-authored-by: “AkashBalasubramani-Router <“akashb@routerprotocol.com>
1 parent ce035cb commit aa501bc

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed
Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,43 @@
1+
use starknet::ContractAddress;
12
#[starknet::interface]
23
trait ITokenWrapper<TContractState> {
34
fn transfer_token(
45
ref self: TContractState,
5-
address: starknet::ContractAddress,
6-
selector: felt252,
7-
calldata: Array<felt252>
6+
address: ContractAddress,
7+
sender: ContractAddress,
8+
recipient: ContractAddress,
9+
amount: u256
810
) -> bool;
911
}
1012

1113
#[starknet::contract]
1214
mod TokenWrapper {
1315
use super::ITokenWrapper;
16+
use serde::Serde;
1417
use starknet::SyscallResultTrait;
18+
use starknet::ContractAddress;
1519

1620
#[storage]
1721
struct Storage {}
1822

1923
impl TokenWrapper of ITokenWrapper<ContractState> {
2024
fn transfer_token(
2125
ref self: ContractState,
22-
address: starknet::ContractAddress,
23-
selector: felt252,
24-
calldata: Array<felt252>
26+
address: ContractAddress,
27+
sender: ContractAddress,
28+
recipient: ContractAddress,
29+
amount: u256
2530
) -> bool {
26-
let mut res = starknet::call_contract_syscall(address, selector, calldata.span())
31+
let mut call_data: Array<felt252> = ArrayTrait::new();
32+
Serde::serialize(@sender, ref call_data);
33+
Serde::serialize(@recipient, ref call_data);
34+
Serde::serialize(@amount, ref call_data);
35+
let mut res = starknet::call_contract_syscall(
36+
address, selector!("transferFrom"), call_data.span()
37+
)
2738
.unwrap_syscall();
2839
Serde::<bool>::deserialize(ref res).unwrap()
2940
}
3041
}
3142
}
43+

src/ch99-02-02-contract-dispatcher-library-dispatcher-and-system-calls.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,6 @@ Using these syscalls can be handy for customized error handling or to get more c
8484

8585
<span class="caption">Listing 99-9: A sample contract using syscalls</span>
8686

87-
To use this syscall, we passed in the contract address, the function selector (which is the `starknet_keccak` hash of the function name), and the calldata (function arguments). At the end, we get returned a serialized value which we'll need to deserialize ourselves!
87+
To use this syscall, we passed in the contract address, the selector of the function we want to call, and the call arguments.
88+
89+
The call arguments must be provided as an array of `felt252`. To build this array, we serialize the expected function parameters into an `Array<felt252>` using the `Serde` trait, and then pass this array as calldata. At the end, we are returned a serialized value which we'll need to deserialize ourselves!

0 commit comments

Comments
 (0)