How to make an autotask on Defender retrieve a public variable

Hey guys,
My aim is to retrieve public variable (uint) from a smart contract developed on BSC Testenet. I can’t understand how to use web3.js in the autotask code.

I tried many times but I can’t catch how it can works. I didn’t find any example for web3.js on Defender.
I know that I can call the getter function of the conract using this:

NameContract.methods.getName().call();

Please help me

1 Like

Hey @FreezyEx! I’d suggest you start by familiarizing yourself with web3js outside Autotasks. Set up a local nodejs project, install web3js, and try querying a contract from there. Once you’re familiar with the library, start moving into Autotasks.

For your specific question, you need to first create an instance of the contract. To do this, you need three things:

  • A provider, which lets you connect to the network
  • The contract address
  • The contract ABI

You can use a connection to Infura or Alchemy as your provider, or, if you have a Defender Relayer for BSC Testnet set up, you can use the Defender Relay client. The contract ABI you can get from your compiled artifacts (look for a field named “abi” in the json files in your build folder in your project, or check the Solidity tab in Remix if you’re using that), and the address is where you deployed it.

Hope this helps!

2 Likes

yes I will train with web3js but in the mean time…
I tried to use also Remix console e scripts. The problem is that on Defender I can’t understand why it give s error on ‘eth’ (web3.eth…) undefined. On Remix I run the script but it shows only the first console log. No errors btw

This is a script that I found as Remix examples on Medium.
I have done exactly the same things the tutorial said but when I run it the console shows the first console log and stop. Nothing else.
(I succeed with remix script (deploy_web3.js) so it is not my fault I think)

Hi @FreezyEx,

You could look at the Learn guides for a generic example: https://docs.openzeppelin.com/learn/deploying-and-interacting?pref=hardhat#interacting-programmatically

1 Like

Finally I succeed in Remix using web3.js
Now I need only to understand how to use it in the autotask. This is the script:

(async () => {
console.log('Running ...')
    
    var abi = [abi here];

    var Mycontract = await new web3.eth.Contract(abi, "address");

let res = await Mycontract.methods.aAmt().call();
console.log("aAmt: ", res.toString());
}

UPDATE

I got it to work also on Autotask on Defender
this is the final script:

const { DefenderRelayProvider } = require('defender-relay-client/lib/web3');
const Web3 = require('web3');

exports.handler = async function(event) {
   const provider = new DefenderRelayProvider(event);
   const web3 = new Web3(provider);
   console.log('Running ...')
    
    var abi = []
 var Mycontract = await new web3.eth.Contract(abi, "address");

let res = await Mycontract.methods.aAmt().call();
console.log("aAmt: ", res.toString());
  return{res}
}
2 Likes