-
Notifications
You must be signed in to change notification settings - Fork 3
/
apiSetTransaction.go
65 lines (55 loc) · 1.69 KB
/
apiSetTransaction.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
package mintersdk
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
// Ответ транзакции
//easyjson:json
type send_transaction struct {
JSONRPC string `json:"jsonrpc"`
ID string `json:"id"`
Result TransSendResponse `json:"result"`
Error ErrorStruct `json:"error"`
}
type TransSendResponse struct {
Code int `json:"code" bson:"code" gorm:"code" db:"code"`
Log string `json:"log" bson:"log" gorm:"log" db:"log"`
Data string `json:"data" bson:"data" gorm:"data" db:"data"`
Hash string `json:"hash" bson:"hash" gorm:"hash" db:"hash"`
}
// Исполнение транзакции закодированной RLP
func (c *SDK) SetTransaction(strRlpEnc string) (string, error) {
fmt.Println("TX RLP:", strRlpEnc)
url := fmt.Sprintf("%s/send_transaction?tx=0x%s", c.MnAddress, strRlpEnc)
res, err := http.Get(url)
if err != nil {
//fmt.Println("ERROR: TxSign::http.Post")
fmt.Println("ERROR: TxSign::http.Get")
return "", err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println("ERROR: TxSign::ioutil.ReadAll")
return "", err
}
var data send_transaction
//json.Unmarshal(body, &data) -- переход на easyjson
err = data.UnmarshalJSON(body)
if err != nil {
panic(err)
}
if data.Error.Code != 0 {
err = errors.New(fmt.Sprint(data.Error.Code, " - ", data.Error.Message, " - ", data.Error.TxResult.Log))
return "", err
}
if data.Result.Code == 0 {
return fmt.Sprintf("Mt%s", strings.ToLower(data.Result.Hash)), nil
} else {
fmt.Printf("ERROR: TxSign: %#v\n", data)
return data.Result.Log, errors.New(fmt.Sprintf("Err:%d-%s", data.Result.Code, data.Result.Log))
}
}