-
Notifications
You must be signed in to change notification settings - Fork 8
/
send_transaction.go
69 lines (58 loc) 路 1.83 KB
/
send_transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package api
import (
"encoding/json"
"fmt"
)
type SendTransactionResponse struct {
Jsonrpc string `json:"jsonrpc"`
ID string `json:"id,omitempty"`
Result *SendTransactionResult `json:"result,omitempty"`
Error *TxError `json:"error,omitempty"`
}
type SendTransactionResult struct {
Code int `json:"code"`
Data string `json:"data"`
Log string `json:"log"`
Hash string `json:"hash"`
}
type TxError struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Data string `json:"data,omitempty"`
TxResult struct {
Code int `json:"code,omitempty"`
Log string `json:"log,omitempty"`
} `json:"tx_result,omitempty"`
}
func (e *TxError) Error() string {
return fmt.Sprintf("code: %d, message: %s, data: \"%s\", tx_result.code: %d, tx_result.log: \"%s\"", e.Code, e.Message, e.Message, e.TxResult.Code, e.TxResult.Log)
}
// Returns the result of raw tx.
// To ensure that transaction was successfully committed to the blockchain,
// you need to find the transaction by the hash and ensure that the status code equals to 0.
func (a *Api) SendRawTransaction(tx string) (*SendTransactionResult, error) {
res, err := a.client.R().SetQueryParam("tx", tx).Get("/send_transaction")
if err != nil {
return nil, err
}
if res.IsError() {
return nil, NewResponseError(res)
}
response := new(SendTransactionResponse)
err = json.Unmarshal(res.Body(), response)
if err != nil {
return nil, err
}
if response.Error != nil {
return nil, response.Error
}
return response.Result, nil
}
// Returns the result of sending signed tx.
func (a *Api) SendTransaction(transaction interface{ Encode() (string, error) }) (*SendTransactionResult, error) {
tx, err := transaction.Encode()
if err != nil {
return nil, err
}
return a.SendRawTransaction(tx)
}