Skip to content

Commit

Permalink
Add support for executeMetaTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-maj committed Nov 17, 2023
1 parent f7887da commit 907e988
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 12 deletions.
16 changes: 16 additions & 0 deletions src/constants/relayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,19 @@ export const ERC20PermitAbi = [
type: "function",
},
];

export const NativeMetaTransaction = [
{
inputs: [
{ internalType: "address", name: "userAddress", type: "address" },
{ internalType: "bytes", name: "functionSignature", type: "bytes" },
{ internalType: "bytes32", name: "sigR", type: "bytes32" },
{ internalType: "bytes32", name: "sigS", type: "bytes32" },
{ internalType: "uint8", name: "sigV", type: "uint8" },
],
name: "executeMetaTransaction",
outputs: [{ internalType: "bytes", name: "", type: "bytes" }],
stateMutability: "payable",
type: "function",
},
];
2 changes: 1 addition & 1 deletion src/schema/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export type ContractExtension =
| "deploy-published"
| "account-factory"
| "account"
| "forwarder";
| "relayer";
83 changes: 72 additions & 11 deletions src/server/routes/relayer/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Static, Type } from "@sinclair/typebox";
import { ethers } from "ethers";
import { ethers, utils } from "ethers";
import { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import {
ERC20PermitAbi,
ERC2771ContextAbi,
ForwarderAbi,
NativeMetaTransaction,
} from "../../../constants/relayer";
import { getRelayerById } from "../../../db/relayer/getRelayerById";
import { queueTx } from "../../../db/transactions/queueTx";
Expand Down Expand Up @@ -42,10 +43,17 @@ const BodySchema = Type.Union([
value: Type.String(),
nonce: Type.String(),
deadline: Type.String(),
r: Type.String(),
s: Type.String(),
v: Type.String(),
}),
signature: Type.String(),
}),
Type.Object({
type: Type.Literal("execute-meta-transaction"),
request: Type.Object({
from: Type.String(),
to: Type.String(),
data: Type.String(),
}),
signature: Type.String(),
}),
]);

Expand Down Expand Up @@ -105,9 +113,62 @@ export async function relayTransaction(fastify: FastifyInstance) {
walletAddress: relayer.backendWalletAddress,
});

if (req.body.type === "permit") {
if (req.body.type === "execute-meta-transaction") {
// Polygon Execute Meta Transaction
const { request, signature } = req.body;
const { v, r, s } = utils.splitSignature(signature);

if (
relayer.allowedContracts &&
!relayer.allowedContracts.includes(request.to.toLowerCase())
) {
return res.status(400).send({
error: {
message: `Requesting to relay transaction to unauthorized contract ${request.to}.`,
},
});
}

const target = await sdk.getContractFromAbi(
request.to.toLowerCase(),
NativeMetaTransaction,
);

const tx = await target.prepare("executeMetaTransaction", [
request.from,
request.data,
r,
s,
v,
]);

const queueId = await queueTx({
tx,
chainId: relayer.chainId,
extension: "relayer",
});

res.status(200).send({
result: {
queueId,
},
});
return;
} else if (req.body.type === "permit") {
// EIP-2612
const { request } = req.body;
const { request, signature } = req.body;
const { v, r, s } = utils.splitSignature(signature);

if (
relayer.allowedContracts &&
!relayer.allowedContracts.includes(request.to.toLowerCase())
) {
return res.status(400).send({
error: {
message: `Requesting to relay transaction to unauthorized contract ${request.to}.`,
},
});
}

const target = await sdk.getContractFromAbi(
request.to.toLowerCase(),
Expand All @@ -119,15 +180,15 @@ export async function relayTransaction(fastify: FastifyInstance) {
request.spender,
request.value,
request.deadline,
request.v,
request.r,
request.s,
v,
r,
s,
]);

const queueId = await queueTx({
tx,
chainId: relayer.chainId,
extension: "forwarder",
extension: "relayer",
});

res.status(200).send({
Expand Down Expand Up @@ -212,7 +273,7 @@ export async function relayTransaction(fastify: FastifyInstance) {
const queueId = await queueTx({
tx,
chainId: relayer.chainId,
extension: "forwarder",
extension: "relayer",
});

res.status(200).send({
Expand Down

0 comments on commit 907e988

Please sign in to comment.