0

Pretty standard question here I think but as a newbie I am having trouble getting my head around this in a clear way.

I am experimenting with a simple forward auction using a couple of contracts and a custom ERC-20 token, and react.js

I have an "Enter bid" button which I would like to call the transfer method from my ERC-20 Token, while at the same time calling a submitBid function from my auction contract. I can get the transfer to work no problem, or I can get the submitBid transaction to send also but I can't do both. I think I need to inherit one contract into the other or something but I don't quite understand inheritance fully yet.

Any advice would be greatly appreciated, posting some code below:

submitBid from Auction Contract:

function submitBid(uint _bid) public payable {
        require(block.timestamp < auctionEndTime, "Auction has ended");
        require(_bid > highestBid, "Bid must be higher than current highest bid");

        
        currentAddressBid[msg.sender] += _bid;
        
        highestBid = _bid;
        highestBidder = msg.sender;
        
        emit HighestBidIncrease(msg.sender,_bid);
}

Javascript enterBid function to call the two methods, is called when user enters a big and clicks "submit" :

enterBid = async() => {
    var amountToSend = this.state.bid * 1000000000000000000;


    await this.tokenInstance.methods.transfer(this.state.tokenSaleAddress, amountToSend.toString()).send({from: this.accounts[0]});
    await this.ForwardAuctionInstance.methods.submitBid(this.state.bid).call();
  }

my token is a standard ERC-20 token using OpenZeppelin's contract's

UPDATED CODE

contract ForwardAuction {

IERC20 tokenAddress;

IERC20 token = IERC20(tokenAddress);

// Auction Parameters
address payable public  beneficiary;
uint auctionEndTime;

// Current state of the auction
address public highestBidder;
uint public highestBid;

mapping(address => uint) public currentAddressBid;

bool ended = false;

event HighestBidIncrease(address bidder, uint bid);
event AuctionEnded(address winner, uint amount);

constructor(
    uint _biddingTime,
    address payable _beneficiary,
    IERC20 _tokenAddress
    ) 
    
    { 
        beneficiary = _beneficiary;
        auctionEndTime = block.timestamp + _biddingTime;
        tokenAddress = _tokenAddress;
        
    }

function returnHighestBid() public view returns(uint) {
    return highestBid;
}


function submitBid(uint _bid) public {
    require(block.timestamp < auctionEndTime, "Auction had ended");
    require(_bid > highestBid, "Bid must be higher than current highest bid");

    token.transfer(address(this), _bid); // This will effectively take the users tokens and send it to the contract. It requires users allowance of your token for your auction contract to be greater than '_bid'


    
    currentAddressBid[msg.sender] += _bid;
    
    highestBid = _bid;
    highestBidder = msg.sender;
    
    emit HighestBidIncrease(msg.sender,_bid);

    

}

javascript:

enterBid = async() => {

var toSend = this.state.bid * 1000000000000000000;
await this.ForwardAuctionInstance.methods.submitBid(toSend.toString()).send({from: this.accounts[0]});

}

0

Im pretty sure you cant call 2 separate contract in 1 transaction. Theres some workaround though. Id have the user interact only with the auction contract and modify the submitBid function accordingly. Something like that should work

//This should be in the part of your code where you declare all your variables. 
IERC20 token = IERC20(yourTokenAddress);

/**
*     rest of your code
*/

// The 'payable' keyword means that this function should be called with a value transfer ( = we excpect the user to send ETH alongside the call. Since you're using a ERC20 token its not necessary. 
function submitBid(uint _bid) public { 
    require(block.timestamp < auctionEndTime, "Auction has ended"); 
    require(_bid > highestBid, "Bid must be higher than current highest bid");
    token.transferFrom(msg.sender, address(this), _bid); // This will effectively take the users tokens and send it to the contract. It requires users allowance of your token for your auction contract to be greater than '_bid'
    currentAddressBid[msg.sender] += _bid;
    
    highestBid = _bid;
    highestBidder = msg.sender;
    
    emit HighestBidIncrease(msg.sender,_bid);

}
11
  • Thanks a lot for your input it has definitely helped me to understand this further. That being said, I still haven't managed to get it working! I am getting "revert -32000" error when trying to send the transaction off now. I;m updating my code in the original post to show you Aug 23 at 17:44
  • I've updated it (sorry if not clear Im struggling with this website.....) I really appreciate your help Aug 23 at 17:50
  • token.transfer(address(this), _bid); This is transferring _bid from the contract to itself (which is useless and throws if the contracts balance is < _bid). You might wanna read about ERC20's transferFrom function. Also maybe we can take this talk elsewhere if you want, feel free to drop your tg/discord/anything i can contact you on.
    – Foxxxey
    Aug 23 at 17:54
  • Oh right, of course. Sorry I had initally followed your suggestion and used transferFrom, however i tried changing it to transfer because it was giving me an error. If I use token.transferFrom(msg.sender, address(this), _bid) i get the same error....presumably for the same reason? I am off out right now but when I'm back I will post my discord! Aug 23 at 18:30
  • I believe this await this.ForwardAuctionInstance.methods.submitBid(this.state.bid).call(); was correct, why did you change it?
    – Foxxxey
    Aug 23 at 18:43

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.