Skip to content

Commit

Permalink
PublicOrWallet client abstraction on sdk calls
Browse files Browse the repository at this point in the history
  • Loading branch information
szmizorsz committed Apr 4, 2024
1 parent f87fd2a commit b7c796f
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 55 deletions.
11 changes: 5 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { WalletClient } from "viem";

import * as library from "./lib";
import { CallWithSyncFeeRequest } from "./lib/callWithSyncFee/types";
import { SponsoredCallRequest } from "./lib/sponsoredCall/types";
Expand All @@ -18,6 +16,7 @@ import {
Config,
RelayRequestOptions,
RelayResponse,
PublicOrWalletClient,
} from "./lib/types";
import {
GELATO_RELAY_1BALANCE_ERC2771_ADDRESS,
Expand Down Expand Up @@ -135,7 +134,7 @@ export class GelatoRelay {
request:
| CallWithSyncFeeERC2771Request
| CallWithSyncFeeConcurrentERC2771Request,
client: WalletClient,
client: PublicOrWalletClient,
options?: RelayRequestOptions,
sponsorApiKey?: string
): Promise<RelayResponse> => {
Expand Down Expand Up @@ -190,7 +189,7 @@ export class GelatoRelay {
*/
sponsoredCallERC2771 = async (
request: CallWithERC2771Request | CallWithConcurrentERC2771Request,
client: WalletClient,
client: PublicOrWalletClient,
sponsorApiKey: string,
options?: RelayRequestOptions
): Promise<RelayResponse> => {
Expand Down Expand Up @@ -220,7 +219,7 @@ export class GelatoRelay {
*/
getSignatureDataERC2771 = (
request: CallWithERC2771Request | CallWithConcurrentERC2771Request,
client: WalletClient,
client: PublicOrWalletClient,
type: ERC2771Type
): Promise<SignatureData> =>
library.getSignatureDataERC2771({ request, client, type }, this.#config);
Expand All @@ -235,7 +234,7 @@ export class GelatoRelay {
getDataToSignERC2771 = (
request: CallWithERC2771Request | CallWithConcurrentERC2771Request,
type: ERC2771Type,
client?: WalletClient
client?: PublicOrWalletClient
): Promise<PayloadToSign> =>
library.getDataToSignERC2771({ request, client, type }, this.#config);

Expand Down
5 changes: 2 additions & 3 deletions src/lib/erc2771/callWithSyncFeeERC2771/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { WalletClient } from "viem";

import { post } from "../../../utils";
import {
ApiKey,
Expand All @@ -10,6 +8,7 @@ import {
RelayCall,
RelayRequestOptions,
RelayResponse,
PublicOrWalletClient,
} from "../../types";
import {
CallWithConcurrentERC2771Struct,
Expand All @@ -27,7 +26,7 @@ export const relayWithCallWithSyncFeeERC2771 = async (
request:
| CallWithSyncFeeERC2771Request
| CallWithSyncFeeConcurrentERC2771Request;
client: WalletClient;
client: PublicOrWalletClient;
sponsorApiKey?: string;
options?: RelayRequestOptions;
},
Expand Down
12 changes: 5 additions & 7 deletions src/lib/erc2771/getDataToSignERC2771/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { WalletClient } from "viem";

import { isConcurrentRequest } from "../../../utils";
import { isNetworkSupported } from "../../network";
import { Config } from "../../types";
import { Config, PublicOrWalletClient } from "../../types";
import {
CallWithERC2771Request,
ERC2771Type,
Expand All @@ -17,7 +15,7 @@ export async function getDataToSignERC2771(
payload: {
request: CallWithERC2771Request;
type: ERC2771Type.CallWithSyncFee | ERC2771Type.SponsoredCall;
client?: WalletClient;
client?: PublicOrWalletClient;
},
config: Config
): Promise<SequentialPayloadToSign>;
Expand All @@ -28,7 +26,7 @@ export async function getDataToSignERC2771(
type:
| ERC2771Type.ConcurrentCallWithSyncFee
| ERC2771Type.ConcurrentSponsoredCall;
client?: WalletClient;
client?: PublicOrWalletClient;
},
config: Config
): Promise<ConcurrentPayloadToSign>;
Expand All @@ -37,7 +35,7 @@ export async function getDataToSignERC2771(
payload: {
request: CallWithERC2771Request | CallWithConcurrentERC2771Request;
type: ERC2771Type;
client?: WalletClient;
client?: PublicOrWalletClient;
},
config: Config
): Promise<PayloadToSign>;
Expand All @@ -46,7 +44,7 @@ export async function getDataToSignERC2771(
payload: {
request: CallWithERC2771Request | CallWithConcurrentERC2771Request;
type: ERC2771Type;
client?: WalletClient;
client?: PublicOrWalletClient;
},
config: Config
): Promise<PayloadToSign> {
Expand Down
12 changes: 5 additions & 7 deletions src/lib/erc2771/getSignatureDataERC2771/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { WalletClient } from "viem";

import { isConcurrentRequest, signTypedDataV4 } from "../../../utils";
import { Config } from "../../types";
import { Config, PublicOrWalletClient } from "../../types";
import {
SignatureData,
CallWithERC2771Request,
Expand All @@ -15,7 +13,7 @@ import { getDataToSignERC2771 } from "../getDataToSignERC2771/index.js";
export async function getSignatureDataERC2771(
payload: {
request: CallWithERC2771Request;
client: WalletClient;
client: PublicOrWalletClient;
type: ERC2771Type.CallWithSyncFee | ERC2771Type.SponsoredCall;
},
config: Config
Expand All @@ -24,7 +22,7 @@ export async function getSignatureDataERC2771(
export async function getSignatureDataERC2771(
payload: {
request: CallWithConcurrentERC2771Request;
client: WalletClient;
client: PublicOrWalletClient;
type:
| ERC2771Type.ConcurrentCallWithSyncFee
| ERC2771Type.ConcurrentSponsoredCall;
Expand All @@ -35,7 +33,7 @@ export async function getSignatureDataERC2771(
export async function getSignatureDataERC2771(
payload: {
request: CallWithERC2771Request | CallWithConcurrentERC2771Request;
client: WalletClient;
client: PublicOrWalletClient;
type: ERC2771Type;
},
config: Config
Expand All @@ -44,7 +42,7 @@ export async function getSignatureDataERC2771(
export async function getSignatureDataERC2771(
payload: {
request: CallWithERC2771Request | CallWithConcurrentERC2771Request;
client: WalletClient;
client: PublicOrWalletClient;
type: ERC2771Type;
},
config: Config
Expand Down
7 changes: 3 additions & 4 deletions src/lib/erc2771/sponsoredCallERC2771/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { WalletClient } from "viem";

import { isConcurrentRequest, post } from "../../../utils";
import {
ApiKey,
Expand All @@ -8,6 +6,7 @@ import {
RelayCall,
RelayRequestOptions,
RelayResponse,
PublicOrWalletClient,
} from "../../types";
import {
CallWithConcurrentERC2771Request,
Expand All @@ -23,7 +22,7 @@ import { safeTransformStruct } from "../utils/safeTransformStruct.js";
export const relayWithSponsoredCallERC2771 = async (
payload: {
request: CallWithERC2771Request | CallWithConcurrentERC2771Request;
client: WalletClient;
client: PublicOrWalletClient;
sponsorApiKey: string;
options?: RelayRequestOptions;
},
Expand All @@ -35,7 +34,7 @@ export const relayWithSponsoredCallERC2771 = async (
const sponsoredCallERC2771 = async (
payload: {
request: CallWithERC2771Request | CallWithConcurrentERC2771Request;
client: WalletClient;
client: PublicOrWalletClient;
sponsorApiKey: string;
options?: RelayRequestOptions;
},
Expand Down
16 changes: 7 additions & 9 deletions src/lib/erc2771/utils/populatePayloadToSign.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { WalletClient } from "viem";

import {
CallWithConcurrentERC2771Request,
CallWithERC2771Request,
Expand All @@ -8,10 +6,10 @@ import {
PayloadToSign,
SequentialPayloadToSign,
} from "../types";
import { Config } from "../../types";
import { Config, PublicOrWalletClient } from "../../types";
import {
isConcurrentRequest,
isLocalSigner,
isWalletClient,
populateOptionalUserParameters,
} from "../../../utils";

Expand All @@ -25,7 +23,7 @@ export async function populatePayloadToSign(
type:
| ERC2771Type.ConcurrentCallWithSyncFee
| ERC2771Type.ConcurrentSponsoredCall;
client?: WalletClient;
client?: PublicOrWalletClient;
},
config: Config
): Promise<ConcurrentPayloadToSign>;
Expand All @@ -34,7 +32,7 @@ export async function populatePayloadToSign(
payload: {
request: CallWithERC2771Request;
type: ERC2771Type.CallWithSyncFee | ERC2771Type.SponsoredCall;
client?: WalletClient;
client?: PublicOrWalletClient;
},
config: Config
): Promise<SequentialPayloadToSign>;
Expand All @@ -43,7 +41,7 @@ export async function populatePayloadToSign(
payload: {
request: CallWithConcurrentERC2771Request | CallWithERC2771Request;
type: ERC2771Type;
client?: WalletClient;
client?: PublicOrWalletClient;
},
config: Config
): Promise<PayloadToSign> {
Expand All @@ -69,7 +67,7 @@ export async function populatePayloadToSign(
{
struct: safeStruct,
type,
isSigner: client ? isLocalSigner(client) : undefined,
isSigner: client ? isWalletClient(client) : undefined,
},
config
);
Expand Down Expand Up @@ -99,7 +97,7 @@ export async function populatePayloadToSign(
{
struct: safeStruct,
type,
isSigner: client ? isLocalSigner(client) : undefined,
isSigner: client ? isWalletClient(client) : undefined,
},
config
);
Expand Down
4 changes: 3 additions & 1 deletion src/lib/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Hex, ByteArray } from "viem";
import { Hex, ByteArray, WalletClient, PublicClient } from "viem";

export enum RelayCall {
CallWithSyncFee,
Expand Down Expand Up @@ -77,3 +77,5 @@ export type SafeRequestPayload<T> = {
? SafeRequestPayload<T[K]>
: T[K];
};

export type PublicOrWalletClient = PublicClient | WalletClient;
15 changes: 15 additions & 0 deletions src/utils/getPublicClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { PublicClient, publicActions } from "viem";

import { PublicOrWalletClient } from "../lib/types";

export const getPublicClient = (client: PublicOrWalletClient): PublicClient => {
let publicClient: PublicClient;
if (client.type === "walletClient") {
publicClient = client.extend(publicActions) as PublicClient;
} else {
// publicClient
publicClient = client as PublicClient;
}

return publicClient;
};
9 changes: 5 additions & 4 deletions src/utils/getUserNonce.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { WalletClient, publicActions, parseAbi } from "viem";
import { parseAbi } from "viem";

import { USER_NONCE_ABI } from "../constants";
import { Config } from "../lib/types";
import { Config, PublicOrWalletClient } from "../lib/types";
import { ERC2771Type } from "../lib/erc2771/types";

import { getGelatoRelayERC2771Address } from "./relayAddress";
import { getPublicClient } from "./getPublicClient";

export const getUserNonce = async (
payload: {
account: string;
type: ERC2771Type;
client: WalletClient;
client: PublicOrWalletClient;
},
config: Config
): Promise<bigint> => {
const { account, type, client } = payload;

const chainId = BigInt(await client.getChainId());

const publicClient = client.extend(publicActions);
const publicClient = getPublicClient(client);

const nonce = await publicClient.readContract({
abi: parseAbi(USER_NONCE_ABI),
Expand Down
6 changes: 3 additions & 3 deletions src/utils/isSigner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WalletClient } from "viem";
import { PublicOrWalletClient } from "../lib/types";

export const isLocalSigner = (client: WalletClient): boolean => {
return client?.account?.type === "local";
export const isWalletClient = (client: PublicOrWalletClient): boolean => {
return client.type === "walletClient";
};
10 changes: 4 additions & 6 deletions src/utils/populateOptionalUserParameters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { WalletClient } from "viem";

import { DEFAULT_DEADLINE_GAP } from "../constants";
import {
CallWithConcurrentERC2771Request,
Expand All @@ -8,7 +6,7 @@ import {
CallWithERC2771RequestOptionalParameters,
ERC2771Type,
} from "../lib/erc2771/types";
import { Config } from "../lib/types";
import { Config, PublicOrWalletClient } from "../lib/types";

import { calculateDeadline } from "./calculateDeadline";
import { getUserNonce } from "./getUserNonce";
Expand All @@ -21,7 +19,7 @@ export async function populateOptionalUserParameters(
type:
| ERC2771Type.ConcurrentCallWithSyncFee
| ERC2771Type.ConcurrentSponsoredCall;
client?: WalletClient;
client?: PublicOrWalletClient;
},

config: Config
Expand All @@ -31,7 +29,7 @@ export async function populateOptionalUserParameters(
payload: {
request: CallWithERC2771Request;
type: ERC2771Type.CallWithSyncFee | ERC2771Type.SponsoredCall;
client?: WalletClient;
client?: PublicOrWalletClient;
},

config: Config
Expand All @@ -41,7 +39,7 @@ export async function populateOptionalUserParameters(
payload: {
request: CallWithConcurrentERC2771Request | CallWithERC2771Request;
type: ERC2771Type;
client?: WalletClient;
client?: PublicOrWalletClient;
},

config: Config
Expand Down
11 changes: 6 additions & 5 deletions src/utils/signTypedDataV4.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { WalletClient } from "viem";

import { PublicOrWalletClient } from "../lib/types";
import {
CallWithSyncFeeERC2771PayloadToSign,
SponsoredCallERC2771PayloadToSign,
SponsoredCallConcurrentERC2771PayloadToSign,
CallWithSyncFeeConcurrentERC2771PayloadToSign,
} from "../lib/erc2771/types";

import { isWalletClient } from "./isSigner";

export const signTypedDataV4 = async (
client: WalletClient,
client: PublicOrWalletClient,
payload:
| SponsoredCallERC2771PayloadToSign
| CallWithSyncFeeERC2771PayloadToSign
| SponsoredCallConcurrentERC2771PayloadToSign
| CallWithSyncFeeConcurrentERC2771PayloadToSign
): Promise<string> => {
if (!client.account) {
if (!isWalletClient(client) || !client.account) {
throw new Error(
"Account not found on client. Please, provide an account during the client creation."
"The provided client is not a wallet client, or account not found on the client. Please, provide an account during the client creation."
);
}

Expand Down

0 comments on commit b7c796f

Please sign in to comment.