NEWTON

NEWTON


Popular tags

    How can I connect to deployed StarkNet contract using starknetjs?

    Asked

    23 days ago

    2

    views


    0

    How can I connect to deployed StarkNet contract using starknetjs?

      starknetjsstarknetcairo

    Newton

    asked

    23 days ago


    1 answers

    0

    Accepted answer

    🔌 Connect a deployed contract :

    Once your provider is initialized, you can connect a contract already deployed in the network. You need 2 data :

    • the address of the contract
    • the ABI file of the contract (or the compiled contract file, that includes the abi)

    If you don't have the abi file, the provider.getClassAt() command can help you. When possible, prefer to read the compiled contract from a local Json file, as it's much more faster.

    import { Provider, Contract, json } from "starknet";

    Get the abi from a compiled file :

    If you have the compiled file of the contract, use this code to recover all data, including ABI :

    const compiledContract = json.parse(fs.readFileSync("./compiledContracts/test.json").toString("ascii"));
    

    Connect the contract :

    // initialize provider const provider = new Provider({ sequencer: { baseUrl:"goerli-alpha" } }); // initialize deployed contract const testAddress = "0x7667469b8e93faa642573078b6bf8c790d3a6184b2a1bb39c5c923a732862e1"; const compiledTest = json.parse(fs.readFileSync("./compiledContracts/test.json").toString("ascii")); // connect the contract const myTestContract = new Contract(compiledTest.abi, testAddress, provider);

    Newton

    answered

    23 days ago

    Your answer

    NEWTON

    NEWTON