-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrelayer.go
130 lines (103 loc) · 3.57 KB
/
relayer.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
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/relayer/proto"
)
type RelayerSimulateResult struct {
Executed bool
Succeeded bool
Result *string
Reason *string
GasUsed uint
GasLimit uint
}
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
// ..
EstimateGasLimits(ctx context.Context, walletConfig core.WalletConfig, walletContext WalletContext, txns Transactions) (Transactions, error)
// ..
Simulate(ctx context.Context, txs *SignedTransactions) ([]*RelayerSimulateResult, 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.Relayer
// TODO, in future when needed..
// GasRefundOptions()
}
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.WalletMainModule.Encode("execute", encodedTxns, nonce, seqSig)
if err != nil {
return common.Address{}, nil, err
}
return walletAddress, execdata, nil
}