0

I have deployed a simple smart contract to the ropsten chain. I am just starting out and trying to learn. Not new to python but new to smart contracts and especially web3.

This is the code for the function I am trying to call from python.

function getAmountOutMin(address _tokenIn, address _tokenOut, uint256 _amountIn) 
external view returns (uint256) {

    address[] memory path;
    if (_tokenIn == WETH || _tokenOut == WETH) {
        path = new address[](2);
        path[0] = _tokenIn;
        path[1] = _tokenOut;
    } else {
        path = new address[](3);
        path[0] = _tokenIn;
        path[1] = WETH;
        path[2] = _tokenOut;
    }
    
    uint256[] memory amountOutMins = IUniswapV2Router(UNISWAP_V2_ROUTER).getAmountsOut(_amountIn, path);
    return amountOutMins[path.length -1];  
}  

This is the python code I am using to Call the function:

infura_url = 'MY Ropsten Infura url'
web3 = Web3(Web3.HTTPProvider(infura_url))
contract_addr = '0xA54a5015D5bCE08c517A8CAEC8e0CF7A7F179399'
my_abi = json.loads('ABI')
my_contract = web3.eth.contract(address=contract_addr, abi=my_abi)

# uniswap address and abi
uniswap_router = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D'
uniswap_factory = '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f'
uniswap_factory_abi = json.loads('ABI')

contract = web3.eth.contract(address=uniswap_factory, abi=uniswap_factory_abi)

def main():
    ret = my_contract.caller.check_in() // function I made in contract to return 1 to check if connected to contract
    print(ret)
    min_out = my_contract.caller.getAmountOutMin(web3.toChecksumAddress('0xf70949Bc9B52DEFfCda63B0D15608d601e3a7C49'), web3.toChecksumAddress('0xFc67a5421156B29aC073F62861C097b56225a4F8'), web3.toWei(1, 'ether'))
    //the first address is BAT listed on ropsten, second is WETH
    print(min_out)

from what I can tell, I dont need to submit a transaction as the function is not modifying any data on the chain. But I could be wrong.

This is the error I get when running the code, after the check_in() function returns 1:

web3.exceptions.ContractLogicError: execution reverted

I just want to be pointed in the right direction, a full answer does not have to be given.

1

Okay, self own..

So to anyone else starting out.. the answer to this is always make sure you are using the right token addresses. In my code here, the weth address is completely wrong for ropsten.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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