Buy NFT from wallet1 which has all minted nft to the user wallet connected with phantom, Javascript, Solana - cryptography

Here is my code to transfer nft from Alice Account (which has my all minted nfts) and feePayer account which will buy from the Alice Wallet. Well this works perfectly fine like this. But the problem is when the same transection I try to sign and send with phantom wallet it gives me an error (You can check it by the commented line 101 to 108) when I uncomment the phantom signAndSend method code and try to execute it, it gives me error RPC req rejected but the same is successfully done with the connection.sendTransaction.
That was problem 1,
Now problem 2 is that
In this case, I had used feePayer which had the keypair already, but if I want to make it as user connects with connectWallet button, ill had only publicKey of the user. Now in that situaion how can i make the the nft transfer from Alice wallet to the user who connected and clicked on buy?
JS Code:
const sendNft = async () => {
const feePayer = Keypair.fromSecretKey(
bs58.decode(
"3DdVyZuANr5en2PQymCPmoFBMsfdhjaRHqnk3ejW*************************************************"
)
);
// G2FAbFQPFa5qKXCetoFZQEvF9BVvCKbvUZvodpVidnoY
const alice = Keypair.fromSecretKey(
bs58.decode(
"2YQDdnfxiHPKu9GypLX1yXaQTQojvDSPgFkDxrU*************************************************"
)
);
const mintPubkey = new PublicKey(
"A8SJfwzKJAaMrY6Lb9FxZCfmVMVLcjKvRuzAiNiU6of5"
);
const getProvider = async () => {
if ("solana" in window) {
// opens wallet to connect to
await window.solana.connect();
const provider = window.solana;
if (provider.isPhantom) {
console.log("Is Phantom installed? ", provider.isPhantom);
return provider;
}
} else {
window.open("https://www.phantom.app/", "_blank");
}
};
window.solana.on("connect", () => console.log("connected!"))
const network = "https://api.devnet.solana.com";
const connection = new Connection(network);
var prodivder = await getProvider();
console.log(prodivder.publicKey.toString())
var provider = await getProvider()
console.log(provider)
// calculate ATA
let accountAlice = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID, // always ASSOCIATED_TOKEN_PROGRAM_ID
TOKEN_PROGRAM_ID, // always TOKEN_PROGRAM_ID
mintPubkey, // mint
alice.publicKey // owner
);
console.log(`ATA: ${accountAlice.toBase58()}`);
let tx3 = new Transaction().add(
Token.createAssociatedTokenAccountInstruction(
ASSOCIATED_TOKEN_PROGRAM_ID, // always ASSOCIATED_TOKEN_PROGRAM_ID
TOKEN_PROGRAM_ID, // always TOKEN_PROGRAM_ID
mintPubkey, // mint
accountAlice, // ata
alice.publicKey, // owner of token account
alice.publicKey // fee payer
)
);
// calculate ATA
let accountPayer = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID, // always ASSOCIATED_TOKEN_PROGRAM_ID
TOKEN_PROGRAM_ID, // always TOKEN_PROGRAM_ID
mintPubkey, // mint
feePayer.publicKey // owner
);
console.log(`ATA: ${accountPayer.toBase58()}`);
let tx2 = new Transaction().add(
Token.createAssociatedTokenAccountInstruction(
ASSOCIATED_TOKEN_PROGRAM_ID, // always ASSOCIATED_TOKEN_PROGRAM_ID
TOKEN_PROGRAM_ID, // always TOKEN_PROGRAM_ID
mintPubkey, // mint
accountPayer, // ata
feePayer.publicKey, // owner of token account
feePayer.publicKey // fee payer
)
);
// console.log(`txhash: ${await connection.sendTransaction(tx2, [feePayer])}`);
let tx = new Transaction().add(
Token.createTransferCheckedInstruction(
TOKEN_PROGRAM_ID, // always TOKEN_PROGRAM_ID
accountAlice, // from (should be a token account)
mintPubkey, // mint
accountPayer, // to (should be a token account)
alice.publicKey, // owner of from
[], // for multisig account, leave empty.
1, // amount, if your deciamls is 8, send 10^8 for 1 token
0 // decimals
)
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Commenting out this, thus is the phantom codes
tx2.recentBlockhash = (await connection.getRecentBlockhash()).blockhash;
tx2.feePayer = feePayer.publicKey;
const { signature } = await window.solana.signAndSendTransaction(tx2);
await connection.confirmTransaction(signature);
// phantom code ends
console.log(`txhash: ${await connection.sendTransaction(tx, [feePayer, alice /* fee payer + owner */])}`);
};

