my english is very bad.i am sorry.
I found an example of an auction where you can pay with ethers. I would like to pay with ERC20 tokens. Please tell me how to do this in the placeBID () and FinalizeAuction () functions. I display it all through web3js on the browser. I also created my token and transferred them to all accounts via metamask. I am a noob in this thread. I run everything on the ropsten test environment. Thanks in advance.
AuctionBox.sol
pragma solidity 0.5.3;
import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/math/SafeMath.sol";
import "ballot_test.sol";
contract AuctionBox{
Auction[] public auctions;
function createAuction (
string memory _title,
uint _startPrice,
string memory _description
) public{
// set the new instance
Auction newAuction = new Auction(msg.sender, _title, _startPrice, _description);
// push the auction address to auctions array
auctions.push(newAuction);
}
function returnAllAuctions() public view returns(Auction[] memory){
return auctions;
}
}
contract Auction {
TRC20 trc20;
using SafeMath for uint256;
//AuctionBox trc20;
address payable private owner;
string title;
uint startPrice;
string description;
enum State{Default, Running, Finalized}
State public auctionState;
uint public highestPrice;
address payable public highestBidder;
mapping(address => uint) public bids;
/** @dev constructor to creat an auction
* @param _owner who call createAuction() in AuctionBox contract
* @param _title the title of the auction
* @param _startPrice the start price of the auction
* @param _description the description of the auction
*/
constructor(
address payable _owner,
string memory _title,
uint _startPrice,
string memory _description
) public {
// initialize auction
owner = _owner;
title = _title;
startPrice = _startPrice;
description = _description;
auctionState = State.Running;
}
modifier notOwner(){
require(msg.sender != owner);
_;
}
/** @dev Function to place a bid
* @return true
*/
function placeBid() public payable notOwner returns(bool) {
require(auctionState == State.Running);
require(msg.value > 0);
// update the current bid
// uint currentBid = bids[msg.sender] + msg.value;
uint currentBid = bids[msg.sender].add(msg.value);
//uint currentBid = balanceOf[msg.sender];
require(currentBid > highestPrice);
// set the currentBid links with msg.sender
bids[msg.sender] = currentBid;
//balanceOf[msg.sender] = currentBid;
// update the highest price
highestPrice = currentBid;
highestBidder = msg.sender;
//trc20.transferFrom(msg.sender, owner, 100);
return true;
}
function finalizeAuction() public{
//the owner and bidders can finalize the auction.
require(msg.sender == owner || bids[msg.sender] > 0);
address payable recipiant;
uint value;
// owner can get highestPrice
if(msg.sender == owner){
recipiant = owner;
value = highestPrice;
}
// highestBidder can get no money
else if (msg.sender == highestBidder){
recipiant = highestBidder;
value = 0;
}
// Other bidders can get back the money
else {
recipiant = msg.sender;
value = bids[msg.sender];
}
// initialize the value
bids[msg.sender] = 0;
recipiant.transfer(value);
//transfer(recipiant, 100);
auctionState = State.Finalized;
}
/** @dev Function to return the contents od the auction
* @return the title of the auction
* @return the start price of the auction
* @return the description of the auction
* @return the state of the auction
*/
function returnContents() public view returns(
string memory,
uint,
string memory,
State
) {
return (
title,
startPrice,
description,
auctionState
);
}
}
ballot_test.sol
pragma solidity 0.5.3;
contract TRC20{
string public name;
string public symbol;
uint8 public decimals = 8;
uint256 public totalSupply;
mapping (address=> uint256) public balanceOf;
mapping(address=> mapping(address=>uint256)) public allowance;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed _owner, address indexed _spender, uint256 value);
event Burn(address indexed from, uint256 value);
uint256 initialSupply = 1000000;
string tokenName = 'CodeXpertToken';
string tokenSymbol = 'CDX';
constructor() public{
totalSupply = initialSupply*10**uint256(decimals);
balanceOf[msg.sender] = totalSupply;
name = tokenName;
symbol = tokenSymbol;
}
function _transfer(address _from, address _to, uint _value) internal{
//require(_to!=0x0);
require(balanceOf[_from]>=_value);
require(balanceOf[_to] + _value>=balanceOf[_to]);
uint previousBalances = balanceOf[_from] + balanceOf[_to];
balanceOf[_from] -= _value;
balanceOf[_to] +=_value;
emit Transfer(_from, _to, _value);
assert(balanceOf[_from]+balanceOf[_to]==previousBalances);
}
function transfer(address _to, uint256 _value) public payable returns (bool success){
_transfer(msg.sender, _to, _value);
return true;
}
function transferFrom(address _from, address _to, uint256 _value) public returns(bool success){
require(_value<= allowance[_from][msg.sender]);
_transfer(_from, _to, _value);
return true;
}
function approve(address _spender, uint256 _value) public returns (bool success){
allowance[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
}
trc20.balanceOf(<address>). A contract usualy don't do that sincetrc20.transferFrom()will fail if you don't have enough balance. If you want to use an ERC20 it is enough to include the interface, for example OpenZeppelin's IERC20, then assign the address to a variableIERC20 token = 0x123412341234...;. In order to usetransferFromthe owner has to approve the contract address. – Ismael Feb 03 '20 at 15:22trc20.approve(<auctionBoxAddress>, <amount>, { from: <tokensOwnerAddress> })– Ismael Feb 04 '20 at 14:45