Skip to content

Commit

Permalink
[fix] verified tx works on sepolia
Browse files Browse the repository at this point in the history
  • Loading branch information
tavakyan committed Jul 30, 2024
1 parent b541273 commit 5fb9b99
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 58 deletions.
52 changes: 39 additions & 13 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,55 @@ program
program
.command('deposit')
.description('Deposit Ether into the Eclipse rollup')
.requiredOption('-k, --key-file <path>', 'Path to the Ethereum private key file')
.requiredOption('-d, --destination <address>', 'Destination address on the rollup (base58 encoded)')
.requiredOption('-a, --amount <ether>', 'Amount in Ether to deposit')
.option('--mainnet', 'Use Ethereum Mainnet')
.option('--sepolia', 'Use Sepolia test network')
.option('-r, --rpc-url <url>', 'Optional: JSON RPC URL to override the default')
.requiredOption('-k, --key-file <path>', 'Path to the Ethereum private key file')
.action((options) => {
if (!options.mainnet && !options.sepolia) {
console.error('Error: You must specify either --mainnet or --sepolia');
process.exit(1);
}
runDeposit(options);
});

// Defaults to 'deposit' subcommand if no other subcommand is provided
program
.arguments('[args...]')
.action((args, cmdObj) => {
if (cmdObj.opts().keyFile || cmdObj.opts().destination || cmdObj.opts().amount || cmdObj.opts().mainnet || cmdObj.opts().sepolia || cmdObj.opts().rpcUrl) {
runDeposit(cmdObj.opts());
let chainName = '';
if (options.mainnet) {
chainName = 'mainnet'
} else if (options.sepolia) {
chainName = 'sepolia'
} else {
console.error('Error: Missing arguments for deposit.');
program.outputHelp();
throw new Error("Invalid chain name");
}
runDeposit({
destination: options.destination,
amount: options.amount,
chainName: chainName,
keyFile: options.keyFile
});
});
// destination, amount, chain_name, keyFile,
// Defaults to 'deposit' subcommand if no other subcommand is provided
// program
// .arguments('[args...]')
// .action((args, cmdObj) => {
// let chainName = '';
// if (cmdObj.opts().mainnet) {
// chainName = 'mainnet'
// } else if (cmdObj.opts().sepolia) {
// chainName = 'sepolia'
// } else {
// throw new Error("Invalid chain name");
// }
// if (cmdObj.opts().destination || cmdObj.opts().amount || cmdObj.opts().keyFile ) {
// runDeposit({
// destination: cmdObj.opts().destination,
// amount: cmdObj.opts().amount,
// chainName: chainName,
// keyFile: cmdObj.opts().keyFile
// });
// } else {
// console.error('Error: Missing arguments for deposit.');
// program.outputHelp();
// }
// });

program.parse(process.argv);
2 changes: 1 addition & 1 deletion example-private-key.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

5 changes: 3 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { readFileSync } from 'fs';
import { mainnet, sepolia } from 'viem/chains';

// Define network configurations
export const NETWORK_CONFIG = {
mainnet: {
etherBridgeAddress: '0x83cB71D80078bf670b3EfeC6AD9E5E6407cD0fd1',
rpcUrl: 'https://eth.llamarpc.com',
chain: mainnet,
},
sepolia: {
etherBridgeAddress: '0xA54BCb72deDdEA36ef2B4dA12741eFcdC7c72eF1',
rpcUrl: 'https://rpc2.sepolia.org',
chain: sepolia,
},
};

Expand Down
21 changes: 7 additions & 14 deletions src/deposit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import bs58 from 'bs58';

export async function deposit(
client,
account,
etherBridgeAddress,
destination,
amountWei,
Expand Down Expand Up @@ -38,22 +39,14 @@ export async function deposit(
args: [destinationBytes32, amountWei],
});

// Convert amountWei to a hex string
const valueHex = `0x${amountWei.toString(16)}`;

// Send the transaction
const tx = await client.request({
method: 'eth_sendTransaction',
params: [
{
from: client.account.address,
to: etherBridgeAddress,
data,
value: valueHex,
},
],
const hash = await client.sendTransaction({
data: data.toString(),
account,
to: etherBridgeAddress,
value: amountWei,
});
console.log(`Transaction hash: ${tx.hash}`);
console.log(`Transaction hash: ${hash}`);
} catch (error) {
console.error(`Error during deposit: ${error.message}`);
throw error;
Expand Down
53 changes: 25 additions & 28 deletions src/lib.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,45 @@
import { createClient, http, parseEther } from 'viem';
import { createWalletClient, http, parseEther } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { NETWORK_CONFIG, getPrivateKeyFromFile } from './config.js';
import { validateSolanaAddress } from './validate.js';
import { deposit } from './deposit.js';

export async function runDeposit({
keyFile,
destination,
amount,
mainnet,
sepolia,
rpcUrl,
}) {
export async function runDeposit({ destination, amount, chainName, keyFile }) {
try {
// Determine the network and validate
const network = mainnet ? 'mainnet' : sepolia ? 'sepolia' : null;
if (!network) {
throw new Error('Invalid network selection.');
}

// Validate and decode destination address
if (!validateSolanaAddress(destination)) {
throw new Error('Invalid Solana address.');
}

// Parse and validate the amount
const amountWei = parseEther(amount);
const minAmountWei = parseEther('0.002');
if (amountWei < minAmountWei) {
throw new Error(
'Insufficient deposit value. Min is 0.002 ether / 2_000_000 gwei',
);
}

// Retrieve the private key from file
const privateKey = getPrivateKeyFromFile(keyFile);

// Get network-specific configuration
const { etherBridgeAddress, rpcUrl: defaultRpcUrl } =
NETWORK_CONFIG[network];
const effectiveRpcUrl = rpcUrl || defaultRpcUrl;
// Retrieve and validate the private key from file
let privateKey = getPrivateKeyFromFile(keyFile);
if (!privateKey.startsWith('0x')) {
privateKey = `0x${privateKey}`;
}
const account = privateKeyToAccount(privateKey);

// Set up the client
const transport = http(effectiveRpcUrl);
const client = createClient({
transport: transport,
account: { privateKey },
// Create wallet client from chain
const networkConfig = NETWORK_CONFIG[chainName];
if (!networkConfig) {
throw new Error(`No configuration found for chain: ${chainName}`);
}
const { chain, etherBridgeAddress } = networkConfig;
const client = createWalletClient({
chain: chain,
transport: http(),
});

// Call the deposit function with the validated inputs
await deposit(client, etherBridgeAddress, destination, amountWei);
await deposit(client, account, etherBridgeAddress, destination, amountWei);
} catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
Expand Down

0 comments on commit 5fb9b99

Please sign in to comment.