Related

How can I mint a Hedera token that is not owned by the contract I am using to mint it?

I want to mint a Hedera Token with a smart contract. This token is not owned by the smart contract I am writing. I have been told on the Discord to 'put the private key in the contract' but this is not very specific and feels weird. The mintToken function provided in the Hedera Token Service takes as parameter:
an address
an amount
some metadata in the case of NFTs.
It's not impossible to mint a token made of a smart contract with HederaTokenService.
You may use a smart contract to make a token on hedera like ERC-20 or ERC-721, they works but they are not recognized as tokens in hedera network.
You have to use HederaTokenService to make a token on hedera.
You can use a smart contract using HederaTokenService interface(its address is 0x167) or transactions like below.
You seems using HederaTokenService.sol.
The second parameter only has meaning when you mint a fungible token.
And the second parameter only has meaning when you mint a non-funtible token.
You can create a fungible token and mint like this.
IHederaTokenService.TokenKey[]
memory keys = new IHederaTokenService.TokenKey[](1);
// Set this contract as supply
keys[0] = getSingleKey(
KeyType.SUPPLY,
KeyValueType.CONTRACT_ID,
address(this)
);
IHederaTokenService.HederaToken memory token;
token.name = _name;
token.symbol = _symbol;
token.treasury = address(this);
token.memo = _memo;
token.tokenSupplyType = true; // set supply to FINITE
token.maxSupply = _maxSupply;
token.freezeDefault = false;
token.tokenKeys = keys;
token.expiry = createAutoRenewExpiry(address(this), _autoRenewPeriod); // Contract automatically renew by himself
(int256 responseCode, address createdToken) = HederaTokenService
.createFungibleToken(token, 0, _decimals);
if (responseCode != HederaResponseCodes.SUCCESS) {
revert("Failed to create fungible token");
}
tokenAddress = createdToken;
(int256 response, uint64 newSupply, ) = HederaTokenService.mintToken(
tokenAddress,
uint64(amount),
new bytes[](0)
);
Or a Non-fungible token like this.
IHederaTokenService.TokenKey[]
memory keys = new IHederaTokenService.TokenKey[](2);
// Set this contract as supply
keys[0] = getSingleKey(
KeyType.SUPPLY,
KeyValueType.CONTRACT_ID,
address(this)
);
keys[1] = getSingleKey(
KeyType.PAUSE,
KeyValueType.CONTRACT_ID,
address(this)
);
IHederaTokenService.HederaToken memory token;
token.name = _name;
token.symbol = _symbol;
token.treasury = address(this);
token.memo = _memo;
token.tokenSupplyType = true; // set supply to FINITE
token.maxSupply = int64(int256(_maxSupply));
token.freezeDefault = false;
token.tokenKeys = keys;
token.expiry = createAutoRenewExpiry(address(this), _autoRenewPeriod); // Contract automatically renew by himself
(int256 responseCode, address createdToken) = HederaTokenService
.createNonFungibleToken(token);
if (responseCode != HederaResponseCodes.SUCCESS) {
revert("Failed to create non-fungible token");
}
tokenAddress = createdToken;
(int256 response, uint64 newSupply, ) = HederaTokenService.mintToken(
tokenAddress,
uint64(amount),
new bytes[](0)
);
(int256 response, , int64[] memory serialNumbers) = HederaTokenService
.mintToken(tokenAddress, 0, metadata);
Here is a code of using transactions.
Hope it will help you.
const {
Client,
AccountId,
AccountBalanceQuery,
ContractExecuteTransaction,
ContractFunctionParameters,
ContractCallQuery,
TokenCreateTransaction,
Hbar,
HbarUnit,
TransactionRecord,
TokenSupplyType,
TokenType,
PrivateKey,
AccountCreateTransaction,
TokenId,
TokenMintTransaction,
TokenAssociateTransaction,
TokenDissociateTransaction,
TransferTransaction,
} = require("#hashgraph/sdk");
// Allow access to ourenv file variables
require("dotenv").config();
const print = require("./utils.js").print;
// Grab your account ID and private key from the .env file
const operatorAccountId = AccountId.fromString(process.env.MY_ACCOUNT_ID);
const operatorPrivateKey = PrivateKey.fromString(process.env.MY_PRIVATE_KEY);
const operatorPublicKey = operatorPrivateKey.publicKey;
const treasuryId = AccountId.fromString(process.env.TREASURY_ID);
const treasuryKey = PrivateKey.fromString(process.env.TREASURY_PVKEY);
const aliceId = AccountId.fromString(process.env.ALICE_ID);
const aliceKey = PrivateKey.fromString(process.env.ALICE_PVKEY);
const supplyKey = PrivateKey.fromString(process.env.SUPPLY_PVKEY);
const tokenId = TokenId.fromString("0.0.*");
// If we weren't able to grab it, we should throw a new error
if (operatorPrivateKey == null || operatorAccountId == null) {
throw new Error(
"environment variables MY_ACCOUNT_ID and MY_PRIVATE_KEY must be present"
);
}
// Create our connection to the Hedera network
// The Hedera JS SDK makes this really easy!
const client = Client.forTestnet();
// Set your client account ID and private key used to pay for transaction fees and sign transactions
client.setOperator(operatorAccountId, operatorPrivateKey);
async function printBalance(accountAlias, accountId) {
var currentBalance = await new AccountBalanceQuery()
.setAccountId(accountId.toString())
.execute(client);
console.log(
`Balance of ${accountAlias} (accountId ${accountId}): ${currentBalance.toString()}`
);
}
async function queryContract() {
const tx = new ContractCallQuery()
.setGas(300000)
.setContractId(tokenId.toString())
.setFunction(
"tokenURI",
new ContractFunctionParameters().addUint256(12)
// .addString("Golden")
);
// .setQueryPayment(Hbar.fromTinybars(300000));
const contractFunctionResult = await tx.execute(client);
console.log();
const ret_0 = contractFunctionResult.getString(0);
console.log(`return value :>> ${ret_0.toString()}`);
}
async function generateKey() {
const privateKey = await PrivateKey.generateED25519Async();
console.log(
`New key generated.\nPublic Key is ${privateKey.publicKey}\nPrivateKey is ${privateKey}`
);
return privateKey;
}
async function createAccount() {
const privateKey = await generateKey();
const transaction = new AccountCreateTransaction()
.setKey(privateKey.publicKey)
.setInitialBalance(new Hbar(1000));
//Sign the transaction with the client operator private key and submit to a Hedera network
const txResponse = await transaction.execute(client);
//Request the receipt of the transaction
const receipt = await txResponse.getReceipt(client);
//Get the account ID
const newAccountId = receipt.accountId;
console.log("The new account ID is " + newAccountId);
}
async function writeContract() {
const tx = new ContractExecuteTransaction()
.setContractId(contractId)
.setGas(300000) // Increase if revert
.setPayableAmount(Hbar.from(1.00000001)) // Increase if revert
.setFunction("pay", new ContractFunctionParameters().addString("Donate"));
const txRes = await tx.execute(client);
print("txResponse", txRes);
const receipt = await txRes.getReceipt(client);
print("recepit", receipt);
const txRec = await txRes.getRecord(client);
print("txRecord", txRec);
const value = txRec.contractFunctionResult.getUint256(0);
console.log(`First return value is: ${value.toString()}`);
const ret_0 = txRec.contractFunctionResult.getString(1);
console.log(`second return value is: ${ret_0.toString()}`);
// console.log(`First return value is: ${AccountId.fromSolidityAddress(ret_0)}`);
}
async function createToken() {
const transaction = await new TokenCreateTransaction()
.setTokenName("My Token")
.setTokenSymbol("MYT")
.setTokenType(TokenType.NonFungibleUnique)
.setDecimals(0)
.setInitialSupply(0)
.setTreasuryAccountId(treasuryId)
.setSupplyType(TokenSupplyType.Finite)
.setMaxSupply(1000)
.setSupplyKey(supplyKey)
.freezeWith(client);
//Sign the transaction with the token adminKey and the token treasury account private key
const signTx = await transaction.sign(treasuryKey);
//Sign the transaction with the client operator private key and submit to a Hedera network
const txRes = await signTx.execute(client);
print("txResponse", txRes);
const receipt = await txRes.getReceipt(client);
print("recepit", receipt);
const txRec = await txRes.getRecord(client);
print("txRecord", txRec);
//Get the token ID
let tokenId = receipt.tokenId;
//Log the token ID
console.log(`- Created NFT with Token ID: ${tokenId} \n`);
}
async function mintToken() {
// Mint new NFT
let mintTx = await new TokenMintTransaction()
.setTokenId(tokenId)
.setMetadata([
Buffer.from(
"A METADATA LINK"
),
])
.freezeWith(client);
//Sign the transaction with the supply key
let mintTxSign = await mintTx.sign(supplyKey);
print("Singed Tx", mintTxSign);
//Submit the transaction to a Hedera network
let minttxRes = await mintTxSign.execute(client);
print("Tx Response", minttxRes);
//Get the transaction receipt
let mintRx = await minttxRes.getReceipt(client);
print("Tx Receipt", mintRx);
//Get the transaction record
let mintRec = await minttxRes.getRecord(client);
print("Tx Record", mintRec);
//Log the serial number
console.log(
`- Created NFT ${tokenId} with serial: ${mintRx.serials[0].low} \n`
);
}
async function associateToken(tokenIds) {
//Create the associate transaction and sign with Alice's key
let tx = await new TokenAssociateTransaction()
.setAccountId(aliceId)
.setTokenIds(tokenIds)
.freezeWith(client)
.sign(aliceKey);
//Submit the transaction to a Hedera network
let txRes = await tx.execute(client);
print("Tx Response", txRes);
//Get the transaction receipt
let txRx = await txRes.getReceipt(client);
print("Tx Receipt", txRx);
//Get the transaction record
let txRec = await txRes.getRecord(client);
print("Tx Record", txRec);
//Confirm the transaction was successful
console.log(`- NFT association with Alice's account: ${txRx.status}\n`);
}
async function dissociateToken(tokenIds) {
//Create the associate transaction and sign with Alice's key
let tx = await new TokenDissociateTransaction()
.setAccountId(operatorAccountId)
.setTokenIds(tokenIds)
.freezeWith(client)
.sign(aliceKey);
//Submit the transaction to a Hedera network
let txRes = await tx.execute(client);
print("Tx Response", txRes);
//Get the transaction receipt
let txRx = await txRes.getReceipt(client);
print("Tx Receipt", txRx);
//Get the transaction record
let txRec = await txRes.getRecord(client);
print("Tx Record", txRec);
//Confirm the transaction was successful
console.log(`- NFT association with Alice's account: ${txRx.status}\n`);
}
async function transferToken() {
// Check the balance before the transfer for the treasury account
var balanceCheckTx = await new AccountBalanceQuery()
.setAccountId(treasuryId)
.execute(client);
console.log(
`- Treasury balance: ${balanceCheckTx.tokens._map.get(
tokenId.toString()
)} NFTs of ID ${tokenId}`
);
// Check the balance before the transfer for Alice's account
var balanceCheckTx = await new AccountBalanceQuery()
.setAccountId(aliceId)
.execute(client);
console.log(
`- Alice's balance: ${balanceCheckTx.tokens._map.get(
tokenId.toString()
)} NFTs of ID ${tokenId}`
);
// Transfer the NFT from treasury to Alice
// Sign with the treasury key to authorize the transfer
let tokenTransferTx = await new TransferTransaction()
.addNftTransfer(tokenId, 2 /*SN*/, treasuryId, aliceId)
.freezeWith(client)
.sign(treasuryKey);
let tokenTransferSubmit = await tokenTransferTx.execute(client);
print("Tx Response", tokenTransferSubmit);
let tokenTransferRx = await tokenTransferSubmit.getReceipt(client);
print("Tx Receipt", tokenTransferRx);
let tokenTransferRec = await tokenTransferSubmit.getRecord(client);
print("Tx Record", tokenTransferRec);
console.log(
`\n- NFT transfer from Treasury to Alice: ${tokenTransferRx.status} \n`
);
// Check the balance of the treasury account after the transfer
var balanceCheckTx = await new AccountBalanceQuery()
.setAccountId(treasuryId)
.execute(client);
console.log(
`- Treasury balance: ${balanceCheckTx.tokens._map.get(
tokenId.toString()
)} NFTs of ID ${tokenId}`
);
// Check the balance of Alice's account after the transfer
var balanceCheckTx = await new AccountBalanceQuery()
.setAccountId(aliceId)
.execute(client);
console.log(
`- Alice's balance: ${balanceCheckTx.tokens._map.get(
tokenId.toString()
)} NFTs of ID ${tokenId}`
);
}
// Hedera is an asynchronous environment :)
(async function () {
await printBalance("Operator", operatorAccountId);
try {
// await printBalance("Treasury", treasuryId);
// await createAccount();
// await generateKey();
// await createToken();
// await mintToken();
// await associateToken();
// await dissociateToken();
// await transferToken();
// await queryContract();
// await writeContract();
// await printBalance("Treasury", treasuryId);
} catch (err) {
console.error(err);
}
await printBalance("Operator", operatorAccountId);
process.exit();
})();

