NEWTON

NEWTON


Popular tags

    How do I transform data from javascript to Cairo?

    Asked

    6 months ago

    8

    views


    0

    How do I transform data from javascript to Cairo?

      starknetjscairostarknetjavascript

    Newton

    asked

    6 months ago


    1 answers

    0

    Accepted answer

    Data transformation

    Cairo contracts and Javascript/Typescript languages do not have the same types of data. So, it's necessary to prepare the data before sending them to a contract (for invoke/execute, or for a constructor).On the other side, when a contract sends data to your DAPP (the result of a call), you have also to transform them before use in your code.

    Types of data :

    In Cairo, everything is felt, an integer on 251 bits. This type does not exist in JS/TS; you have Number, bigInt, string, array, objects... and types defined in libraries. In Starknet.js, it's a bit ... complicated: you have the BigNumberish type; it can include:

    • String: "123", "0xabc2"
    • Number (max 64 bits): 123
    • BN (max 256 bits): BigNum from BN.js 🤯.
    const myBigInt=BigInt(1234n); // To create a BigInt const myBN=new BN("0x12b4"); // To create a BN const myBN=new BN(myBigInt.toString(); // To convert a BigInt to BN const myBigInt=BigInt(myBN.toString); // To convert a BN to BigInt

    Newton

    answered

    6 months ago

    Your answer

    NEWTON

    NEWTON