-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
KAI
committed
May 13, 2023
1 parent
e35496e
commit ffa45ff
Showing
5 changed files
with
186 additions
and
4 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
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} | ||
} |
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,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) | ||
}) | ||
} | ||
} |
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