How to transfer NFT spl-token using phantom wallet and solana web3js - javascript

I can transfer the Solana from one account to another account using phantom wallet using this code
const transferTransaction = new Transaction()
.add(SystemProgram.transfer({
fromPubkey: alice.publicKey,
toPubkey: feePayer.publicKey,
lamports: lamportsToSend
}))
const network = "https://api.devnet.solana.com";
const connection = new Connection(network);
transferTransaction.recentBlockhash = (await connection.getRecentBlockhash()).blockhash;
transferTransaction.feePayer = alice.publicKey;
const { signature } = await window.solana.signAndSendTransaction(transferTransaction);
await connection.confirmTransaction(signature);
console.log(signature);
but I am wondering how can I transfer the NFT if I have the nft minted address?

To transfer an NFT, you first need to find out the address of the NFT's mint and the owner's address. Then instead of calling SystemProgram.transfer, you'll use Token.createTransferCheckedInstruction.
There's a great example at the Solana Cookbook for transferring SPL tokens: https://solanacookbook.com/references/token.html#transfer-token

Related

How to extract image URL from ens avatar text record

When using ethers JavaScript library to retrieve ENS text records from the Ethereum Name Service. I got the NFT URI from the text record but not the image URL. Please is there any way/service to convert this metadata URI to an HTTPS image URL.
TO RECREATE:
TERMINAL : npm install --save ethers
Code:
var ethers = require('ethers');
var url = 'https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161';
var provider = new ethers.providers.JsonRpcProvider(url);
async function main() {
const resolver = await provider.getResolver("brantly.eth");
const avatar = await resolver.getAvatar();
const avatarMetaData = await resolver.getText("avatar");
console.log(`Avatar Metadata: ${avatarMetaData}`);
}
main();
.
Output: Avatar Metadata: eip155:1/erc721:0xb7F7F6C52F2e2fdb1963Eab30438024864c313F6/2430
The EIP155... Is not an imageURL.
I'd prefer it came in the form of {https://...}.
Is there any way to achieve this or convert the NFT URI EIP155... To an image or image URL.
As far as I know you will have to go through the entire process to get that IPFS URI.
You will want to take the contract address and token ID from that returned avatar string (the last 2 parts) and call the tokenURI ABI method, parse the JSON and read image there:
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(address, abi, signer);
const uri = await contract.tokenURI(tokenId)
const data = (await axios.get(uri)).data;
const image = data.image;

How to modify my code to send custom SPL Token instead of regular SOL?

I'm building a website where people log in to their phantom wallet then by clicking on a button they will send a certain amount of our custom token to one wallet.
The code shown below is working with SOL and I would like to make it work with our custom SPL token, I have the mint address of the token but I couldn't find any way to make it work. Could anyone help me?
async function transferSOL(toSend) {
// Detecing and storing the phantom wallet of the user (creator in this case)
var provider = await getProvider();
console.log("Public key of the emitter: ",provider.publicKey.toString());
// Establishing connection
var connection = new web3.Connection(
"https://api.mainnet-beta.solana.com/"
);
// I have hardcoded my secondary wallet address here. You can take this address either from user input or your DB or wherever
var recieverWallet = new web3.PublicKey("address of the wallet recieving the custom SPL Token");
var transaction = new web3.Transaction().add(
web3.SystemProgram.transfer({
fromPubkey: provider.publicKey,
toPubkey: recieverWallet,
lamports: (web3.LAMPORTS_PER_SOL)*toSend //Investing 1 SOL. Remember 1 Lamport = 10^-9 SOL.
}),
);
// Setting the variables for the transaction
transaction.feePayer = await provider.publicKey;
let blockhashObj = await connection.getRecentBlockhash();
transaction.recentBlockhash = await blockhashObj.blockhash;
// Request creator to sign the transaction (allow the transaction)
let signed = await provider.signTransaction(transaction);
// The signature is generated
let signature = await connection.sendRawTransaction(signed.serialize());
// Confirm whether the transaction went through or not
console.log(await connection.confirmTransaction(signature));
//Signature chhap diya idhar
console.log("Signature: ", signature);
}
I'd like to specify that people will use phantom and I cant have access to their private keys (cause it was needed in all the answers I found on internet)
You're very close! You just need to replace the web3.SystemProgram.transfer instruction with an instruction to transfer SPL tokens, referencing the proper accounts. There's an example at the Solana Cookbook covering this exactly situation: https://solanacookbook.com/references/token.html#transfer-token
You can do this with help of anchor and spl-token, which is used for dealing with custom tokens on solana.
This is a custom transfer function. You'll need the mint address of the token, wallet from which the tokens will be taken( which you get in front end when user connects wallet. Can make use of solana-web3), to address and amount.
import * as splToken from "#solana/spl-token";
import { web3, Wallet } from "#project-serum/anchor";
async function transfer(tokenMintAddress: string, wallet: Wallet, to: string, connection: web3.Connection, amount: number) {
const mintPublicKey = new web3.PublicKey(tokenMintAddress);
const {TOKEN_PROGRAM_ID} = splToken
const fromTokenAccount = await splToken.getOrCreateAssociatedTokenAccount(
connection,
wallet.payer,
mintPublicKey,
wallet.publicKey
);
const destPublicKey = new web3.PublicKey(to);
// Get the derived address of the destination wallet which will hold the custom token
const associatedDestinationTokenAddr = await splToken.getOrCreateAssociatedTokenAccount(
connection,
wallet.payer,
mintPublicKey,
destPublicKey
);
const receiverAccount = await connection.getAccountInfo(associatedDestinationTokenAddr.address);
const instructions: web3.TransactionInstruction[] = [];
instructions.push(
splToken.createTransferInstruction(
fromTokenAccount.address,
associatedDestinationTokenAddr.address,
wallet.publicKey,
amount,
[],
TOKEN_PROGRAM_ID
)
);
const transaction = new web3.Transaction().add(...instructions);
transaction.feePayer = wallet.publicKey;
transaction.recentBlockhash = (await connection.getRecentBlockhash()).blockhash;
const transactionSignature = await connection.sendRawTransaction(
transaction.serialize(),
{ skipPreflight: true }
);
await connection.confirmTransaction(transactionSignature);
}

