Transfer NFT using Token.createTransferInstruction - node.js

We have a transaction that transfers SOL & an NFT from the wallet's owner to our wallet.
The transaction contains simple instructions:
Token.createAssociatedTokenAccountInstruction
Conditionnal, depending on the destination (our wallet)
Token.createTransferInstruction
Transfers from the owner's token account to our token account.
SystemProgram.transfer
SOL transfer.
Here is the transfer code (spl-token v0.1.8)
let createTransferInstructions = async function (mint, from, to, cluster) {
let connection = new Connection(cluster, "confirmed")
const mintPublicKey = new PublicKey(mint);
const ownerPublicKey = new PublicKey(from);
const destPublicKey = new PublicKey(to);
// GET SOURCE ASSOCIATED ACCOUNT
const associatedSourceTokenAddr = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
mintPublicKey,
ownerPublicKey
);
// GET DESTINATION ASSOCIATED ACCOUNT
const associatedDestinationTokenAddr = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
mintPublicKey,
destPublicKey
);
const receiverAccount = await connection.getAccountInfo(associatedDestinationTokenAddr);
const instructions = [];
if (receiverAccount === null)
instructions.push(
Token.createAssociatedTokenAccountInstruction(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
mintPublicKey,
associatedDestinationTokenAddr,
destPublicKey,
ownerPublicKey
)
)
instructions.push(
Token.createTransferInstruction(
TOKEN_PROGRAM_ID,
associatedSourceTokenAddr,
associatedDestinationTokenAddr,
ownerPublicKey,
[],
1
)
);
return instructions;
}
Here is the transaction code:
var transaction = new this.solana.Transaction({
feePayer: new this.solana.PublicKey(from),
recentBlockhash: await blockhashObj.blockhash
});
for (let transferInstruction of transferInstructions) {
transaction.add(transferInstruction);
}
transaction.add(this.solana.SystemProgram.transfer({
fromPubkey: new this.solana.PublicKey(this.provider.publicKey),
toPubkey: new this.solana.PublicKey(farm.address),
lamports: 10000000
}));
The owners have to validate the transaction with their wallets (phantom, solflare etc..)
And everything works great, for most NFTs.
Some NFT owners are complaining about not being able to validate the transaction on the wallet, an error message indicates that the transaction can't be confirmed.
However, the NFT can be transferred successfully using the wallet's interface, can be listed on marketplaces etc. Even when the NFT is transferred to another wallet, the transaction above works perfectly.
I'm wondering if this has anything to do with the associated token account?
If so, what is the solution?

Related

I am not be able to transfer NFT using #hashgraph/sdk hedera npm. How to transfer nft using #hashgraph/sdk's in built method?

I am not able to transfer NFT using #hashgraph/SDK hedera npm. How to transfer nft using #hashgraph/SDK's inbuilt method?
Also, Nft is already associated with the seller account.
Also, I am transferring token which is available in hashpack nft section.
I am getting the below error on calling the TransferTransaction method of #hashgraph/sdk#2.17.1.
Code:
const TransferNft = async (tokenId, sellerAccount, sellerId, buyerAccount, buyerId) => {
try {
const client = await getClient();
tokenId = TokenId.fromString(tokenId);
let sellerKey = await getPrivateKey(sellerId);
sellerKey = PrivateKey.fromString(sellerKey);
let buyerKey = await getPrivateKey(buyerId);
buyerKey = PrivateKey.fromString(buyerKey);
// 2nd NFT TRANSFER NFT Alice->Bob
let tokenTransferTx2 = await new TransferTransaction()
.addNftTransfer(tokenId, 2, sellerAccount, buyerAccount)
.addHbarTransfer(sellerAccount, Hbar.fromTinybars(100))
.addHbarTransfer(buyerAccount, Hbar.fromTinybars(-100))
.freezeWith(client)
.sign(sellerKey);
let tokenTransferTx2Sign = await tokenTransferTx2.sign(buyerKey);
let tokenTransferSubmit2 = await tokenTransferTx2Sign.execute(client);
let tokenTransferRx2 = await tokenTransferSubmit2.getReceipt(client);
console.log(`\n NFT transfer Alice->Bob status: ${tokenTransferRx2.status} \n`);
return tokenTransferRx2.status;
} catch (error) {
console.log('Error in HederaToken/TransferNft/TransferNft: \n', error)
}
};
receipt for transaction 0.0.40217130#1663228521.315536859 contained error status TOKEN_NOT_ASSOCIATED_TO_ACCOUNT
Error:
Before an account that is not the treasury for an HTS token can receive or send a specific token ID, they must be “associated” with the token — this helps reduce unwanted spam and other concerns from users that don’t want to be associated with any of the variety of tokens that are created on the Hedera network.
This association between an account and a token ID can be done in two ways, manually or automatically. Note that automatic associations can be done for both existing and newly created accounts.
Here's how you would do an auto-association for an existing account via an account update:
// AUTO-ASSOCIATION FOR ALICE'S ACCOUNT
let associateTx = await new AccountUpdateTransaction()
.setAccountId(aliceId)
.setMaxAutomaticTokenAssociations(100)
.freezeWith(client)
.sign(aliceKey);
let associateTxSubmit = await associateTx.execute(client);
let associateRx = await associateTxSubmit.getReceipt(client);
console.log(`Alice NFT Auto-Association: ${associateRx.status} \n`);
Here's how you would do a manual association for an existing account:
// MANUAL ASSOCIATION FOR BOB'S ACCOUNT
let associateBobTx = await new TokenAssociateTransaction()
.setAccountId(bobId)
.setTokenIds([tokenId])
.freezeWith(client)
.sign(bobKey);
let associateBobTxSubmit = await associateBobTx.execute(client);
let associateBobRx = await associateBobTxSubmit.getReceipt(client);
console.log(`Bob NFT Manual Association: ${associateBobRx.status} \n`);

