Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
lmxdawn committed Jan 18, 2022
1 parent d59a2c2 commit 2fb84d6
Show file tree
Hide file tree
Showing 19 changed files with 651 additions and 100 deletions.
57 changes: 47 additions & 10 deletions client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"time"
)

// Response ...
Expand All @@ -21,30 +22,36 @@ func (br Response) Success() bool {

type HttpClient struct {
protocol string
coinName string
rechargeNotifyUrl string
withdrawNotifyUrl string
timeout int // 超时时间,毫秒单位
}

// NewHttpClient 创建
func NewHttpClient(protocol, rechargeNotifyUrl, withdrawNotifyUrl string) *HttpClient {
func NewHttpClient(protocol, coinName, rechargeNotifyUrl, withdrawNotifyUrl string) *HttpClient {
return &HttpClient{
protocol,
coinName,
rechargeNotifyUrl,
withdrawNotifyUrl,
1000 * 10,
}
}

// RechargeSuccess 充值成功通知
func (h *HttpClient) RechargeSuccess(hash string, address string, value int64) error {
func (h *HttpClient) RechargeSuccess(hash string, status uint, address string, value int64) error {

data := make(map[string]interface{})
data["protocol"] = h.protocol
data["coinName"] = h.coinName
data["hash"] = hash
data["status"] = status
data["address"] = address
data["value"] = value

var res Response
err := post(h.withdrawNotifyUrl, data, &res)
err := h.post(h.withdrawNotifyUrl, data, &res)
if err != nil {
return err
}
Expand All @@ -57,17 +64,20 @@ func (h *HttpClient) RechargeSuccess(hash string, address string, value int64) e
}

// WithdrawSuccess 提现成功通知
func (h *HttpClient) WithdrawSuccess(hash string, orderId string, address string, value int64) error {
func (h *HttpClient) WithdrawSuccess(hash string, status uint, orderId string, address string, value int64) error {

data := make(map[string]interface{})
data["protocol"] = h.protocol
data["coinName"] = h.coinName
data["hash"] = hash
data["status"] = status
data["orderId"] = orderId
data["address"] = address
data["value"] = value

var res Response
err := post(h.withdrawNotifyUrl, data, &res)
err := h.post(h.withdrawNotifyUrl, data, &res)

if err != nil {
return err
}
Expand All @@ -80,7 +90,7 @@ func (h *HttpClient) WithdrawSuccess(hash string, orderId string, address string
}

// get 请求
func get(urlStr string, params url.Values, res interface{}) error {
func (h *HttpClient) get(urlStr string, params url.Values, res interface{}) error {

Url, err := url.Parse(urlStr)
if err != nil {
Expand All @@ -89,12 +99,26 @@ func get(urlStr string, params url.Values, res interface{}) error {
//如果参数中有中文参数,这个方法会进行URLEncode
Url.RawQuery = params.Encode()
urlPath := Url.String()
resp, err := http.Get(urlPath)

client := &http.Client{
Timeout: time.Millisecond * time.Duration(h.timeout),
}
req, err := http.NewRequest(http.MethodGet, urlPath, nil)
if err != nil {
// handle error
return err
}
resp, err := client.Do(req)
if err != nil {
// handle error
return err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

err = json.Unmarshal(body, res)
if err != nil {
Expand All @@ -105,18 +129,31 @@ func get(urlStr string, params url.Values, res interface{}) error {
}

// post 请求
func post(urlStr string, data map[string]interface{}, res interface{}) error {
func (h *HttpClient) post(urlStr string, data map[string]interface{}, res interface{}) error {
bytesData, err := json.Marshal(data)
if err != nil {
return err
}
resp, err := http.Post(urlStr, "application/json", bytes.NewReader(bytesData))

client := &http.Client{
Timeout: time.Millisecond * time.Duration(h.timeout),
}
req, err := http.NewRequest(http.MethodPost, urlStr, bytes.NewReader(bytesData))
if err != nil {
// handle error
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
// handle error
return err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
return err
}

Expand Down
48 changes: 24 additions & 24 deletions config/config-example.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
app:
port: 10001
engines:
- name: ETH
- coin_name: ETH
contract:
protocol: eth
rpc: https://mainnet.infura.io/v3/adee4ead47844d238802431fcb7683c6
rpc: https://ropsten.infura.io/v3/adee4ead47844d238802431fcb7683c6
file: data/eth
wallet_prefix: wallet-
hash_prefix: hash-
block_init: 14000513
block_init: 11828416
block_count: 1
block_after_time: 1
receipt_count: 1
receipt_count: 20
receipt_after_time: 1
confirms: 1
recharge_notify_url: https://mainnet.infura.io
withdraw_notify_url: https://mainnet.infura.io
withdraw_private_key: dfxdsfsdf
recharge_notify_url: http://localhost:10001/api/withdraw
withdraw_notify_url: http://localhost:10001/api/withdraw
withdraw_private_key: 4006894ada31cfc87211e0b428c7f812789636cf6f79efef5d2acc2b60617cf1
decimals: 18
- name: Tether USD
contract: 0xdac17f958d2ee523a2206206994597c13d831ec7
protocol: eth
rpc: https://mainnet.infura.io/v3/adee4ead47844d238802431fcb7683c6
file: data/usdt
wallet_prefix: wallet-
hash_prefix: hash-
block_init: 14000513
block_count: 1
block_after_time: 1
receipt_count: 1
receipt_after_time: 1
confirms: 1
recharge_notify_url: https://mainnet.infura.io
withdraw_notify_url: https://mainnet.infura.io
withdraw_private_key: dfxdsfsdf
decimals: 18
# - coin_name: TEST
# contract: 0xeDc4019BCBdA0f7acCBB4833ffF54d618d9b7f82
# protocol: eth
# rpc: https://ropsten.infura.io/v3/adee4ead47844d238802431fcb7683c6
# file: data/usdt
# wallet_prefix: wallet-
# hash_prefix: hash-
# block_init: 11824838
# block_count: 1
# block_after_time: 1
# receipt_count: 1
# receipt_after_time: 1
# confirms: 1
# recharge_notify_url: https://mainnet.infura.io
# withdraw_notify_url: https://mainnet.infura.io
# withdraw_private_key: 4006894ada31cfc87211e0b428c7f812789636cf6f79efef5d2acc2b60617cf1
# decimals: 18
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ type AppConfig struct {
}

type EngineConfig struct {
Name string `yaml:"name"` // 名称
Contract string `yaml:"contract"` // 合约地址(为空表示主币)
Protocol string `yaml:"protocol"` // 协议名称
CoinName string `yaml:"coin_name"` // 币种名称
Rpc string `yaml:"rpc"` // rpc配置
File string `yaml:"file"` // db文件配置
WalletPrefix string `yaml:"wallet_prefix"` // 钱包的存储前缀
Expand Down
5 changes: 0 additions & 5 deletions contracts/erc20.sol

This file was deleted.

1 change: 0 additions & 1 deletion contracts/erc20_sol_ERC20.abi

This file was deleted.

Loading

0 comments on commit 2fb84d6

Please sign in to comment.