Getting an error when calling a function of a smart contract using etherjs

Hi everyone,

I deployed this token on binance smart chain testnet. I’m trying to call changeAdmin function of this contract using etherjs but getting an error.

the command that I execute
yarn hardhat run --network testnet scripts/bypass.js

hardhat.config.js

.....testnet: {
  url: "https://data-seed-prebsc-1-s1.binance.org:8545",
  chainId: 97,
  gasPrice: 20000000000,
},......

the script

const ethers = require(‘ethers’)

async function main() {
let network = ‘https://data-seed-prebsc-1-s1.binance.org:8545/
let provider = ethers.getDefaultProvider(network)

let privKey = PRIVATE_KEY_OF_THE_CREATOR_OF_THE_CONTRACT
let wallet = new ethers.Wallet(privKey, provider)

let contractAddr = ADDRESS_OF_THE_CONTRACT 
let contractABI = ABI_OF_THE_CONTRACT
let contract = new ethers.Contract(contractAddr, contractABI, wallet)

let newOwnerAddr = "0xCc2c513ef320db14f......"
let txResponse= await  contract.methods.changeAdmin(newOwnerAddr)
let txReceipt = await txResponse.wait()
console.log(txReceipt)

}

main()
.catch((error) => {
console.error(error)
process.exit(1)
})

the error i encountered
TypeError: Cannot read properties of undefined (reading ‘changeAdmin’)

Thanks in advance, have a nice day :slight_smile:

contract.methods.changeAdmin(newOwnerAddr)

should be

contract.changeAdmin(newOwnerAddr).

Thanks, I tried it but I got another error:
reason: ‘cannot estimate gas; transaction may fail or may require manual gas limit’,
code: ‘UNPREDICTABLE_GAS_LIMIT’,

The script:

const ethers = require('ethers')


async function main() {
    let network = 'https://data-seed-prebsc-1-s1.binance.org:8545/'
    let provider = ethers.getDefaultProvider(network)


    let privKey = "PRIVATE-KEY"
    let wallet = new ethers.Wallet(privKey, provider)

    let contractAddr = "0x066229c2D5E3673186b756a6c1f3C8689560B474" 
    let contractABI = ABI-OF-THE-CONTRACT
    let contract = new ethers.Contract(contractAddr, contractABI, provider)


    let contractWithWallet = contract.connect(wallet)
    let newOwnerAddr = "0xCc2c513ef320db14fc01b4a828EF548DAfF51429"
    let txResponse= await  contractWithWallet.changeAdmin(newOwnerAddr)
    let txReceipt = await txResponse.wait()
    console.log(txReceipt)


}

main()
    .catch((error) => {
        console.error(error)
        process.exit(1)
    })

Make sure the contract address and chain are all correct. Try setting a gas limit to see if you can get past it.

await contractWithWallet.changeAdmin(newOwnerAddr, { gasLimit: 100000 })

1 Like