-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add evm provider #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,7 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { Account, type Call, CallData, RpcProvider } from "starknet"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { env } from "./env"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import type { CoTTransaction } from "../types"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { ethers } from "ethers"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
interface GraphQLResponse<T> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
data?: T; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
errors?: Array<{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -82,3 +83,54 @@ export const executeStarknetTransaction = async (call: Call): Promise<any> => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return error instanceof Error ? error : new Error("Unknown error occurred"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export interface EvmTransaction extends CoTTransaction { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
network: "evm"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
method: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
value?: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
gasLimit?: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export async function executeEvmTransaction( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
transaction: EvmTransaction | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
): Promise<any> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Initialize provider (customize based on your needs) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const provider = new ethers.JsonRpcProvider(process.env.EVM_RPC_URL); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Initialize wallet/signer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY!, provider); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Create contract instance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!transaction.abi) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
throw new Error('Contract ABI is required'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const contract = new ethers.Contract( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
transaction.contractAddress, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
transaction.abi, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
wallet | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!contract[transaction.method]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
throw new Error(`Method ${transaction.method} not found in contract ABI`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Prepare transaction options | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const txOptions = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
value: BigInt(transaction.value || "0"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
gasLimit: BigInt(transaction.gasLimit || "0"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+120
to
+123
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve gas limit handling. The current implementation might lead to transaction failures if gas limit is not properly set. Consider these improvements:
- const txOptions = {
- value: BigInt(transaction.value || "0"),
- gasLimit: BigInt(transaction.gasLimit || "0"),
- };
+ const txOptions: ethers.ContractTransactionRequest = {
+ value: BigInt(transaction.value || "0"),
+ };
+
+ if (transaction.gasLimit) {
+ txOptions.gasLimit = BigInt(transaction.gasLimit);
+ } else {
+ // Estimate gas if not provided
+ try {
+ txOptions.gasLimit = await contract[transaction.method].estimateGas(
+ ...transaction.calldata,
+ { value: txOptions.value }
+ );
+ } catch (error) {
+ throw new Error(`Gas estimation failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
+ }
+ }
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Execute transaction | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const tx = await contract[transaction.method]( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
...transaction.calldata, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
txOptions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const receipt = await tx.wait(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return receipt; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+126
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance transaction execution and receipt handling. The current implementation lacks proper transaction monitoring and receipt validation. Consider these improvements:
- const tx = await contract[transaction.method](
- ...transaction.calldata,
- txOptions
- );
- const receipt = await tx.wait();
-
- return receipt;
+ try {
+ const tx = await contract[transaction.method](
+ ...transaction.calldata,
+ txOptions
+ );
+
+ // Wait for 2 confirmations
+ const receipt = await tx.wait(2);
+
+ if (!receipt.status) {
+ throw new Error('Transaction failed');
+ }
+
+ return receipt;
+ } catch (error: any) {
+ // Handle revert reasons
+ if (error.data) {
+ throw new Error(`Transaction reverted: ${error.data}`);
+ }
+ throw error;
+ } 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return error instanceof Error ? error : new Error("Unknown error occurred"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+133
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve error handling specificity. The current error handling loses valuable information by converting all errors to a generic message. Consider handling specific error types differently. } catch (error) {
- return error instanceof Error ? error : new Error("Unknown error occurred");
+ if (error instanceof ethers.ContractTransactionExecutionError) {
+ return new Error(`Transaction execution failed: ${error.message}`);
+ } else if (error instanceof ethers.ContractTransactionRevertedError) {
+ return new Error(`Transaction reverted: ${error.message}`);
+ } else if (error instanceof ethers.ProviderError) {
+ return new Error(`Provider error: ${error.message}`);
+ } else {
+ return error instanceof Error ? error : new Error("Unknown error occurred");
+ } 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add environment variable validation.
The code assumes environment variables will be present without validation.
Add validation before using environment variables:
📝 Committable suggestion