Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
lmxdawn committed Jan 14, 2022
1 parent d95411f commit 6589208
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
4 changes: 2 additions & 2 deletions client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ type HttpClient struct {
}

// NewHttpClient 创建
func NewHttpClient(protocol, rechargeNotifyUrl, withdrawNotifyUrl string) (*HttpClient, error) {
func NewHttpClient(protocol, rechargeNotifyUrl, withdrawNotifyUrl string) *HttpClient {
return &HttpClient{
protocol,
rechargeNotifyUrl,
withdrawNotifyUrl,
}, nil
}
}

// RechargeSuccess 充值成功通知
Expand Down
50 changes: 37 additions & 13 deletions engine/concurrent.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package engine

import (
"github.com/lmxdawn/wallet/client"
"github.com/lmxdawn/wallet/config"
"github.com/lmxdawn/wallet/db"
"github.com/lmxdawn/wallet/scheduler"
Expand All @@ -13,7 +14,7 @@ type Worker interface {
getTransaction(uint64) ([]types.Transaction, error)
getTransactionReceipt(*types.Transaction) error
createWallet() (*types.Wallet, error)
sendTransaction(string, string, int64, int) (string, error)
sendTransaction(string, string, int64) (string, error)
}

type Scheduler interface {
Expand All @@ -33,6 +34,7 @@ type ConCurrentEngine struct {
config config.EngineConfig
Protocol string
db db.Database
http *client.HttpClient
}

// Run 启动
Expand Down Expand Up @@ -103,18 +105,37 @@ func (c *ConCurrentEngine) createReceiptWorker() {
c.scheduler.ReceiptSubmit(transaction)
continue
}
if transaction.Status == 1 {
log.Info().Msgf("交易完成:%v", transaction.Hash)
} else {
log.Info().Msgf("交易失败:%v", transaction.Hash)
}
err = c.db.Put(transaction.To, "123")
if err != nil {
log.Info().Msgf("存入失败")
if transaction.Status != 1 {
log.Error().Msgf("交易失败:%v", transaction.Hash)
continue
}
has, err := c.db.Has(transaction.To)
if has && err == nil {
log.Info().Msgf("查询到值")
log.Info().Msgf("交易完成:%v", transaction.Hash)

// 判断是否存在
if ok, err := c.db.Has(transaction.Hash); err == nil && ok {
orderId, err := c.db.Get(transaction.Hash)
if err != nil {
log.Error().Msgf("未查询到订单:%v, %v", transaction.Hash, err)
// 重新提交
c.scheduler.ReceiptSubmit(transaction)
continue
}
err = c.http.WithdrawSuccess(transaction.Hash, orderId, transaction.To, transaction.Value.Int64())
if err != nil {
log.Error().Msgf("提现回调通知失败:%v, %v", transaction.Hash, err)
// 重新提交
c.scheduler.ReceiptSubmit(transaction)
continue
}
_ = c.db.Delete(transaction.Hash)
} else if ok, err := c.db.Has(transaction.To); err == nil && ok {
err = c.http.RechargeSuccess(transaction.Hash, transaction.To, transaction.Value.Int64())
if err != nil {
log.Error().Msgf("充值回调通知失败:%v, %v", transaction.Hash, err)
// 重新提交
c.scheduler.ReceiptSubmit(transaction)
continue
}
}
}
}()
Expand All @@ -141,7 +162,7 @@ func (c *ConCurrentEngine) DeleteWallet(address string) error {

// SendTransaction 发送交易
func (c *ConCurrentEngine) SendTransaction(orderId string, toAddress string, value int64) (string, error) {
hash, err := c.Worker.sendTransaction(c.config.WithdrawPrivateKey, toAddress, value, c.config.Decimals)
hash, err := c.Worker.sendTransaction(c.config.WithdrawPrivateKey, toAddress, value)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -178,12 +199,15 @@ func NewEngine(config config.EngineConfig) (*ConCurrentEngine, error) {
worker = NewEthWorker(config.Confirms, config.Contract, config.Rpc)
}

http := client.NewHttpClient(config.Protocol, config.RechargeNotifyUrl, config.WithdrawNotifyUrl)

return &ConCurrentEngine{
//scheduler: scheduler.NewSimpleScheduler(), // 简单的任务调度器
scheduler: scheduler.NewQueueScheduler(), // 队列的任务调度器
Worker: worker,
config: config,
Protocol: config.Protocol,
db: keyDB,
http: http,
}, nil
}
7 changes: 3 additions & 4 deletions engine/eth_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/ethereum/go-ethereum/rlp"
"github.com/lmxdawn/wallet/client"
"github.com/lmxdawn/wallet/types"
"math"
"math/big"
)

Expand Down Expand Up @@ -121,7 +120,7 @@ func (e *EthWorker) createWallet() (*types.Wallet, error) {
}

// sendTransaction 创建并发送裸交易
func (e *EthWorker) sendTransaction(privateKeyStr string, toAddress string, amount int64, decimals int) (string, error) {
func (e *EthWorker) sendTransaction(privateKeyStr string, toAddress string, amount int64) (string, error) {

privateKey, err := crypto.HexToECDSA(privateKeyStr)
if err != nil {
Expand All @@ -140,8 +139,8 @@ func (e *EthWorker) sendTransaction(privateKeyStr string, toAddress string, amou
return "", err
}

value := big.NewInt(amount * int64(math.Pow10(decimals))) // in wei (1 eth)
gasLimit := uint64(21000) // in units
value := big.NewInt(amount) // in wei (1 eth)
gasLimit := uint64(21000) // in units
gasPrice, err := e.http.SuggestGasPrice(context.Background())
if err != nil {
return "", err
Expand Down

0 comments on commit 6589208

Please sign in to comment.