Hello, I'm facing a problem with my smart contract
here's my js code
App.contracts.TokenSale.getETHPrice().then((x) => {
const value = (50 / x.toNumber()).toFixed(2);
const big = BigInt(inputvalue * 10**18);
App.contracts.TokenSale.buyToken(big, {
from: '0xf2a0AE4343541aE687BAd616301DfE0F5cb156Ae',
value: inputvalue * value * 10**18,
gas: 5000000, // Gas limit
});
});
Token:
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import '@openzeppelin/contracts/token/ERC20/ERC20.sol';

contract Token is ERC20 {
constructor() ERC20("CursedToken", "CT") {
_mint(msg.sender, 1000000000 * 10**18);
}
}

Mar 21, 2022, 2:12 PM
Vendor
import './Token.sol';
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract Vendor {
address payable public admin;
address payable private ethFunds = payable(0xf2a0AE4343541aE687BAd616301DfE0F5cb156Ae);
Token public token;
uint256 public tokensSold;
int public tokenPriceUSD;
AggregatorV3Interface internal priceFeed;

uint256 public transactionCount;

event Sell(address _buyer, uint256 _amount);

struct Transaction {
address buyer;
uint256 amount;
}

mapping(uint256 => Transaction) public transaction;

constructor(Token _token) {
priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331);
tokenPriceUSD = 50;
token = _token;
admin = payable(msg.sender);
}

function getETHPrice() public view returns(int) {
(, int price, , , ) = priceFeed.latestRoundData();
return (price / 10**8);
}

function facuTokenPriceInETH() public view returns(int) {
int ethPrice = getETHPrice();
return tokenPriceUSD / ethPrice;
}

function buyToken(uint256 _amount) public payable {
int facuTokenPriceETH = facuTokenPriceInETH();
// Check that the buyer sends the enough ETH
require(int(msg.value) >= facuTokenPriceETH * int(_amount));
// Check that the sale contract provides the enough ETH to make this transaction.
require(token.balanceOf(address(this)) >= _amount);
// Make the transaction inside of the require
// transfer returns a boolean value.
require(token.transfer(msg.sender, _amount));
// Transfer the ETH of the buyer to us
ethFunds.transfer(msg.value);
// Increase the amount of tokens sold
tokensSold += _amount;
// Increase the amount of transactions
transaction[transactionCount] = Transaction(msg.sender, _amount);
transactionCount++;
// Emit the Sell event
emit Sell(msg.sender, _amount);
}

function endSale() public {
require(msg.sender == admin);
// Return the tokens that were left inside of the sale contract
uint256 amount = token.balanceOf(address(this));
require(token.transfer(admin, amount));
selfdestruct(payable(admin));
}

}
In the moment I try to buy it returns me failed transaction or even creates multiple transactions it works with 0 tokens
Mar 21, 2022, 2:12 PM

© 2024 Draquery.com All rights reserved.