I am writing simple solidity code that's will be running inside evm using golang (go-ethereum). I have successfully run the code, but I can't find the right return value of the function.
Test.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
contract Test {
function add(uint256 x, uint256 y) public pure returns (uint256 z) {
z = x + y;
}
}
Test.abi
[{"inputs":[{"internalType":"uint256","name":"x","type":"uint256"},{"internalType":"uint256","name":"y","type":"uint256"}],"name":"add","outputs":[{"internalType":"uint256","name":"z","type":"uint256"}],"stateMutability":"pure","type":"function"}]
Test.bin
608060405234801561001057600080fd5b506101d6806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063771602f714610030575b600080fd5b61004a600480360381019061004591906100b1565b610060565b6040516100579190610100565b60405180910390f35b6000818361006e919061014a565b905092915050565b600080fd5b6000819050919050565b61008e8161007b565b811461009957600080fd5b50565b6000813590506100ab81610085565b92915050565b600080604083850312156100c8576100c7610076565b5b60006100d68582860161009c565b92505060206100e78582860161009c565b9150509250929050565b6100fa8161007b565b82525050565b600060208201905061011560008301846100f1565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006101558261007b565b91506101608361007b565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff038211156101955761019461011b565b5b82820190509291505056fea264697066735822122014e7f0e58470438055309f2b412f3d927c7c5dc3d862cf3c37497aa2b907b08464736f6c634300080f0033
And here is the code to run the compiled solidity.
main.go
package main
import (
"fmt"
"io/ioutil"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/vm/runtime"
)
func e(err error) {
if err != nil {
panic(err)
}
}
func main() {
srcAbi, err := ioutil.ReadFile("./Test.abi")
e(err)
srcBin, err := ioutil.ReadFile("./Test.bin")
e(err)
abis, err := abi.JSON(strings.NewReader(string(srcAbi)))
e(err)
code := common.Hex2Bytes(string(srcBin))
inputs, err := abis.Pack("add", big.NewInt(1), big.NewInt(4))
e(err)
ret, _, err := runtime.Execute(code, inputs, nil)
e(err)
fmt.Println(hexutil.Encode(ret))
}
Is there I missed something?
the output I expected is hexcode of 5, but the output I got was this: 0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063771602f714610030575b600080fd5b61004a600480360381019061004591906100b1565b610060565b6040516100579190610100565b60405180910390f35b6000818361006e919061014a565b905092915050565b600080fd5b6000819050919050565b61008e8161007b565b811461009957600080fd5b50565b6000813590506100ab81610085565b92915050565b600080604083850312156100c8576100c7610076565b5b60006100d68582860161009c565b92505060206100e78582860161009c565b9150509250929050565b6100fa8161007b565b82525050565b600060208201905061011560008301846100f1565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006101558261007b565b91506101608361007b565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff038211156101955761019461011b565b5b82820190509291505056fea264697066735822122014e7f0e58470438055309f2b412f3d927c7c5dc3d862cf3c37497aa2b907b08464736f6c634300080f0033