Skip to content

Commit

Permalink
fix: create account estimate fees (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
palace22 authored May 29, 2024
1 parent a1a086c commit 4368c62
Show file tree
Hide file tree
Showing 16 changed files with 1,120 additions and 97 deletions.
605 changes: 605 additions & 0 deletions src/chains/evm/common/constants/abi/wormhole-data-adapter-abi.ts

Large diffs are not rendered by default.

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
19 changes: 16 additions & 3 deletions src/chains/evm/common/utils/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { getContract } from "viem";
import { ChainType } from "../../../../common/types/chain.js";
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";
import type { Address, Client, WalletClient } from "viem";

export function getERC20Contract(
Expand All @@ -30,14 +32,25 @@ 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,
});
}

export function getWormholeDataAdapterContract(
provider: Client,
address: Address,
): GetReadContractReturnType<typeof WormholeDataAdapterAbi> {
return getContract({
abi: WormholeDataAdapterAbi,
address: convertFromGenericAddress<ChainType.EVM>(address, ChainType.EVM),
client: { public: provider },
});
}
27 changes: 27 additions & 0 deletions src/chains/evm/common/utils/gmp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { concat } from "viem";

import {
UINT16_LENGTH,
UINT256_LENGTH,
} from "../../../../common/constants/bytes.js";
import { convertNumberToBytes } from "../../../../common/utils/bytes.js";

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

export function encodeWormholeEvmPayloadWithMetadata(
returnAdapterId: AdapterType,
returnGasLimit: bigint,
sender: GenericAddress,
handler: GenericAddress,
payload: Hex,
): Hex {
return concat([
convertNumberToBytes(returnAdapterId, UINT16_LENGTH),
convertNumberToBytes(returnGasLimit, UINT256_LENGTH),
sender,
handler,
payload,
]);
}
Loading

0 comments on commit 4368c62

Please sign in to comment.