forked from fbsobreira/gotron-sdk
/
bank.go
81 lines (66 loc) · 2.01 KB
/
bank.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
package client
import (
"context"
"fmt"
"github.com/atong007/gotron-sdk/pkg/common"
"github.com/atong007/gotron-sdk/pkg/proto/api"
"github.com/atong007/gotron-sdk/pkg/proto/core"
"github.com/golang/protobuf/proto"
)
// FreezeBalance from base58 address
func (g *GrpcClient) FreezeBalance(from, delegateTo string,
resource core.ResourceCode, frozenBalance int64) (*api.TransactionExtention, error) {
var err error
contract := &core.FreezeBalanceContract{}
if contract.OwnerAddress, err = common.DecodeCheck(from); err != nil {
return nil, err
}
contract.FrozenBalance = frozenBalance
contract.FrozenDuration = 3 // Tron Only allows 3 days freeze
if len(delegateTo) > 0 {
if contract.ReceiverAddress, err = common.DecodeCheck(delegateTo); err != nil {
return nil, err
}
}
contract.Resource = resource
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()
tx, err := g.Client.FreezeBalance2(ctx, contract)
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
}
return tx, nil
}
// UnfreezeBalance from base58 address
func (g *GrpcClient) UnfreezeBalance(from, delegateTo string, resource core.ResourceCode) (*api.TransactionExtention, error) {
var err error
contract := &core.UnfreezeBalanceContract{}
if contract.OwnerAddress, err = common.DecodeCheck(from); err != nil {
return nil, err
}
if len(delegateTo) > 0 {
if contract.ReceiverAddress, err = common.DecodeCheck(delegateTo); err != nil {
return nil, err
}
}
contract.Resource = resource
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()
tx, err := g.Client.UnfreezeBalance2(ctx, contract)
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
}
return tx, nil
}