From 68687092db72ec0980ff94f31bf4b7bb8a5660c4 Mon Sep 17 00:00:00 2001 From: Stefan Liu Date: Thu, 2 Dec 2021 15:29:46 +0800 Subject: [PATCH] Add get fee method --- chains/bridge/bridge.go | 7 ++++++- chains/bridge/fee.go | 27 +++++++++++++++++++++++++++ chains/bridge/tx.go | 20 ++++++++++++++++++++ tools/ding.go | 23 +++++++++++++++++++++++ wallet/wallet.go | 2 +- 5 files changed, 77 insertions(+), 2 deletions(-) diff --git a/chains/bridge/bridge.go b/chains/bridge/bridge.go index 44e16d6..d6f58ea 100644 --- a/chains/bridge/bridge.go +++ b/chains/bridge/bridge.go @@ -26,7 +26,8 @@ import ( ) type Client struct { - address string + address string + explorer string } func New(url string) *Client { @@ -35,6 +36,10 @@ func New(url string) *Client { } } +func (c *Client) SetExplorer(url string) { + c.explorer = url +} + func (c *Client) Address() string { return c.address } diff --git a/chains/bridge/fee.go b/chains/bridge/fee.go index 6467c28..6198069 100644 --- a/chains/bridge/fee.go +++ b/chains/bridge/fee.go @@ -52,3 +52,30 @@ func (r *CheckFeeRequest) Missing() bool { func (c *Client) CheckFee(req map[string]*CheckFeeRequest) (err error) { return tools.PostJsonFor(c.address+"/newcheckfee", req, &req) } + +type GetFeeRequest struct { + SrcChainId uint64 + DstChainId uint64 + SwapTokenHash string + Hash string +} + +type GetFeeResponse struct { + SrcChainId uint64 + DstChainId uint64 + SwapTokenHash string + TokenAmount string + UsdtAmount string + TokenAmountWithPrecision string + Balance string + BalanceWithPrecision string +} + +func (c *Client) GetFee(req *GetFeeRequest) (res *GetFeeResponse, err error) { + res = new(GetFeeResponse) + err = tools.PostJsonFor(c.address+"/getfee", req, res) + if err != nil { + return nil, err + } + return +} diff --git a/chains/bridge/tx.go b/chains/bridge/tx.go index d8d4b02..d20ac8f 100644 --- a/chains/bridge/tx.go +++ b/chains/bridge/tx.go @@ -48,6 +48,17 @@ type CheckTxResponse struct { } } +type TxBody struct { + Chainid uint64 + Txhash string +} + +type TxResponse struct { + Fchaintx *TxBody + Mchaintx *TxBody + Tchaintx *TxBody +} + func (c *Client) CheckTx(req *CheckTxRequest) (res *CheckTxResponse, err error) { res = new(CheckTxResponse) err = tools.PostJsonFor(c.address+"/transactionofhash", req, res) @@ -56,3 +67,12 @@ func (c *Client) CheckTx(req *CheckTxRequest) (res *CheckTxResponse, err error) } return } + +func (c *Client) FetchTx(req *CheckTxRequest) (res *TxResponse, err error) { + res = new(TxResponse) + err = tools.GetJsonFor(c.explorer+"/getcrosstx?txhash="+req.Hash, res) + if err != nil { + res = nil + } + return +} diff --git a/tools/ding.go b/tools/ding.go index 952692a..0b4a1ad 100644 --- a/tools/ding.go +++ b/tools/ding.go @@ -129,3 +129,26 @@ func PostJsonFor(url string, payload interface{}, result interface{}) error { } return nil } + +func GetJsonFor(url string, result interface{}) error { + req, err := http.NewRequest("GET", url, nil) + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + respBody, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + err = json.Unmarshal(respBody, result) + if err != nil { + log.Error("GetJson response", "url", url, "Body", string(respBody)) + } else { + log.Debug("GetJson response", "url", url, "Body", string(respBody)) + } + return nil +} diff --git a/wallet/wallet.go b/wallet/wallet.go index 478f5fd..171cae4 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -164,7 +164,7 @@ func (w *Wallet) SendWithAccount(account accounts.Account, addr common.Address, return } if gasLimit == 0 { - msg := ethereum.CallMsg{From: account.Address, To: &addr, GasPrice: gasPrice, Value: big.NewInt(0), Data: data} + msg := ethereum.CallMsg{From: account.Address, To: &addr, GasPrice: gasPrice, Value: amount, Data: data} gasLimit, err = w.sdk.Node().EstimateGas(context.Background(), msg) if err != nil { nonces.Update(false)