full-blockchain-solidity-course-py icon indicating copy to clipboard operation
full-blockchain-solidity-course-py copied to clipboard

lesson 10: VirtualMachineError: revert: SafeMath: subtraction overflow

Open DireWolf707 opened this issue 3 years ago • 5 comments

Getting this error while depositing to lending pool:

bandicam 2022-01-24 07-04-58-792

get_weth.py

from scripts.utils import get_account
from brownie import interface,network,config
from web3 import Web3

ETHER_DEPOSIT = 0.1
WEI_DEPOSIT = Web3.toWei(ETHER_DEPOSIT,"ether")

def get_weth():
    account=get_account()
    weth = interface.IWeth(config["networks"][network.show_active()]["weth_token"]) 
    tx = weth.deposit({
        "from":account,"value":WEI_DEPOSIT
    })       
    tx.wait(1)    
    print(f"got {ETHER_DEPOSIT} weth")

def main():
    get_weth()

aave_borrow.py

from web3 import Web3
from scripts.utils import get_account,FORKED_BLOCKCHAIN
from brownie import accounts, network,config,interface
from scripts.get_weth import get_weth,WEI_DEPOSIT

def main():
    account = get_account()
    # get some weth(erc-20 version of eth) for eth
    if network.show_active() in FORKED_BLOCKCHAIN:
        get_weth()
    # get contract of lending pool to approve of our weth and deposit weth
    lending_pool = get_lending_pool()
    # approve aave to use our weth token so that it can spend it on our behalf
    erc20_address = config["networks"][network.show_active()]["weth_token"]
    approve_erc20(lending_pool.address,WEI_DEPOSIT,erc20_address,account)
    # deposit to aave
    print("depositing")
    tx = lending_pool.deposit(erc20_address,WEI_DEPOSIT,account.address,0,{
        "from":account
    })
    tx.wait(1)
    print("deposited")

def approve_erc20(spender,value,erc20_address,account):
    print("approving erc 20")
    erc20 = interface.IERC20(erc20_address)
    tx = erc20.approve(spender,value,{
        "from":account
    })
    tx.wait(1)
    print("approved")

def get_lending_pool():
    # get lending pool address provider contract to get address of lending pool contract
    lending_pool_address_provider = interface.ILendingPoolAddressesProvider(config["networks"][network.show_active()]["lending_pool_address_provider"])
    lending_pool_address = lending_pool_address_provider.getLendingPool()
    # get lending pool contract from its address
    lending_pool = interface.ILendingPool(lending_pool_address)
    return lending_pool

utils.py

from brownie import network,config,accounts

LOCAL_BLOCKCHAIN = ('ganache-local','development')
FORKED_BLOCKCHAIN = ('mainnet-fork','mainnet-fork-dev')

def get_account(index=None,id=None):
    if index:
        return accounts[index]
    if id:
        return accounts.load(id)
    curr_network = network.show_active()
    if curr_network in LOCAL_BLOCKCHAIN + FORKED_BLOCKCHAIN :
        return accounts[0]
    if curr_network in config["networks"]:
        return accounts.add(config["wallets"]["from_key"])
    return None

TRIED:

  • using both infura and alchemy mainnet-fork
  • git clone from course repo but same error in that too

DireWolf707 avatar Jan 24 '22 07:01 DireWolf707

@PatrickAlphaC please help with this issue whenever you have time. Thank you

DireWolf707 avatar Jan 25 '22 06:01 DireWolf707

Hello @DireWolf707 seems to be a problem with safe math library, like you trying to substract more than the maximum amount permited, uint256 does not allot negative numbers at all, could you try using solc v 0.8.0 instead?

cromewar avatar Jan 30 '22 17:01 cromewar

@cromewar we dont make any .sol file in lesson 10.We just interact with aave smart contract.

DireWolf707 avatar Jan 30 '22 19:01 DireWolf707

@DireWolf707 change the datatype of ILendingPool.sol to int256 and it'll work fine.You can check my github if you got any errors as I tried my best to document everything.Happy learning :)

spo0ds avatar Apr 21 '22 10:04 spo0ds

Nice @spo0ds !!

cromewar avatar Apr 21 '22 13:04 cromewar