-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.go
96 lines (80 loc) · 3.06 KB
/
utils.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
package sequence
import (
"context"
"fmt"
"math/big"
"github.com/0xsequence/ethkit/ethtxn"
"github.com/0xsequence/ethkit/ethwallet"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/0xsequence/ethkit/go-ethereum/core/types"
"github.com/0xsequence/go-sequence/contracts"
"github.com/0xsequence/go-sequence/core"
v1 "github.com/0xsequence/go-sequence/core/v1"
v2 "github.com/0xsequence/go-sequence/core/v2"
)
var zeroAddress = common.Address{}
func DeploySequenceWallet(sender *ethwallet.Wallet, walletConfig core.WalletConfig, walletContext WalletContext) (common.Address, *types.Transaction, ethtxn.WaitReceipt, error) {
if sender.GetProvider() == nil {
return common.Address{}, nil, nil, ErrProviderNotSet
}
provider := sender.GetProvider()
chainID, err := provider.ChainID(context.Background())
if err != nil {
return common.Address{}, nil, nil, err
}
walletAddress, _, deployData, err := EncodeWalletDeployment(walletConfig, walletContext)
if err != nil {
return common.Address{}, nil, nil, err
}
deployTx, err := sender.NewTransaction(context.Background(), ðtxn.TransactionRequest{
To: &walletContext.FactoryAddress,
Data: deployData,
// TODO: Move this hardcoded gas limit to a configuration
// or fix it with a contract patch
GasLimit: 131072,
})
signedDeployTx, err := sender.SignTx(deployTx, chainID)
if err != nil {
return common.Address{}, nil, nil, err
}
tx, waitReceipt, err := sender.SendTransaction(context.Background(), signedDeployTx)
return walletAddress, tx, waitReceipt, nil
}
func EncodeWalletDeployment(walletConfig core.WalletConfig, walletContext WalletContext) (common.Address, common.Address, []byte, error) {
walletImageHash := walletConfig.ImageHash().Hex()
walletAddress, err := AddressFromImageHash(walletImageHash, walletContext)
if err != nil {
return common.Address{}, common.Address{}, nil, err
}
if _, ok := walletConfig.(*v1.WalletConfig); ok {
deployData, err := contracts.WalletFactory.ABI.Pack("deploy", walletContext.MainModuleAddress, common.HexToHash(walletImageHash))
if err != nil {
return common.Address{}, common.Address{}, nil, err
}
return walletAddress, walletContext.FactoryAddress, deployData, nil
} else if _, ok := walletConfig.(*v2.WalletConfig); ok {
deployData, err := contracts.V2.WalletFactory.ABI.Pack("deploy", walletContext.MainModuleAddress, common.HexToHash(walletImageHash))
if err != nil {
return common.Address{}, common.Address{}, nil, err
}
return walletAddress, walletContext.FactoryAddress, deployData, nil
}
return common.Address{}, common.Address{}, nil, fmt.Errorf("unsupported wallet config version")
}
func DecodeRevertReason(logs []*types.Log) []string {
reasons := []string{}
for _, log := range logs {
_, reason, err := V1DecodeTxFailedEvent(log)
if err != nil {
_, reason, _, _ = V2DecodeTxFailedEvent(log)
}
reasons = append(reasons, reason)
}
return reasons
}
func ParseHexOrDec(s string) (*big.Int, bool) {
if len(s) > 2 && s[0:2] == "0x" {
return new(big.Int).SetString(s[2:], 16)
}
return new(big.Int).SetString(s, 10)
}