Commit 03c27007 authored by 贾浩@五瓣科技's avatar 贾浩@五瓣科技

update send gas

parent 7bac45a0
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"flag" "flag"
"sdk_api/config" "sdk_api/config"
"sdk_api/dao" "sdk_api/dao"
"sdk_api/gassender"
"sdk_api/server" "sdk_api/server"
"sdk_api/service" "sdk_api/service"
...@@ -18,21 +19,34 @@ func init() { ...@@ -18,21 +19,34 @@ func init() {
func main() { func main() {
flag.Parse() flag.Parse()
conf, err := config.New() cfg, err := config.New()
if err != nil { if err != nil {
panic(err) panic(err)
} }
da, err := dao.New(conf) gs := runGasSender(cfg)
select {}
da, err := dao.New(cfg)
if err != nil { if err != nil {
panic(err) panic(err)
} }
if conf.Debug { if cfg.Debug {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
} }
svs := service.New(conf, da) svs := service.New(cfg, da, gs)
server.StartServer(svs, cfg)
}
server.StartServer(svs, conf) func runGasSender(cfg *config.Config) *gassender.GasSender {
gs, err := gassender.NewGasSender(cfg.GasSender.RPC, cfg.GasSender.PrivateKey)
if err != nil {
panic(err)
}
gs.Run()
// gs.SendAONGas("0x0000000077024042e797Ae28A163C27E389CC5b2", 1)
return gs
} }
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
debug = true debug = true
[mysql] [mysql]
host = "127.0.0.1" host = "sdk-db"
port = 3306 port = 3306
user = "root" user = "root"
password = "XN2UARuys3zy4Oux" password = "XN2UARuys3zy4Oux"
...@@ -12,3 +12,9 @@ max_idle_conn = 2 ...@@ -12,3 +12,9 @@ max_idle_conn = 2
[server] [server]
listen = "0.0.0.0:8080" listen = "0.0.0.0:8080"
[gas_sender]
rpc = "https://sepolia.rpc.aonnet.io"
# 0x0000000077024042e797Ae28A163C27E389CC5b2
private_key = "39494cd233573c94d6b4d24847f2f4d5da9d0b384b61f3ad4fae9abd5c48e6fc"
...@@ -11,6 +11,7 @@ type Config struct { ...@@ -11,6 +11,7 @@ type Config struct {
Sender SenderConfig `toml:"sender"` Sender SenderConfig `toml:"sender"`
MySQL MysqlConfig `toml:"mysql"` MySQL MysqlConfig `toml:"mysql"`
Server ServerConfig `toml:"server"` Server ServerConfig `toml:"server"`
GasSender GasSenderConfig `toml:"gas_sender"`
} }
type SenderConfig struct { type SenderConfig struct {
...@@ -32,6 +33,11 @@ type ServerConfig struct { ...@@ -32,6 +33,11 @@ type ServerConfig struct {
Listen string `toml:"listen"` Listen string `toml:"listen"`
} }
type GasSenderConfig struct {
PrivateKey string `toml:"private_key"`
RPC string `toml:"rpc"`
}
var confPath = flag.String("c", "config.toml", "config file path") var confPath = flag.String("c", "config.toml", "config file path")
func New() (config *Config, err error) { func New() (config *Config, err error) {
......
package gassender
import (
"context"
"crypto/ecdsa"
"math/big"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
log "github.com/sirupsen/logrus"
)
type GasSender struct {
privateKey *ecdsa.PrivateKey
client *ethclient.Client
taskCh chan *gasTask
chainId *big.Int
}
type gasTask struct {
dest common.Address
value *big.Int
}
func NewGasSender(rpc, privateKey string) (*GasSender, error) {
ecdsaKey, err := crypto.HexToECDSA(common.Bytes2Hex(common.FromHex(privateKey)))
if err != nil {
return nil, err
}
log.WithField("address", crypto.PubkeyToAddress(ecdsaKey.PublicKey)).Info("aon gas sender address")
client, err := ethclient.Dial(rpc)
if err != nil {
return nil, err
}
chainId, err := client.ChainID(context.Background())
if err != nil {
return nil, err
}
return &GasSender{
privateKey: ecdsaKey,
client: client,
taskCh: make(chan *gasTask, 32),
chainId: chainId,
}, nil
}
func (gs *GasSender) Run() {
go func() {
for {
select {
case task := <-gs.taskCh:
gs.sendTx(task)
}
}
}()
}
func (gs *GasSender) SendAONGas(dest string, amount int) {
addr := common.HexToAddress(dest)
value := new(big.Int).Mul(big.NewInt(int64(amount)), big.NewInt(1000000000000000000))
gs.taskCh <- &gasTask{
dest: addr,
value: value,
}
}
func (gs *GasSender) sendTx(task *gasTask) {
log.WithFields(log.Fields{
"address": task.dest,
"value": task.value,
}).Info("new send gas task")
nonce, err := gs.client.PendingNonceAt(context.Background(), crypto.PubkeyToAddress(gs.privateKey.PublicKey))
if err != nil {
log.WithError(err).Error("get nonce failed")
return
}
tx := &types.LegacyTx{
Nonce: nonce,
GasPrice: big.NewInt(1000000000),
Gas: 21000,
To: &task.dest,
Value: task.value,
}
signer := types.NewEIP155Signer(gs.chainId)
signedTx, err := types.SignNewTx(gs.privateKey, signer, tx)
if err != nil {
log.WithError(err).Error("sign tx failed")
return
}
err = gs.client.SendTransaction(context.Background(), signedTx)
if err != nil {
log.WithError(err).Error("send tx failed")
return
}
txLog := log.WithField("txHash", signedTx.Hash().Hex())
txLog.Info("tx broadcasted")
for i := 0; i < 5; i++ {
time.Sleep(time.Second * 2)
receipt, err := gs.client.TransactionReceipt(context.Background(), signedTx.Hash())
if err != nil && err == ethereum.NotFound {
txLog.Info("tx receipt not found, retrying...")
continue
}
if err != nil {
txLog.WithError(err).Error("send gas tx failed")
return
}
if receipt.Status != 1 {
txLog.Error("send gas tx failed")
return
}
txLog.Info("send gas tx confirmed")
return
}
txLog.Error("tx receipt not found, timeout")
}
...@@ -9,7 +9,7 @@ require ( ...@@ -9,7 +9,7 @@ require (
github.com/gin-gonic/gin v1.10.0 github.com/gin-gonic/gin v1.10.0
github.com/golang-jwt/jwt/v5 v5.2.1 github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0 github.com/google/uuid v1.6.0
github.com/sirupsen/logrus v1.6.0 github.com/sirupsen/logrus v1.9.0
github.com/tidwall/gjson v1.17.1 github.com/tidwall/gjson v1.17.1
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gorm.io/driver/mysql v1.5.6 gorm.io/driver/mysql v1.5.6
...@@ -17,6 +17,8 @@ require ( ...@@ -17,6 +17,8 @@ require (
) )
require ( require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/bits-and-blooms/bitset v1.13.0 // indirect github.com/bits-and-blooms/bitset v1.13.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.3 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.3 // indirect
github.com/bytedance/sonic v1.11.6 // indirect github.com/bytedance/sonic v1.11.6 // indirect
...@@ -26,38 +28,45 @@ require ( ...@@ -26,38 +28,45 @@ require (
github.com/consensys/bavard v0.1.13 // indirect github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/ethereum/c-kzg-4844/bindings/go v0.0.0-20230126171313-363c7d7593b4 // indirect github.com/ethereum/c-kzg-4844/bindings/go v0.0.0-20230126171313-363c7d7593b4 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect github.com/goccy/go-json v0.10.2 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/holiman/uint256 v1.2.4 // indirect github.com/holiman/uint256 v1.2.4 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/leodido/go-urn v1.4.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/supranational/blst v0.3.12 // indirect github.com/supranational/blst v0.3.12 // indirect
github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect github.com/tidwall/pretty v1.2.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect golang.org/x/crypto v0.23.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.7.0 // indirect golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect golang.org/x/text v0.15.0 // indirect
golang.org/x/tools v0.20.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
......
This diff is collapsed.
...@@ -10,7 +10,6 @@ import ( ...@@ -10,7 +10,6 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
...@@ -91,7 +90,6 @@ func createUser(c *gin.Context) { ...@@ -91,7 +90,6 @@ func createUser(c *gin.Context) {
return return
} }
addr := crypto.PubkeyToAddress(*ecdsaPub) addr := crypto.PubkeyToAddress(*ecdsaPub)
log.Debugln(addr.Hex())
if strings.ToLower(addr.Hex()[2:]) != address { if strings.ToLower(addr.Hex()[2:]) != address {
c.JSON(200, withError("invalid signature")) c.JSON(200, withError("invalid signature"))
return return
......
...@@ -3,16 +3,19 @@ package service ...@@ -3,16 +3,19 @@ package service
import ( import (
"sdk_api/config" "sdk_api/config"
"sdk_api/dao" "sdk_api/dao"
"sdk_api/gassender"
) )
type Service struct { type Service struct {
d *dao.Dao d *dao.Dao
cfg *config.Config cfg *config.Config
gs *gassender.GasSender
} }
func New(conf *config.Config, da *dao.Dao) *Service { func New(conf *config.Config, da *dao.Dao, g *gassender.GasSender) *Service {
return &Service{ return &Service{
d: da, d: da,
cfg: conf, cfg: conf,
gs: g,
} }
} }
...@@ -45,5 +45,7 @@ func (s *Service) SetKeystore(uid, address, keystore string) (ok bool, err error ...@@ -45,5 +45,7 @@ func (s *Service) SetKeystore(uid, address, keystore string) (ok bool, err error
log.WithError(err).Error("set keystore failed") log.WithError(err).Error("set keystore failed")
return return
} }
s.gs.SendAONGas(address, 1)
return true, nil return true, nil
} }
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