-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathutils.go
More file actions
120 lines (105 loc) · 3.96 KB
/
utils.go
File metadata and controls
120 lines (105 loc) · 3.96 KB
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
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"
v3 "github.com/0xsequence/go-sequence/core/v3"
)
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,
})
if err != nil {
return common.Address{}, nil, nil, err
}
signedDeployTx, err := sender.SignTransaction(deployTx, chainID)
if err != nil {
return common.Address{}, nil, nil, err
}
tx, waitReceipt, err := sender.SendTransaction(context.Background(), signedDeployTx)
if err != nil {
return common.Address{}, nil, nil, err
}
return walletAddress, tx, waitReceipt, nil
}
func EncodeWalletDeployment(walletConfig core.WalletConfig, walletContext WalletContext) (common.Address, common.Address, []byte, error) {
imageHash := walletConfig.ImageHash()
return EncodeWalletDeploymentWithImageHash(walletConfig, walletContext, imageHash)
}
func EncodeWalletDeploymentWithImageHash(walletConfig core.WalletConfig, walletContext WalletContext, imageHash core.ImageHash) (common.Address, common.Address, []byte, error) {
address, err := AddressFromImageHash(imageHash, walletContext)
if err != nil {
return common.Address{}, common.Address{}, nil, err
}
if _, ok := walletConfig.(*v1.WalletConfig); ok {
deployData, err := contracts.V1.WalletFactory.ABI.Pack("deploy", walletContext.MainModuleAddress, imageHash.Hash)
if err != nil {
return common.Address{}, common.Address{}, nil, err
}
return address, walletContext.FactoryAddress, deployData, nil
} else if _, ok := walletConfig.(*v2.WalletConfig); ok {
deployData, err := contracts.V2.WalletFactory.ABI.Pack("deploy", walletContext.MainModuleAddress, imageHash.Hash)
if err != nil {
return common.Address{}, common.Address{}, nil, err
}
return address, walletContext.FactoryAddress, deployData, nil
} else if _, ok := walletConfig.(*v3.WalletConfig); ok {
deployData, err := contracts.V3.WalletFactory.ABI.Pack("deploy", walletContext.MainModuleAddress, imageHash.Hash)
if err != nil {
return common.Address{}, common.Address{}, nil, err
}
return address, 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, _, err = V2DecodeTxFailedEvent(log)
}
if err != nil {
var reasonErr error
_, _, reasonErr, err = V3DecodeCallFailedEvent(log)
reason = fmt.Sprint(reasonErr)
}
if err != nil {
var reasonErr error
_, _, reasonErr, err = V3DecodeCallAbortedEvent(log)
reason = fmt.Sprint(reasonErr)
}
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)
}