How can I choose my own smart contract addresses on RSK before deploying it?

I have noticed that some smart contracts
have addresses that start with 0x0000, 0x1234, or0x1337 - and I'm not sure how the deployers managed to do this.
​
Is it possible to do this on RSK?
Building upon the previous answer,
using the CREATE2 opcode -
it is not only possible to predict the address
that a smart contract will be deployed at,
but also (partially) choose the address
that you would like it to be deployed at.
For example, if you want a vanity address that starts
with 4 leading zeroes, you use the following helper function,
which checks for a match of the required vanity address:
function hasLeadingZeros(address = '0x0000', zeros = 4) {
for (let i = 2; i <= zeros + 1; i += 1) {
if (address.charAt(i) !== '0') return false;
}
return true;
}
The next helper function uses trial and error,
since keccak256 as with other hashes are 1-way functions,
to determine which salt parameter will result
in obtaining a required vanity address.
function getSaltForVanityAddress(deployerAddress, initCodeHash) {
let salt = 0;
let vanityAddress = '';
let saltHash;
while (!hasLeadingZeros(vanityAddress)) {
salt += 1;
saltHash = ethers.utils.keccak256(salt);
vanityAddress = ethers.utils.getCreate2Address(
deployerAddress,
saltHash,
initCodeHash,
);
}
return saltHash;
}
By combine the above, you can use
factory.deployCreate2(salt);
to deploy a smart contract with the address needed.
Here is a test case to illustrate:
it('Should deploy Child to the address starting with four zeros', async () => {
const Child = await ethers.getContractFactory('Child');
const { data: initCode } =
Child.getDeployTransaction(deployer.address);
const initCodeHash = ethers.utils.keccak256(initCode);
const salt = getSaltForVanityAddress(factory.address, initCodeHash);
const deployTx = await factory.deployCreate2(salt);
const txReceipt = await deployTx.wait();
const deployEvent = txReceipt.events.find(
(event) => event.event === 'ContractDeployed',
);
expect(hasLeadingZeros(
deployEvent.args.childContract)
).to.be.true;
console.log(
`Found salt '${salt}' such that the\ndeployed s/c address is ${deployEvent.args.childContract}`,
);
});
Found salt '0xb20ba450430132a5f0191bd27fc7e8018b39435484af81bbdf289296ae61ea75' such that the
deployed s/c address is 0x0000b36619Df7e2816577549b78CBB8b189e8d6B
✔ Should deploy Child to the address starting with four zeros (4973ms)

