Skip to content

Commit

Permalink
supported transaction-simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
KAI committed May 13, 2023
1 parent e35496e commit ffa45ff
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 4 deletions.
8 changes: 6 additions & 2 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type ISimulate interface {
}

type Simulate struct {
Host string
Account string
Project string
Token string
Expand All @@ -26,10 +27,11 @@ type Platform string
const (
SimulateTenderly Platform = "tenderly"
SimulateBlockNative Platform = "blocknative"
SimulateCoinSummer Platform = "coinsummer"
)

func NewSimulate(account, project, token string, tps int) *Simulate {
return &Simulate{Account: account, Project: project, Token: token, Tps: tps}
func NewSimulate(host, account, project, token string, tps int) *Simulate {
return &Simulate{Host: host, Account: account, Project: project, Token: token, Tps: tps}
}

func (s *Simulate) SimulateGetter(platform Platform) ISimulate {
Expand All @@ -38,6 +40,8 @@ func (s *Simulate) SimulateGetter(platform Platform) ISimulate {
return providers.NewTenderly(s.Account, s.Project, s.Token, s.Tps)
case SimulateBlockNative:
return nil
case SimulateCoinSummer:
return providers.NewPrivateTxSimulator(s.Host, s.Tps)
default:
return providers.NewTenderly(s.Account, s.Project, s.Token, s.Tps)
}
Expand Down
106 changes: 106 additions & 0 deletions providers/rustSimulator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package providers

import (
"bytes"
"context"
"encoding/json"
"fmt"
"golang.org/x/time/rate"
"io/ioutil"
"log"
"net/http"
"time"
)

type PrivateTxSimulator struct {
Host string
Limiter *rate.Limiter
}

func (p *PrivateTxSimulator) AddProject(name string) (string, error) {
//TODO implement me
panic("implement me")
}

func (p *PrivateTxSimulator) RenameProject(name string) (string, error) {
//TODO implement me
panic("implement me")
}

func (p *PrivateTxSimulator) AddForkEnv(chainId, name string) (string, error) {
//TODO implement me
panic("implement me")
}

func (p *PrivateTxSimulator) RenameForkEnv(forkId, chainId, name string) (string, error) {
//TODO implement me
panic("implement me")
}

func (p *PrivateTxSimulator) DeleteForkEnv(forkId string) (string, error) {
//TODO implement me
panic("implement me")
}

func (p *PrivateTxSimulator) SimulateTxForFork(forkId, params string) (string, error) {
//TODO implement me
panic("implement me")
}

func (p *PrivateTxSimulator) Simulations(params string) (string, error) {
_ = p.Limiter.Wait(context.Background())
header := p.getReqHeader()

mapParam := make(map[string]interface{})
err := json.Unmarshal([]byte(params), &mapParam)
if err != nil {
return "", err
}

url := p.Host + "/simulate"
return NewNet(url, header, mapParam).Request(PostTy)
}

func (p *PrivateTxSimulator) BundledSimulations(params string) (string, error) {
_ = p.Limiter.Wait(context.Background())
url := p.Host + "/simulate-bundle"
return reqPost(url, params)
}

func reqPost(url, data string) (string, error) {
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(data)))
if err != nil {
log.Fatal("NewRequest: ", err)
}
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("req error:%s", err)
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("parse resp data error: %s", err)
}
return string(body), nil
}

func (p *PrivateTxSimulator) ReqJsonRpc(rpc string, params *RpcParams) (string, error) {
//TODO implement me
panic("implement me")
}

func (p *PrivateTxSimulator) getReqHeader() (header map[string]string) {
header = make(map[string]string)
header["Content-Type"] = "application/json"
return
}

func NewPrivateTxSimulator(host string, tps int) *PrivateTxSimulator {
rateLimiter := rate.NewLimiter(rate.Every(time.Second*1), tps)
host += "/api/v1"
return &PrivateTxSimulator{Host: host, Limiter: rateLimiter}
}
21 changes: 21 additions & 0 deletions providers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ type TenderlySimulations struct {
Simulations []TenderlySimulation `json:"simulations"`
}

