-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathrelayer.go
More file actions
143 lines (113 loc) · 4.29 KB
/
relayer.go
File metadata and controls
143 lines (113 loc) · 4.29 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package sequence
import (
"context"
"fmt"
"math/big"
"time"
"github.com/0xsequence/ethkit/ethrpc"
"github.com/0xsequence/ethkit/ethtxn"
"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"
"github.com/0xsequence/go-sequence/lib/simulator"
"github.com/0xsequence/go-sequence/relayer/proto"
)
type SimulateResult struct {
simulator.Result
GasLimit uint64
}
type RelayerFeeTokenType uint32
const (
UNKNOWN RelayerFeeTokenType = iota
ERC20_TOKEN
ERC1155_TOKEN
)
type RelayerFeeToken struct {
ChainID *big.Int
Name string
Symbol string
Type RelayerFeeTokenType
Decimals *uint32
LogoURL string
ContractAddress *common.Address
TokenID *big.Int
}
type RelayerFeeOption struct {
Token RelayerFeeToken
To common.Address
Value *big.Int
GasLimit *big.Int
}
type RelayerFeeQuote string
type Relayer interface {
// ..
GetProvider() *ethrpc.Provider
Simulate(ctx context.Context, wallet common.Address, transactions Transactions) ([]*SimulateResult, error)
// NOTE: nonce space is 160 bits wide
GetNonce(ctx context.Context, walletConfig core.WalletConfig, walletContext WalletContext, space *big.Int, blockNum *big.Int) (*big.Int, error)
// Relay will submit the Sequence signed meta transaction to the relayer. The method will block until the relayer
// responds with the native transaction hash (*types.Transaction), which means the relayer has submitted the transaction
// request to the network. Clients can use WaitReceipt to wait until the metaTxnID has been mined.
Relay(ctx context.Context, signedTxs *SignedTransactions, quote ...*RelayerFeeQuote) (MetaTxnID, *types.Transaction, ethtxn.WaitReceipt, error)
//
FeeOptions(ctx context.Context, signedTxs *SignedTransactions) ([]*RelayerFeeOption, *RelayerFeeQuote, error)
// ..
Wait(ctx context.Context, metaTxnID MetaTxnID, optTimeout ...time.Duration) (MetaTxnStatus, *types.Receipt, error)
// ..
Client() proto.RelayerClient
// TODO: consider removing this altogether and moving it to
// the services/relayer package, or alternatively move some of the types
// from this file to services/relayer.go and remove dependency of 'sequence'
// package from the 'relayer' package.
}
type MetaTxnID string
func (id MetaTxnID) String() string {
return string(id)
}
type MetaTxnStatus uint8
const (
MetaTxnStatusUnknown MetaTxnStatus = iota
MetaTxnExecuted
MetaTxnFailed
MetaTxnReverted
)
// returns `to` address (either guest or wallet) and `data` of signed-metatx-calldata, aka execdata
func EncodeTransactionsForRelaying(relayer Relayer, walletAddress common.Address, walletConfig core.WalletConfig, walletContext WalletContext, txns Transactions, nonce *big.Int, seqSig []byte) (common.Address, []byte, error) {
// TODO/NOTE: first version, we assume the wallet is deployed, then we can add bundlecreation after.
// .....
if len(txns) == 0 {
return common.Address{}, nil, fmt.Errorf("cannot encode empty transactions")
}
// Encode transaction to be sent to a deployed wallet
var err error
if walletAddress == (common.Address{}) {
walletAddress, err = AddressFromWalletConfig(walletConfig, walletContext)
if err != nil {
return common.Address{}, nil, err
}
}
encodedTxns, err := txns.EncodedTransactions()
if err != nil {
return common.Address{}, nil, err
}
execdata, err := contracts.V1.WalletMainModule.Encode("execute", encodedTxns, nonce, seqSig)
if err != nil {
return common.Address{}, nil, err
}
return walletAddress, execdata, nil
}
func EncodeTransactionsForRelayingV3(relayer Relayer, walletAddress common.Address, chainID *big.Int, walletConfig core.WalletConfig, walletContext WalletContext, txns Transactions, space *big.Int, nonce *big.Int, seqSig []byte) (common.Address, []byte, error) {
if len(txns) == 0 {
return common.Address{}, nil, fmt.Errorf("cannot encode empty transactions")
}
payload, err := txns.Payload(walletAddress, chainID, space, nonce)
if err != nil {
return common.Address{}, nil, err
}
execdata, err := contracts.V3.WalletStage1Module.Encode("execute", payload.Encode(walletAddress), seqSig)
if err != nil {
return common.Address{}, nil, err
}
return walletAddress, execdata, nil
}