How to save keypairs and send tokens in Solana, using node.js?

I am attempting at using Solana over Ethereum for a particular solution.
I am using NPM to target Solana's API's. The packages being used are #solana/web3.js and #solana/spl-token. I've been able to create a successful connection and generate keypairs. I would like to be able to save a generated keypair, but the problem I face is that it's encoded, and the second issue to highlight is when I use the .toString(); it decodes it just fine but that can't be used in an airdrop.
const solanaWeb3 = require("#solana/web3.js"),
const solanatoken = require("#solana/spl-token");
(async () => {
// Connect to cluster
var connection = new solanaWeb3.Connection(
solanaWeb3.clusterApiUrl("devnet"),
"confirmed"
);
// Generate a new wallet keypair and airdrop SOL
var wallet = solanaWeb3.Keypair.generate();
console.log("public key...", wallet.publicKey)
// The stringPK is an example of what wallet.publicKey returns. When taking wallet.publicKey and using .toString(); it returns the decoded publicKey but that can't be used in an airdrop.
const stringPK = `_bn: <BN: e8a4421b46f76fdeac76819ab21708cc4a4b9c9c7fc2a22e373443539cee1a81>`;
console.log("private key...", JSON.stringify(wallet.secretKey.toString()));
console.log(solanaWeb3.PublicKey.decode(wallet.publicKey));
var airdropSignature = await connection.requestAirdrop(
stringPK,
solanaWeb3.LAMPORTS_PER_SOL
);
//wait for airdrop confirmation
await connection.confirmTransaction(airdropSignature);
let account = await connection.getAccountInfo(wallet.publicKey);
console.log(account);
})();
To save the generated keypair in JS, your best bet is to use secretKey on the Keypair object [1], write that to a file. To load it back up, you'll read that file and then use fromSecretKey to get back your Keypair [2]
As for your other issue, requestAirdrop takes a PublicKey, so instead, you should just do:
var airdropSignature = await connection.requestAirdrop(
wallet.publicKey,
solanaWeb3.LAMPORTS_PER_SOL
);
[1] https://solana-labs.github.io/solana-web3.js/classes/Keypair.html#secretKey
[2] https://solana-labs.github.io/solana-web3.js/classes/Keypair.html#fromSecretKey

Transaction to Uniswap failing on Ropsten TestNet

