Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,18 @@ export const createClient = (config: ClientConfig = {chain: localnet}): GenLayer
.extend(walletActions)
.extend(client => accountActions(client as unknown as GenLayerClient<GenLayerChain>));

// Create a client with all actions except transaction actions
const clientWithAllActions = {
// First add transaction actions, then contract actions that depend on them
const clientWithTransactionActions = {
...clientWithBasicActions,
...contractActions(clientWithBasicActions as unknown as GenLayerClient<GenLayerChain>, publicClient),
...transactionActions(clientWithBasicActions as unknown as GenLayerClient<GenLayerChain>, publicClient),
...chainActions(clientWithBasicActions as unknown as GenLayerClient<GenLayerChain>),
...genlayerWalletActions(clientWithBasicActions as unknown as GenLayerClient<GenLayerChain>),
...transactionActions(clientWithBasicActions as unknown as GenLayerClient<GenLayerChain>, publicClient),
} as unknown as GenLayerClient<GenLayerChain>;

// Then add contract actions that can now access transaction actions
const clientWithAllActions = {
...clientWithTransactionActions,
...contractActions(clientWithTransactionActions as unknown as GenLayerClient<GenLayerChain>, publicClient),
} as unknown as GenLayerClient<GenLayerChain>;

// Add transaction actions last, after all other actions are in place
Expand Down
14 changes: 13 additions & 1 deletion src/contracts/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,26 @@ const _sendTransaction = async ({
const validatedSenderAccount = validateAccount(senderAccount);

const nonce = await client.getCurrentNonce({address: validatedSenderAccount.address});
let estimatedGas: bigint;
try {
estimatedGas = await client.estimateTransactionGas({
from: validatedSenderAccount.address,
to: client.chain.consensusMainContract?.address as Address,
data: encodedData,
value: value,
});
} catch (error) {
console.warn("Gas estimation failed, using fallback value:", error);
estimatedGas = 200_000n;
}
const transactionRequest = await client.prepareTransactionRequest({
account: validatedSenderAccount,
to: client.chain.consensusMainContract?.address as Address,
data: encodedData,
type: "legacy",
nonce: Number(nonce),
value: value,
gas: 21000n,
gas: estimatedGas,
});

if (validatedSenderAccount?.type !== "local") {
Expand Down
20 changes: 20 additions & 0 deletions src/transactions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ export const transactionActions = (client: GenLayerClient<GenLayerChain>, public
})) as unknown as GenLayerRawTransaction;
return decodeTransaction(transaction);
},
estimateTransactionGas: async (transactionParams: {
from?: Address;
to: Address;
data?: `0x${string}`;
value?: bigint;
}): Promise<bigint> => {
const formattedParams = {
from: transactionParams.from || client.account?.address,
to: transactionParams.to,
data: transactionParams.data || "0x",
value: transactionParams.value ? `0x${transactionParams.value.toString(16)}` as `0x${string}` : "0x0" as `0x${string}`,
};

const gasHex = await client.request({
method: "eth_estimateGas",
params: [formattedParams],
}) as string;

return BigInt(gasHex);
},
});


3 changes: 1 addition & 2 deletions src/transactions/decoders.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {GenLayerTransaction, GenLayerRawTransaction, DecodedCallData, DecodedDeployData} from "../types/transactions";
import {transactionsStatusNumberToName, transactionResultNumberToName, voteTypeNumberToName, VoteType} from "../types/transactions";
import {GenLayerTransaction, GenLayerRawTransaction, DecodedCallData, DecodedDeployData,transactionsStatusNumberToName, transactionResultNumberToName, voteTypeNumberToName, VoteType} from "../types/transactions";
import {b64ToArray, calldataToUserFriendlyJson, resultToUserFriendlyJson} from "../utils/jsonifier";
import {fromRlp, fromHex, Hex, Address} from "viem";
import * as calldataAbi from "../abi/calldata";
Expand Down
7 changes: 7 additions & 0 deletions src/types/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type GenLayerMethod =
| {method: "gen_getContractCode"; params: [address: Address]}
| {method: "sim_getTransactionsForAddress"; params: [address: Address, filter?: "all" | "from" | "to"]}
| {method: "eth_getTransactionCount"; params: [address: Address, block: string]}
| {method: "eth_estimateGas"; params: [transactionParams: any]}
| {method: "gen_call"; params: [requestParams: any]};

/*
Expand Down Expand Up @@ -80,6 +81,12 @@ export type GenLayerClient<TGenLayerChain extends GenLayerChain> = Omit<
}) => Promise<`0x${string}`>;
getTransaction: (args: {hash: TransactionHash}) => Promise<GenLayerTransaction>;
getCurrentNonce: (args: {address: Address}) => Promise<number>;
estimateTransactionGas: (transactionParams: {
from?: Address;
to: Address;
data?: `0x${string}`;
value?: bigint;
}) => Promise<bigint>;
waitForTransactionReceipt: (args: {
hash: TransactionHash;
status?: TransactionStatus;
Expand Down