type CoinSummerSimulation struct {
ChainId int `json:"chainId"`
From string `json:"from"`
To string `json:"to"`
Data string `json:"data"`
GasLimit uint64 `json:"gasLimit"`
Value string `json:"value"`
BlockNumber uint64 `json:"blockNumber"`
}

type CoinSummerSimulationRes struct {
SimulationId int `json:"simulationId"`
GasUsed uint64 `json:"gasUsed"`
BlockNumber uint64 `json:"blockNumber"`
Success bool `json:"success"`
Trace interface{} `json:"trace"`
FormattedTrace interface{} `json:"formattedTrace"`
Logs interface{} `json:"logs"`
ExitReason string `json:"exitReason"`
}

type TenderlySimulation struct {
NetworkId string `json:"network_id"`
Save bool `json:"save"`
Expand Down
51 changes: 51 additions & 0 deletions rustSimulation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package simulate_tx_api

import "testing"

const host = "http://127.0.0.1:10000"

func TestNewSimulate_simulate(t *testing.T) {
tests := []struct {
name string
param string
}{
{
name: "simulation",
param: "{\n \"chainId\": 1,\n \"from\": \"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045\",\n \"to\": \"0x66fc62c1748e45435b06cf8dd105b73e9855f93e\",\n \"data\": \"0xffa2ca3b44eea7c8e659973cbdf476546e9e6adfd1c580700537e52ba7124933a97904ea000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000001d0e30db00300ffffffffffffc02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000186a0\",\n \"gasLimit\": 500000,\n \"value\": \"100000\",\n \"blockNumber\": 16784600\n}",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
simulations, err := NewSimulate(host, "", "", "", 5).SimulateGetter(SimulateCoinSummer).Simulations(tt.param)
if err != nil {
t.Errorf("simulation error: %s", err)
return
}
t.Log(simulations)
})
}
}

func TestNewSimulate_simulateBundle(t *testing.T) {
tests := []struct {
name string
param string
}{
{
name: "simulate-bundle",
param: "[\n {\n \"chainId\": 1,\n \"from\": \"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045\",\n \"to\": \"0x66fc62c1748e45435b06cf8dd105b73e9855f93e\",\n \"data\": \"0xffa2ca3b44eea7c8e659973cbdf476546e9e6adfd1c580700537e52ba7124933a97904ea000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000001d0e30db00300ffffffffffffc02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000186a0\",\n \"gasLimit\": 500000,\n \"value\": \"100000\",\n \"blockNumber\": 16784600\n }\n]",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
simulations, err := NewSimulate(host, "", "", "", 5).SimulateGetter(SimulateCoinSummer).BundledSimulations(tt.param)
if err != nil {
t.Errorf("simulation error: %s", err)
return
}
t.Log(simulations)
})
}
}
4 changes: 2 additions & 2 deletions simulate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestBundledSimulations(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
simulator := NewSimulate(tt.account, tt.project, tt.token, tt.tps).SimulateGetter(SimulateTenderly)
simulator := NewSimulate("", tt.account, tt.project, tt.token, tt.tps).SimulateGetter(SimulateTenderly)
simulations, err := simulator.BundledSimulations(tt.params)
if err != nil {
t.Errorf("req bundled simulations error: %s", err)
Expand Down Expand Up @@ -185,7 +185,7 @@ func TestTenderly(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
simulator := NewSimulate(tt.account, tt.project, tt.token, tt.tps).SimulateGetter(SimulateTenderly)
simulator := NewSimulate("", tt.account, tt.project, tt.token, tt.tps).SimulateGetter(SimulateTenderly)
switch tt.action {
case actionAddProject:
tt.testAddProject(simulator)
Expand Down

0 comments on commit ffa45ff

Please sign in to comment.