Skip to content

Commit

Permalink
fix: user address in buildMessagePayload
Browse files Browse the repository at this point in the history
  • Loading branch information
palace22 committed May 29, 2024
1 parent b11a478 commit f586e90
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/chains/evm/common/utils/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { EVM_FOLKS_CHAIN_ID } from "../constants/chain.js";
import type { EvmChainId } from "../types/chain.js";
import type { Account, Address, WalletClient } from "viem";

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

export function getSignerAccount(signer: WalletClient): Account {
export function getEvmSignerAccount(signer: WalletClient): Account {
if (signer.account) return signer.account;
throw new Error("EVM Signer account is not set");
}
Expand Down
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 @@ -5,7 +5,7 @@ import { convertFromGenericAddress } from "../../../../common/utils/address.js";
import { ERC20Abi } from "../constants/abi/erc-20-abi.js";
import { WormholeDataAdapterAbi } from "../constants/abi/wormhole-data-adapter-abi.js";

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

import type { GenericAddress } from "../../../../common/types/chain.js";
import type { GetReadContractReturnType } from "../types/contract.js";
Expand All @@ -32,14 +32,14 @@ export async function sendERC20Approve(
) {
const erc20 = getERC20Contract(provider, address, signer);
const allowance = await erc20.read.allowance([
getSignerAddress(signer),
getEvmSignerAddress(signer),
receiver,
]);

// approve if not enough
if (allowance < amount)
return await erc20.write.approve([receiver, BigInt(amount)], {
account: getSignerAccount(signer),
account: getEvmSignerAccount(signer),
chain: signer.chain,
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/chains/evm/common/utils/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ export async function estimateEVMWormholeDataGasLimit(
buildMessagePayload(
messageBuilderParams.action,
messageBuilderParams.accountId,
getRandomGenericAddress(),
messageBuilderParams.userAddress,
buildEvmMessageData(messageBuilderParams),
),
),
Expand Down
10 changes: 5 additions & 5 deletions src/chains/evm/spoke/modules/folks-evm-account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getSignerAccount } from "../../common/utils/chain.js";
import { getEvmSignerAccount } from "../../common/utils/chain.js";
import {
getBridgeRouterSpokeContract,
getSpokeCommonContract,
Expand Down Expand Up @@ -233,7 +233,7 @@ export const write = {
};

return await spokeCommon.write.createAccount([params, accountId], {
account: getSignerAccount(signer),
account: getEvmSignerAccount(signer),
chain: signer.chain,
gas: gasLimit,
value: adapterFee,
Expand Down Expand Up @@ -274,7 +274,7 @@ export const write = {
return await spokeCommon.write.inviteAddress(
[params, accountId, folksChainIdToInvite, addressToInvite],
{
account: getSignerAccount(signer),
account: getEvmSignerAccount(signer),
chain: signer.chain,
gasLimit: gasLimit,
value: adapterFee,
Expand Down Expand Up @@ -312,7 +312,7 @@ export const write = {
};

return await spokeCommon.write.acceptInviteAddress([params, accountId], {
account: getSignerAccount(signer),
account: getEvmSignerAccount(signer),
chain: signer.chain,
gasLimit: gasLimit,
value: adapterFee,
Expand Down Expand Up @@ -352,7 +352,7 @@ export const write = {
return await spokeCommon.write.unregisterAddress(
[params, accountId, folksChainIdToUnregister],
{
account: getSignerAccount(signer),
account: getEvmSignerAccount(signer),
chain: signer.chain,
gasLimit: gasLimit,
value: adapterFee,
Expand Down
10 changes: 5 additions & 5 deletions src/chains/evm/spoke/modules/folks-evm-loan.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TokenType } from "../../../../common/types/token.js";
import { getSignerAccount } from "../../common/utils/chain.js";
import { getEvmSignerAccount } from "../../common/utils/chain.js";
import { sendERC20Approve } from "../../common/utils/contract.js";
import { getHubTokenData } from "../../hub/utils/chain.js";
import {
Expand Down Expand Up @@ -266,7 +266,7 @@ export const write = {
return await spokeCommon.write.createLoan(
[params, accountId, loanId, loanTypeId],
{
account: getSignerAccount(signer),
account: getEvmSignerAccount(signer),
chain: signer.chain,
gasLimit: gasLimit,
value: adapterFee,
Expand Down Expand Up @@ -305,7 +305,7 @@ export const write = {
};

return await spokeCommon.write.deleteLoan([params, accountId, loanId], {
account: getSignerAccount(signer),
account: getEvmSignerAccount(signer),
chain: signer.chain,
gasLimit: gasLimit,
value: adapterFee,
Expand Down Expand Up @@ -354,7 +354,7 @@ export const write = {
};

return await spokeToken.write.deposit([params, accountId, loanId, amount], {
account: getSignerAccount(signer),
account: getEvmSignerAccount(signer),
chain: signer.chain,
gasLimit: gasLimit,
value: adapterFee,
Expand Down Expand Up @@ -397,7 +397,7 @@ export const write = {
return await spokeCommon.write.withdraw(
[params, accountId, loanId, poolId, receiverChainId, amount, isFAmount],
{
account: getSignerAccount(signer),
account: getEvmSignerAccount(signer),
chain: signer.chain,
gasLimit: gasLimit,
value: adapterFee,
Expand Down
7 changes: 7 additions & 0 deletions src/common/types/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ export type FolksCoreConfig = {
network: NetworkType;
provider: FolksCoreProvider;
};

export type FolksEvmSigner = {
signer: EVMSigner;
chainType: ChainType.EVM;
};

export type FolksChainSigner = FolksEvmSigner;
1 change: 1 addition & 0 deletions src/common/types/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export type MessageDataParams =
| WithdrawMessageDataParams;

export type MessageBuilderParams = {
userAddress: GenericAddress;
accountId: Hex;
adapters: MessageAdapters;
sender: GenericAddress;
Expand Down
16 changes: 16 additions & 0 deletions src/common/utils/chain.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { getEvmSignerAddress } from "../../chains/evm/common/utils/chain.js";
import {
getHubChainAdapterAddress,
isHubChain,
} from "../../chains/evm/hub/utils/chain.js";
import { exhaustiveCheck } from "../../utils/exhaustive-check.js";
import { FOLKS_CHAIN, SPOKE_CHAIN } from "../constants/chain.js";
import { ChainType } from "../types/chain.js";

import type {
FolksChainId,
Expand All @@ -11,6 +14,7 @@ import type {
SpokeChain,
GenericAddress,
} from "../types/chain.js";
import type { FolksChainSigner } from "../types/core.js";
import type { AdapterType } from "../types/message.js";
import type { FolksTokenId, SpokeTokenData } from "../types/token.js";

Expand Down Expand Up @@ -136,3 +140,15 @@ export function getAdapterAddress(
return getHubChainAdapterAddress(network, adapterType);
return getSpokeChainAdapterAddress(folksChainId, network, adapterType);
}

export function getSignerGenericAddress(
folksChainSigner: FolksChainSigner,
): GenericAddress {
const chainType = folksChainSigner.chainType;
switch (chainType) {
case ChainType.EVM:
return getEvmSignerAddress(folksChainSigner.signer);
default:
return exhaustiveCheck(chainType);
}
}
35 changes: 30 additions & 5 deletions src/xchain/modules/folks-account.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { getSignerAddress } from "../../chains/evm/common/utils/chain.js";
import { FolksHubAccount } from "../../chains/evm/hub/modules/index.js";
import { getHubChain } from "../../chains/evm/hub/utils/chain.js";
import { FolksEvmAccount } from "../../chains/evm/spoke/modules/index.js";
import { ChainType } from "../../common/types/chain.js";
import { Action } from "../../common/types/message.js";
import { assertAdapterSupportsDataMessage } from "../../common/utils/adapter.js";
import { convertFromGenericAddress } from "../../common/utils/address.js";
import {
assertSpokeChainSupported,
getSignerGenericAddress,
getSpokeChain,
} from "../../common/utils/chain.js";
import {
Expand Down Expand Up @@ -47,7 +48,13 @@ export const prepare = {
);
const hubChain = getHubChain(folksChain.network);

const userAddress = getSignerGenericAddress({
signer: FolksCore.getFolksSigner().signer,
chainType: folksChain.chainType,
});

const messageBuilderParams: MessageBuilderParams = {
userAddress,
accountId,
adapters,
action: Action.CreateAccount,
Expand Down Expand Up @@ -78,7 +85,7 @@ export const prepare = {
case ChainType.EVM:
return await FolksEvmAccount.prepare.createAccount(
FolksCore.getProvider<ChainType.EVM>(folksChain.folksChainId),
getSignerAddress(FolksCore.getSigner<ChainType.EVM>()),
convertFromGenericAddress(userAddress, folksChain.chainType),
messageToSend,
accountId,
adapters,
Expand Down Expand Up @@ -108,11 +115,17 @@ export const prepare = {
);
const hubChain = getHubChain(folksChain.network);

const userAddress = getSignerGenericAddress({
signer: FolksCore.getFolksSigner().signer,
chainType: folksChain.chainType,
});

const data: InviteAddressMessageData = {
folksChainIdToInvite,
addressToInvite,
};
const messageToSend = buildMessageToSend(folksChain.chainType, {
userAddress,
accountId,
adapters,
action: Action.InviteAddress,
Expand All @@ -127,7 +140,7 @@ export const prepare = {
case ChainType.EVM:
return await FolksEvmAccount.prepare.inviteAddress(
FolksCore.getProvider<ChainType.EVM>(folksChain.folksChainId),
getSignerAddress(FolksCore.getSigner<ChainType.EVM>()),
convertFromGenericAddress(userAddress, folksChain.chainType),
messageToSend,
accountId,
folksChainIdToInvite,
Expand All @@ -154,7 +167,13 @@ export const prepare = {
);
const hubChain = getHubChain(folksChain.network);

const userAddress = getSignerGenericAddress({
signer: FolksCore.getFolksSigner().signer,
chainType: folksChain.chainType,
});

const messageToSend = buildMessageToSend(folksChain.chainType, {
userAddress,
accountId,
adapters,
action: Action.AcceptInviteAddress,
Expand All @@ -169,7 +188,7 @@ export const prepare = {
case ChainType.EVM:
return await FolksEvmAccount.prepare.acceptInvite(
FolksCore.getProvider<ChainType.EVM>(folksChain.folksChainId),
getSignerAddress(FolksCore.getSigner<ChainType.EVM>()),
convertFromGenericAddress(userAddress, folksChain.chainType),
messageToSend,
accountId,
adapters,
Expand Down Expand Up @@ -198,10 +217,16 @@ export const prepare = {
);
const hubChain = getHubChain(folksChain.network);

const userAddress = getSignerGenericAddress({
signer: FolksCore.getFolksSigner().signer,
chainType: folksChain.chainType,
});

const data: UnregisterAddressMessageData = {
folksChainIdToUnregister,
};
const messageToSend = buildMessageToSend(folksChain.chainType, {
userAddress,
accountId,
adapters,
action: Action.UnregisterAddress,
Expand All @@ -216,7 +241,7 @@ export const prepare = {
case ChainType.EVM:
return await FolksEvmAccount.prepare.unregisterAddress(
FolksCore.getProvider<ChainType.EVM>(folksChain.folksChainId),
getSignerAddress(FolksCore.getSigner<ChainType.EVM>()),
convertFromGenericAddress(userAddress, folksChain.chainType),
messageToSend,
accountId,
folksChainIdToUnregister,
Expand Down
Loading

0 comments on commit f586e90

Please sign in to comment.