-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfil_eoa_transactor.go
171 lines (143 loc) · 4.38 KB
/
fil_eoa_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
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"
lotustypes "github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
cbg "github.com/whyrusleeping/cbor-gen"
)
type EthClientShimFilEoa struct {
EthClientShimMethods
from address.Address
api *lapi.FullNodeStruct
signedMsgCache *SignedMessageCache
}
func NewFilEoaWalletTransactor(
ctx context.Context,
api *lapi.FullNodeStruct,
client *ethclient.Client,
from address.Address,
fromPrivateKey []byte,
passphrase string,
) (*EthClientShim, *bind.TransactOpts, error) {
shimImpl := &EthClientShimFilEoa{
from: from,
api: api,
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()
filMsg := &lotustypes.Message{
To: delegatedToAddr,
From: from,
Value: filbig.NewFromGo(tx.Value()),
Method: builtin.MethodsEVM.InvokeContract,
Params: calldata,
Nonce: tx.Nonce(),
GasLimit: int64(tx.Gas()),
GasFeeCap: filbig.NewFromGo(tx.GasFeeCap()),
GasPremium: filbig.NewFromGo(tx.GasTipCap()),
}
signedMsg, err := SignMsg(fromPrivateKey, filMsg)
if err != nil {
return tx, err
}
shimImpl.signedMsgCache.Add(tx.Hash(), signedMsg)
return tx, nil
},
Context: ctx,
}
return shimmedEthClient, opts, nil
}
func (c *EthClientShimFilEoa) 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 *EthClientShimFilEoa) 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()
msg := &lotustypes.Message{
To: delegatedToAddr,
From: c.from,
Value: filbig.NewFromGo(call.Value),
Method: builtin.MethodsEVM.InvokeContract,
Params: calldata,
}
msgWithGas, err := c.api.GasEstimateMessageGas(ctx, msg, 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 *EthClientShimFilEoa) 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 *EthClientShimFilEoa) GetOuterTxHash(innerHash common.Hash) common.Hash {
return c.signedMsgCache.GetOuterHash(innerHash)
}