The deployment of InsuranceProvider is working fine and the calling of newContract() with the required parameters is successfully creating/deploying the InsuranceConsumer contract. Even, the payOutContract() is working correctly in terms of transferring the ETH balance from the InsuranceConsumer to the client's wallet.
The issue is with the refundToInsurer() function, as it's expected to transfer the ETH balance from the InsuranceConsumer to the insurer's wallet, but it's transaction is getting failed/reverted.
Here's the code:
SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "#chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract InsuranceProvider {
address payable public insurer;
AggregatorV3Interface internal priceFeed;
modifier onlyOwner() {
require(insurer == msg.sender, "Only Insurance provider can do this");
_;
}
constructor() payable {
priceFeed = AggregatorV3Interface(
0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e
);
insurer = payable(msg.sender);
}
function newContract(
address payable _client,
uint256 _premium,
uint256 _payoutValue
) public payable onlyOwner returns (address) {
//create contract, send payout amount so contract is fully funded plus a small buffer
InsuranceConsumer i = (new InsuranceConsumer){
value: ((_payoutValue * 1 ether) / (uint256(getLatestPrice())))
}(_client, _premium, _payoutValue);
return address(i);
}
function getLatestPrice() public view returns (int256) {
(, int256 price, , uint256 timeStamp, ) = priceFeed.latestRoundData();
// If the round is not complete yet, timestamp is 0
require(timeStamp > 0, "Round not complete");
return price;
}
function payOutContract(address _contract) public {
InsuranceConsumer i = InsuranceConsumer(_contract);
// Transfer agreed amount to client
i.payOutContract();
}
function refundToInsurer(address _contract) public onlyOwner {
InsuranceConsumer i = InsuranceConsumer(_contract);
// Transfer back the amount to insurer
i.refundToInsurer();
}
}
contract InsuranceConsumer {
AggregatorV3Interface internal priceFeed;
address payable public insurer;
address payable client;
uint256 startDate;
uint256 premium;
uint256 payoutValue;
constructor(
address payable _client,
uint256 _premium,
uint256 _payoutValue
) payable {
//set ETH/USD Price Feed
priceFeed = AggregatorV3Interface(
0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e
);
//first ensure insurer has fully funded the contract
require(
msg.value >= _payoutValue / uint256(getLatestPrice()),
"Not enough funds sent to contract"
);
//now initialize values for the contract
insurer = payable(msg.sender);
client = _client;
startDate = block.timestamp; //contract will be effective immediately on creation
premium = _premium;
payoutValue = _payoutValue;
}
function payOutContract() public {
//Transfer agreed amount to client
client.transfer(address(this).balance);
}
function refundToInsurer() public {
// Transfer back the amount to insurer
insurer.transfer(address(this).balance);
}
function getLatestPrice() public view returns (int256) {
(, int256 price, , uint256 timeStamp, ) = priceFeed.latestRoundData();
// If the round is not complete yet, timestamp is 0
require(timeStamp > 0, "Round not complete");
return price;
}
}
Can anyone please help by pointing out the logical mistake that I'm doing in the refundToInsurer() function ?
As, we're creating the InsuranceConsumer using the newContract() function of InsuranceProvider. Therefore, the msg.sender of the InsuranceConsumer is going to be the InsuranceProvider itself, not the insurer's wallet.
So, when we're calling the refundInsurer() of InsuranceConsumer via InsuranceProvider, then it's doing:
insurer.transfer(address(this).balance);
It means to transfer the available ETH in the InsuranceConsumer to the InsuranceProvider (not the insurer's wallet), and since InsuranceProvider is not expected to be a receiver, that's why it's reverting the transaction.
So, the corrected code will be:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "#chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract InsuranceProvider {
address payable public insurer;
AggregatorV3Interface internal priceFeed;
modifier onlyOwner() {
require(insurer == msg.sender, "Only Insurance provider can do this");
_;
}
constructor() payable {
priceFeed = AggregatorV3Interface(
0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e
);
insurer = payable(msg.sender);
}
function newContract(
address payable _client,
uint256 _premium,
uint256 _payoutValue
) public payable onlyOwner returns (address) {
//create contract, send payout amount so contract is fully funded plus a small buffer
InsuranceConsumer i = (new InsuranceConsumer){
value: ((_payoutValue * 1 ether) / (uint256(getLatestPrice())))
}(_client, _premium, _payoutValue);
return address(i);
}
function getLatestPrice() public view returns (int256) {
(, int256 price, , uint256 timeStamp, ) = priceFeed.latestRoundData();
// If the round is not complete yet, timestamp is 0
require(timeStamp > 0, "Round not complete");
return price;
}
function payOutContract(address _contract) public onlyOwner {
// Transfer agreed amount to client
InsuranceConsumer i = InsuranceConsumer(_contract);
i.payOutContract();
}
function refundToInsurer(address _contract) public onlyOwner {
// Transfer back the amount to insurer
InsuranceConsumer i = InsuranceConsumer(_contract);
i.refundToInsurer(insurer);
}
}
contract InsuranceConsumer {
AggregatorV3Interface internal priceFeed;
address payable public insurer;
address payable client;
uint256 startDate;
uint256 premium;
uint256 payoutValue;
constructor(
address payable _client,
uint256 _premium,
uint256 _payoutValue
) payable {
//set ETH/USD Price Feed
priceFeed = AggregatorV3Interface(
0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e
);
//first ensure insurer has fully funded the contract
require(
msg.value >= _payoutValue / uint256(getLatestPrice()),
"Not enough funds sent to contract"
);
//now initialize values for the contract
insurer = payable(msg.sender);
client = _client;
startDate = block.timestamp; //contract will be effective immediately on creation
premium = _premium;
payoutValue = _payoutValue;
}
function payOutContract() public {
//Transfer agreed amount to client
client.transfer(address(this).balance);
}
function refundToInsurer(address payable _insurer) public {
// Transfer back the amount to insurer
_insurer.transfer(address(this).balance);
}
function getLatestPrice() public view returns (int256) {
(, int256 price, , uint256 timeStamp, ) = priceFeed.latestRoundData();
// If the round is not complete yet, timestamp is 0
require(timeStamp > 0, "Round not complete");
return price;
}
}
Now, while calling the refundInsurer(), we're explicitly passing the insurer as the parameter, and it's taking the value of msg.sender from InsuranceProvider, so the msg.sender is going to be the insurer's wallet (with which the InsuranceProvider is being deployed) in this case.
And now, when we're calling the refundInsurer() of InsuranceConsumer via InsuranceProvider, then it's doing:
_insurer.transfer(address(this).balance);
It means to transfer the available ETH in the InsuranceConsumer to the insurer's wallet. So, the transaction will be successful resulting in the withdrawal of the funds from the InsuranceConsumer to the insurer's wallet.
Here is my code:
// SPDX-License-Identifier: UNLICENCED
pragma solidity <0.9.0;
interface UniswapInterface{
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
}
interface IERC20{
function balanceOf(address account) external view returns (uint256);
function decimals() external view returns (uint8);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
}
contract swapContract{
address public UniSwapRouterAddress = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
address public USDCAddress = 0x2fB298BDbeF468638AD6653FF8376575ea41e768;
address public BTCAddress = 0x577D296678535e4903D59A4C929B718e1D575e0A;
IERC20 USDC = IERC20(0x2fB298BDbeF468638AD6653FF8376575ea41e768);
IERC20 BTC = IERC20(0x577D296678535e4903D59A4C929B718e1D575e0A);
UniswapInterface UniSwapRouter = UniswapInterface(UniSwapRouterAddress);
function approveUSDC() public{
USDC.approve(UniSwapRouterAddress, 999**9);
}
function approveBTC() public{
BTC.approve(UniSwapRouterAddress, 999**9);
}
function deposit(uint256 amount) public{
require(amount > 0, "0 is not accepted!");
uint256 allowance = USDC.allowance(msg.sender, address(this));
require(allowance >= amount, "Check USDC allowance");
USDC.transferFrom(msg.sender, address(this), amount);
}
function withdraw() public {
USDC.transfer(msg.sender, USDC.balanceOf(address(this)));
}
function swapUSDC() public {
address[] memory Path = new address[](2);
Path[0] = USDCAddress;
Path[1] = BTCAddress;
UniSwapRouter.swapExactTokensForTokens(
100000000,
0,
Path,
address(this),
block.timestamp + 240
);
}
}
I simply want to swap 1 USDC (its decimal is 8) to BTC on Rinkeby testnetwork.
I tried so many changes like increasing deadline, change in amountIn and amountOutMin but no gain. Also the pool on Uniswap has liquidity.
The error just says:
Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
execution reverted
I have checked the stackoverflow for answer but could not find a solution
I tried to figure out what the problem might be and realized that most likely there is no such pool.
If you go to the Uniswap UI, you will see that the pool was created only for V3, and you are trying to use the V2 address of router.
There are two ways to solve the problem, the first is to create a liquidity pool in V2 or to make an exchange through V3.
Attached is the source code for the smart contract, could you advise if there's a function in it I can use to recover the 1.5 bnb? I still have access to the creation wallet and the contract isn't renounced.
it would be nice to try and work this out if possible as I am still learning to code.
/**
*Submitted for verification at BscScan.com on 2021-08-27
*/
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
abstract contract Context {
function _msgSender() internal virtual view returns (address payable) {
return msg.sender;
}
function _msgData() internal virtual view returns (bytes memory) {
this;
return msg.data;
}
}
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount)
external
returns (bool);
function allowance(address owner, address spender)
external
view
returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
}
library SafeMath {
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}
function sub(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}
function div(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
require(b > 0, errorMessage);
uint256 c = a / b;
return c;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
function mod(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}
library Address {
function isContract(address account) internal view returns (bool) {
bytes32 codehash;
bytes32 accountHash
= 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
assembly {
codehash := extcodehash(account)
}
return (codehash != accountHash && codehash != 0x0);
}
function sendValue(address payable recipient, uint256 amount) internal {
require(
address(this).balance >= amount,
"Address: insufficient balance"
);
(bool success, ) = recipient.call{value: amount}("");
require(
success,
"Address: unable to send value, recipient may have reverted"
);
}
function functionCall(address target, bytes memory data)
internal
returns (bytes memory)
{
return functionCall(target, data, "Address: low-level call failed");
}
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return _functionCallWithValue(target, data, 0, errorMessage);
}
function functionCallWithValue(
address target,
bytes memory data,
uint256 value
) internal returns (bytes memory) {
return
functionCallWithValue(
target,
data,
value,
"Address: low-level call with value failed"
);
}
function functionCallWithValue(
address target,
bytes memory data,
uint256 value,
string memory errorMessage
) internal returns (bytes memory) {
require(
address(this).balance >= value,
"Address: insufficient balance for call"
);
return _functionCallWithValue(target, data, value, errorMessage);
}
function _functionCallWithValue(
address target,
bytes memory data,
uint256 weiValue,
string memory errorMessage
) private returns (bytes memory) {
require(isContract(target), "Address: call to non-contract");
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory returndata) = target.call{value: weiValue}(
data
);
if (success) {
return returndata;
} else {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
contract Ownable is Context {
address private _owner;
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);
constructor() internal {
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
function owner() public view returns (address) {
return _owner;
}
modifier onlyOwner() {
require(_owner == _msgSender(), "Ownable: caller is not the owner");
_;
}
function renounceOwnership() public virtual onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(
newOwner != address(0),
"Ownable: new owner is the zero address"
);
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
interface IUniswapV2Factory {
function createPair(address tokenA, address tokenB) external returns (address pair);
}
interface IUniswapV2Pair {
function sync() external;
}
interface IUniswapV2Router01 {
function factory() external pure returns (address);
function WETH() external pure returns (address);
function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB, uint liquidity);
function addLiquidityETH(
address token,
uint amountTokenDesired,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external payable returns (uint amountToken, uint amountETH, uint liquidity);
}
interface IUniswapV2Router02 is IUniswapV2Router01 {
function removeLiquidityETHSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountETH);
function swapExactTokensForETHSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
function swapExactETHForTokensSupportingFeeOnTransferTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable;
}
// change "name1" into ur name
contract BlueMoon is Context, IERC20, Ownable {
using SafeMath for uint256;
using Address for address;
//change "name1" and "symbol"
string private _name = "BlueMoon";
string private _symbol = "BMOON";
uint8 private _decimals = 8;
mapping(address => uint256) internal _reflectionBalance;
mapping(address => uint256) internal _tokenBalance;
mapping(address => mapping(address => uint256)) internal _allowances;
uint256 private constant MAX = ~uint256(0);
// change this for total supply (100e8 = 100) (100000000e8 = 100000000) (dont forget the e8 it has to be there)
uint256 internal _tokenTotal = 1_000_000_000e8;
// change this for total supply ^^^^^^^^^^^^^^^^^^^^^
uint256 internal _reflectionTotal = (MAX - (MAX % _tokenTotal));
mapping(address => bool) isTaxless;
mapping(address => bool) internal _isExcluded;
address[] internal _excluded;
uint256 public _feeDecimal = 2;
// thats the distribution to holders (400 = 4%)
uint256 public _taxFee = 100;
// thats the amount for liquidity pool
uint256 public _liquidityFee = 1000;
// this amount gets burned by every transaction
uint256 public _burnFee = 100;
// this goes to a specific wallet (line 403)
uint256 public _marketingFee = 300;
uint256 public _taxFeeTotal;
uint256 public _burnFeeTotal;
uint256 public _liquidityFeeTotal;
uint256 public _marketingFeeTotal;
address public marketingWallet;
bool public isTaxActive = true;
bool private inSwapAndLiquify;
bool public swapAndLiquifyEnabled = true;
uint256 public maxTxAmount = _tokenTotal;
uint256 public minTokensBeforeSwap = 10_000e8;
IUniswapV2Router02 public uniswapV2Router;
address public uniswapV2Pair;
event SwapAndLiquifyEnabledUpdated(bool enabled);
event SwapAndLiquify(uint256 tokensSwapped,uint256 ethReceived, uint256 tokensIntoLiqudity);
modifier lockTheSwap {
inSwapAndLiquify = true;
_;
inSwapAndLiquify = false;
}
constructor() public {
IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E); // for BSC
// IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D); // for Ethereum
// IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506); // for Sushi testnet
uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory()).createPair(address(this), _uniswapV2Router.WETH());
uniswapV2Router = _uniswapV2Router;
//paste the wallet adress, that earns the marketingFee here
marketingWallet = 0xD64E87ad309f21De5725e91EAFB82c50e486D374;
//paste the wallet ^^^^^^^^^^^^^^^^^^^^^ adress, that earns the marketingFee here
isTaxless[_msgSender()] = true;
isTaxless[address(this)] = true;
_reflectionBalance[_msgSender()] = _reflectionTotal;
emit Transfer(address(0), _msgSender(), _tokenTotal);
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns (uint8) {
return _decimals;
}
function totalSupply() public override view returns (uint256) {
return _tokenTotal;
}
function balanceOf(address account) public override view returns (uint256) {
if (_isExcluded[account]) return _tokenBalance[account];
return tokenFromReflection(_reflectionBalance[account]);
}
function transfer(address recipient, uint256 amount)
public
override
virtual
returns (bool)
{
_transfer(_msgSender(),recipient,amount);
return true;
}
function allowance(address owner, address spender)
public
override
view
returns (uint256)
{
return _allowances[owner][spender];
}
function approve(address spender, uint256 amount)
public
override
returns (bool)
{
_approve(_msgSender(), spender, amount);
return true;
}
function transferFrom(
address sender,
address recipient,
uint256 amount
) public override virtual returns (bool) {
_transfer(sender,recipient,amount);
_approve(sender,_msgSender(),_allowances[sender][_msgSender()].sub( amount,"ERC20: transfer amount exceeds allowance"));
return true;
}
function increaseAllowance(address spender, uint256 addedValue)
public
virtual
returns (bool)
{
_approve(
_msgSender(),
spender,
_allowances[_msgSender()][spender].add(addedValue)
);
return true;
}
function decreaseAllowance(address spender, uint256 subtractedValue)
public
virtual
returns (bool)
{
_approve(
_msgSender(),
spender,
_allowances[_msgSender()][spender].sub(
subtractedValue,
"ERC20: decreased allowance below zero"
)
);
return true;
}
function isExcluded(address account) public view returns (bool) {
return _isExcluded[account];
}
function reflectionFromToken(uint256 tokenAmount, bool deductTransferFee)
public
view
returns (uint256)
{
require(tokenAmount <= _tokenTotal, "Amount must be less than supply");
if (!deductTransferFee) {
return tokenAmount.mul(_getReflectionRate());
} else {
return
tokenAmount.sub(tokenAmount.mul(_taxFee).div(10** _feeDecimal + 2)).mul(
_getReflectionRate()
);
}
}
function tokenFromReflection(uint256 reflectionAmount)
public
view
returns (uint256)
{
require(
reflectionAmount <= _reflectionTotal,
"Amount must be less than total reflections"
);
uint256 currentRate = _getReflectionRate();
return reflectionAmount.div(currentRate);
}
function excludeAccount(address account) external onlyOwner() {
require(
account != address(uniswapV2Router),
"ERC20: We can not exclude Uniswap router."
);
require(!_isExcluded[account], "ERC20: Account is already excluded");
if (_reflectionBalance[account] > 0) {
_tokenBalance[account] = tokenFromReflection(
_reflectionBalance[account]
);
}
_isExcluded[account] = true;
_excluded.push(account);
}
function includeAccount(address account) external onlyOwner() {
require(_isExcluded[account], "ERC20: Account is already included");
for (uint256 i = 0; i < _excluded.length; i++) {
if (_excluded[i] == account) {
_excluded[i] = _excluded[_excluded.length - 1];
_tokenBalance[account] = 0;
_isExcluded[account] = false;
_excluded.pop();
break;
}
}
}
function _approve(
address owner,
address spender,
uint256 amount
) private {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
function _transfer(
address sender,
address recipient,
uint256 amount
) private {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
require(amount > 0, "Transfer amount must be greater than zero");
require(amount <= maxTxAmount, "Transfer Limit exceeded!");
uint256 contractTokenBalance = balanceOf(address(this));
bool overMinTokenBalance = contractTokenBalance >= minTokensBeforeSwap;
if (!inSwapAndLiquify && overMinTokenBalance && sender != uniswapV2Pair && swapAndLiquifyEnabled) {
swapAndLiquify(contractTokenBalance);
}
uint256 transferAmount = amount;
uint256 rate = _getReflectionRate();
if(isTaxActive && !isTaxless[_msgSender()] && !isTaxless[recipient] && !inSwapAndLiquify){
transferAmount = collectFee(sender,amount,rate);
}
_reflectionBalance[sender] = _reflectionBalance[sender].sub(amount.mul(rate));
_reflectionBalance[recipient] = _reflectionBalance[recipient].add(transferAmount.mul(rate));
if (_isExcluded[sender]) {
_tokenBalance[sender] = _tokenBalance[sender].sub(amount);
}
if (_isExcluded[recipient]) {
_tokenBalance[recipient] = _tokenBalance[recipient].add(transferAmount);
}
emit Transfer(sender, recipient, transferAmount);
}
function collectFee(address account, uint256 amount, uint256 rate) private returns (uint256) {
uint256 transferAmount = amount;
//#dev tax fee
if(_taxFee != 0){
uint256 taxFee = amount.mul(_taxFee).div(10**(_feeDecimal + 2));
transferAmount = transferAmount.sub(taxFee);
_reflectionTotal = _reflectionTotal.sub(taxFee.mul(rate));
_taxFeeTotal = _taxFeeTotal.add(taxFee);
}
//#dev liquidity fee
if(_liquidityFee != 0){
uint256 liquidityFee = amount.mul(_liquidityFee).div(10**(_feeDecimal + 2));
transferAmount = transferAmount.sub(liquidityFee);
_reflectionBalance[address(this)] = _reflectionBalance[address(this)].add(liquidityFee.mul(rate));
if(_isExcluded[address(this)]){
_tokenBalance[address(this)] = _tokenBalance[address(this)].add(liquidityFee);
}
_liquidityFeeTotal = _liquidityFeeTotal.add(liquidityFee);
emit Transfer(account,address(this),liquidityFee);
}
//#dev burn fee
if(_burnFee != 0){
uint256 burnFee = amount.mul(_burnFee).div(10**(_feeDecimal + 2));
transferAmount = transferAmount.sub(burnFee);
_tokenTotal = _tokenTotal.sub(burnFee);
_reflectionTotal = _reflectionTotal.sub(burnFee.mul(rate));
_burnFeeTotal = _burnFeeTotal.add(burnFee);
emit Transfer(account,address(0),burnFee);
}
//#dev Marketing fee
if(_marketingFee != 0){
uint256 marketingFee = amount.mul(_marketingFee).div(10**(_feeDecimal + 2));
transferAmount = transferAmount.sub(marketingFee);
_reflectionBalance[marketingWallet] = _reflectionBalance[marketingWallet].add(marketingFee.mul(rate));
if (_isExcluded[marketingWallet]) {
_tokenBalance[marketingWallet] = _tokenBalance[marketingWallet].add(marketingFee);
}
_marketingFeeTotal = _marketingFeeTotal.add(marketingFee);
emit Transfer(account,marketingWallet,marketingFee);
}
return transferAmount;
}
function _getReflectionRate() private view returns (uint256) {
uint256 reflectionSupply = _reflectionTotal;
uint256 tokenSupply = _tokenTotal;
for (uint256 i = 0; i < _excluded.length; i++) {
if (
_reflectionBalance[_excluded[i]] > reflectionSupply ||
_tokenBalance[_excluded[i]] > tokenSupply
) return _reflectionTotal.div(_tokenTotal);
reflectionSupply = reflectionSupply.sub(
_reflectionBalance[_excluded[i]]
);
tokenSupply = tokenSupply.sub(_tokenBalance[_excluded[i]]);
}
if (reflectionSupply < _reflectionTotal.div(_tokenTotal))
return _reflectionTotal.div(_tokenTotal);
return reflectionSupply.div(tokenSupply);
}
function swapAndLiquify(uint256 contractTokenBalance) private lockTheSwap {
if(contractTokenBalance > maxTxAmount)
contractTokenBalance = maxTxAmount;
uint256 half = contractTokenBalance.div(2);
uint256 otherHalf = contractTokenBalance.sub(half);
uint256 initialBalance = address(this).balance;
swapTokensForEth(half);
uint256 newBalance = address(this).balance.sub(initialBalance);
addLiquidity(otherHalf, newBalance);
emit SwapAndLiquify(half, newBalance, otherHalf);
}
function swapTokensForEth(uint256 tokenAmount) private {
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = uniswapV2Router.WETH();
_approve(address(this), address(uniswapV2Router), tokenAmount);
uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
tokenAmount,
0,
path,
address(this),
block.timestamp
);
}
function addLiquidity(uint256 tokenAmount, uint256 ethAmount) private {
_approve(address(this), address(uniswapV2Router), tokenAmount);
uniswapV2Router.addLiquidityETH{value: ethAmount}(
address(this),
tokenAmount,
0,
0,
address(this),
block.timestamp
);
}
function setPair(address pair) external onlyOwner {
uniswapV2Pair = pair;
}
function setMarketingWallet(address account) external onlyOwner {
marketingWallet = account;
}
function setTaxless(address account, bool value) external onlyOwner {
isTaxless[account] = value;
}
function setSwapAndLiquifyEnabled(bool enabled) external onlyOwner {
swapAndLiquifyEnabled = enabled;
SwapAndLiquifyEnabledUpdated(enabled);
}
function setTaxActive(bool value) external onlyOwner {
isTaxActive = value;
}
function setTaxFee(uint256 fee) external onlyOwner {
_taxFee = fee;
}
function setBurnFee(uint256 fee) external onlyOwner {
_burnFee = fee;
}
function setLiquidityFee(uint256 fee) external onlyOwner {
_liquidityFee = fee;
}
function setMarketingFee(uint256 fee) external onlyOwner {
_marketingFee = fee;
}
function setMaxTxAmount(uint256 amount) external onlyOwner {
maxTxAmount = amount;
}
function setMinTokensBeforeSwap(uint256 amount) external onlyOwner {
minTokensBeforeSwap = amount;
}
receive() external payable {}
}
as far as im aware im sure most contracts have some sort of clear stuck balance function in them which you can use. but I dont see one here. is it an oversight? or can it still be called using other functions.
Thanks for any advise. am willing to try what is recommended.
I'm working on a smart contract and followed this video here: https://www.youtube.com/watch?v=s677QFT6e4U&t=911s. I copied the code exactly, but when I try to call the fallback function I get the following error: Gas required exceeds block gas limit: 300000000. Even though the fallback function is as follows (it does nothing):
function () payable {
}
How could this be using too much gas?
CONTRACT CODE:
pragma solidity ^0.4.11;
import './IERC20.sol';
import './SafeMath.sol';
contract AToken is IERC20 {
using SafeMath for uint256;
uint256 public _totalSupply = 0;
uint256 public constant hardLimit = 45000000;
string public constant symbol = "ABC";
string public constant name = "Alphabet";
uint8 public constant decimals = 18;
//1 ETH = 25000 Alphabet
uint256 public constant RATE = 25000;
address public owner;
mapping(address => uint256) balances;
mapping(address => mapping(address => uint256)) allowed;
function () payable {
createTokens();
}
function SnapToken() {
owner = msg.sender;
}
function createTokens() payable {
//require(msg.value > 0);
//uint256 tokens = msg.value.mul(RATE);
//require(tokens.add(_totalSupply) <= hardLimit);
//balances[msg.sender] = balances[msg.sender].add(tokens);
//_totalSupply = _totalSupply.add(tokens);
//owner.transfer(msg.value);
}
function totalSupply() constant returns (uint256 totalSupply) {
return _totalSupply;
}
function balanceOf(address _owner) constant returns (uint256 balance) {
return balances[_owner];
}
function transfer(address _to, uint256 _value) returns (bool success) {
require(balances[msg.sender] >= _value && _value > 0);
balances[msg.sender] = balances[msg.sender].sub(_value);
balances[_to] = balances[_to].add(_value);
Transfer(msg.sender, _to, _value);
return true;
}
function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
require(allowed[_from][msg.sender] >= _value && balances[_from] >= _value && _value > 0);
balances[_from] = balances[_from].sub(_value);
balances[_to] = balances[_to].add(_value);
allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
Transfer(_from, _to, _value);
return true;
}
function approve(address _spender, uint256 _value) returns (bool success) {
//allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_value);
Approval(msg.sender, _spender, _value);
return true;
}
function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
return allowed[_owner][_spender];
}
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 value);
}
I commented out some stuff to see if this would reduce the gas requirement but unfortunately not. Have you faced this before?
Thanks
The contract, as posted, does execute when calling the fallback function (Tested in Remix). However, it will fail once you uncomment the logic in createTokens()
Fallback functions have low gas limits (2300) and, therefore, are very limited in what they can do. You can't do things like write to storage, call external functions, or send ether out as you will instantly hit the limit. It should primarily be used to enable your contract to receive ether and maybe log an event.
In the example you posted above, remove the call to createTokens() in your fallback function and just call that function directly from your client.
Documentation on Fallback Functions
Example client code:
const abiDefinition = ...;
const contractAddress = ...;
const account = ...;
const amountInEther = ...;
const contract = web3.eth.contract(abiDefinition);
const contractInstance = contract.at(contractAddress);
const transactionObj = {
from: account,
value: web3.toWei(amountInEther, 'ether'),
};
contractInstance.createTokens.sendTransaction(transactionObj, (error, result) = {
...
};
Also, as a side note, your value calculations are incorrect. msg.value is in Wei, not ether. Sending in 1 ether causes you to go well above your hardlimit. It's recommended to work with Wei in your contracts, so you should adjust your RATE.