-
Notifications
You must be signed in to change notification settings - Fork 43
/
peggy_contract.go
154 lines (125 loc) · 3.79 KB
/
peggy_contract.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
package peggy
import (
"context"
"math"
"math/big"
"strings"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
"github.com/InjectiveLabs/metrics"
"github.com/InjectiveLabs/peggo/orchestrator/ethereum/committer"
"github.com/InjectiveLabs/peggo/orchestrator/ethereum/provider"
wrappers "github.com/InjectiveLabs/peggo/solidity/wrappers/Peggy.sol"
"github.com/InjectiveLabs/sdk-go/chain/peggy/types"
)
type PeggyContract interface {
committer.EVMCommitter
Address() common.Address
SendToCosmos(
ctx context.Context,
erc20 common.Address,
amount *big.Int,
cosmosAccAddress sdk.AccAddress,
senderAddress common.Address,
) (*common.Hash, error)
SendTransactionBatch(
ctx context.Context,
currentValset *types.Valset,
batch *types.OutgoingTxBatch,
confirms []*types.MsgConfirmBatch,
) (*common.Hash, error)
SendEthValsetUpdate(
ctx context.Context,
oldValset *types.Valset,
newValset *types.Valset,
confirms []*types.MsgValsetConfirm,
) (*common.Hash, error)
GetTxBatchNonce(
ctx context.Context,
erc20ContractAddress common.Address,
callerAddress common.Address,
) (*big.Int, error)
GetValsetNonce(
ctx context.Context,
callerAddress common.Address,
) (*big.Int, error)
GetPeggyID(
ctx context.Context,
callerAddress common.Address,
) (common.Hash, error)
GetERC20Symbol(
ctx context.Context,
erc20ContractAddress common.Address,
callerAddress common.Address,
) (symbol string, err error)
SubscribeToPendingTxs(
alchemyWebsocketURL string)
}
func NewPeggyContract(
ethCommitter committer.EVMCommitter,
peggyAddress common.Address,
pendingTxInputList PendingTxInputList,
pendingTxWaitDuration time.Duration,
) (PeggyContract, error) {
ethPeggy, err := wrappers.NewPeggy(peggyAddress, ethCommitter.Provider())
if err != nil {
return nil, err
}
svc := &peggyContract{
EVMCommitter: ethCommitter,
peggyAddress: peggyAddress,
ethPeggy: ethPeggy,
pendingTxInputList: pendingTxInputList,
pendingTxWaitDuration: pendingTxWaitDuration,
svcTags: metrics.Tags{
"svc": "peggy_contract",
},
}
return svc, nil
}
type peggyContract struct {
committer.EVMCommitter
ethProvider provider.EVMProvider
peggyAddress common.Address
ethPeggy *wrappers.Peggy
pendingTxInputList PendingTxInputList
pendingTxWaitDuration time.Duration
svcTags metrics.Tags
}
func (s *peggyContract) Address() common.Address {
return s.peggyAddress
}
// maxUintAllowance is uint constant MAX_UINT = 2**256 - 1
var maxUintAllowance = big.NewInt(0).Sub(big.NewInt(0).Exp(big.NewInt(2), big.NewInt(256), nil), big.NewInt(1))
var (
peggyABI, _ = abi.JSON(strings.NewReader(wrappers.PeggyABI))
erc20ABI, _ = abi.JSON(strings.NewReader(wrappers.ERC20ABI))
)
func sigToVRS(sigHex string) (v uint8, r, s common.Hash) {
signatureBytes := common.FromHex(sigHex)
vParam := signatureBytes[64]
if vParam == byte(0) {
vParam = byte(27)
} else if vParam == byte(1) {
vParam = byte(28)
}
v = vParam
r = common.BytesToHash(signatureBytes[0:32])
s = common.BytesToHash(signatureBytes[32:64])
return
}
// The total power in the Peggy bridge is normalized to u32 max every
// time a validator set is created. This value of up to u32 max is then
// stored in a i64 to prevent overflow during computation.
const totalPeggyPower int64 = math.MaxUint32
// peggyPowerToPercent takes in an amount of power in the peggy bridge, returns a percentage of total
func peggyPowerToPercent(total *big.Int) float32 {
d := decimal.NewFromBigInt(total, 0)
f, _ := d.Div(decimal.NewFromInt(totalPeggyPower)).Shift(2).Float64()
return float32(f)
}
var ErrInsufficientVotingPowerToPass = errors.New("insufficient voting power")