Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Problem: etherjs smartcontract cannot change state #257

Closed
leejw51crypto opened this issue Jul 12, 2021 · 17 comments
Closed

Problem: etherjs smartcontract cannot change state #257

leejw51crypto opened this issue Jul 12, 2021 · 17 comments
Assignees

Comments

@leejw51crypto
Copy link
Contributor

leejw51crypto commented Jul 12, 2021

cannot change the state of the smart contract
src:
https://github.com/leejw51crypto/ethermint.git
branch: etherjs
folder: mytest/mytest

how to reproduce

env variable for testing

MYMNEMONICS <- your mnemonics

1. git clone https://github.com/leejw51crypto/ethermint.git
2. cd ethermint
3. git checkout etherjs
4. cd mytest/mytest
5. make

when run with ganache

BigNumber { _hex: '0x00', _isBigNumber: true }
{
  nonce: 17,
  gasPrice: BigNumber { _hex: '0x04a817c800', _isBigNumber: true },
  gasLimit: BigNumber { _hex: '0xa2ae', _isBigNumber: true },
  to: '0x2831Fb598Fb659EC881585557C3e36414BfC32e5',
  value: BigNumber { _hex: '0x00', _isBigNumber: true },
  data: '0x6057361d0000000000000000000000000000000000000000000000000000000000000015',
  chainId: 1337,
  v: 2709,
  r: '0x14ecfc5d6e69a89278ab8e2a4334898f25e2677cc38e0bd509791b5a07bdffaf',
  s: '0x6fca8a9e61a78efe115f9983b3f1b89a79e1eca0331dd84f52a3a06d9da210fc',
  from: '0x48b212A71eBbB202F7cfD1aACee3A36FdE2Fbc51',
  hash: '0xe0e6d9b1f4802245fa8a652bcdbe02c8dbf300d15491b590ff1c8c105d10617e',
  type: null,
  wait: [Function (anonymous)]
}
BigNumber { _hex: '0x15', _isBigNumber: true }

when run with ethermint

BigNumber { _hex: '0x00', _isBigNumber: true }
{
  nonce: 6,
  gasPrice: BigNumber { _hex: '0x14', _isBigNumber: true },
  gasLimit: BigNumber { _hex: '0x52d4', _isBigNumber: true },
  to: '0x5589184cE12715763c409b0e33ad6f8f075d0377',
  value: BigNumber { _hex: '0x00', _isBigNumber: true },
  data: '0x6057361d0000000000000000000000000000000000000000000000000000000000000015',
  chainId: 2,
  v: 39,
  r: '0x35db8a1ca2054e49c1d913ac3566f654a2c47072b71592926141af57afb93fed',
  s: '0x0ff0108bc624f304d329d78e6bb43c7da1ff60f436ae3626b47b7c33a24de54d',
  from: '0x48b212A71eBbB202F7cfD1aACee3A36FdE2Fbc51',
  hash: '0xac7a4efd5df15dd6b798b4f58bdc7f66e3e54eaf17f624e0525c539de57b14db',
  type: null,
  wait: [Function (anonymous)]
}
BigNumber { _hex: '0x00', _isBigNumber: true }
@leejw51crypto leejw51crypto self-assigned this Jul 12, 2021
@FrancoCRO
Copy link

I think it's due to incorrect gas estimation on Tharsis
Ganache: 0xa2ae = 41646
Tharsis: 0x52d4 = 21204

Which is just slightly larger than 21000 and is unlikely enough
Try specify gasLimit manually in the ethers.js deploy script

@leejw51crypto
Copy link
Contributor Author

thanks, i'll check

@yihuang
Copy link
Contributor

yihuang commented Jul 12, 2021

what's the result of transaction, not included in block? any error message?

@leejw51crypto
Copy link
Contributor Author

tx included to the block,
sequence number increases,
although store function didn't run successfully.
client: await tx.wait() hangs
server:

