This repository has been archived by the owner on Oct 20, 2020. It is now read-only.
forked from go-steem/rpc
-
Notifications
You must be signed in to change notification settings - Fork 6
/
trx.go
116 lines (96 loc) · 2.52 KB
/
trx.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package client
import (
"time"
"github.com/asuleymanov/steem-go/api"
"github.com/asuleymanov/steem-go/transactions"
"github.com/asuleymanov/steem-go/types"
)
//BResp of response when sending a transaction.
type BResp struct {
ID string
BlockNum int32
TrxNum int32
Expired bool
JSONTrx string
}
//SendTrx generates and sends an array of transactions to STEEM.
func (client *Client) SendTrx(username string, strx []types.Operation) (*BResp, error) {
var bresp BResp
// Getting the necessary parameters
props, err := client.API.GetDynamicGlobalProperties()
if err != nil {
return nil, err
}
// Creating a Transaction
refBlockPrefix, err := transactions.RefBlockPrefix(props.HeadBlockID)
if err != nil {
return nil, err
}
tx := transactions.NewSignedTransaction(&types.Transaction{
RefBlockNum: transactions.RefBlockNum(props.HeadBlockNumber),
RefBlockPrefix: refBlockPrefix,
})
// Adding Operations to a Transaction
for _, val := range strx {
tx.PushOperation(val)
}
expTime := time.Now().Add(59 * time.Minute).UTC()
tm := types.Time{
Time: &expTime,
}
tx.Expiration = &tm
// Obtain the key required for signing
privKeys, err := client.SigningKeys(strx[0])
if err != nil {
return nil, err
}
// Sign the transaction
if err := tx.Sign(privKeys, client.chainID); err != nil {
return nil, err
}
// Sending a transaction
var resp *api.BroadcastResponse
if client.AsyncProtocol {
err = client.API.BroadcastTransaction(tx.Transaction)
} else {
resp, err = client.API.BroadcastTransactionSynchronous(tx.Transaction)
}
bresp.JSONTrx, _ = JSONTrxString(tx)
if err != nil {
return &bresp, err
}
if resp != nil && !client.AsyncProtocol {
bresp.ID = resp.ID
bresp.BlockNum = resp.BlockNum
bresp.TrxNum = resp.TrxNum
bresp.Expired = resp.Expired
return &bresp, nil
}
return &bresp, nil
}
func (client *Client) GetTrx(strx []types.Operation) (*types.Transaction, error) {
// Getting the necessary parameters
props, err := client.API.GetDynamicGlobalProperties()
if err != nil {
return nil, err
}
// Creating a Transaction
refBlockPrefix, err := transactions.RefBlockPrefix(props.HeadBlockID)
if err != nil {
return nil, err
}
tx := &types.Transaction{
RefBlockNum: transactions.RefBlockNum(props.HeadBlockNumber),
RefBlockPrefix: refBlockPrefix,
}
// Adding Operations to a Transaction
for _, val := range strx {
tx.PushOperation(val)
}
expTime := time.Now().Add(59 * time.Minute).UTC()
tm := types.Time{
Time: &expTime,
}
tx.Expiration = &tm
return tx, nil
}