From 310f6870040c6405a974501b2bb36fc4f5e8d4ec Mon Sep 17 00:00:00 2001 From: palace22 Date: Thu, 30 May 2024 14:09:27 +0200 Subject: [PATCH] refactor: branded type --- src/chains/evm/common/types/module.ts | 26 ++++---- src/chains/evm/common/utils/chain.ts | 7 ++- src/chains/evm/common/utils/contract.ts | 11 ++-- src/chains/evm/common/utils/gmp.ts | 2 +- src/chains/evm/common/utils/message.ts | 13 ++-- src/chains/evm/hub/constants/chain.ts | 60 ++++++++++--------- .../evm/hub/modules/folks-hub-account.ts | 12 ++-- src/chains/evm/hub/modules/folks-hub-loan.ts | 5 +- src/chains/evm/hub/types/chain.ts | 6 +- src/chains/evm/hub/types/token.ts | 2 +- src/chains/evm/hub/utils/chain.ts | 2 +- src/chains/evm/hub/utils/contract.ts | 6 +- .../evm/spoke/modules/folks-evm-account.ts | 38 ++++++------ .../evm/spoke/modules/folks-evm-loan.ts | 57 +++++++++--------- src/chains/evm/spoke/utils/contract.ts | 10 +++- src/common/constants/chain.ts | 53 ++++++++-------- src/common/constants/gmp.ts | 7 ++- src/common/types/address.ts | 7 ++- src/common/types/brand.ts | 3 + src/common/types/chain.ts | 3 +- src/common/types/gmp.ts | 2 +- src/common/types/lending.ts | 4 ++ src/common/types/message.ts | 16 ++--- src/common/types/token.ts | 2 +- src/common/utils/address.ts | 23 ++++--- src/common/utils/chain.ts | 4 +- src/common/utils/messages.ts | 8 +-- src/xchain/modules/folks-account.ts | 38 ++++++++---- src/xchain/modules/folks-loan.ts | 36 ++++++----- 29 files changed, 254 insertions(+), 209 deletions(-) create mode 100644 src/common/types/brand.ts create mode 100644 src/common/types/lending.ts diff --git a/src/chains/evm/common/types/module.ts b/src/chains/evm/common/types/module.ts index 746e462..24871a8 100644 --- a/src/chains/evm/common/types/module.ts +++ b/src/chains/evm/common/types/module.ts @@ -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; @@ -12,35 +12,35 @@ 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 = { @@ -48,11 +48,11 @@ export type PrepareDepositCall = { } & PrepareCall; export type PrepareWithdrawCall = { - spokeCommonAddress: Address; + spokeCommonAddress: GenericAddress; } & PrepareCall; export type PrepareBorrowCall = { - spokeCommonAddress: Address; + spokeCommonAddress: GenericAddress; } & PrepareCall; export type PrepareRepayCall = { @@ -60,9 +60,9 @@ export type PrepareRepayCall = { } & PrepareCall; export type PrepareRepayWithCollateralCall = { - spokeCommonAddress: Address; + spokeCommonAddress: GenericAddress; } & PrepareCall; export type PrepareSwitchBorrowTypeCall = { - spokeCommonAddress: Address; + spokeCommonAddress: GenericAddress; } & PrepareCall; diff --git a/src/chains/evm/common/utils/chain.ts b/src/chains/evm/common/utils/chain.ts index 2979bdb..9f6bb09 100644 --- a/src/chains/evm/common/utils/chain.ts +++ b/src/chains/evm/common/utils/chain.ts @@ -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"); } diff --git a/src/chains/evm/common/utils/contract.ts b/src/chains/evm/common/utils/contract.ts index 73deed6..85f8b4d 100644 --- a/src/chains/evm/common/utils/contract.ts +++ b/src/chains/evm/common/utils/contract.ts @@ -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, @@ -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); @@ -46,7 +49,7 @@ export async function sendERC20Approve( export function getWormholeDataAdapterContract( provider: Client, - address: Address, + address: GenericAddress, ): GetReadContractReturnType { return getContract({ abi: WormholeDataAdapterAbi, diff --git a/src/chains/evm/common/utils/gmp.ts b/src/chains/evm/common/utils/gmp.ts index 867c46e..951723a 100644 --- a/src/chains/evm/common/utils/gmp.ts +++ b/src/chains/evm/common/utils/gmp.ts @@ -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"; diff --git a/src/chains/evm/common/utils/message.ts b/src/chains/evm/common/utils/message.ts index 8a168bc..052dcab 100644 --- a/src/chains/evm/common/utils/message.ts +++ b/src/chains/evm/common/utils/message.ts @@ -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 { @@ -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, @@ -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"); @@ -377,7 +382,7 @@ export async function estimateEVMWormholeDataGasLimit( receiverValue: bigint, returnGasLimit: bigint, sourceWormholeChainId: number, - wormholeRelayer: GenericAddress, + wormholeRelayer: EvmAddress, wormholeDataAdapterAddress: GenericAddress, sourceWormholeDataAdapterAddress: GenericAddress, ) { diff --git a/src/chains/evm/hub/constants/chain.ts b/src/chains/evm/hub/constants/chain.ts index 20937b3..afa7111 100644 --- a/src/chains/evm/hub/constants/chain.ts +++ b/src/chains/evm/hub/constants/chain.ts @@ -6,6 +6,10 @@ 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"; @@ -13,67 +17,67 @@ import type { HubTokenData } from "../types/token.js"; export const HUB_CHAIN: Record = { [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, }, [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: { @@ -82,11 +86,11 @@ export const HUB_CHAIN: Record = { 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, @@ -97,7 +101,7 @@ export const HUB_CHAIN: Record = { folksTokenId: FolksTokenId.AVAX, poolId: TESTNET_POOLS[FolksTokenId.AVAX], poolAddress: convertToGenericAddress( - "0x0922880C7e18112aB479E85Fc190Ba666c3F1020", + "0x0922880C7e18112aB479E85Fc190Ba666c3F1020" as EvmAddress, ChainType.EVM, ), tokenAddress: null, @@ -109,7 +113,7 @@ export const HUB_CHAIN: Record = { folksTokenId: FolksTokenId.ETH_eth_sep, poolId: TESTNET_POOLS[FolksTokenId.ETH_eth_sep], poolAddress: convertToGenericAddress( - "0x58ad9F0e5Ced36401E36594C3265FA7475f24B3d", + "0x58ad9F0e5Ced36401E36594C3265FA7475f24B3d" as EvmAddress, ChainType.EVM, ), tokenAddress: null, @@ -121,7 +125,7 @@ export const HUB_CHAIN: Record = { folksTokenId: FolksTokenId.ETH_base_sep, poolId: TESTNET_POOLS[FolksTokenId.ETH_base_sep], poolAddress: convertToGenericAddress( - "0x9c0D98AFAfB59F3e30F1d3B3221D59ac3A159e0b", + "0x9c0D98AFAfB59F3e30F1d3B3221D59ac3A159e0b" as EvmAddress, ChainType.EVM, ), tokenAddress: null, @@ -133,7 +137,7 @@ export const HUB_CHAIN: Record = { folksTokenId: FolksTokenId.LINK_eth_sep, poolId: TESTNET_POOLS[FolksTokenId.LINK_eth_sep], poolAddress: convertToGenericAddress( - "0xc276f7e429F46346c668E1896e527baAD4D21414", + "0xc276f7e429F46346c668E1896e527baAD4D21414" as EvmAddress, ChainType.EVM, ), tokenAddress: null, diff --git a/src/chains/evm/hub/modules/folks-hub-account.ts b/src/chains/evm/hub/modules/folks-hub-account.ts index d55d033..35537b6 100644 --- a/src/chains/evm/hub/modules/folks-hub-account.ts +++ b/src/chains/evm/hub/modules/folks-hub-account.ts @@ -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, ): Promise { const hubChain = getHubChain(network); @@ -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; diff --git a/src/chains/evm/hub/modules/folks-hub-loan.ts b/src/chains/evm/hub/modules/folks-hub-loan.ts index 6008ed8..9f8b958 100644 --- a/src/chains/evm/hub/modules/folks-hub-loan.ts +++ b/src/chains/evm/hub/modules/folks-hub-loan.ts @@ -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, diff --git a/src/chains/evm/hub/types/chain.ts b/src/chains/evm/hub/types/chain.ts index 22d10c3..7d03811 100644 --- a/src/chains/evm/hub/types/chain.ts +++ b/src/chains/evm/hub/types/chain.ts @@ -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"; diff --git a/src/chains/evm/hub/types/token.ts b/src/chains/evm/hub/types/token.ts index 2f6f250..bbb1afb 100644 --- a/src/chains/evm/hub/types/token.ts +++ b/src/chains/evm/hub/types/token.ts @@ -1,4 +1,4 @@ -import type { GenericAddress } from "../../../../common/types/chain.js"; +import type { GenericAddress } from "../../../../common/types/address.js"; import type { LoanType } from "../../../../common/types/module.js"; import type { ITokenData, TokenType } from "../../../../common/types/token.js"; diff --git a/src/chains/evm/hub/utils/chain.ts b/src/chains/evm/hub/utils/chain.ts index b12f6d2..ba6aa64 100644 --- a/src/chains/evm/hub/utils/chain.ts +++ b/src/chains/evm/hub/utils/chain.ts @@ -1,8 +1,8 @@ import { HUB_CHAIN } from "../constants/chain.js"; +import type { GenericAddress } from "../../../../common/types/address.js"; import type { FolksChainId, - GenericAddress, NetworkType, } from "../../../../common/types/chain.js"; import type { AdapterType } from "../../../../common/types/message.js"; diff --git a/src/chains/evm/hub/utils/contract.ts b/src/chains/evm/hub/utils/contract.ts index ab82154..3d42a8b 100644 --- a/src/chains/evm/hub/utils/contract.ts +++ b/src/chains/evm/hub/utils/contract.ts @@ -5,13 +5,13 @@ import { convertFromGenericAddress } from "../../../../common/utils/address.js"; import { AccountManagerAbi } from "../constants/abi/account-manager-abi.js"; import { BridgeRouterHubAbi } from "../constants/abi/bridge-router-hub-abi.js"; -import type { GenericAddress } from "../../../../common/types/chain.js"; +import type { GenericAddress } from "../../../../common/types/address.js"; import type { GetReadContractReturnType } from "../../common/types/contract.js"; -import type { Address, Client, WalletClient } from "viem"; +import type { Client, WalletClient } from "viem"; export function getAccountManagerContract( provider: Client, - address: Address, + address: GenericAddress, signer?: WalletClient, ): GetReadContractReturnType { return getContract({ diff --git a/src/chains/evm/spoke/modules/folks-evm-account.ts b/src/chains/evm/spoke/modules/folks-evm-account.ts index 28c03cb..cea9b90 100644 --- a/src/chains/evm/spoke/modules/folks-evm-account.ts +++ b/src/chains/evm/spoke/modules/folks-evm-account.ts @@ -4,10 +4,12 @@ import { getSpokeCommonContract, } from "../utils/contract.js"; +import type { EvmAddress } from "../../../../common/types/address.js"; import type { FolksChainId, SpokeChain, } from "../../../../common/types/chain.js"; +import type { AccountId } from "../../../../common/types/lending.js"; import type { MessageAdapters, MessageParams, @@ -19,20 +21,14 @@ import type { PrepareInviteAddressCall, PrepareUnregisterAddressCall, } from "../../common/types/module.js"; -import type { - Address, - Client, - EstimateGasParameters, - Hex, - WalletClient, -} from "viem"; +import type { Client, EstimateGasParameters, WalletClient } from "viem"; export const prepare = { async createAccount( provider: Client, - sender: Address, + sender: EvmAddress, messageToSend: MessageToSend, - accountId: Hex, + accountId: AccountId, adapters: MessageAdapters, spokeChain: SpokeChain, transactionOptions: EstimateGasParameters = { account: sender }, @@ -72,11 +68,11 @@ export const prepare = { async inviteAddress( provider: Client, - sender: Address, + sender: EvmAddress, messageToSend: MessageToSend, - accountId: Hex, + accountId: AccountId, folksChainIdToInvite: number, - addressToInvite: Address, + addressToInvite: EvmAddress, adapters: MessageAdapters, spokeChain: SpokeChain, transactionOptions: EstimateGasParameters = { account: sender }, @@ -117,9 +113,9 @@ export const prepare = { async acceptInvite( provider: Client, - sender: Address, + sender: EvmAddress, messageToSend: MessageToSend, - accountId: Hex, + accountId: AccountId, adapters: MessageAdapters, spokeChain: SpokeChain, transactionOptions: EstimateGasParameters = { account: sender }, @@ -160,9 +156,9 @@ export const prepare = { async unregisterAddress( provider: Client, - sender: Address, + sender: EvmAddress, messageToSend: MessageToSend, - accountId: Hex, + accountId: AccountId, folksChainIdToUnregister: FolksChainId, adapters: MessageAdapters, spokeChain: SpokeChain, @@ -206,7 +202,7 @@ export const write = { async createAccount( provider: Client, signer: WalletClient, - accountId: Hex, + accountId: AccountId, prepareCall: PrepareCreateAccountCall, ) { const { @@ -243,9 +239,9 @@ export const write = { async inviteAddress( provider: Client, signer: WalletClient, - accountId: Hex, + accountId: AccountId, folksChainIdToInvite: number, - addressToInvite: Address, + addressToInvite: EvmAddress, prepareCall: PrepareInviteAddressCall, ) { const { @@ -285,7 +281,7 @@ export const write = { async acceptInvite( provider: Client, signer: WalletClient, - accountId: Hex, + accountId: AccountId, prepareCall: PrepareAcceptInviteAddressCall, ) { const { @@ -322,7 +318,7 @@ export const write = { async unregisterAddress( provider: Client, signer: WalletClient, - accountId: Hex, + accountId: AccountId, folksChainIdToUnregister: FolksChainId, prepareCall: PrepareUnregisterAddressCall, ) { diff --git a/src/chains/evm/spoke/modules/folks-evm-loan.ts b/src/chains/evm/spoke/modules/folks-evm-loan.ts index 6ecb147..adac32b 100644 --- a/src/chains/evm/spoke/modules/folks-evm-loan.ts +++ b/src/chains/evm/spoke/modules/folks-evm-loan.ts @@ -8,16 +8,19 @@ import { getSpokeTokenContract, } from "../utils/contract.js"; +import type { EvmAddress } from "../../../../common/types/address.js"; import type { FolksChainId, NetworkType, SpokeChain, } from "../../../../common/types/chain.js"; +import type { AccountId, LoanId } from "../../../../common/types/lending.js"; import type { MessageAdapters, MessageParams, MessageToSend, } from "../../../../common/types/message.js"; +import type { LoanType } from "../../../../common/types/module.js"; import type { FolksTokenId, SpokeTokenData, @@ -28,22 +31,16 @@ import type { PrepareDepositCall, PrepareWithdrawCall, } from "../../common/types/module.js"; -import type { - Address, - Client, - EstimateGasParameters, - Hex, - WalletClient, -} from "viem"; +import type { Client, EstimateGasParameters, WalletClient } from "viem"; export const prepare = { async createLoan( provider: Client, - sender: Address, + sender: EvmAddress, messageToSend: MessageToSend, - accountId: Hex, - loanId: Hex, - loanTypeId: number, + accountId: AccountId, + loanId: LoanId, + loanTypeId: LoanType, adapters: MessageAdapters, spokeChain: SpokeChain, transactionOptions: EstimateGasParameters = { account: sender }, @@ -84,10 +81,10 @@ export const prepare = { async deleteLoan( provider: Client, - sender: Address, + sender: EvmAddress, messageToSend: MessageToSend, - accountId: Hex, - loanId: Hex, + accountId: AccountId, + loanId: LoanId, adapters: MessageAdapters, spokeChain: SpokeChain, transactionOptions: EstimateGasParameters = { account: sender }, @@ -128,10 +125,10 @@ export const prepare = { async deposit( provider: Client, - sender: Address, + sender: EvmAddress, messageToSend: MessageToSend, - accountId: Hex, - loanId: Hex, + accountId: AccountId, + loanId: LoanId, amount: bigint, adapters: MessageAdapters, spokeChain: SpokeChain, @@ -175,11 +172,11 @@ export const prepare = { async withdraw( provider: Client, - sender: Address, + sender: EvmAddress, messageToSend: MessageToSend, network: NetworkType, - accountId: Hex, - loanId: Hex, + accountId: AccountId, + loanId: LoanId, folksTokenId: FolksTokenId, amount: bigint, isFAmount: boolean, @@ -235,9 +232,9 @@ export const write = { async createLoan( provider: Client, signer: WalletClient, - accountId: Hex, - loanId: Hex, - loanTypeId: number, + accountId: AccountId, + loanId: LoanId, + loanTypeId: LoanType, prepareCall: PrepareCreateLoanCall, ) { const { @@ -277,8 +274,8 @@ export const write = { async deleteLoan( provider: Client, signer: WalletClient, - accountId: Hex, - loanId: Hex, + accountId: AccountId, + loanId: LoanId, prepareCall: PrepareDeleteLoanCall, ) { const { @@ -315,8 +312,8 @@ export const write = { async deposit( provider: Client, signer: WalletClient, - accountId: Hex, - loanId: Hex, + accountId: AccountId, + loanId: LoanId, amount: bigint, includeApprove = true, prepareCall: PrepareDepositCall, @@ -342,7 +339,7 @@ export const write = { provider, token.spokeAddress, signer, - spokeToken.address, + spokeToken.address as EvmAddress, amount, ); @@ -364,8 +361,8 @@ export const write = { async withdraw( provider: Client, signer: WalletClient, - accountId: Hex, - loanId: Hex, + accountId: AccountId, + loanId: LoanId, poolId: number, amount: bigint, isFAmount: boolean, diff --git a/src/chains/evm/spoke/utils/contract.ts b/src/chains/evm/spoke/utils/contract.ts index c37d68e..7ab8599 100644 --- a/src/chains/evm/spoke/utils/contract.ts +++ b/src/chains/evm/spoke/utils/contract.ts @@ -6,7 +6,7 @@ import { BridgeRouterSpokeAbi } from "../constants/abi/bridge-router-spoke-abi.j import { SpokeCommonAbi } from "../constants/abi/spoke-common-abi.js"; import { SpokeTokenAbi } from "../constants/abi/spoke-token-abi.js"; -import type { GenericAddress } from "../../../../common/types/chain.js"; +import type { GenericAddress } from "../../../../common/types/address.js"; import type { GetReadContractReturnType } from "../../common/types/contract.js"; import type { GetContractReturnType, Client, WalletClient } from "viem"; @@ -23,7 +23,9 @@ export function getSpokeCommonContract( provider: Client, address: GenericAddress, signer?: WalletClient, -) { +): + | GetReadContractReturnType + | GetContractReturnType { return getContract({ abi: SpokeCommonAbi, address: convertFromGenericAddress(address, ChainType.EVM), @@ -56,7 +58,9 @@ export function getSpokeTokenContract( provider: Client, address: GenericAddress, signer?: WalletClient, -) { +): + | GetReadContractReturnType + | GetContractReturnType { return getContract({ abi: SpokeTokenAbi, address: convertFromGenericAddress(address, ChainType.EVM), diff --git a/src/common/constants/chain.ts b/src/common/constants/chain.ts index 6af2d02..3195459 100644 --- a/src/common/constants/chain.ts +++ b/src/common/constants/chain.ts @@ -11,6 +11,7 @@ import { convertToGenericAddress } from "../utils/address.js"; import { TESTNET_POOLS } from "./pool.js"; +import type { EvmAddress } from "../types/address.js"; import type { FolksChainId, FolksChain, @@ -70,16 +71,16 @@ export const SPOKE_CHAIN: Record< [FOLKS_CHAIN_ID.AVALANCHE_FUJI]: { folksChainId: FOLKS_CHAIN_ID.AVALANCHE_FUJI, spokeCommonAddress: convertToGenericAddress( - "0x99477F62999AfbE2B0ca9CcAAdC2f2655F8B0C84", + "0x99477F62999AfbE2B0ca9CcAAdC2f2655F8B0C84" as EvmAddress, ChainType.EVM, ), bridgeRouterAddress: convertToGenericAddress( - "0xD0Cd0acEaf81fCf9C90ae60102eeC0E6C8095480", + "0xD0Cd0acEaf81fCf9C90ae60102eeC0E6C8095480" as EvmAddress, ChainType.EVM, ), adapters: { [AdapterType.HUB]: convertToGenericAddress( - "0xB01296Ea267463FDe2fcE5Fad5067B4d875A44Ba", + "0xB01296Ea267463FDe2fcE5Fad5067B4d875A44Ba" as EvmAddress, ChainType.EVM, ), }, @@ -89,11 +90,11 @@ export const SPOKE_CHAIN: Record< folksTokenId: FolksTokenId.USDC, poolId: TESTNET_POOLS[FolksTokenId.USDC], spokeAddress: convertToGenericAddress( - "0xC8ad4B23B4F07A27CDDAB1A8AE2Da54377f87426", + "0xC8ad4B23B4F07A27CDDAB1A8AE2Da54377f87426" as EvmAddress, ChainType.EVM, ), tokenAddress: convertToGenericAddress( - "0x5425890298aed601595a70ab815c96711a31bc65", + "0x5425890298aed601595a70ab815c96711a31bc65" as EvmAddress, ChainType.EVM, ), tokenDecimals: 6, @@ -103,7 +104,7 @@ export const SPOKE_CHAIN: Record< folksTokenId: FolksTokenId.AVAX, poolId: TESTNET_POOLS[FolksTokenId.AVAX], spokeAddress: convertToGenericAddress( - "0xF18F75595bA066efAdf4b24C7F263ba878C6B6f3", + "0xF18F75595bA066efAdf4b24C7F263ba878C6B6f3" as EvmAddress, ChainType.EVM, ), tokenAddress: null, @@ -114,28 +115,28 @@ export const SPOKE_CHAIN: Record< [FOLKS_CHAIN_ID.ETHEREUM_SEPOLIA]: { folksChainId: FOLKS_CHAIN_ID.ETHEREUM_SEPOLIA, spokeCommonAddress: convertToGenericAddress( - "0x132E1514A0aa02601c9eEBE42F8fDbEf11874089", + "0x132E1514A0aa02601c9eEBE42F8fDbEf11874089" as EvmAddress, ChainType.EVM, ), bridgeRouterAddress: convertToGenericAddress( - "0x498C9827D5230ACB708710Bd9eB0e1019CC2D711", + "0x498C9827D5230ACB708710Bd9eB0e1019CC2D711" as EvmAddress, ChainType.EVM, ), adapters: { [AdapterType.WORMHOLE_DATA]: convertToGenericAddress( - "0xeccb7067D8f0615eCe450236f2DF47b4dcc6ba8B", + "0xeccb7067D8f0615eCe450236f2DF47b4dcc6ba8B" as EvmAddress, ChainType.EVM, ), [AdapterType.WORMHOLE_CCTP]: convertToGenericAddress( - "0xf1565F622FEd835E55aCEacE0D04A4c9786056D2", + "0xf1565F622FEd835E55aCEacE0D04A4c9786056D2" as EvmAddress, ChainType.EVM, ), [AdapterType.CCIP_DATA]: convertToGenericAddress( - "0x084A113581915b3eF832E5d5bBdc30073001D4B2", + "0x084A113581915b3eF832E5d5bBdc30073001D4B2" as EvmAddress, ChainType.EVM, ), [AdapterType.CCIP_TOKEN]: convertToGenericAddress( - "0x59b5cB2c7413608e00CfFe074F2ac57165eB37e0", + "0x59b5cB2c7413608e00CfFe074F2ac57165eB37e0" as EvmAddress, ChainType.EVM, ), }, @@ -145,11 +146,11 @@ export const SPOKE_CHAIN: Record< folksTokenId: FolksTokenId.USDC, poolId: TESTNET_POOLS[FolksTokenId.USDC], spokeAddress: convertToGenericAddress( - "0x71EEc2B912a3Cef6Bf134899D437Ba17Fc9588D2", + "0x71EEc2B912a3Cef6Bf134899D437Ba17Fc9588D2" as EvmAddress, ChainType.EVM, ), tokenAddress: convertToGenericAddress( - "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238", + "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238" as EvmAddress, ChainType.EVM, ), tokenDecimals: 6, @@ -159,7 +160,7 @@ export const SPOKE_CHAIN: Record< folksTokenId: FolksTokenId.ETH_eth_sep, poolId: TESTNET_POOLS[FolksTokenId.ETH_eth_sep], spokeAddress: convertToGenericAddress( - "0xD3743aBf2D83725c06b12EC2C97c6b9dAC0D8a6F", + "0xD3743aBf2D83725c06b12EC2C97c6b9dAC0D8a6F" as EvmAddress, ChainType.EVM, ), tokenAddress: null, @@ -170,11 +171,11 @@ export const SPOKE_CHAIN: Record< folksTokenId: FolksTokenId.LINK_eth_sep, poolId: TESTNET_POOLS[FolksTokenId.LINK_eth_sep], spokeAddress: convertToGenericAddress( - "0xF85227ba19CC14e1b3FdcF9B2EBE199d64BE4CE9", + "0xF85227ba19CC14e1b3FdcF9B2EBE199d64BE4CE9" as EvmAddress, ChainType.EVM, ), tokenAddress: convertToGenericAddress( - "0x514910771af9ca656af840dff83e8264ecf986ca", + "0x514910771af9ca656af840dff83e8264ecf986ca" as EvmAddress, ChainType.EVM, ), tokenDecimals: 18, @@ -184,28 +185,28 @@ export const SPOKE_CHAIN: Record< [FOLKS_CHAIN_ID.BASE_SEPOLIA]: { folksChainId: FOLKS_CHAIN_ID.BASE_SEPOLIA, spokeCommonAddress: convertToGenericAddress( - "0x6Eac0286F42c8C0Cbc9997dB3b01b025EeD794f4", + "0x6Eac0286F42c8C0Cbc9997dB3b01b025EeD794f4" as EvmAddress, ChainType.EVM, ), bridgeRouterAddress: convertToGenericAddress( - "0xeccb7067D8f0615eCe450236f2DF47b4dcc6ba8B", + "0xeccb7067D8f0615eCe450236f2DF47b4dcc6ba8B" as EvmAddress, ChainType.EVM, ), adapters: { [AdapterType.WORMHOLE_DATA]: convertToGenericAddress( - "0xf1565F622FEd835E55aCEacE0D04A4c9786056D2", + "0xf1565F622FEd835E55aCEacE0D04A4c9786056D2" as EvmAddress, ChainType.EVM, ), [AdapterType.WORMHOLE_CCTP]: convertToGenericAddress( - "0x084A113581915b3eF832E5d5bBdc30073001D4B2", + "0x084A113581915b3eF832E5d5bBdc30073001D4B2" as EvmAddress, ChainType.EVM, ), [AdapterType.CCIP_DATA]: convertToGenericAddress( - "0x59b5cB2c7413608e00CfFe074F2ac57165eB37e0", + "0x59b5cB2c7413608e00CfFe074F2ac57165eB37e0" as EvmAddress, ChainType.EVM, ), [AdapterType.CCIP_TOKEN]: convertToGenericAddress( - "0x132E1514A0aa02601c9eEBE42F8fDbEf11874089", + "0x132E1514A0aa02601c9eEBE42F8fDbEf11874089" as EvmAddress, ChainType.EVM, ), }, @@ -215,11 +216,11 @@ export const SPOKE_CHAIN: Record< folksTokenId: FolksTokenId.USDC, poolId: TESTNET_POOLS[FolksTokenId.USDC], spokeAddress: convertToGenericAddress( - "0x6789da551F420bfb607Fffb43bf8936f9dfb7d4C", + "0x6789da551F420bfb607Fffb43bf8936f9dfb7d4C" as EvmAddress, ChainType.EVM, ), tokenAddress: convertToGenericAddress( - "0x036CbD53842c5426634e7929541eC2318f3dCF7e", + "0x036CbD53842c5426634e7929541eC2318f3dCF7e" as EvmAddress, ChainType.EVM, ), tokenDecimals: 6, @@ -229,7 +230,7 @@ export const SPOKE_CHAIN: Record< folksTokenId: FolksTokenId.ETH_base_sep, poolId: TESTNET_POOLS[FolksTokenId.ETH_base_sep], spokeAddress: convertToGenericAddress( - "0x457f30Bc85E885e4D519975C1dd87F397d4817B7", + "0x457f30Bc85E885e4D519975C1dd87F397d4817B7" as EvmAddress, ChainType.EVM, ), tokenAddress: null, diff --git a/src/common/constants/gmp.ts b/src/common/constants/gmp.ts index 21de947..6a76290 100644 --- a/src/common/constants/gmp.ts +++ b/src/common/constants/gmp.ts @@ -3,6 +3,7 @@ import { convertToGenericAddress } from "../utils/address.js"; import { FOLKS_CHAIN_ID } from "./chain.js"; +import type { EvmAddress } from "../types/address.js"; import type { FolksChainId } from "../types/chain.js"; import type { WormholeData } from "../types/gmp.js"; @@ -10,21 +11,21 @@ export const WORMHOLE_DATA: Partial> = { [FOLKS_CHAIN_ID.AVALANCHE_FUJI]: { wormholeChainId: 6, wormholeRelayer: convertToGenericAddress( - "0xA3cF45939bD6260bcFe3D66bc73d60f19e49a8BB", + "0xA3cF45939bD6260bcFe3D66bc73d60f19e49a8BB" as EvmAddress, ChainType.EVM, ), }, [FOLKS_CHAIN_ID.ETHEREUM_SEPOLIA]: { wormholeChainId: 10002, wormholeRelayer: convertToGenericAddress( - "0x7B1bD7a6b4E61c2a123AC6BC2cbfC614437D0470", + "0x7B1bD7a6b4E61c2a123AC6BC2cbfC614437D0470" as EvmAddress, ChainType.EVM, ), }, [FOLKS_CHAIN_ID.BASE_SEPOLIA]: { wormholeChainId: 10004, wormholeRelayer: convertToGenericAddress( - "0x93BAD53DDfB6132b0aC8E37f6029163E63372cEE", + "0x93BAD53DDfB6132b0aC8E37f6029163E63372cEE" as EvmAddress, ChainType.EVM, ), }, diff --git a/src/common/types/address.ts b/src/common/types/address.ts index 25ab6c6..7969ba9 100644 --- a/src/common/types/address.ts +++ b/src/common/types/address.ts @@ -1,8 +1,11 @@ +import type { Branded } from "./brand.js"; import type { ChainType } from "./chain.js"; -import type { Address } from "viem"; + +export type GenericAddress = Branded<`0x${string}`, "GenericAddress">; +export type EvmAddress = Branded<`0x${string}`, "EvmAddress">; type AddressTypeMap = { - [ChainType.EVM]: Address; + [ChainType.EVM]: EvmAddress; }; export type AddressType = AddressTypeMap[T]; diff --git a/src/common/types/brand.ts b/src/common/types/brand.ts new file mode 100644 index 0000000..3e9401f --- /dev/null +++ b/src/common/types/brand.ts @@ -0,0 +1,3 @@ +declare const __brand: unique symbol; +type Brand = { [__brand]: B }; +export type Branded = T & Brand; diff --git a/src/common/types/chain.ts b/src/common/types/chain.ts index 8b64db0..d1b1a3a 100644 --- a/src/common/types/chain.ts +++ b/src/common/types/chain.ts @@ -1,3 +1,4 @@ +import type { GenericAddress } from "./address.js"; import type { AdapterType } from "./message.js"; import type { FolksTokenId, SpokeTokenData } from "./token.js"; import type { EvmChainName } from "../../chains/evm/common/types/chain.js"; @@ -15,8 +16,6 @@ export enum NetworkType { export type FolksChainName = EvmChainName; export type FolksChainId = (typeof FOLKS_CHAIN_ID)[keyof typeof FOLKS_CHAIN_ID]; -export type GenericAddress = `0x${string}`; - export type IFolksChain = { folksChainId: FolksChainId; }; diff --git a/src/common/types/gmp.ts b/src/common/types/gmp.ts index fa31217..5626f74 100644 --- a/src/common/types/gmp.ts +++ b/src/common/types/gmp.ts @@ -1,4 +1,4 @@ -import type { GenericAddress } from "./chain.js"; +import type { GenericAddress } from "./address.js"; export type WormholeData = { wormholeChainId: number; diff --git a/src/common/types/lending.ts b/src/common/types/lending.ts new file mode 100644 index 0000000..fa13d64 --- /dev/null +++ b/src/common/types/lending.ts @@ -0,0 +1,4 @@ +import type { Branded } from "./brand.js"; + +export type AccountId = Branded<`0x${string}`, "AccountId">; +export type LoanId = Branded<`0x${string}`, "LoanId">; diff --git a/src/common/types/message.ts b/src/common/types/message.ts index b89efa8..d3711f9 100644 --- a/src/common/types/message.ts +++ b/src/common/types/message.ts @@ -1,4 +1,6 @@ -import type { FolksChainId, GenericAddress } from "./chain.js"; +import type { GenericAddress } from "./address.js"; +import type { FolksChainId } from "./chain.js"; +import type { AccountId, LoanId } from "./lending.js"; import type { LoanType } from "./module.js"; import type { TokenType } from "./token.js"; import type { FINALITY } from "../constants/message.js"; @@ -77,23 +79,23 @@ export type UnregisterAddressMessageData = { // Data: loan export type CreateLoanMessageData = { - loanId: Hex; + loanId: LoanId; loanTypeId: LoanType; }; export type DeleteLoanMessageData = { - accountId: Hex; - loanId: Hex; + accountId: AccountId; + loanId: LoanId; }; export type DepositMessageData = { - loanId: Hex; + loanId: LoanId; poolId: number; amount: bigint; }; export type WithdrawMessageData = { - loanId: Hex; + loanId: LoanId; poolId: number; receiverFolksChainId: FolksChainId; amount: bigint; @@ -179,7 +181,7 @@ export type MessageDataParams = export type MessageBuilderParams = { userAddress: GenericAddress; - accountId: Hex; + accountId: AccountId; adapters: MessageAdapters; sender: GenericAddress; destinationChainId: FolksChainId; diff --git a/src/common/types/token.ts b/src/common/types/token.ts index 864a1d4..1dc8fab 100644 --- a/src/common/types/token.ts +++ b/src/common/types/token.ts @@ -1,4 +1,4 @@ -import type { GenericAddress } from "./chain.js"; +import type { GenericAddress } from "./address.js"; export enum FolksTokenId { USDC = "USDC", diff --git a/src/common/utils/address.ts b/src/common/utils/address.ts index 7448992..f75be3f 100644 --- a/src/common/utils/address.ts +++ b/src/common/utils/address.ts @@ -8,18 +8,25 @@ import { } from "../constants/bytes.js"; import { ChainType } from "../types/chain.js"; -import type { AddressType } from "../types/address.js"; -import type { GenericAddress } from "../types/chain.js"; -import type { Address } from "viem"; +import type { + AddressType, + EvmAddress, + GenericAddress, +} from "../types/address.js"; +import type { AccountId } from "../types/lending.js"; export function getRandomGenericAddress(): GenericAddress { return pad(privateKeyToAccount(generatePrivateKey()).address, { size: BYTES32_LENGTH, - }); + }) as GenericAddress; } export function isGenericAddress(address: GenericAddress): boolean { - return address.length === 64 + 2 && address.startsWith("0x"); + return address.length === 64 + 2; +} + +export function isAccountId(accountId: AccountId): boolean { + return accountId.length === 64 + 2; } export function convertToGenericAddress( @@ -28,7 +35,9 @@ export function convertToGenericAddress( ): GenericAddress { switch (fromChainType) { case ChainType.EVM: - return pad(address as Address, { size: BYTES32_LENGTH }); + return pad(address as EvmAddress, { + size: BYTES32_LENGTH, + }) as GenericAddress; default: return exhaustiveCheck(fromChainType); } @@ -42,7 +51,7 @@ export function convertFromGenericAddress( case ChainType.EVM: return getAddress( sliceHex(address, BYTES32_LENGTH - EVM_ADDRESS_BYTES_LENGTH), - ); + ) as EvmAddress; default: return exhaustiveCheck(toChainType); } diff --git a/src/common/utils/chain.ts b/src/common/utils/chain.ts index bdb67ae..041880e 100644 --- a/src/common/utils/chain.ts +++ b/src/common/utils/chain.ts @@ -9,12 +9,12 @@ import { ChainType } from "../types/chain.js"; import { convertToGenericAddress } from "./address.js"; +import type { GenericAddress } from "../types/address.js"; import type { + FolksChain, FolksChainId, NetworkType, - FolksChain, SpokeChain, - GenericAddress, } from "../types/chain.js"; import type { FolksChainSigner } from "../types/core.js"; import type { AdapterType } from "../types/message.js"; diff --git a/src/common/utils/messages.ts b/src/common/utils/messages.ts index 6806177..6ae8d97 100644 --- a/src/common/utils/messages.ts +++ b/src/common/utils/messages.ts @@ -12,12 +12,8 @@ import { convertFromGenericAddress } from "./address.js"; import { getFolksChain, getSpokeChainAdapterAddress } from "./chain.js"; import type { HubChain } from "../../chains/evm/hub/types/chain.js"; -import type { - FolksChain, - FolksChainId, - GenericAddress, - NetworkType, -} from "../types/chain.js"; +import type { GenericAddress } from "../types/address.js"; +import type { FolksChain, FolksChainId, NetworkType } from "../types/chain.js"; import type { FolksProvider } from "../types/core.js"; import type { WormholeData } from "../types/gmp.js"; import type { diff --git a/src/xchain/modules/folks-account.ts b/src/xchain/modules/folks-account.ts index cb561b0..33b0dcd 100644 --- a/src/xchain/modules/folks-account.ts +++ b/src/xchain/modules/folks-account.ts @@ -7,6 +7,7 @@ import { assertAdapterSupportsDataMessage } from "../../common/utils/adapter.js" import { convertFromGenericAddress } from "../../common/utils/address.js"; import { assertSpokeChainSupported, + getFolksChain, getSignerGenericAddress, getSpokeChain, } from "../../common/utils/chain.js"; @@ -17,7 +18,9 @@ import { import { exhaustiveCheck } from "../../utils/exhaustive-check.js"; import { FolksCore } from "../core/folks-core.js"; -import type { FolksChainId, GenericAddress } from "../../common/types/chain.js"; +import type { EvmAddress, GenericAddress } from "../../common/types/address.js"; +import type { FolksChainId } from "../../common/types/chain.js"; +import type { AccountId } from "../../common/types/lending.js"; import type { InviteAddressMessageData, MessageAdapters, @@ -31,10 +34,9 @@ import type { PrepareInviteAddressCall, PrepareUnregisterAddressCall, } from "../../common/types/module.js"; -import type { Address, Hex } from "viem"; export const prepare = { - async createAccount(accountId: Hex, adapters: MessageAdapters) { + async createAccount(accountId: AccountId, adapters: MessageAdapters) { const folksChain = FolksCore.getSelectedFolksChain(); // check adapters are compatible @@ -96,12 +98,16 @@ export const prepare = { }, async inviteAddress( - accountId: Hex, + accountId: AccountId, folksChainIdToInvite: FolksChainId, addressToInvite: GenericAddress, adapters: MessageAdapters, ) { const folksChain = FolksCore.getSelectedFolksChain(); + const folksChainToInvite = getFolksChain( + folksChainIdToInvite, + folksChain.network, + ); // check adapters are compatible assertAdapterSupportsDataMessage( @@ -158,7 +164,10 @@ export const prepare = { messageToSend, accountId, folksChainIdToInvite, - addressToInvite, + convertFromGenericAddress( + addressToInvite, + folksChainToInvite.chainType, + ), adapters, spokeChain, ); @@ -167,7 +176,7 @@ export const prepare = { } }, - async acceptInvite(accountId: Hex, adapters: MessageAdapters) { + async acceptInvite(accountId: AccountId, adapters: MessageAdapters) { const folksChain = FolksCore.getSelectedFolksChain(); // check adapters are compatible @@ -229,7 +238,7 @@ export const prepare = { }, async unregisterAddress( - accountId: Hex, + accountId: AccountId, folksChainIdToUnregister: FolksChainId, adapters: MessageAdapters, ) { @@ -300,7 +309,10 @@ export const prepare = { }; export const write = { - async createAccount(accountId: Hex, prepareCall: PrepareCreateAccountCall) { + async createAccount( + accountId: AccountId, + prepareCall: PrepareCreateAccountCall, + ) { const folksChain = FolksCore.getSelectedFolksChain(); assertSpokeChainSupported(folksChain.folksChainId, folksChain.network); @@ -319,9 +331,9 @@ export const write = { }, async inviteAddress( - accountId: Hex, + accountId: AccountId, folksChainIdToInvite: number, - addressToInvite: Address, + addressToInvite: EvmAddress, prepareCall: PrepareInviteAddressCall, ) { const folksChain = FolksCore.getSelectedFolksChain(); @@ -344,7 +356,7 @@ export const write = { }, async acceptInvite( - accountId: Hex, + accountId: AccountId, prepareCall: PrepareAcceptInviteAddressCall, ) { const folksChain = FolksCore.getSelectedFolksChain(); @@ -365,7 +377,7 @@ export const write = { }, async unregisterAddress( - accountId: Hex, + accountId: AccountId, folksChainIdToUnregister: FolksChainId, prepareCall: PrepareUnregisterAddressCall, ) { @@ -389,7 +401,7 @@ export const write = { }; export const read = { - async accountInfo(accountId: Hex, folksChainIds?: Array) { + async accountInfo(accountId: AccountId, folksChainIds?: Array) { return FolksHubAccount.getAccountInfo( FolksCore.getHubProvider(), FolksCore.getSelectedNetwork(), diff --git a/src/xchain/modules/folks-loan.ts b/src/xchain/modules/folks-loan.ts index e5d6c7f..093dbb9 100644 --- a/src/xchain/modules/folks-loan.ts +++ b/src/xchain/modules/folks-loan.ts @@ -25,6 +25,7 @@ import { exhaustiveCheck } from "../../utils/exhaustive-check.js"; import { FolksCore } from "../core/folks-core.js"; import type { FolksChainId } from "../../common/types/chain.js"; +import type { AccountId, LoanId } from "../../common/types/lending.js"; import type { CreateLoanMessageData, DeleteLoanMessageData, @@ -41,12 +42,11 @@ import type { PrepareWithdrawCall, } from "../../common/types/module.js"; import type { FolksTokenId } from "../../common/types/token.js"; -import type { Hex } from "viem"; export const prepare = { async createLoan( - accountId: Hex, - loanId: Hex, + accountId: AccountId, + loanId: LoanId, loanTypeId: LoanType, adapters: MessageAdapters, ) { @@ -100,7 +100,11 @@ export const prepare = { } }, - async deleteLoan(accountId: Hex, loanId: Hex, adapters: MessageAdapters) { + async deleteLoan( + accountId: AccountId, + loanId: LoanId, + adapters: MessageAdapters, + ) { const folksChain = FolksCore.getSelectedFolksChain(); assertAdapterSupportsDataMessage( @@ -151,8 +155,8 @@ export const prepare = { }, async deposit( - accountId: Hex, - loanId: Hex, + accountId: AccountId, + loanId: LoanId, loanType: LoanType, folksTokenId: FolksTokenId, amount: bigint, @@ -226,8 +230,8 @@ export const prepare = { }, async withdraw( - accountId: Hex, - loanId: Hex, + accountId: AccountId, + loanId: LoanId, folksTokenId: FolksTokenId, amount: bigint, isFAmount: boolean, @@ -329,8 +333,8 @@ export const prepare = { export const write = { async createLoan( - accountId: Hex, - loanId: Hex, + accountId: AccountId, + loanId: LoanId, loanTypeId: LoanType, prepareCall: PrepareCreateLoanCall, ) { @@ -354,8 +358,8 @@ export const write = { }, async deleteLoan( - accountId: Hex, - loanId: Hex, + accountId: AccountId, + loanId: LoanId, prepareCall: PrepareCreateLoanCall, ) { const folksChain = FolksCore.getSelectedFolksChain(); @@ -377,8 +381,8 @@ export const write = { }, async deposit( - accountId: Hex, - loanId: Hex, + accountId: AccountId, + loanId: LoanId, amount: bigint, includeApproval: boolean, prepareCall: PrepareDepositCall, @@ -404,8 +408,8 @@ export const write = { }, async withdraw( - accountId: Hex, - loanId: Hex, + accountId: AccountId, + loanId: LoanId, folksTokenId: FolksTokenId, amount: bigint, isFAmount: boolean,