Commit dd650af2 authored by vicotor's avatar vicotor

add rpc proxy in config

parent f68cffcd
.PHONY: default swapper docker
GOBIN = $(shell pwd)/build/bin
GOBIN = $(shell pwd)/build
GO ?= latest
GOFILES_NOVENDOR := $(shell go list -f "{{.Dir}}" ./...)
TAG=latest
......@@ -13,11 +13,12 @@ swag:
swag init -g openapi/server.go
swapper:
go build -o=${GOBIN}/$@ -gcflags "all=-N -l" .
@rm -rf ${GOBIN}
go build -o $@ -gcflags "all=-N -l" .
@echo "Done building."
docker:
docker build -t swapper:${TAG} .
clean:
rm -fr build/*
rm -fr build
......@@ -39,7 +39,7 @@ param:
# swap volume for each transaction, unit is 0.1 usdt. example: 1 is 0.1 usdt.
volume: 1
# count of transactions per user.
count: 2
count: 1
# gas_price unit is Gwei.
gas_price: 1
# slippage, unit is 1/1000. example: 20 means 2.0%.
......@@ -50,12 +50,14 @@ param:
pair:
# pair address.
pair: "0xcB0AE3B8337f0Cc35563e6b9fC357F2298C0D24a"
# klko address
# klko token address
token: "0x215324115417a13879f865Dc5BB651B18A781a59"
rpc:
env: "bsc"
pool: 20
url: "https://bscnode.bitheart.org"
proxy: "http://127.0.0.1:7890"
log:
level: "debug"
......@@ -67,7 +69,7 @@ users: "accounts.csv"
After check the configure, you can run the program.
```shell
./build/bin/swapper
./swapper
```
#### 6. check result
......
package clientpool
import (
"context"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
log "github.com/sirupsen/logrus"
"net/http"
"net/url"
"sync"
"time"
)
// use atomic pool
......@@ -16,14 +22,41 @@ type poolConfig struct {
var pconfig = &poolConfig{}
func InitPool(cnt int, rpc string) {
func dialUrl(rpcurl string, proxy string) (*ethclient.Client, error) {
if proxy == "" {
return ethclient.Dial(rpcurl)
} else {
// set http proxy
purl, err := url.Parse(proxy)
if err != nil {
return ethclient.Dial(rpcurl)
}
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.Proxy = http.ProxyURL(purl)
hcli := &http.Client{
Transport: transport,
Timeout: time.Second * 20,
}
rpcCli, err := rpc.DialOptions(context.Background(), rpcurl, rpc.WithHTTPClient(hcli))
if err != nil {
return ethclient.Dial(rpcurl)
}
log.WithField("proxy", proxy).Info("create eth client use proxy")
return ethclient.NewClient(rpcCli), nil
}
}
func InitPool(cnt int, rpc string, proxy string) {
pconfig.Index = 0
pconfig.RPCNode = rpc
pconfig.clients = make([]*ethclient.Client, 0)
for i := 0; i < cnt; i++ {
c, _ := ethclient.Dial(pconfig.RPCNode)
if c, err := dialUrl(pconfig.RPCNode, proxy); err != nil {
log.WithError(err).Fatal("failed to connect to rpc node")
} else {
pconfig.clients = append(pconfig.clients, c)
}
}
}
func (p *poolConfig) getClient() *ethclient.Client {
......
......@@ -2,7 +2,7 @@ package root
import (
"code.wuban.net.cn/service/pancakeswapper/config"
"code.wuban.net.cn/service/pancakeswapper/swapper"
"code.wuban.net.cn/service/pancakeswapper/core"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
......@@ -15,7 +15,7 @@ import (
var (
cfgFile string
rootCmd = &cobra.Command{
Use: "swapper [command]",
Use: "core [command]",
Short: "Swapper is a script for pancake.",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
bindFlags(cmd)
......@@ -64,13 +64,13 @@ func runCommand(cmd *cobra.Command, _ []string) {
setlog(cfg.Log.Level)
log.WithField("config", cfg).Info("load config success")
server, err := swapper.NewSwapper(cfg)
server, err := core.NewSwapper(cfg)
if err != nil {
log.Fatal(err)
}
server.Run()
log.Info("app exit, bye bye !!!")
fmt.Println("app exit, bye bye !!!")
return
}
......
......@@ -4,7 +4,7 @@ param:
# swap volume for each transaction, unit is 0.1 usdt. example: 1 is 0.1 usdt.
volume: 1
# count of transactions per user.
count: 2
count: 1
# gas_price unit is Gwei.
gas_price: 1
# slippage, unit is 1/1000. example: 20 means 2.0%.
......@@ -21,6 +21,8 @@ pair:
rpc:
env: "bsc"
pool: 20
url: "https://bscnode.bitheart.org"
proxy: "http://127.0.0.1:7890"
log:
level: "debug"
......
......@@ -34,6 +34,8 @@ type PairConfig struct {
type RpcConfig struct {
Env string `yaml:"env"`
PoolSize int `yaml:"pool"`
Url string `yaml:"url"`
Proxy string `yaml:"proxy"`
}
type LogConfig struct {
......
......@@ -11,5 +11,5 @@ var DefaultConfig = Config{
Slippage: 20,
Deadline: 20,
},
Users: "accounts.json",
Users: "accounts.csv",
}
package swapper
package core
import (
"code.wuban.net.cn/service/pancakeswapper/clientpool"
......@@ -22,7 +22,7 @@ func NewSwapper(conf *config.Config) (*Swapper, error) {
func (s *Swapper) Run() {
env := types.GetEnv(s.cfg.Rpc.Env)
clientpool.InitPool(s.cfg.Rpc.PoolSize, env.RPC)
clientpool.InitPool(s.cfg.Rpc.PoolSize, s.cfg.Rpc.Url, s.cfg.Rpc.Proxy)
work.WorkInit(env.ChainId)
// 1. read account info from json file
......
......@@ -5,7 +5,6 @@ import (
"code.wuban.net.cn/service/pancakeswapper/contracts/buildparam"
"code.wuban.net.cn/service/pancakeswapper/contracts/v3pool"
"code.wuban.net.cn/service/pancakeswapper/types"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
......@@ -126,11 +125,11 @@ func GetAmountOutMin(cfg *config.Config, client *ethclient.Client, env types.Env
fee := new(big.Int).Div(new(big.Int).Mul(amountIn, feeRate), big.NewInt(1e6))
amountIn = amountIn.Sub(amountIn, fee)
fmt.Println("sub fee", amountIn.Text(10))
//fmt.Println("sub fee", amountIn.Text(10))
amountOut := new(big.Int).Div(amountIn, price)
fmt.Println("amount out", amountOut.Text(10))
//fmt.Println("amount out", amountOut.Text(10))
minAmountOut := new(big.Int).Mul(amountOut, big.NewInt(1000-slippage))
minAmountOut = minAmountOut.Div(minAmountOut, big.NewInt(1000))
......
......@@ -6,7 +6,7 @@ var (
func init() {
allEnv["bsc"] = Env{
RPC: "https://four.rpc.48.club",
//RPC: "https://four.rpc.48.club",
USDT: "0x55d398326f99059ff775485246999027b3197955",
Permit2: "0x31c2F6fcFf4F8759b3Bd5Bf0e1084A055615c768",
Router: "0x1b81D678ffb9C0263b24A97847620C99d213eB14",
......@@ -16,7 +16,7 @@ func init() {
}
type Env struct {
RPC string
//RPC string
USDT string
//KLKO string
Permit2 string
......
......@@ -183,12 +183,16 @@ func accountApprove(client *ethclient.Client, acc *types.Account, env types.Env)
addNewTx := func(tx *ethtypes.Transaction) {
waitTx(client, tx)
}
allow, _ := usdt.Allowance(&bind.CallOpts{
allow, err := usdt.Allowance(&bind.CallOpts{
From: acc.Addr,
BlockNumber: nil,
Context: context.Background(),
}, acc.Addr, common.HexToAddress(env.Router))
if allow.Int64() == 0 {
if err != nil {
log.WithError(err).Warn("allow permit2 permit failed")
return fmt.Errorf("get usdt allowance failed: %v", err)
}
if allow == nil || allow.Int64() == 0 {
if tx, err := usdt.Approve(txopts, common.HexToAddress(env.Router), abi.MaxUint256); err != nil {
return fmt.Errorf("usdt approve pool failed: %v", err)
} else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment