Hello guys!
So I am having this error when I try to run one of my contracts with metamask and on ganache
"We are not able to estimate gas. There might be an error in the contract and this transaction may fail."
Is there anyone who once came across this bug? I. Stuck
Dec 30, 2022, 11:51 AM
I tried stack overflow and exchange but no solution helped me yet
Dec 30, 2022, 11:51 AM
Your contract might have a bug
sometimes, you get that error when the function you are calling has an error or bug which was caught
Dec 30, 2022, 11:58 AM
Above are the functions I m calling
So sometimes the contract fails to identify the transporter's account
Dec 30, 2022, 12:06 PM
What does RawMaterial do
its an external contract
it could be the cause
Dec 30, 2022, 12:09 PM
Wrong routers ⚠️
Your creators wallet or dev wallet must be the same or match .. example deploying on bsc must be routers on bsc ...
Dec 30, 2022, 12:58 PM
😐 did you read his text that follow?
Dec 30, 2022, 12:59 PM
You must know the router addresses
🧑💻
Dec 30, 2022, 1:00 PM
to calculate say 0.1% you do
0.1 * 1000
to give you the basis point equivalent of 0.1
then lets say you want to get the 0.1% of 2000
in basis point
0.1 = 100
1 = 1000
10 = 10_000
0.1 = 100
1 = 1000
10 = 10_000
so to calculate 0.1% of 2000 =
(100 * 2000) / 10_000
(100 * 2000) / 10_000
yes
same as I posted
so multiple your % by 1_000 to get the desired basis point
0.0075 = 7.5 in basis point
the % eg. 0.0075 must be in basis point in your contract
for lower values use higher multiplier
0.0075 you can use 10_000
to get a non fractional basis point
so 0.0075 * 10_000 = 75
then (value * percentage) / 100_000;
you noticed the divisor here is / 100_000;
not 10_000
because we increased the multiplier
yes thats how it works
(value * percentage) / 100_000; exactly this?
the decimals doesnt matter here as it will be calculated in the decimals of the value provided
(75 * 75000000000000) / 100_000;
correct me if am mistaking
wait I think the order or the operators matters
(((1000 * 1000000000000000000000) / 1000) * 75 ) / 100_000;
(((price * buyAmount) / 1000) * (feeRate / 100000))
Dec 30, 2022, 1:29 PM
here would round zero because would be 75/100000
Dec 30, 2022, 1:32 PM
seen that
should be
(( (price * buyAmount) / 1000) * feeRate) / 100000)
Dec 30, 2022, 1:34 PM
returned 7500000000000000000
Dec 30, 2022, 1:35 PM
you can calcuate with the slippage
to get max amountIn and max amount out
Tell me what do you intend to achieve
Dec 30, 2022, 1:36 PM
take a part of as fee from the amount provided as parameter on the function
the fee is dynamic
Dec 30, 2022, 1:38 PM
so lets say you need 0.0075% taken from the fee right?
Dec 30, 2022, 1:39 PM
yea
Dec 30, 2022, 1:40 PM
I see
so first we compute the fee amount
Fee is 2500
if we use 10_000 divisor
lets get a function to handle this
so we get a function
function computePercent(uint256 _percentageBPS, uint256 _amount) public pure returns(uint) {
return (_amount.mul(_percentageBPS)).div(100_000);
}
return (_amount.mul(_percentageBPS)).div(100_000);
}
this will compute the percentage
let say a buy amount is 4000.......
uint feeAmt = computePercent(feePercent, buyAmt);
then now we take our 0.0075
uint feeShare = computePercent(feeSharePercent, feeAmt);
then now we take our 0.0075
uint feeShare = computePercent(feeSharePercent, feeAmt);
this looks cleaner
so you will need to call the compute percent twice
uint feeAmt = computePercent(feePercent, buyAmt);
uint feeShare = computePercent(feeSharePercent, feeAmt);
uint feeAmt = computePercent(feePercent, buyAmt);
uint feeShare = computePercent(feeSharePercent, feeAmt);
where our
uint feePecent = 2500; // 0.25
uint feeSharePercent = 75; // 0.0075
uint feePecent = 2500; // 0.25
uint feeSharePercent = 75; // 0.0075
the buyAmt = buyAmount
Dec 30, 2022, 1:46 PM
oh btw, im gettin gfrom the value not from buy amount, so for example
if you buying 10 btc of a price of 10k, we are talking of 100k amount
if you buying 10 btc of a price of 10k, we are talking of 100k amount
Dec 30, 2022, 1:47 PM
Yes so you compute the buy amount
Dec 30, 2022, 1:48 PM
buy amount on frontend 1 becomes 1000 on contract for example to compute fractions
Dec 30, 2022, 1:48 PM
keeping the code modular helps avoid logic errors
nono
Dec 30, 2022, 1:49 PM
so (price * buyAmount) / 1000
Dec 30, 2022, 1:49 PM
the amount is always in decimals
18th decimals
yes
Dec 30, 2022, 1:49 PM
oh sorry, not amount is the price
which is uint64
Dec 30, 2022, 1:50 PM
I suggest new project use 18 decimals
Dec 30, 2022, 1:51 PM
USDC and other stablecoin
Dec 30, 2022, 1:51 PM
Yes
Dec 30, 2022, 1:51 PM
had to put additional checkings
but all math doing at standard
Dec 30, 2022, 1:52 PM
the slippage is used to get the min amount
example 1% slippage means you could get 1% less of the max amount
how do you get the price on chain?
Dec 30, 2022, 1:53 PM
so for example if price is 0.50 and buying 10 math is > 500 * 10000000000000000000 / 1000 to get 5 * 10**18 > cost 5 usdc
Dec 30, 2022, 1:56 PM
Yes
slippage is added due to flatuation in price
Yes I do
a second
trying to get an example for you
function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts);
function getAmountsIn(uint amountOut, address[] memory path) public view returns (uint[] memory amounts);
]
]
or https://docs.uniswap.org/contracts/v2/reference/smart-contracts/library#getamountout
if you have reserves
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut);
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) internal pure returns (uint amountIn);
Dec 30, 2022, 2:08 PM
i think is this > The result of (((1000 * 10000000000000000000) / 1000) * 75) / 100000 is 7500000000000000.
correct?
correct?
7.5e+14
Dec 30, 2022, 2:28 PM
We have no idea on how you get the price and buy amount
Dec 30, 2022, 2:29 PM
actually it would need to be 750000000000000
Dec 30, 2022, 2:29 PM
Your calculation must be precise
Dec 30, 2022, 2:30 PM
buyAmount is 18 decimals and price floats multiplied by 1000 minimum 0.001
Dec 30, 2022, 2:31 PM
uint256 public fee = 0.0075 ether;
function calcFee(uint256 amount) public view returns (uint256) {
uint256 feeAmount = amount * fee;
return feeAmount;
}
function calcFee(uint256 amount) public view returns (uint256) {
uint256 feeAmount = amount * fee;
return feeAmount;
}
amount in ether
if you want to pass in wei
uint256 public fee = 0.0075 ether;
function calcFee(uint256 amount) public view returns (uint256) {
uint256 feeAmount = (amount * fee) / 1 ether;
return feeAmount;
}
uint256 public fee = 0.0075 ether;
function calcFee(uint256 amount) public view returns (uint256) {
uint256 feeAmount = (amount * fee) / 1 ether;
return feeAmount;
}
if you plan on a set function for your fee, if dividing with 1 ether, use this to avoid dividing by 0
if (fee == 0) {
return 0;
}
if (fee == 0) {
return 0;
}
Dec 30, 2022, 4:18 PM
thank you bro
Hi
uint256 public fee = 0.05 ether;
uint256 amountinValue = (2000 * 2000000000000000000 ) / 1000; (4 ether)
uint256 feeAmount = (amountinValue * fee) / 1 ether;
why this returning 2 ether (2000000000000000000) in calcs?
uint256 public fee = 0.05 ether;
uint256 amountinValue = (2000 * 2000000000000000000 ) / 1000; (4 ether)
uint256 feeAmount = (amountinValue * fee) / 1 ether;
why this returning 2 ether (2000000000000000000) in calcs?
Dec 30, 2022, 5:14 PM
what are you passing in exactly ?
Dec 30, 2022, 5:15 PM
value 4, so basically it should return 0.05% of 4
Dec 30, 2022, 5:16 PM
uint256 public fee = 0.0005 ether;, or divide by 100
Dec 30, 2022, 5:18 PM
2000 > price
2000000000000000000 > amount
1000 > calc
to buy 2 base you need 4 of quote asset at 2 price each, returning 4 ok
using 4 on the % calculation but returns 2 instead of 0.20
2000000000000000000 > amount
1000 > calc
to buy 2 base you need 4 of quote asset at 2 price each, returning 4 ok
using 4 on the % calculation but returns 2 instead of 0.20
Dec 30, 2022, 5:18 PM
like if you say 0.25 * 100 to get 25%
add two decimal places
or divide by 100.. i would add decimal place to save a math operation
This is 5% pass in 4 ether and you get back 0.2
uint256 public fee = 0.05 ether;
function calcFee(uint256 amount) public view returns (uint256) {
uint256 feeAmount = (amount * fee) / 1 ether;
return feeAmount;
}
uint256 public fee = 0.05 ether;
function calcFee(uint256 amount) public view returns (uint256) {
uint256 feeAmount = (amount * fee) / 1 ether;
return feeAmount;
}
Dec 30, 2022, 5:25 PM
depending on the amount need to change decimals?
Dec 30, 2022, 5:29 PM
no, decimals are always the same, 0.25 is 25%... just move the dot two places
0.025 would be 2.5%
Dec 30, 2022, 5:30 PM
so 0.01% is 0.0001 ether?
Dec 30, 2022, 5:31 PM
yes
Dec 30, 2022, 5:31 PM
ah cool
Dec 30, 2022, 5:32 PM