Failed To Transfer BEP20 Smart Contract Token

I am writing an API to transfer my custom bep20 contract token from the owner's account to another account. For example, I have 10,000 units of my token in my wallet and i want to transfer any specified amount to another account using web3js inside the node server. here is what I have done so far and is not working properly. am having Failed as the response and also Error: Transaction has been reverted by the EVM:
Here is my token transferring function in node:
async transferToken(pkey, tokenContractAddress , toAddress , amount , chainId, callback) {
//import account by private key
let account = this.web3.eth.accounts.privateKeyToAccount(pkey);
let wallet = this.web3.eth.accounts.wallet.add(account);
// ABI to transfer ERC20 Token
let abi = ABI_CONFIG;
// calculate ERC20 token amount
let tokenAmount = this.web3.utils.toWei(amount.toString(), 'ether')
// Get ERC20 Token contract instance
let contract = new this.web3.eth.Contract(abi, tokenContractAddress, {from: wallet.address});
const data = await contract.methods.transfer(toAddress, tokenAmount).encodeABI();
// The gas price is determined by the last few blocks median gas price.
const gasPrice = await this.web3.eth.getGasPrice();
const gasLimit = 90000;
// Build a new transaction object.
const rawTransaction = {
'from': wallet.address,
'nonce': this.web3.utils.toHex(this.web3.eth.getTransactionCount(wallet.address)),
'gasPrice': this.web3.utils.toHex(gasPrice),
'gasLimit': this.web3.utils.toHex(gasLimit),
'to': tokenContractAddress,
'value': 0,
'data': data,
'chainId': chainId
};
const res = await contract.methods.transfer(toAddress, tokenAmount).send({
from: wallet.address,
gas: 30400
});
console.log(res);
// Get Name
let name = await contract.methods.name().call();
// Get Symbol
let symbol = await contract.methods.symbol().call();
/* send to hyperledger */
const map = {
"action_type" : "SEND_TOKEN",
"from_wallet_address" : wallet.address,
"to_wallet_address" : toAddress,
"amount" : this.web3.utils.toWei(amount.toString(), 'ether'),
"tx_hash" : res.transactionHash,
"gasLimit" : 21000,
"gasPrice" : gasPrice,
"fee" : gasPrice * 21000,
"token_smart_contract" : tokenContractAddress,
"token_name" : name,
"token_symbol" : symbol,
"status" : "SUCCESS"
}
callback({data: res, map});
}
There is not enough gas to complete the operation as most of the 30400 was used by the transaction. I think you must increase your gas limit from 30400 to 100000 for example.

Polygon transaction working just fine on Mumbai but not on Mainnet

