I am using the Alchemy SDK to build an ethereum wallet app on react native. The docs (https://docs.alchemy.com/docs/how-to-send-transactions-on-ethereum) show me how to send Eth and I have already tested this and it works. However, I don't see anywhere in the docs on how to specify which token should be sent. I am wondering how I can set up my transaction object so that it will send the correct token. Our app handles Eth and all erc20 tokens currently so that the user can see their balances.
Here is the method for sending Eth using Alchemy's SDK
const { Alchemy, Network, Wallet, Utils } = require("alchemy-sdk");
const dotenv = require("dotenv");
dotenv.config();
const { API_KEY, PRIVATE_KEY } = process.env;
const settings = {
apiKey: API_KEY,
network: Network.ETH_GOERLI,
};
const alchemy = new Alchemy(settings);
let wallet = new Wallet(PRIVATE_KEY);
async function main() {
const nonce = await alchemy.core.getTransactionCount(
wallet.address,
"latest"
);
let transaction = {
to: "0xa238b6008Bc2FBd9E386A5d4784511980cE504Cd",
value: Utils.parseEther("0.001"),
gasLimit: "21000",
maxPriorityFeePerGas: Utils.parseUnits("5", "gwei"),
maxFeePerGas: Utils.parseUnits("20", "gwei"),
nonce: nonce,
type: 2,
chainId: 5,
};
let rawTransaction = await wallet.signTransaction(transaction);
let tx = await alchemy.core.sendTransaction(rawTransaction);
console.log("Sent transaction", tx);
}
main();
I also found this article in the docs which explains the transaction object (https://docs.alchemy.com/docs/understanding-the-transaction-object-on-ethereum) but I still do not see where I would specify the token that I am sending.
The Alchemy SDK does not contain any CORE methods that you can use to connect to an interface of a deployed ERC20 contract with a view to using it to send transactions. However, you can connect with a deployed instance of a contract using a library like web3js
or EthersJS
To send tokens from an ERC20 contract, you need to use a library like Ethersjs
to connect to an instance of the deployed contract. An Ethereum
is also required to connect to a deployed instance of the contract. A Provider is an abstraction of a connection to the Ethereum network, providing a concise, consistent interface to standard Ethereum node functionality. With a Provider, you can connect to an instance of a deployed contract and carry out transactions, track events, etc.
To be able to send transactions with an ERC20 token using the Alchemy SDK, you need the following:
To connect to the EthersJS
provider, use your Alchemy API KEY.
The following code is an example code which you can reference:
const { Wallet, Utils } = require("alchemy-sdk");
const dotenv = require("dotenv");
dotenv.config();
const { ALCHEMY_KEY, PRIVATE_KEY } = process.env;
const ethers = require("ethers");
const ABI = require("./ABI.json");
let wallet = new Wallet(PRIVATE_KEY);
const CONTRACT_ADDRESS = "0x...";
const provider = new ethers.providers.AlchemyProvider(
`https://eth-goerli.g.alchemy.com/v2/${ALCHEMY_KEY}`
);
async function main() {
const contract = new ethers.Contract(CONTRACT_ADDRESS, ABI, provider);
const amount = Utils.parseEther("0.001");
await contract.transfer(wallet.address, "<receiver-wallet-address>", amount);
console.log("Sent ", amount);
}
main();