This script configures the Alchemy SDK, initializes a wallet, defines an Ethereum transaction, signs it, and submits it to the blockchain. The resulting transaction hash is then logged for reference.
This repository provides a Node.js example for automating Ethereum transactions using the Alchemy SDK. Alchemy SDK is a powerful blockchain developer platform that simplifies blockchain development tasks. The example demonstrates basic usage of the Alchemy SDK for fetching block information, token balances, NFTs, and listening to pending transactions.
Visit the Alchemy SDK to create an app. After creating the app, you'll be provided with an API key and necessary details.
To integrate the Alchemy SDK into your project, follow these steps:
- Install the Alchemy SDK Package
npm install alchemy-sdk
create the index.js file for the below:
import { Network, Alchemy } from 'alchemy-sdk';
const settings = {
apiKey: "your Alchemy API-KEY",
network: Network.your_ref_network,
};
const alchemy = new Alchemy(settings);
// Get the latest block
const latestBlock = alchemy.core.getBlockNumber();
// Get all outbound transfers for a provided address
alchemy.core
.getTokenBalances('provided address')
.then(console.log);
// Get all the NFTs owned by an address
const nfts = alchemy.nft.getNftsForOwner("reference eth address");
// Listen to all new pending transactions
alchemy.ws.on(
{ method: "alchemy_pendingTransactions",
fromAddress: "reference eth address" },
(res) => console.log(res)
);
Note: Replace 'your_private_key_here' with your actual private key. Do not share it with anyone!
API_KEY=your_reference_alchemy_API_key
PRIVATE_KEY=your_private_key_here
node index.js