1

Let's say I'm using Web3 or Nethereum, both of which apparently require the contract's ABI to bind the functions.

Is there no way to call smart contract functions without knowing the complete ABI? What If I know only the smart contract address and function name/parameters?

Thanks in advance!

2 Answers 2

2

I'm not sure about Web3 or Nethereum, but ethersjs allows you to manually input a contract's functions (provided you know the function names/arguments themselves, and therefore what they would look like in the actual ABI) when constructing a contract object. Per their tutorial:

you can safely ignore any methods you don't need or use, providing a smaller subset of the ABI to the contract.

2
  • Thanks, but I'm looking for a server side solution and it doesn't look like ethersjs has a non-js library. Commented May 19, 2021 at 23:19
  • You can use ethers in Node.js
    – Gajus
    Commented May 20, 2021 at 0:28
0

You can use the Etherscan API. They have an endpoint that will let you get a specified contracts ABI. You need to sign up and get an api key before you can start sending requests but it is free. The contract also needs to be verified for this to work.

Once you have signed up, assuming you're using c#, you could get the ABI using this code:

 var client = new HttpClient();
 var contractAddress = "0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413";
 var apiKey = "My-Api-Key"; 

 using (client)
 {
      var abiResponse = await client.GetAsync($"https://api.etherscan.io/api?module=contract&action=getabi&address={contractAddress}&apikey={apiKey}");
      var contractAbi = await abiResponse.Content.ReadAsStringAsync();
  }

You could do the same using js.

const https = require('https');

const contractAddress = "0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413";
const apiKey = "your-api-key";
const endpoint = `/api?module=contract&action=getabi&address=${contractAddress}&apikey=${apiKey}`;

var options = {
host: 'api.etherscan.io',
path: endpoint,
method: 'GET',
headers: { 
    'Content-Type': 'application/json' 
}
};

async function getContractAbi()  {

let request = https.get(options, (res) => 
{ 
    if (res.statusCode !== 200) {
        console.error(`Did not get an OK from the server. Code: 
${res.statusCode}`);
        res.resume();
        return;
    }

    let data = '';

    res.on('data', (chunk) => { data += chunk; });

        res.on('close', () => 
        {
            console.log('Retrieved all data');
            console.log(data);
        }); 
    });

    request.on('error', (err) => { console.error(`Encountered an error trying to 
make a request: ${err.message}`); }); 

}

getContractAbi();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.