I am trying to create a transaction on Uniswap programatically, the flow nd code seems to be there, but for whatever reason the transaction fails on Ropsten for "Warning! Error encountered during contract execution [Reverted]". I am using javascript along with Nodejs as my server. Any suggestions on why it is failing? Code below:
const { ethers } = require("ethers");
const walletAddress = "My_own_address";
const wethErc20Address = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
const uniErc20Address = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984";
const uniswapRouterAbi = [
"function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)",
];
async function buyListedTokenWithEth(
amountEthFloat,
uniswapRouterAddress,
provider
) {
const wallet = new ethers.Wallet(Buffer.from(privateKey, "hex"));
const signer = wallet.connect(provider); //provider is Infura https ROPSTEN url
const exchangeContract = new ethers.Contract(
uniswapRouterAddress,
uniswapRouterAbi,
signer
);
const ethAmount = ethers.utils.parseEther("0.1");
const tx = await exchangeContract.swapExactTokensForTokens(
ethAmount,
0,
[wethErc20Address, uniErc20Address],
walletAddress,
createDeadline(), // Math.floor(Date.now() / 1000) + 20
createGasOverrides() // { gasLimit: ethers.utils.hexlify(300000), gasPrice: gasPriceWei }
);
console.log("https://ropsten.etherscan.io/tx/" + tx.hash);
}
The line:
const tx = await exchangeContract.swapExactTokensForTokens(
Should be:
const tx = await exchangeContract.methods.swapExactTokensForTokens(
The error is correct. Your transaction should not be successful.
Go to the uniswap ropsten router address. https://ropsten.etherscan.io/address/0x7a250d5630b4cf539739df2c5dacb4c659f2488d#readContract
There is a function to view WETH address for ropsten.
This is what it returns
0xc778417e063141139fce010982780140aa0cd5ab //Uniswap-router-factory-weth address
instead of
const wethErc20Address = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
You are asking the uniswap router to use the WETH address you list instead of the actual WETH address that it uses. Think about it this way. Why would an address with 0 liquidity on uniswap work on Uniswap?
Best to write some function to check liquidity before assuming it exist. No, I will not tell you how to do that.

hdwallet provider create wallet for user

I have created a smart contract that facilitates a token sale. I want a user to be able to press a button that automatically generates an ERC20 wallet that tokens can be sent to.
I am using the truffle HDWallet Provider and Infura. I don't want the user to have to use MetaMask or anything else. I will sign the transactions on the backend using the private key of the user's generated wallet.
How would I go about implementing this so a new wallet is created for every new user that wants to perform a transaction?
This is the code that allowed me to create a wallet on a button press. I now need to find a way to store the credentials of these wallets so I can sign transactions and transfer tokens to them.
var bip39 = require('bip39');
const EthereumUtil = require('ethereumjs-util');
const hdkey = require('hdkey');
const mnemonic = bip39.generateMnemonic(); //generates string
const seed = bip39.mnemonicToSeed(mnemonic); //creates seed buffer
const root = hdkey.fromMasterSeed(seed);
const masterPrivateKey = root.privateKey.toString('hex');
const addrNode = root.derive("m/44'/60'/0'/0/0");
const pubKey = EthereumUtil.privateToPublic(addrNode._privateKey);
const addr = EthereumUtil.publicToAddress(pubKey).toString('hex');
const address = EthereumUtil.toChecksumAddress(addr);
Its pretty simple, just create a HDWalletProvider and the provider.addresses has all the accounts you can use:
Here is the code:
const createWallet = function(_providerUrl) {
const mnemonicPhrase = bip39.generateMnemonic()
let provider = new HDWalletProvider({
mnemonic: {
phrase: mnemonicPhrase
},
providerOrUrl: "http://localhost:8545"
});
return provider.addresses
}
Addresses has the accounts
The solution has already been commented above, but I have a better suggestion for you. I think it's better to generate only one mnemonic in your backend and you create infinite accounts from that mnemonic. From each account you extract the private key and store it, store the address of the account and the index of the account.
Management code:
const HDWalletProvider = require("#truffle/hdwallet-provider");
const Web3 = require("web3");
const mnemonicPhrase = "mnemonicPhrase here";
const urlRPC = "https://bsc-dataseed.binance.org";
//Creating another instance to query balances
const Web3Instance = require('web3');
const web3instance = new Web3Instance(new Web3Instance.providers.HttpProvider(urlRPC));
//HD Wallet Provider loads by default only 10 accounts
//We change the limit to as many accounts as we want
//You cannot run the loop below for i greater than 10, because for
const limit = 50;
async function getAccount() {
provider = new HDWalletProvider({
mnemonic: mnemonicPhrase,
numberOfAddresses: limit,
providerOrUrl: urlRPC,
addressIndex: 0,
});
//HDWalletProvider is compatible with Web3
//Use it at Web3 constructor, just like any other Web3 Provider
const web3 = new Web3(provider);
//I realized that iterating a loop to limit always gives an error when it reaches limit
//This happens in this HD Wallet Provider package
//limit - 1 seems to work best
for (let i = 0; i < limit - 1; i ++) {
var get = await web3.eth.getAccounts()
var address = get[i];
var balanceETH = await web3instance.eth.getBalance(address)
//Checking the adress
console.log(address);
console.log(balanceETH);
}
}
getAccount();
Here is more information:
https://ethereum.stackexchange.com/questions/52370/web3-create-account-from-mnemonic-passphrase/144340#144340

Categories