Get all transactions for an NFT on Solana

I want to collect all transactions for an NFT.
For example, you can display all transactions here:
https://explorer.solana.com/address/2Nzt8TYeAfgJDftKzkb7rgYShVvyXTR7cPVvpqaZ2a4V
or here:
https://solscan.io/token/2Nzt8TYeAfgJDftKzkb7rgYShVvyXTR7cPVvpqaZ2a4V#txs
But is there any way to do this with the API?
I checked
solana-py: https://michaelhly.github.io/solana-py/
and solscan api: https://public-api.solscan.io/docs/
But I could not find a way to do it.
You can use the getSignaturesForAddress RPC method on the mint address and walk backward to get all the transactions.
Here is an example in JS:
import {
Connection,
clusterApiUrl,
ConfirmedSignatureInfo,
PublicKey,
} from "#solana/web3.js";
const connection = new Connection(clusterApiUrl("mainnet-beta"));
export const getTxs = async (connection: Connection, pubkey: PublicKey) => {
const txs: ConfirmedSignatureInfo[] = [];
// Walk backward
let lastTransactions = await connection.getConfirmedSignaturesForAddress2(
pubkey
);
let before = lastTransactions[lastTransactions.length - 1].signature;
txs.push(...lastTransactions);
while (true) {
const newTransactions = await connection.getConfirmedSignaturesForAddress2(
pubkey,
{
before,
}
);
if (newTransactions.length === 0) break;
txs.push(...newTransactions);
before = newTransactions[newTransactions.length - 1].signature;
}
return txs;
};
getTxs(
connection,
new PublicKey("2Nzt8TYeAfgJDftKzkb7rgYShVvyXTR7cPVvpqaZ2a4V")
);
The equivalent method in Solana.py is this one https://michaelhly.github.io/solana-py/rpc/api/#solana.rpc.api.Client.get_signatures_for_address

