NEWTON

NEWTON


Popular tags

    What are function argument types in starknet.js?

    Asked

    23 days ago

    7

    views


    0

    What are function argument types in starknet.js?

      starknetjscairostarknet

    Newton

    asked

    23 days ago


    1 answers

    0

    Accepted answer

    There are 4 different types of contract function arguments used in Starknet.js.

    Array of <BigNumberish> :

    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`
    

    Object :

    You can list your parameters in an object:

    • The names of the object parameters are the names of the Cairo function parameters.
    • Simple types have to be converted in strings.
    • For an array, you have to use an array of strings.
    • For a Cairo struct, you have to code this way (example for an Uint256) :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.

    Array of <string> :

    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
    });
    

    Array of <any> :

    With this type, you can include:

    • BigNumberish
    • objects representing a Cairo struct
    • arrays

    , 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.

    summary table for arguments :

    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 :

    Functionarray of < BigNumberish >array of < string >objectarray of < any >MultiInvoke
    Typescript typeRawCalldataCalldata or RawArgs or RawCalldataRawArgsArray< 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

    23 days ago

    Your answer

    NEWTON

    NEWTON