-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathburning.go
126 lines (100 loc) · 3.13 KB
/
burning.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
package substrate
import (
"fmt"
"math/big"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/pkg/errors"
)
var (
ErrBurnTransactionNotFound = fmt.Errorf("burn tx not found")
ErrRefundTransactionNotFound = fmt.Errorf("refund tx not found")
ErrFailedToDecode = fmt.Errorf("failed to decode events, skipping")
)
type BurnTransaction struct {
Block BlockNumber `json:"block"`
Amount types.U64 `json:"amount"`
Source types.OptionAccountID `json:"source"`
Target string `json:"target"`
Signatures []StellarSignature `json:"signatures"`
SequenceNumber types.U64 `json:"sequence_number"`
}
func (s *Substrate) ProposeBurnTransactionOrAddSig(identity Identity, txID uint64, target string, amount *big.Int, signature string, stellarAddress string, sequence_number uint64) error {
cl, meta, err := s.GetClient()
if err != nil {
return err
}
c, err := types.NewCall(meta, "TFTBridgeModule.propose_burn_transaction_or_add_sig",
txID, target, types.U64(amount.Uint64()), signature, stellarAddress, sequence_number,
)
if err != nil {
return errors.Wrap(err, "failed to create call")
}
_, err = s.Call(cl, meta, identity, c)
if err != nil {
return errors.Wrap(err, "failed to propose burn transaction")
}
return nil
}
func (s *Substrate) SetBurnTransactionExecuted(identity Identity, txID uint64) error {
cl, meta, err := s.GetClient()
if err != nil {
return err
}
c, err := types.NewCall(meta, "TFTBridgeModule.set_burn_transaction_executed", txID)
if err != nil {
return errors.Wrap(err, "failed to create call")
}
_, err = s.Call(cl, meta, identity, c)
if err != nil {
return errors.Wrap(err, "failed to set burn transaction executed")
}
return nil
}
func (s *Substrate) GetBurnTransaction(burnTransactionID types.U64) (*BurnTransaction, error) {
cl, meta, err := s.GetClient()
if err != nil {
return nil, err
}
bytes, err := Encode(burnTransactionID)
if err != nil {
return nil, errors.Wrap(err, "substrate: encoding error building query arguments")
}
var burnTx BurnTransaction
key, err := types.CreateStorageKey(meta, "TFTBridgeModule", "BurnTransactions", bytes, nil)
if err != nil {
err = errors.Wrap(err, "failed to create storage key")
return nil, err
}
ok, err := cl.RPC.State.GetStorageLatest(key, &burnTx)
if err != nil {
return nil, err
}
if !ok {
return nil, ErrBurnTransactionNotFound
}
return &burnTx, nil
}
func (s *Substrate) IsBurnedAlready(burnTransactionID types.U64) (exists bool, err error) {
cl, meta, err := s.GetClient()
if err != nil {
return false, err
}
bytes, err := Encode(burnTransactionID)
if err != nil {
return false, errors.Wrap(err, "substrate: encoding error building query arguments")
}
var burnTx BurnTransaction
key, err := types.CreateStorageKey(meta, "TFTBridgeModule", "ExecutedBurnTransactions", bytes, nil)
if err != nil {
err = errors.Wrap(err, "failed to create storage key")
return
}
ok, err := cl.RPC.State.GetStorageLatest(key, &burnTx)
if err != nil {
return false, err
}
if !ok {
return false, nil
}
return true, nil
}