-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfil_msig_proposer_transactor.go
202 lines (169 loc) · 5.21 KB
/
fil_msig_proposer_transactor.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package walletutils
import (
"bytes"
"context"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/filecoin-project/go-address"
filbig "github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/builtin"
lapi "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/actors"
lotustypes "github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
multisig0 "github.com/filecoin-project/specs-actors/actors/builtin/multisig"
cbg "github.com/whyrusleeping/cbor-gen"
)
type EthClientShimFilMsigProposer struct {
EthClientShimMethods
from address.Address
api *lapi.FullNodeStruct
msig address.Address
signedMsgCache *SignedMessageCache
}
func NewFilMsigProposerWalletTransactor(
ctx context.Context,
api *lapi.FullNodeStruct,
client *ethclient.Client,
from address.Address,
fromPrivateKey []byte,
msig address.Address,
) (*EthClientShim, *bind.TransactOpts, error) {
shimImpl := &EthClientShimFilMsigProposer{
from: from,
api: api,
msig: msig,
signedMsgCache: NewSignedMsgCache(),
}
shimmedEthClient := &EthClientShim{
Client: client,
shim: shimImpl,
}
opts := &bind.TransactOpts{
From: common.Address{}, // unused
Signer: func(_ common.Address, tx *types.Transaction) (*types.Transaction, error) {
// get the address of the FEVM smart contract we want to send a msig proposal to
filecoinToAddr, err := ethtypes.ParseEthAddress(tx.To().String())
if err != nil {
return nil, err
}
// convert the 0x -> f4 for interacting with the lotus rpc
delegatedToAddr, err := filecoinToAddr.ToFilecoinAddress()
if err != nil {
return nil, err
}
var buffer bytes.Buffer
if err := cbg.WriteByteArray(&buffer, tx.Data()); err != nil {
return nil, err
}
calldata := buffer.Bytes()
var signedMsg *lotustypes.SignedMessage
// msig
enc, actErr := actors.SerializeParams(&multisig0.ProposeParams{
To: delegatedToAddr,
Value: filbig.NewFromGo(tx.Value()),
Method: builtin.MethodsEVM.InvokeContract,
Params: calldata,
})
if actErr != nil {
return nil, actErr
}
proposeMsg := &lotustypes.Message{
To: msig,
From: from,
Value: filbig.Zero(),
Method: builtin.MethodsMultisig.Propose,
Params: enc,
Nonce: tx.Nonce(),
GasLimit: int64(tx.Gas()),
GasFeeCap: filbig.NewFromGo(tx.GasFeeCap()),
GasPremium: filbig.NewFromGo(tx.GasTipCap()),
}
signedMsg, err = SignMsg(fromPrivateKey, proposeMsg)
if err != nil {
return tx, err
}
shimImpl.signedMsgCache.Add(tx.Hash(), signedMsg)
return tx, nil
},
Context: ctx,
}
return shimmedEthClient, opts, nil
}
func (c *EthClientShimFilMsigProposer) PendingNonceAt(ctx context.Context, _ common.Address) (uint64, error) {
nonce, err := c.api.MpoolGetNonce(ctx, c.from)
if err != nil {
return 0, err
}
return nonce, nil
}
func (c *EthClientShimFilMsigProposer) EstimateGas(ctx context.Context, call ethereum.CallMsg) (uint64, error) {
// call.To is the fevm smart contract we wish to transact with
filecoinToAddr, err := ethtypes.ParseEthAddress(call.To.String())
if err != nil {
return 0, err
}
// convert the 0x -> f4 for interacting with the lotus rpc
delegatedToAddr, err := filecoinToAddr.ToFilecoinAddress()
if err != nil {
return 0, err
}
var buffer bytes.Buffer
if err := cbg.WriteByteArray(&buffer, call.Data); err != nil {
return 0, err
}
calldata := buffer.Bytes()
var proposeMsg *lotustypes.Message
// serialize the inner msig proposal params
enc, actErr := actors.SerializeParams(&multisig0.ProposeParams{
To: delegatedToAddr,
Value: filbig.NewFromGo(call.Value),
Method: builtin.MethodsEVM.InvokeContract,
Params: calldata,
})
if actErr != nil {
return 0, actErr
}
proposeMsg = &lotustypes.Message{
To: c.msig,
From: c.from,
Value: filbig.Zero(),
Method: builtin.MethodsMultisig.Propose,
Params: enc,
}
msgWithGas, err := c.api.GasEstimateMessageGas(ctx, proposeMsg, nil, lotustypes.EmptyTSK)
if err != nil {
return 0, err
}
state, err := c.api.StateCall(ctx, msgWithGas, lotustypes.EmptyTSK)
if err != nil {
return 0, err
}
if err := recursiveSubcallErrorThrown(&state.ExecutionTrace); err != nil {
return 0, err
}
return uint64(msgWithGas.GasLimit), nil
}
func (c *EthClientShimFilMsigProposer) SendTransaction(ctx context.Context, tx *types.Transaction) error {
signedMessage := c.signedMsgCache.Get(tx.Hash())
c.signedMsgCache.Delete(tx.Hash())
// regular filecoin tx
cid, err := c.api.MpoolPush(ctx, signedMessage)
if err != nil {
return err
}
filtx, err := c.api.EthGetTransactionHashByCid(ctx, cid)
if err != nil {
return err
}
filTxHash := common.Hash{}
filTxHash.UnmarshalText([]byte(filtx.String()))
c.signedMsgCache.MapHash(tx.Hash(), filTxHash)
return nil
}
func (c *EthClientShimFilMsigProposer) GetOuterTxHash(innerHash common.Hash) common.Hash {
return c.signedMsgCache.GetOuterHash(innerHash)
}