1
1
import { Static , Type } from "@sinclair/typebox" ;
2
- import { ethers } from "ethers" ;
2
+ import { ethers , utils } from "ethers" ;
3
3
import { FastifyInstance } from "fastify" ;
4
4
import { StatusCodes } from "http-status-codes" ;
5
5
import {
6
6
ERC20PermitAbi ,
7
7
ERC2771ContextAbi ,
8
8
ForwarderAbi ,
9
+ NativeMetaTransaction ,
9
10
} from "../../../constants/relayer" ;
10
11
import { getRelayerById } from "../../../db/relayer/getRelayerById" ;
11
12
import { queueTx } from "../../../db/transactions/queueTx" ;
@@ -42,10 +43,17 @@ const BodySchema = Type.Union([
42
43
value : Type . String ( ) ,
43
44
nonce : Type . String ( ) ,
44
45
deadline : Type . String ( ) ,
45
- r : Type . String ( ) ,
46
- s : Type . String ( ) ,
47
- v : Type . String ( ) ,
48
46
} ) ,
47
+ signature : Type . String ( ) ,
48
+ } ) ,
49
+ Type . Object ( {
50
+ type : Type . Literal ( "execute-meta-transaction" ) ,
51
+ request : Type . Object ( {
52
+ from : Type . String ( ) ,
53
+ to : Type . String ( ) ,
54
+ data : Type . String ( ) ,
55
+ } ) ,
56
+ signature : Type . String ( ) ,
49
57
} ) ,
50
58
] ) ;
51
59
@@ -105,9 +113,62 @@ export async function relayTransaction(fastify: FastifyInstance) {
105
113
walletAddress : relayer . backendWalletAddress ,
106
114
} ) ;
107
115
108
- if ( req . body . type === "permit" ) {
116
+ if ( req . body . type === "execute-meta-transaction" ) {
117
+ // Polygon Execute Meta Transaction
118
+ const { request, signature } = req . body ;
119
+ const { v, r, s } = utils . splitSignature ( signature ) ;
120
+
121
+ if (
122
+ relayer . allowedContracts &&
123
+ ! relayer . allowedContracts . includes ( request . to . toLowerCase ( ) )
124
+ ) {
125
+ return res . status ( 400 ) . send ( {
126
+ error : {
127
+ message : `Requesting to relay transaction to unauthorized contract ${ request . to } .` ,
128
+ } ,
129
+ } ) ;
130
+ }
131
+
132
+ const target = await sdk . getContractFromAbi (
133
+ request . to . toLowerCase ( ) ,
134
+ NativeMetaTransaction ,
135
+ ) ;
136
+
137
+ const tx = await target . prepare ( "executeMetaTransaction" , [
138
+ request . from ,
139
+ request . data ,
140
+ r ,
141
+ s ,
142
+ v ,
143
+ ] ) ;
144
+
145
+ const queueId = await queueTx ( {
146
+ tx,
147
+ chainId : relayer . chainId ,
148
+ extension : "relayer" ,
149
+ } ) ;
150
+
151
+ res . status ( 200 ) . send ( {
152
+ result : {
153
+ queueId,
154
+ } ,
155
+ } ) ;
156
+ return ;
157
+ } else if ( req . body . type === "permit" ) {
109
158
// EIP-2612
110
- const { request } = req . body ;
159
+ const { request, signature } = req . body ;
160
+ const { v, r, s } = utils . splitSignature ( signature ) ;
161
+
162
+ if (
163
+ relayer . allowedContracts &&
164
+ ! relayer . allowedContracts . includes ( request . to . toLowerCase ( ) )
165
+ ) {
166
+ return res . status ( 400 ) . send ( {
167
+ error : {
168
+ message : `Requesting to relay transaction to unauthorized contract ${ request . to } .` ,
169
+ } ,
170
+ } ) ;
171
+ }
111
172
112
173
const target = await sdk . getContractFromAbi (
113
174
request . to . toLowerCase ( ) ,
@@ -119,15 +180,15 @@ export async function relayTransaction(fastify: FastifyInstance) {
119
180
request . spender ,
120
181
request . value ,
121
182
request . deadline ,
122
- request . v ,
123
- request . r ,
124
- request . s ,
183
+ v ,
184
+ r ,
185
+ s ,
125
186
] ) ;
126
187
127
188
const queueId = await queueTx ( {
128
189
tx,
129
190
chainId : relayer . chainId ,
130
- extension : "forwarder " ,
191
+ extension : "relayer " ,
131
192
} ) ;
132
193
133
194
res . status ( 200 ) . send ( {
@@ -212,7 +273,7 @@ export async function relayTransaction(fastify: FastifyInstance) {
212
273
const queueId = await queueTx ( {
213
274
tx,
214
275
chainId : relayer . chainId ,
215
- extension : "forwarder " ,
276
+ extension : "relayer " ,
216
277
} ) ;
217
278
218
279
res . status ( 200 ) . send ( {
0 commit comments