I need your help because I've been stuck on this for several days.
I'm trying to do a super simple swap on Solana. Between Sol and USDC on devnet with Raydium.
const { Connection, Keypair, PublicKey } = require('@solana/web3.js');
const { Liquidity, TokenAmount, Token, Percent } = require('@raydium-io/raydium-sdk');
const devnetConnection = new Connection('https://api.devnet.solana.com');
const privateKey = []; // secretKey
const wallet = Keypair.fromSecretKey(new Uint8Array(privateKey));
const ADDRESSFROM = 'So11111111111111111111111111111111111111112';
const ADDRESSTO = 'EP2ib6dYdEeqD8MfE2ezHCxX3kP3K2eLKkirfPm5eyMx';
const OWNER = '675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8';
async function main() {
const poolKeys = {
id: new PublicKey(OWNER),
};
const TOKENFROM = new Token(devnetConnection, new PublicKey(ADDRESSFROM), 9, wallet.publicKey);
const TOKENTO = new Token(devnetConnection, new PublicKey(ADDRESSTO), 6, wallet.publicKey);
const rawAmountIn = 1;
const amountIn = new TokenAmount(TOKENFROM, rawAmountIn * 10**TOKENFROM.decimals, false);
const slippage = new Percent(50, 10000);
const poolInfo = await Liquidity.fetchInfo(devnetConnection, poolKeys);
const { minAmountOut } = Liquidity.computeAmountOut(poolInfo, amountIn, TOKENTO, slippage);
const { transaction, signers } = await Liquidity.makeSwapTransaction({
connection: devnetConnection,
poolKeys,
userKeys: {
owner: wallet.publicKey,
},
amountIn,
amountOut: minAmountOut,
fixedSide: 'in',
});
const signature = await devnetConnection.sendTransaction(transaction, [wallet, ...signers]);
console.log(`Transaction hash: ${signature}`);
await devnetConnection.confirmTransaction(signature);
console.log(`Transaction confirmed: ${signature}`);
}
main().catch(error => {
console.error(error);
});
I can't resolve this public key issue and make a transaction.
reason: 'invalid public key',
code: 'INVALID_ARGUMENT',
argument: 'publicKey',
The USDC address on devnet is different than mainnet.
You're using the mainnet address of
EP2ib6dYdEeqD8MfE2ezHCxX3kP3K2eLKkirfPm5eyMx, but if you look at that account on devnet, you'll see that it doesn't exist https://explorer.solana.com/address/EP2ib6dYdEeqD8MfE2ezHCxX3kP3K2eLKkirfPm5eyMx?cluster=devnetThis is the official devnet USDC address: https://explorer.solana.com/address/4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU?cluster=devnet