Skip to content

Commit

Permalink
refactor: branded type
Browse files Browse the repository at this point in the history
  • Loading branch information
palace22 committed May 30, 2024
1 parent f917d57 commit 310f687
Show file tree
Hide file tree
Showing 29 changed files with 254 additions and 209 deletions.
26 changes: 13 additions & 13 deletions src/chains/evm/common/types/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { GenericAddress } from "../../../../common/types/address.js";
import type { MessageAdapters } from "../../../../common/types/message.js";
import type { SpokeTokenData } from "../../../../common/types/token.js";
import type { Address } from "viem";

export type PrepareCall = {
adapters: MessageAdapters;
Expand All @@ -12,57 +12,57 @@ export type PrepareCall = {
};

export type PrepareCreateAccountCall = {
spokeCommonAddress: Address;
spokeCommonAddress: GenericAddress;
} & PrepareCall;

export type PrepareInviteAddressCall = {
spokeCommonAddress: Address;
spokeCommonAddress: GenericAddress;
} & PrepareCall;

export type PrepareAcceptInviteAddressCall = {
spokeCommonAddress: Address;
spokeCommonAddress: GenericAddress;
} & PrepareCall;

export type PrepareUnregisterAddressCall = {
spokeCommonAddress: Address;
spokeCommonAddress: GenericAddress;
} & PrepareCall;

export type PrepareAddDelegateCall = {
spokeCommonAddress: Address;
spokeCommonAddress: GenericAddress;
} & PrepareCall;

export type PrepareRemoveDelegateCall = {
spokeCommonAddress: Address;
spokeCommonAddress: GenericAddress;
} & PrepareCall;

export type PrepareCreateLoanCall = {
spokeCommonAddress: Address;
spokeCommonAddress: GenericAddress;
} & PrepareCall;

export type PrepareDeleteLoanCall = {
spokeCommonAddress: Address;
spokeCommonAddress: GenericAddress;
} & PrepareCall;

export type PrepareDepositCall = {
token: SpokeTokenData;
} & PrepareCall;

export type PrepareWithdrawCall = {
spokeCommonAddress: Address;
spokeCommonAddress: GenericAddress;
} & PrepareCall;

export type PrepareBorrowCall = {
spokeCommonAddress: Address;
spokeCommonAddress: GenericAddress;
} & PrepareCall;

export type PrepareRepayCall = {
token: SpokeTokenData;
} & PrepareCall;

export type PrepareRepayWithCollateralCall = {
spokeCommonAddress: Address;
spokeCommonAddress: GenericAddress;
} & PrepareCall;

export type PrepareSwitchBorrowTypeCall = {
spokeCommonAddress: Address;
spokeCommonAddress: GenericAddress;
} & PrepareCall;
7 changes: 4 additions & 3 deletions src/chains/evm/common/utils/chain.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { EVM_FOLKS_CHAIN_ID } from "../constants/chain.js";

import type { EvmAddress } from "../../../../common/types/address.js";
import type { EvmChainId } from "../types/chain.js";
import type { Account, Address, WalletClient } from "viem";
import type { Account, WalletClient } from "viem";

export function getEvmSignerAddress(signer: WalletClient): Address {
if (signer.account?.address) return signer.account.address;
export function getEvmSignerAddress(signer: WalletClient): EvmAddress {
if (signer.account?.address) return signer.account.address as EvmAddress;
throw new Error("EVM Signer address is not set");
}

Expand Down
11 changes: 7 additions & 4 deletions src/chains/evm/common/utils/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import { WormholeDataAdapterAbi } from "../constants/abi/wormhole-data-adapter-a

import { getEvmSignerAccount, getEvmSignerAddress } from "./chain.js";

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

export function getERC20Contract(
provider: Client,
Expand All @@ -27,7 +30,7 @@ export async function sendERC20Approve(
provider: Client,
address: GenericAddress,
signer: WalletClient,
receiver: Address,
receiver: EvmAddress,
amount: bigint,
) {
const erc20 = getERC20Contract(provider, address, signer);
Expand All @@ -46,7 +49,7 @@ export async function sendERC20Approve(

export function getWormholeDataAdapterContract(
provider: Client,
address: Address,
address: GenericAddress,
): GetReadContractReturnType<typeof WormholeDataAdapterAbi> {
return getContract({
abi: WormholeDataAdapterAbi,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/evm/common/utils/gmp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "../../../../common/constants/bytes.js";
import { convertNumberToBytes } from "../../../../common/utils/bytes.js";

import type { GenericAddress } from "../../../../common/types/chain.js";
import type { GenericAddress } from "../../../../common/types/address.js";
import type { AdapterType } from "../../../../common/types/message.js";
import type { Hex } from "viem";

Expand Down
13 changes: 9 additions & 4 deletions src/chains/evm/common/utils/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Action } from "../../../../common/types/message.js";
import { TokenType } from "../../../../common/types/token.js";
import {
getRandomGenericAddress,
isAccountId,
isGenericAddress,
} from "../../../../common/utils/address.js";
import {
Expand All @@ -23,7 +24,11 @@ import { exhaustiveCheck } from "../../../../utils/exhaustive-check.js";
import { getWormholeDataAdapterContract } from "./contract.js";
import { encodeWormholeEvmPayloadWithMetadata } from "./gmp.js";

import type { GenericAddress } from "../../../../common/types/chain.js";
import type {
EvmAddress,
GenericAddress,
} from "../../../../common/types/address.js";
import type { AccountId } from "../../../../common/types/lending.js";
import type {
MessageAdapters,
MessageBuilderParams,
Expand All @@ -44,11 +49,11 @@ export const DEFAULT_MESSAGE_PARAMS = (

export function buildMessagePayload(
action: Action,
accountId: Hex,
accountId: AccountId,
userAddr: GenericAddress,
data: Hex,
): Hex {
if (!isGenericAddress(accountId)) throw Error("Unknown account id format");
if (!isAccountId(accountId)) throw Error("Unknown account id format");
if (!isGenericAddress(userAddr)) throw Error("Unknown user address format");
if (!isHex(data)) throw Error("Unknown data format");

Expand Down Expand Up @@ -377,7 +382,7 @@ export async function estimateEVMWormholeDataGasLimit(
receiverValue: bigint,
returnGasLimit: bigint,
sourceWormholeChainId: number,
wormholeRelayer: GenericAddress,
wormholeRelayer: EvmAddress,
wormholeDataAdapterAddress: GenericAddress,
sourceWormholeDataAdapterAddress: GenericAddress,
) {
Expand Down
60 changes: 32 additions & 28 deletions src/chains/evm/hub/constants/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,78 @@ import { LoanType } from "../../../../common/types/module.js";
import { FolksTokenId, TokenType } from "../../../../common/types/token.js";
import { convertToGenericAddress } from "../../../../common/utils/address.js";

import type {
EvmAddress,
GenericAddress,
} from "../../../../common/types/address.js";
import type { FolksChainId } from "../../../../common/types/chain.js";
import type { HubChain } from "../types/chain.js";
import type { HubTokenData } from "../types/token.js";

export const HUB_CHAIN: Record<NetworkType, HubChain> = {
[NetworkType.MAINNET]: {
folksChainId: 0 as FolksChainId,
hubAddress: "0x",
bridgeRouterAddress: "0x",
hubAddress: "0x" as GenericAddress,
bridgeRouterAddress: "0x" as GenericAddress,
adapters: {
[AdapterType.HUB]: "0x",
[AdapterType.WORMHOLE_DATA]: "0x",
[AdapterType.WORMHOLE_CCTP]: "0x",
[AdapterType.CCIP_DATA]: "0x",
[AdapterType.CCIP_TOKEN]: "0x",
[AdapterType.HUB]: "0x" as GenericAddress,
[AdapterType.WORMHOLE_DATA]: "0x" as GenericAddress,
[AdapterType.WORMHOLE_CCTP]: "0x" as GenericAddress,
[AdapterType.CCIP_DATA]: "0x" as GenericAddress,
[AdapterType.CCIP_TOKEN]: "0x" as GenericAddress,
},
oracleManagerAddress: "0x",
spokeManagerAddress: "0x",
accountManagerAddress: "0x",
loanManagerAddress: "0x",
oracleManagerAddress: "0x" as GenericAddress,
spokeManagerAddress: "0x" as GenericAddress,
accountManagerAddress: "0x" as GenericAddress,
loanManagerAddress: "0x" as GenericAddress,
tokens: {} as Record<FolksTokenId, HubTokenData>,
},
[NetworkType.TESTNET]: {
folksChainId: FOLKS_CHAIN_ID.AVALANCHE_FUJI,
hubAddress: convertToGenericAddress(
"0xaFcA3bE824b6210918D3BeB63762D6211f1e91C3",
"0xaFcA3bE824b6210918D3BeB63762D6211f1e91C3" as EvmAddress,
ChainType.EVM,
),
bridgeRouterAddress: convertToGenericAddress(
"0x46Db2e9cD0787bF791Df2c9AE9963E296847FF1D",
"0x46Db2e9cD0787bF791Df2c9AE9963E296847FF1D" as EvmAddress,
ChainType.EVM,
),
adapters: {
[AdapterType.HUB]: convertToGenericAddress(
"0xB01296Ea267463FDe2fcE5Fad5067B4d875A44Ba",
"0xB01296Ea267463FDe2fcE5Fad5067B4d875A44Ba" as EvmAddress,
ChainType.EVM,
),
[AdapterType.WORMHOLE_DATA]: convertToGenericAddress(
"0x7A6099E5cE3b66B042c9d11c3D472882bd42e23C",
"0x7A6099E5cE3b66B042c9d11c3D472882bd42e23C" as EvmAddress,
ChainType.EVM,
),
[AdapterType.WORMHOLE_CCTP]: convertToGenericAddress(
"0xf7EB478F95470caF349d999047e1D4A713aD7a7f",
"0xf7EB478F95470caF349d999047e1D4A713aD7a7f" as EvmAddress,
ChainType.EVM,
),
[AdapterType.CCIP_DATA]: convertToGenericAddress(
"0x498d72950d7cf912Be48BA5C8894e98A81E204fc",
"0x498d72950d7cf912Be48BA5C8894e98A81E204fc" as EvmAddress,
ChainType.EVM,
),
[AdapterType.CCIP_TOKEN]: convertToGenericAddress(
"0x715Cd24a347552ae07e7d11Df2a59FFcEb2A9b66",
"0x715Cd24a347552ae07e7d11Df2a59FFcEb2A9b66" as EvmAddress,
ChainType.EVM,
),
},
oracleManagerAddress: convertToGenericAddress(
"0xc9cb1F8FcfBB804669d44349d44fB14BE4c665F0",
"0xc9cb1F8FcfBB804669d44349d44fB14BE4c665F0" as EvmAddress,
ChainType.EVM,
),
spokeManagerAddress: convertToGenericAddress(
"0xf27720C8B9C28d8E23bAA0A64347323FBB151CeD",
"0xf27720C8B9C28d8E23bAA0A64347323FBB151CeD" as EvmAddress,
ChainType.EVM,
),
accountManagerAddress: convertToGenericAddress(
"0x5Ff19CF35875C973F63a60e78445F449292c5575",
"0x5Ff19CF35875C973F63a60e78445F449292c5575" as EvmAddress,
ChainType.EVM,
),
loanManagerAddress: convertToGenericAddress(
"0x24f0a8f4D41E8CBe18676F75e0d11b105d1cc0A6",
"0x24f0a8f4D41E8CBe18676F75e0d11b105d1cc0A6" as EvmAddress,
ChainType.EVM,
),
tokens: {
Expand All @@ -82,11 +86,11 @@ export const HUB_CHAIN: Record<NetworkType, HubChain> = {
folksTokenId: FolksTokenId.USDC,
poolId: TESTNET_POOLS[FolksTokenId.USDC],
poolAddress: convertToGenericAddress(
"0xA9F3dfff0E8939514E7C4A0F8CeB0dBED93BbEA5",
"0xA9F3dfff0E8939514E7C4A0F8CeB0dBED93BbEA5" as EvmAddress,
ChainType.EVM,
),
tokenAddress: convertToGenericAddress(
"0x5425890298aed601595a70ab815c96711a31bc65",
"0x5425890298aed601595a70ab815c96711a31bc65" as EvmAddress,
ChainType.EVM,
),
tokenDecimals: 6,
Expand All @@ -97,7 +101,7 @@ export const HUB_CHAIN: Record<NetworkType, HubChain> = {
folksTokenId: FolksTokenId.AVAX,
poolId: TESTNET_POOLS[FolksTokenId.AVAX],
poolAddress: convertToGenericAddress(
"0x0922880C7e18112aB479E85Fc190Ba666c3F1020",
"0x0922880C7e18112aB479E85Fc190Ba666c3F1020" as EvmAddress,
ChainType.EVM,
),
tokenAddress: null,
Expand All @@ -109,7 +113,7 @@ export const HUB_CHAIN: Record<NetworkType, HubChain> = {
folksTokenId: FolksTokenId.ETH_eth_sep,
poolId: TESTNET_POOLS[FolksTokenId.ETH_eth_sep],
poolAddress: convertToGenericAddress(
"0x58ad9F0e5Ced36401E36594C3265FA7475f24B3d",
"0x58ad9F0e5Ced36401E36594C3265FA7475f24B3d" as EvmAddress,
ChainType.EVM,
),
tokenAddress: null,
Expand All @@ -121,7 +125,7 @@ export const HUB_CHAIN: Record<NetworkType, HubChain> = {
folksTokenId: FolksTokenId.ETH_base_sep,
poolId: TESTNET_POOLS[FolksTokenId.ETH_base_sep],
poolAddress: convertToGenericAddress(
"0x9c0D98AFAfB59F3e30F1d3B3221D59ac3A159e0b",
"0x9c0D98AFAfB59F3e30F1d3B3221D59ac3A159e0b" as EvmAddress,
ChainType.EVM,
),
tokenAddress: null,
Expand All @@ -133,7 +137,7 @@ export const HUB_CHAIN: Record<NetworkType, HubChain> = {
folksTokenId: FolksTokenId.LINK_eth_sep,
poolId: TESTNET_POOLS[FolksTokenId.LINK_eth_sep],
poolAddress: convertToGenericAddress(
"0xc276f7e429F46346c668E1896e527baAD4D21414",
"0xc276f7e429F46346c668E1896e527baAD4D21414" as EvmAddress,
ChainType.EVM,
),
tokenAddress: null,
Expand Down
12 changes: 7 additions & 5 deletions src/chains/evm/hub/modules/folks-hub-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import { getFolksChainIdsByNetwork } from "../../../../common/utils/chain.js";
import { getHubChain } from "../utils/chain.js";
import { getAccountManagerContract } from "../utils/contract.js";

import type { EvmAddress } from "../../../../common/types/address.js";
import type {
NetworkType,
FolksChainId,
NetworkType,
} from "../../../../common/types/chain.js";
import type { AccountId } from "../../../../common/types/lending.js";
import type { AccountInfo } from "../types/account.js";
import type { Address, Client, Hex } from "viem";
import type { Client } from "viem";

export async function getAccountInfo(
provider: Client,
network: NetworkType,
accountId: Hex,
accountId: AccountId,
folksChainIds?: Array<FolksChainId>,
): Promise<AccountInfo> {
const hubChain = getHubChain(network);
Expand Down Expand Up @@ -56,12 +58,12 @@ export async function getAccountInfo(
for (const [index, result] of registeredAddresses.entries()) {
const chainId = folksChainIds[index];
if (result.status === "success")
accountInfo.registered.set(chainId, result.result as Address);
accountInfo.registered.set(chainId, result.result as EvmAddress);
}
for (const [index, result] of invitedAddresses.entries()) {
const chainId = folksChainIds[index];
if (result.status === "success")
accountInfo.invited.set(chainId, result.result as Address);
accountInfo.invited.set(chainId, result.result as EvmAddress);
}

return accountInfo;
Expand Down
5 changes: 3 additions & 2 deletions src/chains/evm/hub/modules/folks-hub-loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ import type {
FolksChainId,
NetworkType,
} from "../../../../common/types/chain.js";
import type { AccountId } from "../../../../common/types/lending.js";
import type {
MessageAdapters,
MessageToSend,
} from "../../../../common/types/message.js";
import type { FolksTokenId } from "../../../../common/types/token.js";
import type { Client, Hex } from "viem";
import type { Client } from "viem";

export function getSendTokenAdapterFees(
provider: Client,
network: NetworkType,
accountId: Hex,
accountId: AccountId,
folksTokenId: FolksTokenId,
amount: bigint,
receiverFolksChainId: FolksChainId,
Expand Down
6 changes: 2 additions & 4 deletions src/chains/evm/hub/types/chain.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { HubTokenData } from "./token.js";
import type {
GenericAddress,
IFolksChain,
} from "../../../../common/types/chain.js";
import type { GenericAddress } from "../../../../common/types/address.js";
import type { IFolksChain } from "../../../../common/types/chain.js";
import type { AdapterType } from "../../../../common/types/message.js";
import type { FolksTokenId } from "../../../../common/types/token.js";

Expand Down
Loading

0 comments on commit 310f687

Please sign in to comment.