Hello I'm trying to mint an NFT using Polygon and it works just fine on Mumbai but as soon as i switch over to the mainnet the transaction doesn't go through instead of going through in 5 seconds on mumbai. Even though im using the exact same contract just deployed on the mainnet instead of Mumbai and the code is the same too. All im doing is switching the contract address and rpc url but for some reason it just doesn't work on the Polygon mainnet below is the code im using.
// Init contract
const contractABI = require('../../contract-abi.json');
const contractAddress = config.mintingContractAddress;
const contract = await new this.web3.eth.Contract(contractABI, contractAddress);
// Mint NFT
const nft = contract.methods.mintNFT(user.walletAddress, metadataUploadURL, user.paymentAddress).encodeABI();
// Get gas pricing
const priorityFees = await axios.get('https://gasstation-mainnet.matic.network');
const estBaseGas = await this.web3.eth.estimateGas({
data: nft,
to: contractAddress,
});
console.log('USING GAS: ' + estBaseGas);
// Sign NFT minting transaction
const totalGas = estBaseGas + priorityFees.data.standard;
console.log('TOTALGAS: ', Math.round(totalGas).toString());
const transaction = await this.web3.eth.accounts.signTransaction(
{
from: user.walletAddress,
to: contractAddress,
nonce: await this.web3.eth.getTransactionCount(user.walletAddress, 'pending'), // Get count of all transactions sent to the contract from this address including pending ones
data: nft,
// maxPriorityFee: priorityFees.data.average, Not supported on Polygon MATIC yet
gas: Math.round(totalGas).toString(),
gasPrice: await this.web3.eth.getGasPrice(),
},
wallet.privateKey,
);
this.logger.silly('Finished signing NFT transaction');
// Send the transaction that we signed
const mintT = await this.web3.eth.sendSignedTransaction(transaction.rawTransaction);
this.logger.silly('Sent transaction');
console.log(mintT);
Also tried this for signing
// Get gas pricing
const priorityFees = await axios.get('https://gasstation-mainnet.matic.network');
const estBaseGas = await this.web3.eth.estimateGas({
data: nft,
to: contractAddress,
});
console.log('USING GAS: ' + estBaseGas);
// Sign NFT minting transaction
const totalGas = estBaseGas + priorityFees.data.standard;
console.log('TOTALGAS: ', Math.round(totalGas).toString());
console.log('P', priorityFees.data.standard);
const gp = this.web3.utils.toWei(priorityFees.data.standard.toString(), 'Gwei').toString();
console.log('GP', gp);
const transaction = await this.web3.eth.accounts.signTransaction(
{
from: user.walletAddress,
to: contractAddress,
nonce: await this.web3.eth.getTransactionCount(user.walletAddress, 'pending'), // Get count of all transactions sent to the contract from this address including pending ones
data: nft,
// maxPriorityFee: priorityFees.data.average, Not supported on Polygon MATIC yet
gas: '1000000',
gasPrice: gp,
},
wallet.privateKey,
);
Mempool explorer for transaction that takes forever and nearly instant one.
Forever:
Instant:
One on mainnet that used 30 gwei of gas:
Does anybody know why this is happening?
Also yes i do know that the fast one does have 2 extra gwei in gas but even setting it to that manually it still takes forever and according to https://polygonscan.com/gastracker even with one gwei it should be processed within 30 seconds. Even when using 50 Gwei it seems to take hours to process or maybe it's being dropped? The transactions don't even seem to be getting to the contract they are just stuck somewhere in the chain.
contract address: 0xa915E82285e6F82eD10b0579511F48fD716a2043
contract source code:
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "#openzeppelin/contracts/utils/Counters.sol";
contract MyNFT is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
event MintedNFT(address recipent,string tokenURI,address artist, uint256 tokenID);
mapping(uint256 => address) private artists; // Used to store token ids => artist addresses
// mapping(uint256 => uint256) private royalties; // tokenId => royaltyPercentage
// mapping(uint256 => address) private nftMintInitators; // Used to store token ids => sender addresses
// mapping(uint256 => bool) private royaltiesSet;
constructor(string memory name_, string memory symbol_)
ERC721(name_, symbol_) {
}
// // Support for https://eips.ethereum.org/EIPS/eip-2981
// /// #notice Called with the sale price to determine how much royalty
// // is owed and to whom.
// /// #param _tokenId - the NFT asset queried for royalty information
// /// #param _salePrice - the sale price of the NFT asset specified by _tokenId
// /// #return receiver - address of who should be sent the royalty payment
// /// #return royaltyAmount - the royalty payment amount for _salePrice
// function royaltyInfo(
// uint256 _tokenId,
// uint256 _salePrice
// ) external view returns (
// address receiver,
// uint256 royaltyAmount
// ) {
// return (
// artists[_tokenId],
// _salePrice * royalties[_tokenId] // Take percentage
// );
// }
// function updateRoyaltyPercentage(
// uint256 royaltyPercentage, // In decimal like 0.5 or 0.25 (Send 0.0 for no royalties)
// uint256 tokenID
// ) public {
// if (msg.sender == nftMintInitators[tokenID] && royaltiesSet[tokenID] == false) {
// royalties[tokenID] = royaltyPercentage;
// royaltiesSet[tokenID] = true;
// }
// }
function mintNFT(address recipient,
string memory tokenURI,
address artist // Address for the artist not using _msgSender() because this transaction is sent by the users NFT holding account
)
public
returns (uint256)
{
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
_setTokenURI(newItemId, tokenURI);
artists[newItemId] = artist;
// nftMintInitators[newItemId] = msg.sender;
// royaltiesSet[newItemId] = false;
emit MintedNFT(recipient,tokenURI,artist,newItemId);
return newItemId;
}
}
You can test with simple code just to mint one NFT. Adding gasPrice and gasLimit parameters directly into minting function could help
const hre = require("hardhat");
async function main() {
const NFT = await hre.ethers.getContractFactory("NFTNAME");
const WALLET_ADDRESS = "0xxxxxxxxxxxxxx"
const CONTRACT_ADDRESS = "0xa915E82285e6F82eD10b0579511F48fD716a2043"
const contract = NFT.attach(CONTRACT_ADDRESS);
mintedNFT = await contract.mintNFT(WALLET_ADDRESS,{ gasLimit: 285000, gasPrice: ethers.utils.parseUnits('30', 'gwei')});
console.log("NFT minted:", mintedNFT);
}
main().then(() => process.exit(0)).catch(error => {
console.error(error);
process.exit(1);
});

