hi there, I just published a contract and interacting with it costs very very much gas. I just noticed I accidentally set the public function to be payable functions, does this have any implications on the gas. the contract doesn't do much, just simple swap on UNISWAP

Jan 14, 2021, 8:03 PM
Because the gas price is damn high rn,
And public functions can be payable if it accepting ether, else I don't see the points here
Jan 14, 2021, 8:15 PM
yeah it was an accident but the gas price is dramatically higher than usual
Jan 14, 2021, 8:15 PM
Marking a function payable won't affect the gas cost considerably
Jan 14, 2021, 8:15 PM
so what does? it cost me 0.6 to make an internal swap on the contract
the contract has 0.1 ETH and i'm trying to swap it to DAI via UniswapV2Router so it doesn't do a lot
Jan 14, 2021, 8:17 PM
Probably loops. And how much gas price did you use?
Jan 14, 2021, 8:18 PM
I didn't confirm the transaction
gas price 40 and gas limit is 11875000
I have no loops in that function
Jan 14, 2021, 8:20 PM
what. you just beat the record of 9,5 mill, congrats

damn what are you doing in that?
Jan 14, 2021, 8:20 PM
Then there is something in your contract eating up all those gas
Jan 14, 2021, 8:21 PM
I'm calling Uniswap getAmountsOut and then calling swapExactETHForTokens
initiated the router in the constructor
Jan 14, 2021, 8:22 PM
Payable only implies, and is required, if you want to work with eth in a function. I.e. if you have a function forwardEth() then it has to be payable, however, if it's forwardToken() and not using eth, then not. It is not directly negative or bad when using it.
Jan 14, 2021, 8:22 PM
ok good to know I thought that might be the reason for the high gas price :/
Jan 14, 2021, 8:23 PM
So you didn't do a transaction yet?
Jan 14, 2021, 8:23 PM
no the MetaMask estimation is crazy high
Jan 14, 2021, 8:23 PM
Can you try to deploy it via MetaMask on Ropsten and see how the gas limit is estimated there (gas limit, NOT price)?
Jan 14, 2021, 8:25 PM
sure I did that before when I checked everything was working
on Kovan tho is that good as well?
Jan 14, 2021, 8:25 PM
I don't know, I'd suggest Ropsten here since I worked with that all the time. But if you did on Ropsten you may already see the limit that was used
Do you mind sharing the contract?
Jan 14, 2021, 8:26 PM
11875000
if possible id rather not
Jan 14, 2021, 8:28 PM
Sure, that's no problem. My guess would be that there has to be a loop somehow. That's what's causing the most gas from what I know. Does your contract only send or also receive eth?
Jan 14, 2021, 8:29 PM
let me share the function
https://pastebin.com/ktGd1fm4
as I said just using UNISWAP to make a swap
could something outside this function have implications on the gas cost?
no loops
Jan 14, 2021, 8:33 PM
Which compiler version do you use and do you have a fallback/receive function?
But only if you in some way receive eth back. Can you maybe check the TX in ropsten if there are any internal transaction? Maybe you already see a big list there
Jan 14, 2021, 8:34 PM
0.6.6
Jan 14, 2021, 8:35 PM
Deploy it on testnet from remix, send the transaction with huge fee anyway and debug the transaction using remix. It will show gas consumption at each point
Jan 14, 2021, 8:38 PM
@cryptokk https://kovan.etherscan.io/tx/0x9cf1ed2e187793bc6c10fd7702c8a756d97f065eb737e4a51f7656c527b68a3d that a test I did on Kovan calling this function twice from another function inside the contract
transaction fee was 0.000246894
Jan 14, 2021, 8:40 PM
This only took 246k gas...
Jan 14, 2021, 8:40 PM
It doesn't use much gas there, so you do the same on mainnet but it uses more gas?
Jan 14, 2021, 8:40 PM
I think so let me make sure I didn't do anything wrong :S
you make me doubt myself
Jan 14, 2021, 8:41 PM
Haha no problem
Jan 14, 2021, 8:43 PM
so when I try this on mainnet I get gaslimit 3000000 and when I do the same on kovan I get gaslimit 3000000 too
ok I see the issue but i have no idea why it's happening. When I'm calling the same function from web3.js I get gaslimit of 11875000
Jan 14, 2021, 8:49 PM
Aren't you using MetaMasK? Can you try to send the tx on covan and see what the end result is?
Jan 14, 2021, 8:49 PM
I am using MetaMask and yes I can defiantly try. I'm trying from both Remix and web3.js with MetaMask
ok so I did this on Kovan https://kovan.etherscan.io/tx/0x151d2092be9575af3ebff9973d8b46fcab324dbb517ccbf6b7fd1eed3ee397cd
ok it's working now. it appears like if the transaction fails it costs a lot of gas for some reason
thanks for the help @cryptokk and @mrrobot3_3
Jan 14, 2021, 9:53 PM
Sorry, I'm back now. Do you mean it failed (for example got reverted) but the cost was still high?
Jan 14, 2021, 10:07 PM
I mean I didn’t send the transaction but the estimate is really high when the function fails
Jan 15, 2021, 5:26 AM

© 2024 Draquery.com All rights reserved.