0

I have deployed a simple contract on the BSC testnet which has a simple function that executes a swap on Pancakeswap.

This is how I call the function with Brownie:

def main():
    tx = contract1.buyToken(0.01,10000,'0x094616F0BdFB0b526bD735Bf66Eca0Ad254ca81F','0xC880a918eDB02E9cf006369927a68c5e7F778B4B')
    return True

And I get this error:

Running '\python_projects\docs3\coin_observer\brownie\scripts\testz.py::main'...
  File "c:\users\home\appdata\roaming\python\python38\lib\site-packages\brownie\_cli\run.py", line 50, in main
    return_value, frame = run(
  File "c:\users\home\appdata\roaming\python\python38\lib\site-packages\brownie\project\scripts.py", line 103, in run
    return_value = f_locals[method_name](*args, **kwargs)
  File "\python_projects\docs3\coin_observer\brownie\scripts\testz.py", line 7, in main
    tx = contract1.buyToken(0.01,10000,'0x094616F0BdFB0b526bD735Bf66Eca0Ad254ca81F','0xC880a918eDB02E9cf006369927a68c5e7F778B4B')
AttributeError: 'ContractContainer' object has no attribute 'buyToken'

And here is the Solidity part:

function buyToken(uint amountToSpend, uint amountToBuy, address tokenToSpendAddress, address tokenToBuyAddress) public returns (uint[] memory amounts)  {
    require(address(this).balance > amountToSpend);
    uint256[] memory receivedTokens;
    address[] memory path = new address[](2);
    path[0] = tokenToSpendAddress;
    path[1] = tokenToBuyAddress;
    receivedTokens = _pancakeRouter.swapExactETHForTokens{value: amountToSpend}(amountToBuy,path,address(this),block.timestamp + 30); 
    return receivedTokens;
}

So my problem is why I get the object has no attribute 'buyToken' error? The buyToken exists in the contract and it compiled correctly.

UPDATE I compiled and deployed the contract again and printed the ABI that seems fine, it contains the function, but the error still exists. I assume something with the ContractContainer is wrong.

1
  • Why are you passing 0.01 as parameter? Solidity doesn't support floating point.
    – Ismael
    Commented Apr 6, 2022 at 1:35

2 Answers 2

0

In your brownie script you need to specify the value you want to send with that transaction.

so

def main():
    tx = contract1.buyToken(0.01,10000,'0x094616F0BdFB0b526bD735Bf66Eca0Ad254ca81F','0xC880a918eDB02E9cf006369927a68c5e7F778B4B', {"from": account, "value": 10})
    return True

The value should match the amount you pass to the contract. Hope this helps.

0

I had a same error. The problem was in variable. I accidently write MainContract insted of main_contract. MainContract was a storage of compiled contracts and main_contract is deployed contract

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.