Stripe expanding the Account object in BankAccountListOptions is not working while data.account is set

I'm using Stripe.Net to fetch a customer's bank accounts. However, the account object is null despite data.account is passed to the Expand property as follows.
var serviceb = new BankAccountService();
var opts = new BankAccountListOptions {
Expand = new List<string> {
"data.account"
}
};
var lst = await serviceb.ListAsync(customerId, opts);
var firstBankAccount = lst.First();
firstBankAccount.Account and firstBankAccount.AccountId are null, which I'm unable to figure out what's wrong.
Note: I've linked the customer's bank account through Plaid Link, stripe_bank_access_token, in Sandbox mode. Also, I'm using Stripe on test mode.
var options = new CustomerCreateOptions
{
Description = "User",
Source = Source, // a tok_* which provided by Plaid through `PLAID/processor/stripe/bank_account_token/create` API
Email = "a#a.com"
};
var service = new CustomerService();
await service.CreateAsync(options);
Here is the bank account and the customer that was created on Stripe using the above code.

Stripe error: You cannot use a PaymentMethod as a source for Customers

I am new to NodeJS, and I am trying to integrate Stripe payments, using Firebase Cloud functions. I Followed these steps:
I got the token from client-side, stored it in Firestore, the token looks like this: pm_1FFhvNDcXKDMgaqV...
I've created a Customer on Stripe
exports.createNewStripeCustomer =
functions.auth.user().onCreate(
async (user) => {
const customer = await stripe.customers.create({email:
user.email}) ;
return admin.firestore()
.collection('customers')
.doc(user.uid)
.set({customer_id: customer.id});
}
);
The above code works.
Now I've tried to add a payment source using the token as specified in tutorials and docs and I keep getting the following error:
Error: You cannot use a payment method as a source for Customers. Instead, use the payment methods API to Attach a Customer to a payment method. See https://stripe.com/docs/api/payment_methods/attach
Here's the code that causes the error:
exports.newPaymentSource = functions.firestore.document('customers/{userId}/tokens/{tokenId}').onWrite(async (change, context) => {
//Get token that strike sdk gave to the client...
const data = change.after.data();
if (data ===null) { return null }
const token = data.tokenId;
const snapshot = await firestore.collection('customers').doc(context.params.userId).get();
const customer = snapshot.data().customer_id;
//calling stripe api...
console.log(customer + ":" + token + ":" + context.params.userId);
const respFromStripe = await stripe.customers.createSource(customer, { source: token });
// console.log(respFromStripe);
return firestore.collection('users').doc(context.params.userId)
.collection('sources').doc(respFromStripe.card.fingerprint).set(respFromStripe, { merge: true });
});
PaymentMethod objects (which represent your user's cards) need to be attached to a Customer with /v1/payment_methods/pm_123/attach endpoint or in Stripe-Node:
pm = await stripe.paymentMethods.attach('pm_678', {customer: 'cus_123'});
https://stripe.com/docs/api/payment_methods/attach?lang=node
The way you're using it (customer.createSource()) works for older Tokens (tok_123) and Source (src_123) objects, not PaymentMethods.

Resources