Skip to content

Commit

Permalink
add proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
mmc committed Jun 15, 2024
1 parent 3220928 commit 5b09e67
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 76 deletions.
5 changes: 5 additions & 0 deletions etc/solxen-tx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Name: solxen-tx
LogConf:
Encoding: plain

#HttpProxys:
# - 199.167.236.12:3128
# - 1.1.1.1

Sol:
HdPAth: m/44'/501'/0'/0' # 钱包地址路径 [wallet derivation path]
Url: "https://api.mainnet-beta.solana.com" # rpc地址 [rpc address]
Expand Down
6 changes: 3 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ type Config struct {
// // Topic string
// // }
// DqConf dq.DqConf
LogConf logx.LogConf
Sol Sol

LogConf logx.LogConf
Sol Sol
HttpProxys []string `json:",optional"`
// Vault struct {
// Address *vault.Config
// Token string
Expand Down
6 changes: 3 additions & 3 deletions internal/logic/airdrop.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func (l *Producer) Airdrop() {
for _, account := range l.svcCtx.AddrList {
out, err := l.svcCtx.SolCli.RequestAirdrop(
out, err := l.svcCtx.SolWriteCli.RequestAirdrop(
l.ctx,
account.PublicKey(),
solana.LAMPORTS_PER_SOL*100,
Expand All @@ -19,14 +19,14 @@ func (l *Producer) Airdrop() {
if err != nil {
logx.Errorf("err :%v", err)
}
balance, err := l.svcCtx.SolCli.GetBalance(l.ctx, account.PublicKey(), rpc.CommitmentConfirmed)
balance, err := l.svcCtx.SolReadCli.GetBalance(l.ctx, account.PublicKey(), rpc.CommitmentConfirmed)

logx.Infof("signature: %v account:%v amount:%v before:%v", out.String(), account.PublicKey(), 100, balance.Value)
time.Sleep(1)
}

for _, accout := range l.svcCtx.AddrList {
balance, err := l.svcCtx.SolCli.GetBalance(l.ctx, accout.PublicKey(), rpc.CommitmentConfirmed)
balance, err := l.svcCtx.SolReadCli.GetBalance(l.ctx, accout.PublicKey(), rpc.CommitmentConfirmed)
if err != nil {
return
}
Expand Down
6 changes: 3 additions & 3 deletions internal/logic/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (l *Producer) Balance() error {
}

// 获取账户余额
balance, err := l.svcCtx.SolCli.GetBalance(l.ctx, account.PublicKey(), rpc.CommitmentConfirmed)
balance, err := l.svcCtx.SolReadCli.GetBalance(l.ctx, account.PublicKey(), rpc.CommitmentConfirmed)
if err != nil {
logx.Errorf("failed to get balance for account %v: %v", account.PublicKey(), err)
continue
Expand Down Expand Up @@ -63,12 +63,12 @@ func (l *Producer) Balance() error {
return errorx.Wrap(err, "user_eth_xn_record_pda")
}

err = l.svcCtx.SolCli.GetAccountDataInto(
err = l.svcCtx.SolReadCli.GetAccountDataInto(
l.ctx,
userSolXnRecordPda,
&userSolAccountDataRaw,
)
resp, err := l.svcCtx.SolCli.GetAccountInfoWithOpts(l.ctx, user_token_record_pda, &rpc.GetAccountInfoOpts{
resp, err := l.svcCtx.SolReadCli.GetAccountInfoWithOpts(l.ctx, user_token_record_pda, &rpc.GetAccountInfoOpts{
Commitment: rpc.CommitmentConfirmed,
})
if err != nil {
Expand Down
60 changes: 35 additions & 25 deletions internal/logic/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"math/big"
"math/rand"
httpclient "solxen-tx/pkg/http"
"time"

"solxen-tx/internal/logic/generated/sol_xen_miner"
Expand Down Expand Up @@ -33,6 +35,19 @@ var (

)

func (l *Producer) GetProxyAddr() string {

if len(l.svcCtx.Config.HttpProxys) == 0 {
return ""
}
rand.Seed(time.Now().UnixNano())
index := rand.Intn(len(l.svcCtx.Config.HttpProxys))
httpProxy := l.svcCtx.Config.HttpProxys[index]
// logx.Infof("proxy:%v", fmt.Sprintf("http://%v", httpProxy))
return fmt.Sprintf("http://%v", httpProxy)

}

func (l *Producer) Miner() error {

var (
Expand All @@ -46,21 +61,6 @@ func (l *Producer) Miner() error {
eth.Address = uint8Array
eth.AddressStr = ethAccount.String()

// out := make([]rpc.PriorizationFeeResult, 0)
// feeAccount := []solana.PublicKey{
// solana.MustPublicKeyFromBase58(l.svcCtx.Config.Sol.ProgramId),
// }

// fee := l.svcCtx.Config.Sol.Fee
// if fee == 0 {
// out, _ = l.svcCtx.SolCli.GetRecentPrioritizationFees(l.ctx, feeAccount)
// var feeFata []float64
// for _, item := range out {
// feeFata = append(feeFata, float64(item.PrioritizationFee))
// }
// _fee, _ := stats.Mean(feeFata)
// fee = uint64(_fee) * 1_000_000
// }
feesInit := computebudget.NewSetComputeUnitPriceInstructionBuilder().SetMicroLamports(l.svcCtx.Config.Sol.Fee).Build()

for _index, _account := range l.svcCtx.AddrList {
Expand Down Expand Up @@ -119,7 +119,6 @@ func (l *Producer) Miner() error {
return nil
},
func() error {

userSolXnRecordPda, _, err = solana.FindProgramAddress(
[][]byte{
[]byte("xn-by-sol"),
Expand Down Expand Up @@ -152,7 +151,12 @@ func (l *Producer) Miner() error {
instruction := solana.NewInstruction(l.ProgramIdMiner[kind], mintToken.Accounts(), data)
// l.svcCtx.Lock.Unlock()

recent, err := l.svcCtx.SolCli.GetLatestBlockhash(context.Background(), rpc.CommitmentFinalized)
proxyUrl := l.GetProxyAddr()
if proxyUrl != "" {
l.svcCtx.SolWriteCli = httpclient.NewWithProxy(l.svcCtx.Config.Sol.Url, proxyUrl)
}

recent, err := l.svcCtx.SolReadCli.GetLatestBlockhash(context.Background(), rpc.CommitmentFinalized)
if err != nil {
return errorx.Wrap(err, "network.")
}
Expand All @@ -170,10 +174,15 @@ func (l *Producer) Miner() error {
//
// rand.Seed(time.Now().UnixNano())
// n := rand.Intn(5-0+1) + 0
var rpcClient *rpc.Client
// var rpcClient *rpc.Client
if l.svcCtx.Config.Sol.JitoTip != 0 {
// proxyUrl := l.GetProxyAddr()
if proxyUrl != "" {
l.svcCtx.SolWriteCli = httpclient.NewWithProxy(JitoTxUrl[0], proxyUrl)
} else {
l.svcCtx.SolWriteCli = rpc.New(JitoTxUrl[0])
}

rpcClient = rpc.New(JitoTxUrl[0])
// time.Sleep(1000 * time.Millisecond)

if len(l.Respd.Result) == 0 {
Expand All @@ -193,7 +202,7 @@ func (l *Producer) Miner() error {
tx.AddInstruction(transferInstruction)
} else {
tx.AddInstruction(feesInit)
rpcClient = l.svcCtx.SolCli
// rpcClient = l.svcCtx.SolCli
}
txData, err := tx.Build()

Expand All @@ -218,7 +227,7 @@ func (l *Producer) Miner() error {
)
err = mr.Finish(
func() error {
signature, err = rpcClient.SendTransactionWithOpts(context.TODO(), txData, rpc.TransactionOpts{
signature, err = l.svcCtx.SolWriteCli.SendTransactionWithOpts(context.TODO(), txData, rpc.TransactionOpts{
// _ = rpcClient
// signature, err = l.svcCtx.TxnCli.SendTransactionWithOpts(context.TODO(), txData, rpc.TransactionOpts{
SkipPreflight: true,
Expand All @@ -233,7 +242,7 @@ func (l *Producer) Miner() error {
},

func() error {
err = l.svcCtx.SolCli.GetAccountDataInto(
err = l.svcCtx.SolReadCli.GetAccountDataInto(
l.ctx,
userEthXnRecordPda,
&userAccountDataRaw,
Expand All @@ -246,7 +255,7 @@ func (l *Producer) Miner() error {
},

func() error {
err = l.svcCtx.SolCli.GetAccountDataInto(
err = l.svcCtx.SolReadCli.GetAccountDataInto(
l.ctx,
userSolXnRecordPda,
&userSolAccountDataRaw,
Expand All @@ -262,7 +271,7 @@ func (l *Producer) Miner() error {
return err
}

logx.Infof("account:%v fee:%v jito:%v slot:%v kind:%v hashs:%v superhashes:%v Points:%v t:%v",
logx.Infof("account:%v fee:%v jito:%v slot:%v kind:%v hashs:%v superhashes:%v Points:%v proxy:%v t:%v",
account.PublicKey(),
l.svcCtx.Config.Sol.Fee,
l.svcCtx.Config.Sol.JitoTip,
Expand All @@ -272,6 +281,7 @@ func (l *Producer) Miner() error {
userAccountDataRaw.Hashes,
userAccountDataRaw.Superhashes,
big.NewInt(0).Div(userSolAccountDataRaw.Points.BigInt(), big.NewInt(1_000_000_000)),
proxyUrl,
time.Since(t))

return nil
Expand All @@ -293,7 +303,7 @@ func (l *Producer) CheckAddressBalance() error {
)
for _, addr := range l.svcCtx.AddrList {
fns = append(fns, func() error {
balance, err := l.svcCtx.SolCli.GetBalance(l.ctx, addr.PublicKey(), rpc.CommitmentFinalized)
balance, err := l.svcCtx.SolReadCli.GetBalance(l.ctx, addr.PublicKey(), rpc.CommitmentFinalized)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/logic/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (l *Producer) Mint() error {
instruction := solana.NewInstruction(solana.MustPublicKeyFromBase58(l.svcCtx.Config.Sol.ProgramId), mintToken.Accounts(), data)

signers := []solana.PrivateKey{account.PrivateKey}
recent, err := l.svcCtx.SolCli.GetLatestBlockhash(context.Background(), rpc.CommitmentFinalized)
recent, err := l.svcCtx.SolReadCli.GetLatestBlockhash(context.Background(), rpc.CommitmentFinalized)
rent := recent.Value.Blockhash
tx, err := solana.NewTransactionBuilder().
AddInstruction(feesInit).
Expand Down Expand Up @@ -132,7 +132,7 @@ func (l *Producer) Mint() error {
user_balance_data_raw sol_xen_minter.UserTokensRecord
)

_, err = l.svcCtx.SolCli.SendTransactionWithOpts(context.Background(), tx, rpc.TransactionOpts{
_, err = l.svcCtx.SolWriteCli.SendTransactionWithOpts(context.Background(), tx, rpc.TransactionOpts{
SkipPreflight: false,
})
if err != nil {
Expand All @@ -144,7 +144,7 @@ func (l *Producer) Mint() error {
// if err != nil {
//
// }
resp, err := l.svcCtx.SolCli.GetAccountInfoWithOpts(l.ctx, user_token_record_pda, &rpc.GetAccountInfoOpts{
resp, err := l.svcCtx.SolReadCli.GetAccountInfoWithOpts(l.ctx, user_token_record_pda, &rpc.GetAccountInfoOpts{
Commitment: rpc.CommitmentConfirmed,
})
if err != nil {
Expand Down
52 changes: 13 additions & 39 deletions internal/svc/serviceContext.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package svc

import (
"net"
"net/http"
"solxen-tx/internal/config"
pb "solxen-tx/internal/svc/proto"
httpclient "solxen-tx/pkg/http"
"sync"
"time"

"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
"github.com/klauspost/compress/gzhttp"
)

var (
Expand All @@ -20,54 +19,29 @@ var (
)

type ServiceContext struct {
Config config.Config
Lock sync.RWMutex
AddrList []*solana.Wallet
SolCli *rpc.Client
TxnCli *rpc.Client
Config config.Config
Lock sync.RWMutex
AddrList []*solana.Wallet
SolReadCli *rpc.Client
SolWriteCli *rpc.Client
// TxnCli *rpc.Client
GrpcCli pb.GeyserClient
Blockhash chan solana.Hash
HTTPClient *http.Client
}

func NewServiceContext(c config.Config) *ServiceContext {
s := &ServiceContext{
Config: c,
Lock: sync.RWMutex{},
AddrList: make([]*solana.Wallet, 0),
SolCli: rpc.New(c.Sol.Url),
Config: c,
Lock: sync.RWMutex{},
AddrList: make([]*solana.Wallet, 0),
SolReadCli: rpc.New(c.Sol.Url),
SolWriteCli: rpc.New(c.Sol.Url),
// TxnCli: rpc.New(c.Sol.TxnUrl),
// GrpcCli: NewGrpcCli(c.Sol.GrpcUrl, true),
Blockhash: make(chan solana.Hash, 10),
HTTPClient: newHTTP(),
HTTPClient: httpclient.NewHTTP(),
}
s.GenKeyByWord()
return s
}

func newHTTP() *http.Client {
tr := newHTTPTransport()

return &http.Client{
Timeout: defaultTimeout,
Transport: gzhttp.Transport(tr),
}
}

func newHTTPTransport() *http.Transport {
return &http.Transport{
IdleConnTimeout: defaultTimeout,
MaxConnsPerHost: defaultMaxIdleConnsPerHost,
MaxIdleConnsPerHost: defaultMaxIdleConnsPerHost,
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: defaultTimeout,
KeepAlive: defaultKeepAlive,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
// MaxIdleConns: 100,
TLSHandshakeTimeout: 10 * time.Second,
// ExpectContinueTimeout: 1 * time.Second,
}
}
Loading

0 comments on commit 5b09e67

Please sign in to comment.