Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I got an error when using eth_estimateGas with a value that started with zero like 0x012a05f200 #251

Closed
peerasak-u opened this issue Jul 6, 2022 · 0 comments · Fixed by #252

Comments

@peerasak-u
Copy link
Contributor

I got this error:

{
  "jsonrpc": "2.0",
  "id": 1,
  "error": {
    "code": -32602,
    "message": "invalid argument 0: json: cannot unmarshal hex number with leading zero digits into Go struct field CallArgs.value of type *hexutil.Big"
  }
}

when I send this transaction:

{
  "jsonrpc": "2.0",
  "method": "eth_estimateGas",
  "id": 1,
  "params": [
    {
      "value": "0x012a05f200",
      "to": "0xddcbb8718a8fcb8c73d3194cec55e9767c47b3a5",
      "data": "0x156e29f60000000000000000000000006589e75005a4c2718f8d5b94a7a21f396a8e4cd50000000000000000000000000000000000000000000000007ce66c50e284000000000000000000000000000000000000000000000000000932c874ff30480000",
      "from": "0xe1e06fd942f9b25dd9772658d87f93d463290e69"
    }
  ]
}

and this is what function I called in smart contract on BSC

function mint(address asset, uint256 assetAmount, uint256 pricePerUnit) external payable whenNotPaused nonReentrant returns (uint256) {
		require(asset != address(0), "!asset");
		require(assetAmount > 0, "!amount");
		require(pricePerUnit > 0, "!price");
		require(msg.value == fulfillFee(), "!fee");
		require(manager.assets(asset), "!onlyAsset");

		uint256 inputAmount = (assetAmount * pricePerUnit) / 1 ether;
		uint256 feeAmount = calculateFee(msg.sender, inputAmount, asset);

		IERC20(inputToken).safeTransferFrom(msg.sender, address(this), inputAmount + feeAmount);
		payable(relayer).transfer(msg.value);

		lastId++;
		orders[lastId] = Order(
			msg.sender,
			asset,
			OrderStatus.PENDING,
			inputAmount,
			feeAmount,
			assetAmount,
			pricePerUnit,
			block.timestamp,
			0
		);

		emit Mint(lastId, msg.sender, inputToken, inputAmount, asset, assetAmount, pricePerUnit, feeAmount, msg.value);

		return lastId;
}

I think the problem is in HexExtensions.swift:

public extension Web3Extensions where Base == BigUInt {
    var hexString: String {
        return String(bytes: base.web3.bytes)
    }
}

and

public extension String {
    init(bytes: [UInt8]) {
        self.init("0x" + bytes.map { String(format: "%02hhx", $0) }.joined())
    }
}

if value is BigUInt(5000000000) result from map function will be:

["01", "2a", "05", "f2", "00"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant