diff --git a/package.json b/package.json index 405c8a5..e825aa1 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/constants/index.ts b/src/constants/index.ts index 658dd47..ca816f5 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -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"; @@ -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"; diff --git a/src/index.ts b/src/index.ts index 523b5dc..eabd418 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, @@ -27,8 +29,32 @@ export { ERC2771Type, CallWithSyncFeeERC2771Request, BaseCallWithSyncFeeParams, + Config, }; export class GelatoRelay { + #config: Config; + + constructor(config?: Partial) { + this.#config = this._getConfiguration(config); + } + + /** + * @param {Config} config Configuration + */ + configure = (config: Partial) => { + this.#config = this._getConfiguration(config); + }; + + private _getConfiguration = (config?: Partial): 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 @@ -38,7 +64,8 @@ export class GelatoRelay { callWithSyncFee = ( request: CallWithSyncFeeRequest, options?: RelayRequestOptions - ): Promise => library.relayWithSyncFee(request, options); + ): Promise => + library.relayWithSyncFee({ request, options }, this.#config); /** * @param {CallWithSyncFeeERC2771Request} request - CallWithSyncFeeERC2771 request to be relayed by Gelato Executors @@ -52,7 +79,14 @@ export class GelatoRelay { walletOrProvider: ethers.providers.Web3Provider | ethers.Wallet, options?: RelayRequestOptions ): Promise => - library.relayWithCallWithSyncFeeERC2771(request, walletOrProvider, options); + library.relayWithCallWithSyncFeeERC2771( + { + request, + walletOrProvider, + options, + }, + this.#config + ); /** * @param {SponsoredCallRequest} request SponsoredCallRequest to be relayed by the Gelato Executors. @@ -66,7 +100,10 @@ export class GelatoRelay { sponsorApiKey: string, options?: RelayRequestOptions ): Promise => - library.relayWithSponsoredCall(request, sponsorApiKey, options); + library.relayWithSponsoredCall( + { request, sponsorApiKey, options }, + this.#config + ); /** * @param {CallWithERC2771Request} request - CallWithERC2771Request to be relayed by Gelato Executors @@ -83,10 +120,13 @@ export class GelatoRelay { options?: RelayRequestOptions ): Promise => library.relayWithSponsoredCallERC2771( - request, - walletOrProvider, - sponsorApiKey, - options + { + request, + walletOrProvider, + sponsorApiKey, + options, + }, + this.#config ); /** @@ -101,7 +141,10 @@ export class GelatoRelay { walletOrProvider: ethers.providers.Web3Provider | ethers.Wallet, type: ERC2771Type ): Promise => - library.getSignatureDataERC2771(request, walletOrProvider, type); + library.getSignatureDataERC2771( + { request, walletOrProvider, type }, + this.#config + ); /** * @param {SignatureData["struct"]} struct - Struct that can be obtained from getSignatureDataERC2771 @@ -118,10 +161,13 @@ export class GelatoRelay { options?: RelayRequestOptions ): Promise => library.sponsoredCallERC2771WithSignature( - struct, - signature, - sponsorApiKey, - options + { + struct, + signature, + sponsorApiKey, + options, + }, + this.#config ); /** @@ -139,10 +185,13 @@ export class GelatoRelay { options?: RelayRequestOptions ): Promise => library.callWithSyncFeeERC2771WithSignature( - struct, - syncFeeParams, - signature, - options + { + struct, + syncFeeParams, + signature, + options, + }, + this.#config ); /** @@ -150,25 +199,26 @@ export class GelatoRelay { * @returns {Promise} Boolean to demonstrate if Relay V2 is supported on the provided chain */ isNetworkSupported = (chainId: number): Promise => - library.isNetworkSupported(chainId); + library.isNetworkSupported({ chainId }, this.#config); /** * @returns {Promise} List of networks where Relay V2 is supported */ getSupportedNetworks = (): Promise => - library.getSupportedNetworks(); + library.getSupportedNetworks(this.#config); /** * @param {number} chainId - Chain Id * @returns {Promise} Boolean to demonstrate if the oracle is active on the provided chain */ isOracleActive = (chainId: number): Promise => - library.isOracleActive(chainId); + library.isOracleActive({ chainId }, this.#config); /** * @returns {Promise} List of chain ids where the Gelato Oracle is active */ - getGelatoOracles = (): Promise => library.getGelatoOracles(); + getGelatoOracles = (): Promise => + library.getGelatoOracles(this.#config); /** * @param {number} chainId - Chain Id @@ -176,7 +226,7 @@ export class GelatoRelay { * */ getPaymentTokens = (chainId: number): Promise => - library.getPaymentTokens(chainId); + library.getPaymentTokens({ chainId }, this.#config); /** * @param {number} chainId - Chain Id @@ -195,11 +245,8 @@ export class GelatoRelay { gasLimitL1: BigNumber = BigNumber.from(0) ): Promise => library.getEstimatedFee( - chainId, - paymentToken, - gasLimit, - isHighPriority, - gasLimitL1 + { chainId, paymentToken, gasLimit, isHighPriority, gasLimitL1 }, + this.#config ); /** @@ -210,5 +257,5 @@ export class GelatoRelay { getTaskStatus = ( taskId: string ): Promise => - library.getTaskStatus(taskId); + library.getTaskStatus({ taskId }, this.#config); } diff --git a/src/lib/callWithSyncFee/index.ts b/src/lib/callWithSyncFee/index.ts index 73d1496..b614ab0 100644 --- a/src/lib/callWithSyncFee/index.ts +++ b/src/lib/callWithSyncFee/index.ts @@ -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 => { 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( diff --git a/src/lib/erc2771/callWithSyncFeeERC2771/index.ts b/src/lib/erc2771/callWithSyncFeeERC2771/index.ts index 31f0436..271552a 100644 --- a/src/lib/erc2771/callWithSyncFeeERC2771/index.ts +++ b/src/lib/erc2771/callWithSyncFeeERC2771/index.ts @@ -9,6 +9,7 @@ import { import { isNetworkSupported } from "../../network"; import { BaseCallWithSyncFeeParams, + Config, RelayCall, RelayRequestOptions, RelayResponse, @@ -24,15 +25,22 @@ 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 => { 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`); } @@ -40,7 +48,7 @@ export const relayWithCallWithSyncFeeERC2771 = async ( const parametersToOverride = await populateOptionalUserParameters< CallWithERC2771Request, CallWithERC2771RequestOptionalParameters - >(callWithSyncFeeRequest, walletOrProvider); + >({ request: callWithSyncFeeRequest, walletOrProvider }, config); const struct = await mapRequestToStruct( callWithSyncFeeRequest, parametersToOverride @@ -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 ) ); @@ -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( diff --git a/src/lib/erc2771/callWithSyncFeeERC2771WithSignature/index.ts b/src/lib/erc2771/callWithSyncFeeERC2771WithSignature/index.ts index 7b58c67..896c4ed 100644 --- a/src/lib/erc2771/callWithSyncFeeERC2771WithSignature/index.ts +++ b/src/lib/erc2771/callWithSyncFeeERC2771WithSignature/index.ts @@ -2,6 +2,7 @@ import { post } from "../../../utils"; import { isNetworkSupported } from "../../network"; import { BaseCallWithSyncFeeParams, + Config, RelayCall, RelayRequestOptions, RelayResponse, @@ -9,13 +10,20 @@ import { import { CallWithERC2771Struct, UserAuthSignature } from "../types"; export const callWithSyncFeeERC2771WithSignature = async ( - struct: CallWithERC2771Struct, - syncFeeParams: BaseCallWithSyncFeeParams, - signature: string, - options?: RelayRequestOptions + payload: { + struct: CallWithERC2771Struct; + syncFeeParams: BaseCallWithSyncFeeParams; + signature: string; + options?: RelayRequestOptions; + }, + config: Config ): Promise => { try { - const isSupported = await isNetworkSupported(Number(struct.chainId)); + const { signature, struct, syncFeeParams, options } = payload; + const isSupported = await isNetworkSupported( + { chainId: Number(struct.chainId) }, + config + ); if (!isSupported) { throw new Error(`Chain id [${struct.chainId}] is not supported`); } @@ -26,13 +34,19 @@ export const callWithSyncFeeERC2771WithSignature = async ( RelayRequestOptions & UserAuthSignature, RelayResponse - >(RelayCall.CallWithSyncFeeERC2771, { - ...struct, - ...syncFeeParams, - ...options, - isRelayContext: syncFeeParams.isRelayContext ?? true, - userSignature: signature, - }); + >( + { + relayCall: RelayCall.CallWithSyncFeeERC2771, + request: { + ...struct, + ...syncFeeParams, + ...options, + isRelayContext: syncFeeParams.isRelayContext ?? true, + userSignature: signature, + }, + }, + config + ); } catch (error) { const errorMessage = (error as Error).message; throw new Error( diff --git a/src/lib/erc2771/getSignatureDataERC2771/index.ts b/src/lib/erc2771/getSignatureDataERC2771/index.ts index 3278c40..227bdc0 100644 --- a/src/lib/erc2771/getSignatureDataERC2771/index.ts +++ b/src/lib/erc2771/getSignatureDataERC2771/index.ts @@ -6,6 +6,7 @@ import { signTypedDataV4, } from "../../../utils"; import { isNetworkSupported } from "../../network"; +import { Config } from "../../types"; import { SignatureData, CallWithERC2771Request, @@ -15,15 +16,22 @@ import { import { getPayloadToSign, mapRequestToStruct } from "../utils"; export const getSignatureDataERC2771 = async ( - request: CallWithERC2771Request, - walletOrProvider: ethers.providers.Web3Provider | ethers.Wallet, - type: ERC2771Type + payload: { + request: CallWithERC2771Request; + walletOrProvider: ethers.providers.Web3Provider | ethers.Wallet; + type: ERC2771Type; + }, + config: Config ): Promise => { try { + const { request, type, walletOrProvider } = 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`); } @@ -31,12 +39,15 @@ export const getSignatureDataERC2771 = async ( const parametersToOverride = await populateOptionalUserParameters< CallWithERC2771Request, CallWithERC2771RequestOptionalParameters - >(request, walletOrProvider); + >({ request, walletOrProvider }, config); const struct = await mapRequestToStruct(request, parametersToOverride); const signature = await signTypedDataV4( walletOrProvider, request.user as string, - getPayloadToSign(struct, type, isWallet(walletOrProvider)) + getPayloadToSign( + { struct, type, isWallet: isWallet(walletOrProvider) }, + config + ) ); return { struct, diff --git a/src/lib/erc2771/sponsoredCallERC2771/index.ts b/src/lib/erc2771/sponsoredCallERC2771/index.ts index f81c245..7c55689 100644 --- a/src/lib/erc2771/sponsoredCallERC2771/index.ts +++ b/src/lib/erc2771/sponsoredCallERC2771/index.ts @@ -9,6 +9,7 @@ import { import { isNetworkSupported } from "../../network"; import { ApiKey, + Config, RelayCall, RelayRequestOptions, RelayResponse, @@ -23,30 +24,35 @@ import { import { getPayloadToSign, mapRequestToStruct } from "../utils"; export const relayWithSponsoredCallERC2771 = async ( - request: CallWithERC2771Request, - walletOrProvider: ethers.providers.Web3Provider | ethers.Wallet, - sponsorApiKey: string, - options?: RelayRequestOptions + payload: { + request: CallWithERC2771Request; + walletOrProvider: ethers.providers.Web3Provider | ethers.Wallet; + sponsorApiKey: string; + options?: RelayRequestOptions; + }, + config: Config ): Promise => { - return await sponsoredCallERC2771( - request, - walletOrProvider, - sponsorApiKey, - options - ); + return await sponsoredCallERC2771(payload, config); }; const sponsoredCallERC2771 = async ( - request: CallWithERC2771Request, - walletOrProvider: ethers.providers.Web3Provider | ethers.Wallet, - sponsorApiKey: string, - options?: RelayRequestOptions + payload: { + request: CallWithERC2771Request; + walletOrProvider: ethers.providers.Web3Provider | ethers.Wallet; + sponsorApiKey: string; + options?: RelayRequestOptions; + }, + config: Config ): Promise => { try { + const { request, sponsorApiKey, 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`); } @@ -54,27 +60,36 @@ const sponsoredCallERC2771 = async ( const parametersToOverride = await populateOptionalUserParameters< CallWithERC2771Request, CallWithERC2771RequestOptionalParameters - >(request, walletOrProvider); + >({ request, walletOrProvider }, config); const struct = await mapRequestToStruct(request, parametersToOverride); const signature = await signTypedDataV4( walletOrProvider, request.user as string, getPayloadToSign( - struct, - ERC2771Type.SponsoredCall, - isWallet(walletOrProvider) + { + struct, + type: ERC2771Type.SponsoredCall, + isWallet: isWallet(walletOrProvider), + }, + config ) ); return await post< CallWithERC2771Struct & RelayRequestOptions & UserAuthSignature & ApiKey, RelayResponse - >(RelayCall.SponsoredCallERC2771, { - ...struct, - ...options, - userSignature: signature, - sponsorApiKey, - }); + >( + { + relayCall: RelayCall.SponsoredCallERC2771, + request: { + ...struct, + ...options, + userSignature: signature, + sponsorApiKey, + }, + }, + config + ); } catch (error) { const errorMessage = (error as Error).message; throw new Error( diff --git a/src/lib/erc2771/sponsoredCallERC2771WithSignature/index.ts b/src/lib/erc2771/sponsoredCallERC2771WithSignature/index.ts index 25b6b65..c230d96 100644 --- a/src/lib/erc2771/sponsoredCallERC2771WithSignature/index.ts +++ b/src/lib/erc2771/sponsoredCallERC2771WithSignature/index.ts @@ -2,6 +2,7 @@ import { post } from "../../../utils"; import { isNetworkSupported } from "../../network"; import { ApiKey, + Config, RelayCall, RelayRequestOptions, RelayResponse, @@ -9,13 +10,20 @@ import { import { CallWithERC2771Struct, UserAuthSignature } from "../types"; export const sponsoredCallERC2771WithSignature = async ( - struct: CallWithERC2771Struct, - signature: string, - sponsorApiKey: string, - options?: RelayRequestOptions + payload: { + struct: CallWithERC2771Struct; + signature: string; + sponsorApiKey: string; + options?: RelayRequestOptions; + }, + config: Config ): Promise => { try { - const isSupported = await isNetworkSupported(Number(struct.chainId)); + const { signature, sponsorApiKey, struct, options } = payload; + const isSupported = await isNetworkSupported( + { chainId: Number(struct.chainId) }, + config + ); if (!isSupported) { throw new Error(`Chain id [${struct.chainId}] is not supported`); } @@ -23,12 +31,18 @@ export const sponsoredCallERC2771WithSignature = async ( return await post< CallWithERC2771Struct & RelayRequestOptions & UserAuthSignature & ApiKey, RelayResponse - >(RelayCall.SponsoredCallERC2771, { - ...struct, - ...options, - userSignature: signature, - sponsorApiKey, - }); + >( + { + relayCall: RelayCall.SponsoredCallERC2771, + request: { + ...struct, + ...options, + userSignature: signature, + sponsorApiKey, + }, + }, + config + ); } catch (error) { const errorMessage = (error as Error).message; throw new Error( diff --git a/src/lib/erc2771/utils/getPayloadToSign.ts b/src/lib/erc2771/utils/getPayloadToSign.ts index 1ad9f3e..c313d45 100644 --- a/src/lib/erc2771/utils/getPayloadToSign.ts +++ b/src/lib/erc2771/utils/getPayloadToSign.ts @@ -1,5 +1,5 @@ import { getEIP712Domain } from "../../../utils"; -import { EIP712_DOMAIN_TYPE_DATA } from "../../types"; +import { Config, EIP712_DOMAIN_TYPE_DATA } from "../../types"; import { EIP712_SPONSORED_CALL_ERC2771_TYPE_DATA, SponsoredCallERC2771PayloadToSign, @@ -10,11 +10,15 @@ import { } from "../types"; export const getPayloadToSign = ( - struct: CallWithERC2771Struct, - type: ERC2771Type, - isWallet: boolean + payload: { + struct: CallWithERC2771Struct; + type: ERC2771Type; + isWallet: boolean; + }, + config: Config ): SponsoredCallERC2771PayloadToSign | CallWithSyncFeeERC2771PayloadToSign => { - const domain = getEIP712Domain(struct.chainId as number); + const { isWallet, struct, type } = payload; + const domain = getEIP712Domain({ chainId: struct.chainId as number }, config); switch (type) { case ERC2771Type.SponsoredCall: diff --git a/src/lib/network/index.ts b/src/lib/network/index.ts index a5ab16d..aaaa979 100644 --- a/src/lib/network/index.ts +++ b/src/lib/network/index.ts @@ -1,18 +1,24 @@ import axios from "axios"; -import { GELATO_RELAY_URL } from "../../constants"; import { getHttpErrorMessage } from "../../utils"; +import { Config } from "../types"; -export const isNetworkSupported = async (chainId: number): Promise => { - const supportedNetworks = await getSupportedNetworks(); - return supportedNetworks.includes(chainId.toString()); +export const isNetworkSupported = async ( + payload: { + chainId: number; + }, + config: Config +): Promise => { + const supportedNetworks = await getSupportedNetworks(config); + return supportedNetworks.includes(payload.chainId.toString()); }; -export const getSupportedNetworks = async (): Promise => { +export const getSupportedNetworks = async ( + config: Config +): Promise => { try { - return ( - await axios.get<{ relays: string[] }>(`${GELATO_RELAY_URL}/relays/v2`) - ).data.relays; + return (await axios.get<{ relays: string[] }>(`${config.url}/relays/v2`)) + .data.relays; } catch (error) { throw new Error( `GelatoRelaySDK/getSupportedNetworks: Failed with error: ${getHttpErrorMessage( diff --git a/src/lib/oracle/index.ts b/src/lib/oracle/index.ts index ea8e672..92000a6 100644 --- a/src/lib/oracle/index.ts +++ b/src/lib/oracle/index.ts @@ -1,17 +1,22 @@ import axios from "axios"; import { BigNumber } from "ethers"; -import { GELATO_RELAY_URL } from "../../constants"; import { getHttpErrorMessage } from "../../utils"; +import { Config } from "../types"; -export const isOracleActive = async (chainId: number): Promise => { - const oracles = await getGelatoOracles(); - return oracles.includes(chainId.toString()); +export const isOracleActive = async ( + payload: { + chainId: number; + }, + config: Config +): Promise => { + const oracles = await getGelatoOracles(config); + return oracles.includes(payload.chainId.toString()); }; -export const getGelatoOracles = async (): Promise => { +export const getGelatoOracles = async (config: Config): Promise => { try { - return (await axios.get(`${GELATO_RELAY_URL}/oracles/`)).data.oracles; + return (await axios.get(`${config.url}/oracles/`)).data.oracles; } catch (error) { throw new Error( `GelatoRelaySDK/getGelatoOracles: Failed with error: ${getHttpErrorMessage( @@ -21,10 +26,13 @@ export const getGelatoOracles = async (): Promise => { } }; -export const getPaymentTokens = async (chainId: number): Promise => { +export const getPaymentTokens = async ( + payload: { chainId: number }, + config: Config +): Promise => { try { return ( - await axios.get(`${GELATO_RELAY_URL}/oracles/${chainId}/paymentTokens/`) + await axios.get(`${config.url}/oracles/${payload.chainId}/paymentTokens/`) ).data.paymentTokens; } catch (error) { throw new Error( @@ -36,12 +44,17 @@ export const getPaymentTokens = async (chainId: number): Promise => { }; export const getEstimatedFee = async ( - chainId: number, - paymentToken: string, - gasLimit: BigNumber, - isHighPriority: boolean, - gasLimitL1 = BigNumber.from(0) + payload: { + chainId: number; + paymentToken: string; + gasLimit: BigNumber; + isHighPriority: boolean; + gasLimitL1: BigNumber; + }, + config: Config ): Promise => { + const { chainId, gasLimit, gasLimitL1, isHighPriority, paymentToken } = + payload; const params = { paymentToken, gasLimit: gasLimit.toString(), @@ -49,12 +62,9 @@ export const getEstimatedFee = async ( gasLimitL1: gasLimitL1.toString(), }; try { - const res = await axios.get( - `${GELATO_RELAY_URL}/oracles/${chainId}/estimate`, - { - params, - } - ); + const res = await axios.get(`${config.url}/oracles/${chainId}/estimate`, { + params, + }); return BigNumber.from(res.data.estimatedFee); } catch (error) { throw new Error( diff --git a/src/lib/sponsoredCall/index.ts b/src/lib/sponsoredCall/index.ts index 1b668c4..4b32bf2 100644 --- a/src/lib/sponsoredCall/index.ts +++ b/src/lib/sponsoredCall/index.ts @@ -6,6 +6,7 @@ import { isNetworkSupported } from "../network"; import { ApiKey, BaseRelayParams, + Config, RelayCall, RelayRequestOptions, RelayResponse, @@ -14,15 +15,14 @@ import { import { SponsoredCallRequest } from "./types"; export const relayWithSponsoredCall = async ( - request: SponsoredCallRequest, - sponsorApiKey: string, - options?: RelayRequestOptions + payload: { + request: SponsoredCallRequest; + sponsorApiKey: string; + options?: RelayRequestOptions; + }, + config: Config ): Promise => { - return await sponsoredCall( - request as SponsoredCallRequest, - sponsorApiKey, - options - ); + return await sponsoredCall(payload, config); }; const mapRequestToStruct = async ( @@ -36,12 +36,19 @@ const mapRequestToStruct = async ( }; const sponsoredCall = async ( - request: SponsoredCallRequest, - sponsorApiKey: string, - options?: RelayRequestOptions + payload: { + request: SponsoredCallRequest; + sponsorApiKey: string; + options?: RelayRequestOptions; + }, + config: Config ): Promise => { try { - const isSupported = await isNetworkSupported(Number(request.chainId)); + const { request, sponsorApiKey, options } = payload; + const isSupported = await isNetworkSupported( + { chainId: Number(request.chainId) }, + config + ); if (!isSupported) { throw new Error(`Chain id [${request.chainId}] is not supported`); } @@ -49,11 +56,17 @@ const sponsoredCall = async ( const postResponse = await post< BaseRelayParams & RelayRequestOptions & ApiKey, RelayResponse - >(RelayCall.SponsoredCall, { - ...struct, - ...options, - sponsorApiKey, - }); + >( + { + relayCall: RelayCall.SponsoredCall, + request: { + ...struct, + ...options, + sponsorApiKey, + }, + }, + config + ); return postResponse; } catch (error) { const errorMessage = (error as Error).message; diff --git a/src/lib/status/index.ts b/src/lib/status/index.ts index 5fd481c..363be89 100644 --- a/src/lib/status/index.ts +++ b/src/lib/status/index.ts @@ -1,16 +1,17 @@ import axios from "axios"; -import { GELATO_RELAY_URL } from "../../constants"; import { getHttpErrorMessage } from "../../utils"; +import { Config } from "../types"; import { TransactionStatusResponse } from "./types"; export const getTaskStatus = async ( - taskId: string + payload: { taskId: string }, + config: Config ): Promise => { try { - return (await axios.get(`${GELATO_RELAY_URL}/tasks/status/${taskId}`)).data - .task; + return (await axios.get(`${config.url}/tasks/status/${payload.taskId}`)) + .data.task; } catch (error) { throw new Error( `GelatoRelaySDK/getTaskStatus: Failed with error: ${getHttpErrorMessage( diff --git a/src/lib/types/index.ts b/src/lib/types/index.ts index 6aa1311..3170a1b 100644 --- a/src/lib/types/index.ts +++ b/src/lib/types/index.ts @@ -48,3 +48,10 @@ export type BaseCallWithSyncFeeParams = { feeToken: string; isRelayContext?: boolean; }; + +export type Config = { + url: string; + contract: { + relayERC2771: string; + }; +}; diff --git a/src/utils/eip712.ts b/src/utils/eip712.ts index e22efe0..803a656 100644 --- a/src/utils/eip712.ts +++ b/src/utils/eip712.ts @@ -1,11 +1,15 @@ -import { GELATO_RELAY_ERC2771_ADDRESS } from "../constants"; -import { EIP712Domain } from "../lib/types"; +import { getAddress } from "ethers/lib/utils"; -export const getEIP712Domain = (chainId: number): EIP712Domain => { +import { Config, EIP712Domain } from "../lib/types"; + +export const getEIP712Domain = ( + payload: { chainId: number }, + config: Config +): EIP712Domain => { return { name: "GelatoRelayERC2771", version: "1", - chainId, - verifyingContract: GELATO_RELAY_ERC2771_ADDRESS, + chainId: payload.chainId, + verifyingContract: getAddress(config.contract.relayERC2771), }; }; diff --git a/src/utils/getUserNonce.ts b/src/utils/getUserNonce.ts index 636f20d..0d86d89 100644 --- a/src/utils/getUserNonce.ts +++ b/src/utils/getUserNonce.ts @@ -1,13 +1,19 @@ import { ethers } from "ethers"; +import { getAddress } from "ethers/lib/utils"; -import { GELATO_RELAY_ERC2771_ADDRESS, USER_NONCE_ABI } from "../constants"; +import { USER_NONCE_ABI } from "../constants"; +import { Config } from "../lib/types"; export const getUserNonce = async ( - account: string, - walletOrProvider: ethers.providers.Web3Provider | ethers.Wallet + payload: { + account: string; + walletOrProvider: ethers.providers.Web3Provider | ethers.Wallet; + }, + config: Config ) => { + const { account, walletOrProvider } = payload; const contract = new ethers.Contract( - GELATO_RELAY_ERC2771_ADDRESS, + getAddress(config.contract.relayERC2771), USER_NONCE_ABI, walletOrProvider ); diff --git a/src/utils/populateOptionalUserParameters.ts b/src/utils/populateOptionalUserParameters.ts index ef0915b..a56cade 100644 --- a/src/utils/populateOptionalUserParameters.ts +++ b/src/utils/populateOptionalUserParameters.ts @@ -5,6 +5,7 @@ import { CallWithERC2771Request, CallWithERC2771RequestOptionalParameters, } from "../lib/erc2771/types"; +import { Config } from "../lib/types"; import { calculateDeadline } from "./calculateDeadline"; import { getUserNonce } from "./getUserNonce"; @@ -13,9 +14,13 @@ export const populateOptionalUserParameters = async < Request extends CallWithERC2771Request, OptionalParameters extends CallWithERC2771RequestOptionalParameters >( - request: Request, - walletOrProvider: ethers.providers.Web3Provider | ethers.Wallet + payload: { + request: Request; + walletOrProvider: ethers.providers.Web3Provider | ethers.Wallet; + }, + config: Config ): Promise> => { + const { request, walletOrProvider } = payload; const parametersToOverride: Partial = {}; if (!request.userDeadline) { parametersToOverride.userDeadline = calculateDeadline(DEFAULT_DEADLINE_GAP); @@ -24,8 +29,11 @@ export const populateOptionalUserParameters = async < parametersToOverride.userNonce = BigNumber.from( ( (await getUserNonce( - request.user as string, - walletOrProvider + { + account: request.user as string, + walletOrProvider, + }, + config )) as BigNumber ).toNumber() ).toString(); diff --git a/src/utils/post.ts b/src/utils/post.ts index e8675ab..a069d87 100644 --- a/src/utils/post.ts +++ b/src/utils/post.ts @@ -1,31 +1,31 @@ import axios from "axios"; -import { GELATO_RELAY_URL } from "../constants"; -import { RelayCall } from "../lib/types"; +import { Config, RelayCall } from "../lib/types"; import { getHttpErrorMessage } from "./getHttpErrorMessage"; export const post = async ( - relayCall: RelayCall, - request: Request + payload: { relayCall: RelayCall; request: Request }, + config: Config ): Promise => { try { + const { relayCall, request } = payload; let path: string; switch (relayCall) { case RelayCall.CallWithSyncFee: - path = `${GELATO_RELAY_URL}/relays/v2/call-with-sync-fee`; + path = `${config.url}/relays/v2/call-with-sync-fee`; break; case RelayCall.CallWithSyncFeeERC2771: - path = `${GELATO_RELAY_URL}/relays/v2/call-with-sync-fee-erc2771`; + path = `${config.url}/relays/v2/call-with-sync-fee-erc2771`; break; case RelayCall.SponsoredCall: - path = `${GELATO_RELAY_URL}/relays/v2/sponsored-call`; + path = `${config.url}/relays/v2/sponsored-call`; break; case RelayCall.SponsoredCallERC2771: - path = `${GELATO_RELAY_URL}/relays/v2/sponsored-call-erc2771`; + path = `${config.url}/relays/v2/sponsored-call-erc2771`; break; default: {