0

To preface, I am pretty new to Javascript.

What I'm trying to do is run a function from Visual Studio Code so I can have a quicker code -> test workflow. The code in question is part of a larger application.

I installed the extension "Code Runner", select my function, right-click, and "Run Code".

When I run the function I get ReferenceError: getTokenBalance is not defined. I'm assuming there is a lot more setup that I need to do so I will be able to run .js functions in VSCode.

EDIT: The function is

async function getTokenBalance(wallet_address, token_address) {

    // Default structure of ERC20 smart contract
    let minABI = [
      // balanceOf
      {
        "constant":true,
        "inputs":[{"name":"_owner","type":"address"}],
        "name":"balanceOf",
        "outputs":[{"name":"balance","type":"uint256"}],
        "type":"function"
      },
      // decimals
      {
        "constant":true,
        "inputs":[],
        "name":"decimals",
        "outputs":[{"name":"","type":"uint8"}],
        "type":"function"
      }
    ];

    var map = {};
    map["id"] = "getTokenBalance";
    map["balance"]="-1";

    let contract = new web3.eth.Contract(minABI, token_address);
    console.log(contract);

    try {

        const balance = await contract.methods.balanceOf(wallet_address).call();
        const decimalPlaces = await contract.methods.decimals().call(); // 8
        let newBalance = 0;
        
        if (decimalPlaces) {
            newBalance = balance / (10 ** decimalPlaces);
        }

        console.log(balance);
        console.log(decimalPlaces);
        console.log(newBalance);

        map["balance"] = newBalance;

    } catch(error) {
        console.log(error);
    }

    GMS_API.send_async_event_social(map);
}

And here is the console after running the function

[Running] node "c:\Users\PC\Documents\GameMakerStudio2\gamemaker-web3\extensions\web3\tempCodeRunnerFile.js" c:\Users\PC\Documents\GameMakerStudio2\gamemaker-web3\extensions\web3\tempCodeRunnerFile.js:1 getTokenBalance ^

ReferenceError: getTokenBalance is not defined at Object. (c:\Users\PC\Documents\GameMakerStudio2\gamemaker-web3\extensions\web3\tempCodeRunnerFile.js:1:1) at Module._compile (internal/modules/cjs/loader.js:1072:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10) at Module.load (internal/modules/cjs/loader.js:937:32) at Function.Module._load (internal/modules/cjs/loader.js:778:12) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) at internal/main/run_main_module.js:17:47 [Done] exited with code=1 in 0.163 seconds

Prajwal Kulkarni
  • 1,480
  • 13
  • 22
Nukage
  • 1
  • 1

0 Answers0