-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from KyberNetwork/feat/eth/simulator
feat: ✨ eth: simulator
- Loading branch information
Showing
4 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package eth | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/ethereum/go-ethereum/ethclient/gethclient" | ||
"github.com/ethereum/go-ethereum/rpc" | ||
) | ||
|
||
type Simulator struct { | ||
c *rpc.Client | ||
} | ||
|
||
func NewSimulator(c *rpc.Client) *Simulator { | ||
return &Simulator{ | ||
c: c, | ||
} | ||
} | ||
|
||
func (s *Simulator) EstimateGasWithOverrides( | ||
ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int, | ||
overrides *map[common.Address]gethclient.OverrideAccount, | ||
) (uint64, error) { | ||
var hex hexutil.Uint64 | ||
err := s.c.CallContext( | ||
ctx, &hex, "eth_estimateGas", toCallArg(msg), | ||
toBlockNumArg(blockNumber), overrides, | ||
) | ||
|
||
return uint64(hex), err | ||
} | ||
|
||
func toCallArg(msg ethereum.CallMsg) interface{} { | ||
arg := map[string]interface{}{ | ||
"from": msg.From, | ||
"to": msg.To, | ||
} | ||
if len(msg.Data) > 0 { | ||
arg["input"] = hexutil.Bytes(msg.Data) | ||
} | ||
if msg.Value != nil { | ||
arg["value"] = (*hexutil.Big)(msg.Value) | ||
} | ||
if msg.Gas != 0 { | ||
arg["gas"] = hexutil.Uint64(msg.Gas) | ||
} | ||
if msg.GasPrice != nil { | ||
arg["gasPrice"] = (*hexutil.Big)(msg.GasPrice) | ||
} | ||
return arg | ||
} | ||
|
||
func toBlockNumArg(number *big.Int) string { | ||
if number == nil { | ||
return "latest" | ||
} | ||
if number.Sign() >= 0 { | ||
return hexutil.EncodeBig(number) | ||
} | ||
// It's negative. | ||
if number.IsInt64() { | ||
return rpc.BlockNumber(number.Int64()).String() | ||
} | ||
// It's negative and large, which is invalid. | ||
return fmt.Sprintf("<invalid %d>", number) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package eth_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/KyberNetwork/tradinglib/pkg/convert" | ||
"github.com/KyberNetwork/tradinglib/pkg/eth" | ||
"github.com/ethereum/go-ethereum" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/ethclient/gethclient" | ||
"github.com/ethereum/go-ethereum/rpc" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEstimateGasWithOverrides(t *testing.T) { | ||
t.Skip() | ||
|
||
var ( | ||
url = "https://ethereum.kyberengineering.io" | ||
wallet = common.HexToAddress("0x72CE0F4a9dbB974D3B1a9bF7ca857fD381260e97") | ||
ethValue, _ = convert.FloatToWei(10, 18) | ||
) | ||
|
||
c, err := rpc.Dial(url) | ||
require.NoError(t, err) | ||
|
||
s := eth.NewSimulator(c) | ||
|
||
gasUnit, err := s.EstimateGasWithOverrides(context.Background(), ethereum.CallMsg{ | ||
From: wallet, | ||
To: &wallet, | ||
Value: ethValue, | ||
}, nil, &map[common.Address]gethclient.OverrideAccount{ | ||
wallet: { | ||
Code: []byte("0x6"), | ||
Balance: ethValue, | ||
}, | ||
}, | ||
) | ||
require.NoError(t, err) | ||
t.Log(gasUnit) | ||
} |