NEWTON
Asked
3 months ago
7
views
0
What are function argument types in starknet.js?
Newton
asked
3 months ago
0
Accepted answer
There are 4 different types of contract function arguments used in Starknet.js.
You have to create by yourself this array of < BigNumberish >, in respect with the order of the Cairo function parameters :
const myCallData = [ // array of <BigNumberish>
123, // number 64 bits
"0x2345", // string
bn1, // BN
bi1.toString(), // BigInt converted to string
num1, // number 64 bits
initialUint256.low, initialUint256.high, //object converted to BigNumberish
coord.x0, coord.y0, coord.z0, //object converted to BigNumberish
shortString.encodeShortString('A'),
2, "123", "0x2345" // an array of 2 felts
];
// in Typescript, this object type is : `RawCalldata`
You can list your parameters in an object:
my_uint: { type: 'struct', low: initialUint256.low, high: initialUint256.high }
.Example for a constructor :
{
name: shortString.encodeShortString('MyToken'),
symbol: shortString.encodeShortString('MTK'),
decimals: "18",
initial_supply: { type: 'struct', low: initialTk.low, high: initialTk.high },
recipient: account0.address,
owner: account0.address,
list:["678","321","7890"] // array of 3 cairo felts
}
🚨 In opposition to the object philosophy, your object content has to be ordered in respect to the order of the definition of the Cairo function parameters.
You can't send an array of cairo struct with this object type.
You can create by yourself this array of < string >, with respect to the order of the Cairo function parameters :
const myCallData = [
"123",
"0x2345",
bn1.toString(), // BN converted to string
bi1.toString(), // BigInt converted to string
number.toBN(num1).toString(), // Number 64 bits converted to string
initialUint256.low.toString(), initialUint256.high.toString(),
coord.x0.toString(), coord.y0.toString(), coord.z0.toString(),
shortString.encodeShortString('A'),
"3", "52788", "123", "0x2345", // an array of 3 felts
"2", "100", "0", "234", "456" // an array of 2 Uint256
];
// in Typescript, this object type is : `Calldata`
Or you can use the function stark.compileCalldata()
, which converts an object type to an 'array of string' type: For a Cairo contract, with this constructor :
func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
name: felt, symbol: felt, decimals: felt, initial_supply: Uint256, recipient: felt, owner: felt
)
you will have to create in your code this set of data :
const initialTk = uint256.bnToUint256(100);
const ERC20ConstructorCallData = stark.compileCalldata({
name: shortString.encodeShortString('MyToken'),
symbol: shortString.encodeShortString('MTK'),
decimals: "18",
initial_supply: { type: 'struct', low: initialTk.low, high: initialTk.high },
recipient: account0.address,
owner: account0.address
});
With this type, you can include:
, with respect to the order of the Cairo function parameters.Example :
const myCallData = [
123, // number 64 bits
"0x2345",
bn1, // BigNum
bi1.toString(), // Bigint converted to string
num1, // number 64 bits
initialUint256, // object representing a struct of 2 felt
coord, // object representing a struct of 3 felt
shortString.encodeShortString('A'), // short string
[123, "0x2345"], // for an array of 2 cairo felts
[initialUint256, finallUint256] // for an array of 2 structs (Uint256 here)
];
// in Typescript, the object type is : `Array<any>`
Object representing a Cairo struct is made of BigNumberish elements. Ex :
interface c3D { x0: BigNumberish; y0: BigNumberish; z0: BigNumberish; }
Same for arrays: their elements must have the BigNumberish type.
These 4 types of arguments can't be used at your convenience everywhere. Here a table showing which type can be used in which function :
Function | array of < BigNumberish > | array of < string > | object | array of < any > | MultiInvoke |
---|---|---|---|---|---|
Typescript type | RawCalldata | Calldata or RawArgs or RawCalldata | RawArgs | Array< any > | array |
contract.call contract.metaClass | ⚠️ | ✔️ | |||
contract.invoke contract.metaClass | ⚠️ | ✔️ | |||
account.execute | ✔️ | ✔️ | ✅ | ||
account.deploy | ✔️ | ✔️ | ✅ | ||
account.deployContract | ✔️ | ✔️ | ✅ | ||
account.declareDeploy | ✔️ | ✔️ | |||
account.deployAccount | ✔️ | ✔️ | |||
hash.calculateContractAddressFromHash | ✔️ | ✔️ | |||
⚠️ = only for a list of felt (no array or struct).
for Typescript, you can import these types of data :
import { type Calldata, type RawArgs } from "starknet"; import { type RawCalldata } from "starknet/dist/types/lib";
Newton
answered
3 months ago
What is Starknet.js?
Which lib can help me to make a multicall in JS for StarkNet?
Do you know a function that converts an array of felts (numbers) to a single string in JS? [Cairo Lang]
Is there any issue between using 4.4 and 3.x starknet.js?
Why I randomly get `HttpError: Bad Gateway` from starknet js?
What are Event Indexers in Cairo lang? What is a StarkNet indexer?
What are events in Cairo Language?
how to send token
ApeWorX: How do I fund newly created accounts from the ape console?
What are steps to create a new contract using starknetjs?
Can you explain about cancellation (L1 -> L2) in StarkNet (sequencer)?
What are events in Cairo Language?
Does anyone have an example of a positional request to `starknet_addInvokeTransaction` on devnet that is working?
What are Event Indexers in Cairo lang? What is a StarkNet indexer?