Commit dd650af2 authored by vicotor's avatar vicotor

add rpc proxy in config

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