Skip to content

Commit

Permalink
feat: optional sponsor api key for callWithSyncFee(ERC2771) (#23)
Browse files Browse the repository at this point in the history
* feat: optional sponsor api key for callWithSyncFee(ERC2771)

* fix: gasLimit as string in API request

* v5.3.0
  • Loading branch information
opatavi authored Sep 21, 2023
1 parent ba09a76 commit b3a620c
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gelatonetwork/relay-sdk",
"version": "5.2.0",
"version": "5.3.0",
"description": "SDK to integrate with Gelato Relay",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
16 changes: 12 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,22 @@ export class GelatoRelay {
/**
* @param {CallWithSyncFeeRequest} request - CallWithSyncFee request to be relayed by Gelato Executors
* @param {RelayRequestOptions} [options] - Optional Relay configuration
* @param {string} [sponsorApiKey] Optional Sponsor API key to be used for the call
* @returns {Promise<RelayResponse>} Response object with taskId parameter
*
*/
callWithSyncFee = (
request: CallWithSyncFeeRequest,
options?: RelayRequestOptions
options?: RelayRequestOptions,
sponsorApiKey?: string
): Promise<RelayResponse> =>
library.relayWithSyncFee({ request, options }, this.#config);
library.relayWithSyncFee({ request, sponsorApiKey, options }, this.#config);

/**
* @param {CallWithSyncFeeERC2771Request | CallWithSyncFeeConcurrentERC2771Request} request - Call with sync fee: Sequential ERC2771 or Concurrent ERC2771 request to be relayed by Gelato Executors
* @param {ethers.BrowserProvider | ethers.Wallet} walletOrProvider - BrowserProvider [front-end] or Wallet [back-end] to sign the payload
* @param {RelayRequestOptions} [options] - Optional Relay configuration
* @param {string} [sponsorApiKey] Optional Sponsor API key to be used for the call
* @returns {Promise<RelayResponse>} Response object with taskId parameter
*
*/
Expand All @@ -116,12 +119,14 @@ export class GelatoRelay {
| CallWithSyncFeeERC2771Request
| CallWithSyncFeeConcurrentERC2771Request,
walletOrProvider: ethers.BrowserProvider | ethers.Wallet,
options?: RelayRequestOptions
options?: RelayRequestOptions,
sponsorApiKey?: string
): Promise<RelayResponse> =>
library.relayWithCallWithSyncFeeERC2771(
{
request,
walletOrProvider,
sponsorApiKey,
options,
},
this.#config
Expand Down Expand Up @@ -231,21 +236,24 @@ export class GelatoRelay {
* @param {BaseCallWithSyncFeeParams} syncFeeParams - Call with Sync Fee parameters
* @param {SignatureData["signature"]} signature - Signature that can be obtained from getSignatureDataERC2771
* @param {RelayRequestOptions} [options] - Optional Relay configuration
* @param {string} [sponsorApiKey] Optional Sponsor API key to be used for the call
* @returns {Promise<RelayResponse>} Response object with taskId parameter
*
*/
callWithSyncFeeERC2771WithSignature = (
struct: SignatureData["struct"],
syncFeeParams: BaseCallWithSyncFeeParams,
signature: SignatureData["signature"],
options?: RelayRequestOptions
options?: RelayRequestOptions,
sponsorApiKey?: string
): Promise<RelayResponse> =>
library.callWithSyncFeeERC2771WithSignature(
{
struct,
syncFeeParams,
signature,
options,
sponsorApiKey,
},
this.#config
);
Expand Down
13 changes: 10 additions & 3 deletions src/lib/callWithSyncFee/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { getHttpErrorMessage, post } from "../../utils";
import { isNetworkSupported } from "../network";
import {
ApiKey,
Config,
Optional,
RelayCall,
RelayRequestOptions,
RelayResponse,
Expand All @@ -12,12 +14,13 @@ import { CallWithSyncFeeRequest } from "./types";
export const relayWithSyncFee = async (
payload: {
request: CallWithSyncFeeRequest;
sponsorApiKey?: string;
options?: RelayRequestOptions;
},
config: Config
): Promise<RelayResponse> => {
try {
const { request, options } = payload;
const { request, options, sponsorApiKey } = payload;
const isSupported = await isNetworkSupported(
{ chainId: request.chainId },
config
Expand All @@ -26,16 +29,20 @@ export const relayWithSyncFee = async (
throw new Error(`Chain id [${request.chainId}] is not supported`);
}
return await post<
CallWithSyncFeeRequest & RelayRequestOptions,
CallWithSyncFeeRequest &
RelayRequestOptions &
Optional<ApiKey, "sponsorApiKey">,
RelayResponse
>(
{
relayCall: RelayCall.CallWithSyncFee,
request: {
...request,
isRelayContext: request.isRelayContext ?? true,
...options,
sponsorApiKey,
chainId: request.chainId.toString(),
gasLimit: options?.gasLimit ? options.gasLimit.toString() : undefined,
retries: options?.retries,
},
},
config
Expand Down
19 changes: 16 additions & 3 deletions src/lib/erc2771/callWithSyncFeeERC2771/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { ethers } from "ethers";

import { post } from "../../../utils";
import {
ApiKey,
BaseCallWithSyncFeeParams,
ConcurrencyOptions,
Config,
Optional,
RelayCall,
RelayRequestOptions,
RelayResponse,
Expand All @@ -26,12 +28,13 @@ export const relayWithCallWithSyncFeeERC2771 = async (
| CallWithSyncFeeERC2771Request
| CallWithSyncFeeConcurrentERC2771Request;
walletOrProvider: ethers.BrowserProvider | ethers.Wallet;
sponsorApiKey?: string;
options?: RelayRequestOptions;
},
config: Config
): Promise<RelayResponse> => {
try {
const { request, walletOrProvider, options } = payload;
const { request, walletOrProvider, options, sponsorApiKey } = payload;
if (!walletOrProvider.provider) {
throw new Error(`Missing provider`);
}
Expand All @@ -51,18 +54,23 @@ export const relayWithCallWithSyncFeeERC2771 = async (
BaseCallWithSyncFeeParams &
RelayRequestOptions &
UserAuthSignature &
Optional<ApiKey, "sponsorApiKey"> &
ConcurrencyOptions,
RelayResponse
>(
{
relayCall: RelayCall.CallWithSyncFeeERC2771,
request: {
...safeTransformStruct(struct),
...options,
feeToken,
isRelayContext: isRelayContext ?? true,
userSignature: signature,
isConcurrent,
sponsorApiKey,
gasLimit: options?.gasLimit
? options.gasLimit.toString()
: undefined,
retries: options?.retries,
},
},
config
Expand All @@ -82,18 +90,23 @@ export const relayWithCallWithSyncFeeERC2771 = async (
BaseCallWithSyncFeeParams &
RelayRequestOptions &
UserAuthSignature &
Optional<ApiKey, "sponsorApiKey"> &
ConcurrencyOptions,
RelayResponse
>(
{
relayCall: RelayCall.CallWithSyncFeeERC2771,
request: {
...safeTransformStruct(struct),
...options,
feeToken,
isRelayContext: isRelayContext ?? true,
userSignature: signature,
isConcurrent,
sponsorApiKey,
gasLimit: options?.gasLimit
? options.gasLimit.toString()
: undefined,
retries: options?.retries,
},
},
config
Expand Down
20 changes: 17 additions & 3 deletions src/lib/erc2771/callWithSyncFeeERC2771WithSignature/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { isConcurrentStruct, post } from "../../../utils";
import { isNetworkSupported } from "../../network";
import {
ApiKey,
BaseCallWithSyncFeeParams,
ConcurrencyOptions,
Config,
Optional,
RelayCall,
RelayRequestOptions,
RelayResponse,
Expand All @@ -21,11 +23,13 @@ export const callWithSyncFeeERC2771WithSignature = async (
syncFeeParams: BaseCallWithSyncFeeParams;
signature: string;
options?: RelayRequestOptions;
sponsorApiKey?: string;
},
config: Config
): Promise<RelayResponse> => {
try {
const { signature, struct, syncFeeParams, options } = payload;
const { signature, struct, syncFeeParams, options, sponsorApiKey } =
payload;

const isSupported = await isNetworkSupported(
{ chainId: struct.chainId },
Expand All @@ -42,6 +46,7 @@ export const callWithSyncFeeERC2771WithSignature = async (
BaseCallWithSyncFeeParams &
RelayRequestOptions &
UserAuthSignature &
Optional<ApiKey, "sponsorApiKey"> &
ConcurrencyOptions,
RelayResponse
>(
Expand All @@ -50,10 +55,14 @@ export const callWithSyncFeeERC2771WithSignature = async (
request: {
...safeTransformStruct(struct),
...syncFeeParams,
...options,
isRelayContext: syncFeeParams.isRelayContext ?? true,
userSignature: signature,
isConcurrent,
sponsorApiKey,
gasLimit: options?.gasLimit
? options.gasLimit.toString()
: undefined,
retries: options?.retries,
},
},
config
Expand All @@ -65,6 +74,7 @@ export const callWithSyncFeeERC2771WithSignature = async (
BaseCallWithSyncFeeParams &
RelayRequestOptions &
UserAuthSignature &
Optional<ApiKey, "sponsorApiKey"> &
ConcurrencyOptions,
RelayResponse
>(
Expand All @@ -73,10 +83,14 @@ export const callWithSyncFeeERC2771WithSignature = async (
request: {
...safeTransformStruct(struct),
...syncFeeParams,
...options,
isRelayContext: syncFeeParams.isRelayContext ?? true,
userSignature: signature,
isConcurrent,
sponsorApiKey,
gasLimit: options?.gasLimit
? options.gasLimit.toString()
: undefined,
retries: options?.retries,
},
},
config
Expand Down
10 changes: 8 additions & 2 deletions src/lib/erc2771/sponsoredCallERC2771/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ const sponsoredCallERC2771 = async (
relayCall: RelayCall.SponsoredCallERC2771,
request: {
...safeTransformStruct(struct),
...options,
userSignature: signature,
sponsorApiKey,
isConcurrent,
gasLimit: options?.gasLimit
? options.gasLimit.toString()
: undefined,
retries: options?.retries,
},
},
config
Expand Down Expand Up @@ -105,10 +108,13 @@ const sponsoredCallERC2771 = async (
relayCall: RelayCall.SponsoredCallERC2771,
request: {
...safeTransformStruct(struct),
...options,
userSignature: signature,
sponsorApiKey,
isConcurrent,
gasLimit: options?.gasLimit
? options.gasLimit.toString()
: undefined,
retries: options?.retries,
},
},
config
Expand Down
10 changes: 8 additions & 2 deletions src/lib/erc2771/sponsoredCallERC2771WithSignature/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ export const sponsoredCallERC2771WithSignature = async (
relayCall: RelayCall.SponsoredCallERC2771,
request: {
...safeTransformStruct(struct),
...options,
userSignature: signature,
sponsorApiKey,
isConcurrent,
gasLimit: options?.gasLimit
? options.gasLimit.toString()
: undefined,
retries: options?.retries,
},
},
config
Expand All @@ -71,10 +74,13 @@ export const sponsoredCallERC2771WithSignature = async (
relayCall: RelayCall.SponsoredCallERC2771,
request: {
...safeTransformStruct(struct),
...options,
userSignature: signature,
sponsorApiKey,
isConcurrent,
gasLimit: options?.gasLimit
? options.gasLimit.toString()
: undefined,
retries: options?.retries,
},
},
config
Expand Down
3 changes: 2 additions & 1 deletion src/lib/sponsoredCall/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ const sponsoredCall = async (
relayCall: RelayCall.SponsoredCall,
request: {
...struct,
...options,
sponsorApiKey,
gasLimit: options?.gasLimit ? options.gasLimit.toString() : undefined,
retries: options?.retries,
},
},
config
Expand Down
8 changes: 7 additions & 1 deletion src/lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,11 @@ export type Config = {
};

export type SafeRequestPayload<T> = {
[K in keyof T]: T[K] extends bigint ? string : T[K];
[K in keyof T]: T[K] extends bigint
? string
: T[K] extends bigint | undefined
? string | undefined
: T[K] extends object
? SafeRequestPayload<T[K]>
: T[K];
};

0 comments on commit b3a620c

Please sign in to comment.