forked from fbsobreira/gotron-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.go
196 lines (161 loc) · 6.73 KB
/
func.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package free
import (
"crypto/ecdsa"
"strings"
"time"
"github.com/ethereum/go-ethereum/crypto"
"github.com/fighterlyt/gotron-sdk/pkg/address"
"github.com/fighterlyt/gotron-sdk/pkg/client"
"github.com/fighterlyt/gotron-sdk/pkg/client/transaction"
"github.com/fighterlyt/gotron-sdk/pkg/common"
"github.com/fighterlyt/gotron-sdk/pkg/keystore"
"github.com/fighterlyt/gotron-sdk/pkg/proto/api"
"github.com/fighterlyt/gotron-sdk/pkg/proto/core"
"github.com/shopspring/decimal"
"github.com/pkg/errors"
)
/*UnFreeze 解冻质押TRX,解冻只能一次性解冻
参数:
* client *client.GrpcClient grc 客户端
* from string 质押来源钱包地址
* privateKeyHex string 质押来源钱包私钥
* to string 质押收益钱包
* resource core.ResourceCode 质押资源
返回值:
* error error 错误
*/
func UnFreeze(client *client.GrpcClient, from, privateKeyHex, to string, resource core.ResourceCode) (txID string, err error) { //nolint:lll
var (
account keystore.Account
privateKey *ecdsa.PrivateKey
tx *api.TransactionExtention
)
if privateKey, err = crypto.HexToECDSA(privateKeyHex); err != nil {
return ``, errors.Wrapf(err, "解析私钥错误")
}
fromAddress := address.PubkeyToAddress(privateKey.PublicKey)
k := keystore.NewKeyStore("keystore", keystore.LightScryptN, keystore.LightScryptP)
if k.HasAddress(fromAddress) {
if account, err = k.Find(keystore.Account{Address: fromAddress}); err != nil {
return ``, errors.Wrap(err, "加载账号")
}
} else {
if account, err = k.ImportECDSA(privateKey, ""); err != nil {
return ``, errors.Wrap(err, "导入私钥")
}
}
if err = k.Unlock(account, ""); err != nil {
return ``, errors.Wrap(err, "unlock钱包错误")
}
if tx, err = client.UnfreezeBalance(from, to, resource); err != nil {
return ``, errors.Wrap(err, `操作`)
}
controller := transaction.NewController(client, k, &account, tx.Transaction)
if err = controller.ExecuteTransaction(); err != nil {
return ``, err
}
return strings.TrimPrefix(common.BytesToHexString(tx.GetTxid()), "0x"), nil
}
/*Freeze 冻结质押TRX
参数:
* client *client.GrpcClient grc 客户端
* from string 质押来源钱包地址
* privateKeyHex string 质押来源钱包私钥
* to string 质押收益钱包
* resource core.ResourceCode 质押资源
* frozenBalance int64 质押金额,除以10^6是真正的金额
返回值:
* error error 错误
*/
func Freeze(client *client.GrpcClient, from, privateKeyHex, to string, resource core.ResourceCode, frozenBalance int64) (txID string, err error) { //nolint:lll
var (
account keystore.Account
privateKey *ecdsa.PrivateKey
tx *api.TransactionExtention
)
if privateKey, err = crypto.HexToECDSA(privateKeyHex); err != nil {
return ``, errors.Wrapf(err, "解析私钥错误")
}
fromAddress := address.PubkeyToAddress(privateKey.PublicKey)
k := keystore.NewKeyStore("keystore", keystore.LightScryptN, keystore.LightScryptP)
if k.HasAddress(fromAddress) {
if account, err = k.Find(keystore.Account{Address: fromAddress}); err != nil {
return ``, errors.Wrap(err, "加载账号")
}
} else {
if account, err = k.ImportECDSA(privateKey, ""); err != nil {
return ``, errors.Wrap(err, "导入私钥")
}
}
if err = k.Unlock(account, ""); err != nil {
return ``, errors.Wrap(err, "unlock钱包错误")
}
if tx, err = client.FreezeBalance(from, to, resource, frozenBalance); err != nil {
return ``, errors.Wrap(err, `操作`)
}
controller := transaction.NewController(client, k, &account, tx.Transaction)
if err = controller.ExecuteTransaction(); err != nil {
return ``, err
}
return strings.TrimPrefix(common.BytesToHexString(tx.GetTxid()), "0x"), nil
}
/*Resource 账号资源
参数:
* client *client.GrpcClient 客户端
* address string 地址
返回值:
* bandwidth int64 带宽
* energy int64 能量
* err error 错误
*/
func Resource(client *client.GrpcClient, address string) (bandwidth, energy int64, err error) {
var (
message *api.AccountResourceMessage
)
if message, err = client.GetAccountResource(address); err != nil {
return 0, 0, errors.Wrap(err, `获取资源信息`)
}
bandwidth = message.NetLimit + message.FreeNetLimit // 免费+质押获得
bandwidth -= message.FreeNetUsed // 免费已使用
bandwidth -= message.NetUsed // 质押已使用
return bandwidth, message.EnergyLimit - message.EnergyUsed, nil
}
// DelegatedResource 代理/托管的资源
type DelegatedResource struct {
From string `json:"from,omitempty"` // 来源,质押TRX的一方
To string `json:"to,omitempty"` // 获利的一方
FrozenBalanceForBandwidth decimal.Decimal `json:"frozen_balance_for_bandwidth,omitempty"` // 用于获取带宽的冻结TRX
FrozenBalanceForEnergy decimal.Decimal `json:"frozen_balance_for_energy,omitempty"` // 用于获取能量的冻结TRX
ExpireTimeForBandwidth time.Time `json:"expire_time_for_bandwidth,omitempty"` // 带宽过期时间
ExpireTimeForEnergy time.Time `json:"expire_time_for_energy,omitempty"` // 能量过期时间
}
func NewDelegatedResource(from, to []byte, frozenBalanceForBandwidth, frozenBalanceForEnergy, expireTimeForBandwidth, expireTimeForEnergy int64) *DelegatedResource {
return &DelegatedResource{
From: Base58ToAddress(common.BytesToHexString(from)),
To: Base58ToAddress(common.BytesToHexString(to)),
FrozenBalanceForBandwidth: decimal.New(frozenBalanceForBandwidth, -6),
FrozenBalanceForEnergy: decimal.New(frozenBalanceForEnergy, -6),
ExpireTimeForBandwidth: time.Unix(expireTimeForBandwidth, 0),
ExpireTimeForEnergy: time.Unix(expireTimeForEnergy/1000, 0),
}
}
func FreezeResource(client *client.GrpcClient, address string) (resources []*DelegatedResource, err error) {
var (
list []*api.DelegatedResourceList
)
if list, err = client.GetDelegatedResources(address); err != nil {
return nil, errors.Wrap(err, `获取托管资源信息`)
}
for _, item := range list {
for _, resource := range item.GetDelegatedResource() {
resources = append(resources, NewDelegatedResource(resource.From, resource.To, resource.FrozenBalanceForBandwidth, resource.FrozenBalanceForEnergy,
resource.ExpireTimeForBandwidth, resource.ExpireTimeForEnergy))
}
}
return resources, nil
}
func Base58ToAddress(s string) string {
s = strings.TrimPrefix(s, `0x`)
addr := address.HexToAddress(s)
return addr.String()
}