Skip to content

Commit

Permalink
GN-1727 Configurable gw url and relay erc2771 contract (#22)
Browse files Browse the repository at this point in the history
* GN-1727 Configurable gw url and relay erc2771 contract

* chore: partial configuration

* chore: export Config type

* v3.5.0
  • Loading branch information
opatavi authored Mar 20, 2023
1 parent 055027a commit ceef383
Show file tree
Hide file tree
Showing 19 changed files with 376 additions and 184 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": "3.4.0",
"version": "3.5.0",
"description": "SDK to integrate with Gelato Relay",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
5 changes: 1 addition & 4 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const GELATO_RELAY_URL = "https://relay.gelato.digital"; //Relay GW
export const GELATO_RELAY_URL = "https://api.gelato.digital"; //Relay GW

export const SIGN_TYPED_DATA_V4 = "eth_signTypedData_v4";

Expand All @@ -9,8 +9,5 @@ export const USER_NONCE_ABI = [
"function userNonce(address account) external view returns (uint256)",
];

export const GELATO_RELAY_ADDRESS =
"0xaBcC9b596420A9E9172FD5938620E265a0f9Df92";

export const GELATO_RELAY_ERC2771_ADDRESS =
"0xBf175FCC7086b4f9bd59d5EAE8eA67b8f940DE0d";
101 changes: 74 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import {
import { TransactionStatusResponse } from "./lib/status/types";
import {
BaseCallWithSyncFeeParams,
Config,
RelayRequestOptions,
RelayResponse,
} from "./lib/types";
import { GELATO_RELAY_ERC2771_ADDRESS, GELATO_RELAY_URL } from "./constants";

export {
CallWithSyncFeeRequest,
Expand All @@ -27,8 +29,32 @@ export {
ERC2771Type,
CallWithSyncFeeERC2771Request,
BaseCallWithSyncFeeParams,
Config,
};
export class GelatoRelay {
#config: Config;

constructor(config?: Partial<Config>) {
this.#config = this._getConfiguration(config);
}

/**
* @param {Config} config Configuration
*/
configure = (config: Partial<Config>) => {
this.#config = this._getConfiguration(config);
};

private _getConfiguration = (config?: Partial<Config>): Config => {
return {
url: config?.url ?? GELATO_RELAY_URL,
contract: {
relayERC2771:
config?.contract?.relayERC2771 ?? GELATO_RELAY_ERC2771_ADDRESS,
},
};
};

/**
* @param {CallWithSyncFeeRequest} request - CallWithSyncFee request to be relayed by Gelato Executors
* @param {RelayRequestOptions} [options] - Optional Relay configuration
Expand All @@ -38,7 +64,8 @@ export class GelatoRelay {
callWithSyncFee = (
request: CallWithSyncFeeRequest,
options?: RelayRequestOptions
): Promise<RelayResponse> => library.relayWithSyncFee(request, options);
): Promise<RelayResponse> =>
library.relayWithSyncFee({ request, options }, this.#config);

/**
* @param {CallWithSyncFeeERC2771Request} request - CallWithSyncFeeERC2771 request to be relayed by Gelato Executors
Expand All @@ -52,7 +79,14 @@ export class GelatoRelay {
walletOrProvider: ethers.providers.Web3Provider | ethers.Wallet,
options?: RelayRequestOptions
): Promise<RelayResponse> =>
library.relayWithCallWithSyncFeeERC2771(request, walletOrProvider, options);
library.relayWithCallWithSyncFeeERC2771(
{
request,
walletOrProvider,
options,
},
this.#config
);

/**
* @param {SponsoredCallRequest} request SponsoredCallRequest to be relayed by the Gelato Executors.
Expand All @@ -66,7 +100,10 @@ export class GelatoRelay {
sponsorApiKey: string,
options?: RelayRequestOptions
): Promise<RelayResponse> =>
library.relayWithSponsoredCall(request, sponsorApiKey, options);
library.relayWithSponsoredCall(
{ request, sponsorApiKey, options },
this.#config
);

/**
* @param {CallWithERC2771Request} request - CallWithERC2771Request to be relayed by Gelato Executors
Expand All @@ -83,10 +120,13 @@ export class GelatoRelay {
options?: RelayRequestOptions
): Promise<RelayResponse> =>
library.relayWithSponsoredCallERC2771(
request,
walletOrProvider,
sponsorApiKey,
options
{
request,
walletOrProvider,
sponsorApiKey,
options,
},
this.#config
);

/**
Expand All @@ -101,7 +141,10 @@ export class GelatoRelay {
walletOrProvider: ethers.providers.Web3Provider | ethers.Wallet,
type: ERC2771Type
): Promise<SignatureData> =>
library.getSignatureDataERC2771(request, walletOrProvider, type);
library.getSignatureDataERC2771(
{ request, walletOrProvider, type },
this.#config
);

/**
* @param {SignatureData["struct"]} struct - Struct that can be obtained from getSignatureDataERC2771
Expand All @@ -118,10 +161,13 @@ export class GelatoRelay {
options?: RelayRequestOptions
): Promise<RelayResponse> =>
library.sponsoredCallERC2771WithSignature(
struct,
signature,
sponsorApiKey,
options
{
struct,
signature,
sponsorApiKey,
options,
},
this.#config
);

/**
Expand All @@ -139,44 +185,48 @@ export class GelatoRelay {
options?: RelayRequestOptions
): Promise<RelayResponse> =>
library.callWithSyncFeeERC2771WithSignature(
struct,
syncFeeParams,
signature,
options
{
struct,
syncFeeParams,
signature,
options,
},
this.#config
);

/**
* @param {number} chainId - Chain Id
* @returns {Promise<boolean>} Boolean to demonstrate if Relay V2 is supported on the provided chain
*/
isNetworkSupported = (chainId: number): Promise<boolean> =>
library.isNetworkSupported(chainId);
library.isNetworkSupported({ chainId }, this.#config);

/**
* @returns {Promise<string[]>} List of networks where Relay V2 is supported
*/
getSupportedNetworks = (): Promise<string[]> =>
library.getSupportedNetworks();
library.getSupportedNetworks(this.#config);

/**
* @param {number} chainId - Chain Id
* @returns {Promise<boolean>} Boolean to demonstrate if the oracle is active on the provided chain
*/
isOracleActive = (chainId: number): Promise<boolean> =>
library.isOracleActive(chainId);
library.isOracleActive({ chainId }, this.#config);

/**
* @returns {Promise<string[]>} List of chain ids where the Gelato Oracle is active
*/
getGelatoOracles = (): Promise<string[]> => library.getGelatoOracles();
getGelatoOracles = (): Promise<string[]> =>
library.getGelatoOracles(this.#config);

/**
* @param {number} chainId - Chain Id
* @returns {Promise<string[]>} List of all payment tokens on the provided chain
*
*/
getPaymentTokens = (chainId: number): Promise<string[]> =>
library.getPaymentTokens(chainId);
library.getPaymentTokens({ chainId }, this.#config);

/**
* @param {number} chainId - Chain Id
Expand All @@ -195,11 +245,8 @@ export class GelatoRelay {
gasLimitL1: BigNumber = BigNumber.from(0)
): Promise<BigNumber> =>
library.getEstimatedFee(
chainId,
paymentToken,
gasLimit,
isHighPriority,
gasLimitL1
{ chainId, paymentToken, gasLimit, isHighPriority, gasLimitL1 },
this.#config
);

/**
Expand All @@ -210,5 +257,5 @@ export class GelatoRelay {
getTaskStatus = (
taskId: string
): Promise<TransactionStatusResponse | undefined> =>
library.getTaskStatus(taskId);
library.getTaskStatus({ taskId }, this.#config);
}
36 changes: 27 additions & 9 deletions src/lib/callWithSyncFee/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
import { getHttpErrorMessage, post } from "../../utils";
import { isNetworkSupported } from "../network";
import { RelayCall, RelayRequestOptions, RelayResponse } from "../types";
import {
Config,
RelayCall,
RelayRequestOptions,
RelayResponse,
} from "../types";

import { CallWithSyncFeeRequest } from "./types";

export const relayWithSyncFee = async (
request: CallWithSyncFeeRequest,
options?: RelayRequestOptions
payload: {
request: CallWithSyncFeeRequest;
options?: RelayRequestOptions;
},
config: Config
): Promise<RelayResponse> => {
try {
const isSupported = await isNetworkSupported(Number(request.chainId));
const { request, options } = payload;
const isSupported = await isNetworkSupported(
{ chainId: Number(request.chainId) },
config
);
if (!isSupported) {
throw new Error(`Chain id [${request.chainId}] is not supported`);
}
return await post<
CallWithSyncFeeRequest & RelayRequestOptions,
RelayResponse
>(RelayCall.CallWithSyncFee, {
...request,
isRelayContext: request.isRelayContext ?? true,
...options,
});
>(
{
relayCall: RelayCall.CallWithSyncFee,
request: {
...request,
isRelayContext: request.isRelayContext ?? true,
...options,
},
},
config
);
} catch (error) {
throw new Error(
`GelatoRelaySDK/relayWithSyncFee: Failed with error: ${getHttpErrorMessage(
Expand Down
47 changes: 32 additions & 15 deletions src/lib/erc2771/callWithSyncFeeERC2771/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { isNetworkSupported } from "../../network";
import {
BaseCallWithSyncFeeParams,
Config,
RelayCall,
RelayRequestOptions,
RelayResponse,
Expand All @@ -24,23 +25,30 @@ import {
import { getPayloadToSign, mapRequestToStruct } from "../utils";

export const relayWithCallWithSyncFeeERC2771 = async (
request: CallWithSyncFeeERC2771Request,
walletOrProvider: ethers.providers.Web3Provider | ethers.Wallet,
options?: RelayRequestOptions
payload: {
request: CallWithSyncFeeERC2771Request;
walletOrProvider: ethers.providers.Web3Provider | ethers.Wallet;
options?: RelayRequestOptions;
},
config: Config
): Promise<RelayResponse> => {
try {
const { request, walletOrProvider, options } = payload;
if (!walletOrProvider.provider) {
throw new Error(`Missing provider`);
}
const isSupported = await isNetworkSupported(Number(request.chainId));
const isSupported = await isNetworkSupported(
{ chainId: Number(request.chainId) },
config
);
if (!isSupported) {
throw new Error(`Chain id [${request.chainId}] is not supported`);
}
const { isRelayContext, feeToken, ...callWithSyncFeeRequest } = request;
const parametersToOverride = await populateOptionalUserParameters<
CallWithERC2771Request,
CallWithERC2771RequestOptionalParameters
>(callWithSyncFeeRequest, walletOrProvider);
>({ request: callWithSyncFeeRequest, walletOrProvider }, config);
const struct = await mapRequestToStruct(
callWithSyncFeeRequest,
parametersToOverride
Expand All @@ -49,9 +57,12 @@ export const relayWithCallWithSyncFeeERC2771 = async (
walletOrProvider,
callWithSyncFeeRequest.user as string,
getPayloadToSign(
struct,
ERC2771Type.CallWithSyncFee,
isWallet(walletOrProvider)
{
struct,
type: ERC2771Type.CallWithSyncFee,
isWallet: isWallet(walletOrProvider),
},
config
)
);

Expand All @@ -61,13 +72,19 @@ export const relayWithCallWithSyncFeeERC2771 = async (
RelayRequestOptions &
UserAuthSignature,
RelayResponse
>(RelayCall.CallWithSyncFeeERC2771, {
...struct,
...options,
feeToken,
isRelayContext: isRelayContext ?? true,
userSignature: signature,
});
>(
{
relayCall: RelayCall.CallWithSyncFeeERC2771,
request: {
...struct,
...options,
feeToken,
isRelayContext: isRelayContext ?? true,
userSignature: signature,
},
},
config
);
} catch (error) {
const errorMessage = (error as Error).message;
throw new Error(
Expand Down
Loading

0 comments on commit ceef383

Please sign in to comment.