How to send a message when a trello card is moved to a certain list

I’m currently making a bot that informs me when a card is moved to the list titled Passed Applications.
I have already made the code and it basically sends a message once a card is moved to the specific list, however, it will randomly send a message and pull a card minutes/hours after it has already been pulled and sent the message.
What I’ve done so far is:
trello.js
var Trello = require("node-trello"),
EventEmitter = require("events").EventEmitter,
extend = require("extend"),
config,
trello,
timer,
e;
module.exports = function(options) {
var defaults = {
pollFrequency: 1000 * 60,
minId: 0,
trello: {
key: "",
token: "",
boards: []
},
start: true
};
e = new EventEmitter();
config = extend(true, defaults, options);
trello = new Trello(
process.env.TRELLO_API_KEY,
process.env.TRELLO_OAUTH_TOKEN
);
if (config.start) {
process.nextTick(function() {
start(config.pollFrequency, true);
});
}
function start(frequency, immediate) {
if (timer) {
return;
}
frequency = frequency || config.pollFrequency;
timer = setInterval(poll, frequency);
if (immediate) {
poll();
}
}
function poll() {
config.trello.boards.forEach(function(boardId) {
getBoardActivity(boardId);
});
}
function getBoardActivity(boardId) {
trello.get("/1/boards/" + boardId + "/actions", function(err, resp) {
if (err) {
return e.emit("trelloError", err);
}
var boardActions = resp.reverse();
var actionId;
for (var ix in boardActions) {
actionId = parseInt(boardActions[ix].id, 16);
if (actionId <= config.minId) {
continue;
}
var eventType = boardActions[ix].type;
e.emit(eventType, boardActions[ix], boardId);
}
config.minId = Math.max(config.minId, actionId);
e.emit("maxId", config.minId);
});
}
index.js
const conf = JSON.parse(fs.readFileSync("trelloconfig.json"));
let latestActivityID = fs.existsSync("./latestActivityID") ?
fs.readFileSync("./latestActivityID") :
0;
const eventEnabled = type =>
conf.enabledEvents.length > 0 ? conf.enabledEvents.includes(type) : true;
const TrelloEvents = require("./trello.js");
const events = new TrelloEvents({
pollFrequency: 60000,
minId: latestActivityID,
start: false,
trello: {
boards: conf.boardIDs,
key: process.env.TRELLO_API_KEY,
token: process.env.TRELLO_OAUTH_TOKEN
}
});
client.on("ready", () => {
events.start();
console.log(`[STATUS CHANGE] ${client.user.username} is now online.`);
client.user.setActivity("Cookout Grill");
});
events.on("updateCard", (event, board) => {
if (event.data.old.hasOwnProperty("idList")) {
if (!eventEnabled(`cardListChanged`)) return;
if (event.data.listAfter.name === "Passed Applications") {
let robloxId = event.data.card.name.split(" | ")[0];
client.channels.get("730839109236424756").send(robloxId);
if (database.find(x => x.RobloxUser === robloxId)) {
let data = database.find(x => x.RobloxUser === robloxId);
const person = client.users.get(data.DiscordID);
let embed = new discord.RichEmbed()
.setThumbnail(
"https://www.roblox.com/bust-thumbnail/image?userId=" +
data.RobloxID +
"&width=420&height=420&format=png"
)
.setTitle("APPLICATION RESULTS | Passed")
.setColor("3ef72d")
.setFooter("Cookout Grill", client.user.avatarURL)
.setDescription(
"Greetings, **" +
data.RobloxUser +
"**!\n\nAfter extensive review by our Management Team, we have decided to accept your Trainee Application at Cookout Grill. We believe that your application showed that you’re ready to become a staff member at our establishment.\n\nWe would like to congratulate you on passing your Trainee Application. Your application met our critical expectations and requirements in order to pass.\n\nIn order to work your way up throughout the staff ranks, you must attend a training at [Cookout Grill’s Training Center](https://www.roblox.com/groups/5634772/Cookout-Grill#!/about) during the specific session times. If you’re unable to attend one of our designated sessions, feel free to schedule a private session with a member of our Management Team.\n\nWe wish you the best of luck in continuing throughout the staff ranks at Cookout Grill. If you have any further questions, please do not hesitate to create a ticket in our main Discord Server."
)
.addField("**NEW RANK**", "`Trainee`");
person.send(embed);
roblox.message(
event.data.card.name.split(" | ")[1],
"APPLICATION RESULTS | Passed",
"Greetings, **" +
data.RobloxUser +
"**!\n\nAfter extensive review by our Management Team, we have decided to accept your Trainee Application at Cookout Grill.\n\nWe would like to congratulate you on passing your Trainee Application. Your application met our critical expectations and requirements in order to pass.\n\nIn order to work your way up throughout the staff ranks, you must attend a training at Cookout Grill’s Training Center during the specific session times. If you’re unable to attend one of our designated sessions, feel free to schedule a private session with a member of our Management Team.\n\nWe wish you the best of luck in continuing throughout the staff ranks at Cookout Grill."
);
}
let embed2 = new discord.RichEmbed()
.setTitle(`Card list changed!`)
.setDescription(
`**CARD:** ${
event.data.card.name
} — **[CARD LINK](https://trello.com/c/${
event.data.card.shortLink
})**\n\n**EVENT:** Card moved to list __${
event.data.listAfter.name
}__ from list __${event.data.listBefore.name}__ by **[${
conf.realNames
? event.memberCreator.fullName
: event.memberCreator.username
}](https://trello.com/${event.memberCreator.username})**`
);
client.channels.get("730839109236424756").send(embed2);
Trello.addCommentToCard(
event.data.card.id,
"User has been ranked.",
function(error, trelloCard) {
console.log(error);
}
);
} else return;
} else return;
});

Web3 | TransferFrom() transactions always Failed

I'm trying to send Custom ERC20 Token to Owner Wallet Address using transferFrom() function using Web3.js
However all the Transactions are failed. Which is the same problem occur on Remix IDE.
Some answers on stackOverflow here says that the approve() is needed to call first before the transferFrom() function. so, I tried on Remix frist but I got the same problem. And then tried with using Web3.js like below.
const myContract = new web3.eth.Contract(abi);
const amount = sendAmount;
let address = myAddress;
myContract.options.address = contractAddress;
myContract.options.from = TokenOwner;
let options = myContract.options;
let data1 = myContract.methods.approve(address, amount).encodeABI();
let data2 = myContract.methods.transferFrom(address, TokenOwner, amount).encodeABI();
const ethAccount = fromPrivateKey(toBuffer("0x..."));
const fromPrivateKeyBuffer = ethAccount.getPrivateKey();
web3.eth.getTransactionCount(TokenOwner, (err, count) => {
if (err) return;
const txData = {
chainId: 0x03,
gasPrice: web3.utils.toHex(42000000000),
gasLimit: web3.utils.toHex(90000),
to: contractAddress,
from: TokenOwner,
value: 0x0,
nonce: web3.utils.toHex(count),
data: data1
};
const tx = new ethTx(txData);
tx.sign(fromPrivateKeyBuffer);
const serializedTx = tx.serialize().toString("hex");
if (!serializedTx) {
return;
} else {
web3.eth.sendSignedTransaction(`0x${serializedTx}`, (err, MuiTXHash) => {
console.log("err : ", err, "Data1-MuiTXHash : ", MuiTXHash);
// START DATA2
web3.eth.getTransactionCount(TokenOwner, (err, count) => {
if (err) return;
const txData2 = {
chainId: 0x03,
gasPrice: web3.utils.toHex(42000000000),
gasLimit: web3.utils.toHex(90000),
to: contarctAddress,
from: TokenOwner,
value: 0x0,
nonce: web3.utils.toHex(count + 1),
data: data2
};
const tx2 = new ethTx(txData2);
tx2.sign(fromPrivateKeyBuffer);
const serializedTx2 = tx2.serialize().toString("hex");
if (!serializedTx2) {
return;
} else {
web3.eth.sendSignedTransaction(`0x${serializedTx2}`, (err, MuiTXHash2) => {
console.log("err : ", err, "Data2-MuiTXHash : ", MuiTXHash2);
});
}
});
// END DATA2
});
}
});
};
I got the two Transaction Hash return data and the TransferFrom() transaction is failed again.
What is the problem?
How can I make it success? I have to withdraw the custom ERC20 token from specific address to owner. so I have to use transferFrom() transaction.
Please let me know how to do. Thanks.
I guess the compiling on Remix had some errors so I created new solidity file with same code on it and deployed it. The new smart contract worked well and no errors occurred. The errors was not about my code. If someone's suffering like this problems, then create a new contract address and I don't recommend using 'At Address' on Remix. This is not work properly at all.
I was tried appove() + transferFrom () , allowance() on Solidity working well but on web3 not working error same as you
I guess the transferFrom function should be called by the spender (i.e the address to which the TokenOwner has approved to spend). So,
const txData2 = {
chainId: 0x03,
gasPrice: web3.utils.toHex(42000000000),
gasLimit: web3.utils.toHex(90000),
to: contarctAddress,
from: address, // spender's address
value: 0x0,
nonce: web3.utils.toHex(count + 1),
data: data2
};
And don't forget to use spender's privateKey to sign the txn and spender's address in getTransactionCount
Read Here : Ethereum Wiki
Hope it helps. :)