Skip to content

Commit

Permalink
refactor: use client instead of public-client (#19)
Browse files Browse the repository at this point in the history
Replace `PublicClient` with `Client` to relax the required type and
allow tree-shaking.

https://viem.sh/docs/clients/custom#tree-shaking
  • Loading branch information
stefanofa authored May 27, 2024
1 parent bd09efc commit 95a18d1
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 62 deletions.
4 changes: 2 additions & 2 deletions src/chains/evm/common/types/contract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Abi, GetContractReturnType, PublicClient } from "viem";
import type { Abi, GetContractReturnType, Client } from "viem";

type OmitWrite<T> = Omit<T, "write">;
export type GetReadContractReturnType<TAbi extends Abi> = OmitWrite<
GetContractReturnType<TAbi, PublicClient>
GetContractReturnType<TAbi, Client>
>;
6 changes: 3 additions & 3 deletions src/chains/evm/common/utils/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { ERC20Abi } from "../constants/abi/erc-20-abi.js";
import { getSignerAccount, getSignerAddress } from "./chain.js";

import type { GenericAddress } from "../../../../common/types/chain.js";
import type { Address, PublicClient, WalletClient } from "viem";
import type { Address, Client, WalletClient } from "viem";

export function getERC20Contract(
provider: PublicClient,
provider: Client,
address: GenericAddress,
signer: WalletClient,
) {
Expand All @@ -22,7 +22,7 @@ export function getERC20Contract(
}

export async function sendERC20Approve(
provider: PublicClient,
provider: Client,
address: GenericAddress,
signer: WalletClient,
receiver: Address,
Expand Down
14 changes: 7 additions & 7 deletions src/chains/evm/common/utils/provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createPublicClient, fallback, http } from "viem";
import { createClient, fallback, http } from "viem";

import {
CHAIN_VIEM,
Expand All @@ -10,28 +10,28 @@ import { isEvmChainId } from "./chain.js";

import type { FolksChainId } from "../../../../common/types/chain.js";
import type { EvmChainId } from "../types/chain.js";
import type { PublicClient } from "viem";
import type { Client } from "viem";

export function initProviders(
customProvider: Partial<Record<FolksChainId, PublicClient>>,
): Record<FolksChainId, PublicClient> {
customProvider: Partial<Record<FolksChainId, Client>>,
): Record<FolksChainId, Client> {
return Object.fromEntries(
Object.values(EVM_FOLKS_CHAIN_ID).map((evmFolksChainId) => {
return [
evmFolksChainId,
customProvider[evmFolksChainId] ??
createPublicClient({
createClient({
chain: CHAIN_VIEM[evmFolksChainId],
transport: fallback(
CHAIN_NODE[evmFolksChainId].map((url: string) => http(url)),
),
}),
];
}),
) as Record<FolksChainId, PublicClient>;
) as Record<FolksChainId, Client>;
}

export function getChainId(provider: PublicClient): EvmChainId {
export function getChainId(provider: Client): EvmChainId {
const chainId = provider.chain?.id;
if (chainId === undefined)
throw new Error("EVM provider chain id is undefined");
Expand Down
10 changes: 6 additions & 4 deletions src/chains/evm/hub/modules/folks-hub-account.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { multicall } from "viem/actions";

import { getFolksChainIdsByNetwork } from "../../../../common/utils/chain.js";
import { getHubChain } from "../utils/chain.js";
import { getAccountManagerContract } from "../utils/contract.js";
Expand All @@ -7,10 +9,10 @@ import type {
FolksChainId,
} from "../../../../common/types/chain.js";
import type { AccountInfo } from "../types/account.js";
import type { Address, Hex, PublicClient } from "viem";
import type { Address, Client, Hex } from "viem";

export async function getAccountInfo(
provider: PublicClient,
provider: Client,
network: NetworkType,
accountId: Hex,
folksChainIds?: Array<FolksChainId>,
Expand All @@ -31,7 +33,7 @@ export async function getAccountInfo(
};

// query for registered and invited addresses on each respective chain
const registeredAddresses = await provider.multicall({
const registeredAddresses = await multicall(provider, {
contracts: folksChainIds.map((folksChainId) => ({
address: accountManager.address,
abi: accountManager.abi,
Expand All @@ -41,7 +43,7 @@ export async function getAccountInfo(
allowFailure: true,
});

const invitedAddresses = await provider.multicall({
const invitedAddresses = await multicall(provider, {
contracts: folksChainIds.map((folksChainId) => ({
address: accountManager.address,
abi: accountManager.abi,
Expand Down
4 changes: 2 additions & 2 deletions src/chains/evm/hub/modules/folks-hub-loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import type {
MessageToSend,
} from "../../../../common/types/message.js";
import type { FolksTokenId } from "../../../../common/types/token.js";
import type { Hex, PublicClient } from "viem";
import type { Client, Hex } from "viem";

export function getSendTokenAdapterFees(
provider: PublicClient,
provider: Client,
network: NetworkType,
accountId: Hex,
folksTokenId: FolksTokenId,
Expand Down
6 changes: 3 additions & 3 deletions src/chains/evm/hub/utils/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { BridgeRouterHubAbi } from "../constants/abi/bridge-router-hub-abi.js";

import type { GenericAddress } from "../../../../common/types/chain.js";
import type { GetReadContractReturnType } from "../../common/types/contract.js";
import type { Address, PublicClient, WalletClient } from "viem";
import type { Address, Client, WalletClient } from "viem";

export function getAccountManagerContract(
provider: PublicClient,
provider: Client,
address: Address,
signer?: WalletClient,
): GetReadContractReturnType<typeof AccountManagerAbi> {
Expand All @@ -22,7 +22,7 @@ export function getAccountManagerContract(
}

export function getBridgeRouterHubContract(
provider: PublicClient,
provider: Client,
address: GenericAddress,
): GetReadContractReturnType<typeof BridgeRouterHubAbi> {
return getContract({
Expand Down
26 changes: 13 additions & 13 deletions src/chains/evm/spoke/modules/folks-evm-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ import type {
} from "../../common/types/module.js";
import type {
Address,
Client,
EstimateGasParameters,
Hex,
PublicClient,
WalletClient,
} from "viem";

export const prepare = {
async createAccount(
folksChainId: FolksChainId,
provider: PublicClient,
provider: Client,
sender: Address,
network: NetworkType,
accountId: Hex,
Expand All @@ -70,7 +70,7 @@ export const prepare = {

async inviteAddress(
folksChainId: FolksChainId,
provider: PublicClient,
provider: Client,
sender: Address,
network: NetworkType,
accountId: Hex,
Expand All @@ -96,7 +96,7 @@ export const prepare = {

async acceptInvite(
folksChainId: FolksChainId,
provider: PublicClient,
provider: Client,
sender: Address,
network: NetworkType,
accountId: Hex,
Expand All @@ -118,7 +118,7 @@ export const prepare = {

async unregisterAddress(
folksChainId: FolksChainId,
provider: PublicClient,
provider: Client,
sender: Address,
network: NetworkType,
accountId: Hex,
Expand All @@ -143,7 +143,7 @@ export const prepare = {

export const prepareRaw = {
async createAccount(
provider: PublicClient,
provider: Client,
sender: Address,
network: NetworkType,
accountId: Hex,
Expand Down Expand Up @@ -205,7 +205,7 @@ export const prepareRaw = {
},

async inviteAddress(
provider: PublicClient,
provider: Client,
sender: Address,
network: NetworkType,
accountId: Hex,
Expand Down Expand Up @@ -275,7 +275,7 @@ export const prepareRaw = {
},

async acceptInvite(
provider: PublicClient,
provider: Client,
sender: Address,
network: NetworkType,
accountId: Hex,
Expand Down Expand Up @@ -337,7 +337,7 @@ export const prepareRaw = {
},

async unregisterAddress(
provider: PublicClient,
provider: Client,
sender: Address,
network: NetworkType,
accountId: Hex,
Expand Down Expand Up @@ -401,7 +401,7 @@ export const prepareRaw = {

export const write = {
async createAccount(
provider: PublicClient,
provider: Client,
signer: WalletClient,
accountId: Hex,
prepareCall: PrepareCreateAccountCall,
Expand Down Expand Up @@ -438,7 +438,7 @@ export const write = {
},

async inviteAddress(
provider: PublicClient,
provider: Client,
signer: WalletClient,
accountId: Hex,
folksChainIdToInvite: number,
Expand Down Expand Up @@ -480,7 +480,7 @@ export const write = {
},

async acceptInvite(
provider: PublicClient,
provider: Client,
signer: WalletClient,
accountId: Hex,
prepareCall: PrepareAcceptInviteAddressCall,
Expand Down Expand Up @@ -517,7 +517,7 @@ export const write = {
},

async unregisterAddress(
provider: PublicClient,
provider: Client,
signer: WalletClient,
accountId: Hex,
folksChainIdToUnregister: FolksChainId,
Expand Down
26 changes: 13 additions & 13 deletions src/chains/evm/spoke/modules/folks-evm-loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ import type {
Address,
EstimateGasParameters,
Hex,
PublicClient,
Client,
WalletClient,
} from "viem";

export const prepare = {
async createLoan(
folksChainId: FolksChainId,
provider: PublicClient,
provider: Client,
sender: Address,
network: NetworkType,
accountId: Hex,
Expand All @@ -85,7 +85,7 @@ export const prepare = {

async deleteLoan(
folksChainId: FolksChainId,
provider: PublicClient,
provider: Client,
sender: Address,
network: NetworkType,
accountId: Hex,
Expand All @@ -109,7 +109,7 @@ export const prepare = {

async deposit(
folksChainId: FolksChainId,
provider: PublicClient,
provider: Client,
sender: Address,
network: NetworkType,
accountId: Hex,
Expand Down Expand Up @@ -137,7 +137,7 @@ export const prepare = {

async withdraw(
folksChainId: FolksChainId,
provider: PublicClient,
provider: Client,
sender: Address,
network: NetworkType,
accountId: Hex,
Expand Down Expand Up @@ -172,7 +172,7 @@ export const prepare = {

export const prepareRaw = {
async createLoan(
provider: PublicClient,
provider: Client,
sender: Address,
network: NetworkType,
accountId: Hex,
Expand Down Expand Up @@ -236,7 +236,7 @@ export const prepareRaw = {
},

async deleteLoan(
provider: PublicClient,
provider: Client,
sender: Address,
network: NetworkType,
accountId: Hex,
Expand Down Expand Up @@ -299,7 +299,7 @@ export const prepareRaw = {
},

async deposit(
provider: PublicClient,
provider: Client,
sender: Address,
network: NetworkType,
accountId: Hex,
Expand Down Expand Up @@ -376,7 +376,7 @@ export const prepareRaw = {
},

async withdraw(
provider: PublicClient,
provider: Client,
sender: Address,
network: NetworkType,
accountId: Hex,
Expand Down Expand Up @@ -462,7 +462,7 @@ export const prepareRaw = {

export const write = {
async createLoan(
provider: PublicClient,
provider: Client,
signer: WalletClient,
accountId: Hex,
loanId: Hex,
Expand Down Expand Up @@ -504,7 +504,7 @@ export const write = {
},

async deleteLoan(
provider: PublicClient,
provider: Client,
signer: WalletClient,
accountId: Hex,
loanId: Hex,
Expand Down Expand Up @@ -542,7 +542,7 @@ export const write = {
},

async deposit(
provider: PublicClient,
provider: Client,
signer: WalletClient,
accountId: Hex,
loanId: Hex,
Expand Down Expand Up @@ -591,7 +591,7 @@ export const write = {
},

async withdraw(
provider: PublicClient,
provider: Client,
signer: WalletClient,
accountId: Hex,
loanId: Hex,
Expand Down
Loading

0 comments on commit 95a18d1

Please sign in to comment.