2:13PM INF minted coins from module account amount=2060481306404040550aphoton from=mint module=x/bank
{"pc":0,"op":96,"gas":"0x0","gasCost":"0x3","memory":"0x","memSize":0,"stack":[],"returnStack":null,"returnData":"0x","depth":1,"refund":0,"opName":"PUSH1","error":"out of gas"}
{"output":"","gasUsed":"0x0","time":55167,"error":"out of gas"}
{"output":"","gasUsed":"0x0","time":55167}

@leejw51crypto
Copy link
Contributor Author

working after giving gas options

   let tx = await contractWithSigner.store(ethers.BigNumber.from("0x15").add(currentValue),
        {
            gasPrice: 100,
            gasLimit: 9000000
        })

@FrancoCRO
Copy link

So I think the estimateGas part needs improvement?

@leejw51crypto
Copy link
Contributor Author

yes,i'm checking estimate gas source

@yijiasu-crypto
Copy link
Contributor

the gasLimit is not a number you can just randomly set.. so the providers (truffle/etherjs) should read it from ABI. In ABI there should be a valued defined by the compiler.

if there's no findings from ABI, then it should read from remote (via eth_estimateGas RPC) (MetaMask will do this)

then the value is set before sending the tx

@yijiasu-crypto
Copy link
Contributor

You can set the gasLimit to a very high number (100000..000). This will avoid "out of gas" error in most cases

However validator nodes may reject to enclose this tx into any block

@yijiasu-crypto
Copy link
Contributor

I think the eth_estimateGas is working good (I've check it before)
The reason of your issue is etherjs didn't get the correct estimated gas value before sending the tx

@FrancoCRO
Copy link

@yijiasu-crypto I afraid eth_estimateGas is not working good.

I DID try @leejw51crypto 's contract using both metamask+remix and ethers.js, both are getting incorrect gas estimation (metamask relies on eth_estimateGas)

@leejw51crypto
Copy link
Contributor Author

i checked with ganache, ethermint
both consumed the same gas

 cumulativeGasUsed: BigNumber { _hex: '0xa2ae', _isBigNumber: true },

i'm wondering why , with default setting, etherjs cannot succeed

@leejw51crypto
Copy link
Contributor Author

i'm inspecting this

func (e *PublicAPI) EstimateGas(args evmtypes.CallArgs) (hexutil.Uint64, error) {
	e.logger.Debugln("eth_estimateGas")

	// From ContextWithHeight: if the provided height is 0,
	// it will return an empty context and the gRPC query will use
	// the latest block height for querying.
	data, err := e.doCall(args, 0)
	if err != nil {
		return 0, err
	}

	if data.Reverted {
		return 0, evmtypes.NewExecErrorWithReason(data.Ret)
	}

	return hexutil.Uint64(data.GasUsed), nil
}

@leejw51crypto
Copy link
Contributor Author

leejw51crypto commented Jul 12, 2021

interesting
estimateGas seems correct, but if that amount is delievered via like this
let tx = await contractWithSigner.store(ethers.BigNumber.from("0x15").add(currentValue),
{

        gasLimit: 41646
    })

it works
by default, not working

if Gas is given by rpc argument, always works.(if amount is correct),
Gas is not given , by default, always not working

@leejw51crypto
Copy link
Contributor Author

I think it's due to incorrect gas estimation on Tharsis
Ganache: 0xa2ae = 41646
Tharsis: 0x52d4 = 21204

Which is just slightly larger than 21000 and is unlikely enough
Try specify gasLimit manually in the ethers.js deploy script

wow, the value is very correct!

EstimateGas  0x52d4
AsTransaction GasLimit 21204

put log to check value :-)

	f, _ := os.OpenFile("./text.log",
		os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	defer f.Close()
	f.WriteString(fmt.Sprintf("EstimateGas  %v\n", hexutil.Uint64(data.GasUsed)))

leejw51crypto added a commit to leejw51crypto/ethermint that referenced this issue Jul 12, 2021
Solution: increase base gas to make similar with ethereum
@leejw51crypto
Copy link
Contributor Author

submitted pr

@leejw51crypto
Copy link
Contributor Author

fixed by
#272

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants