-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbridge.go
67 lines (53 loc) · 1.3 KB
/
bridge.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
package substrate
import (
"fmt"
"math/big"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/pkg/errors"
)
var (
errValidatorNotFound = fmt.Errorf("validator not found")
)
func (s *Substrate) IsValidator(identity Identity) (exists bool, err error) {
cl, meta, err := s.GetClient()
if err != nil {
return false, err
}
var validators []AccountID
key, err := types.CreateStorageKey(meta, "TFTBridgeModule", "Validators")
if err != nil {
err = errors.Wrap(err, "failed to create storage key")
return
}
ok, err := cl.RPC.State.GetStorageLatest(key, &validators)
if err != nil || !ok {
if !ok {
return false, errValidatorNotFound
}
return
}
exists = false
for _, validator := range validators {
if validator.String() == identity.Address() {
return true, nil
}
}
return
}
func (s *Substrate) SwapToStellar(identity Identity, targetStellarAddress string, amount big.Int) error {
cl, meta, err := s.GetClient()
if err != nil {
return err
}
c, err := types.NewCall(meta, "TFTBridgeModule.swap_to_stellar",
targetStellarAddress, types.NewU128(amount),
)
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 swap to stellar")
}
return nil
}