Transaction with "SystemProgram.createAccount" results in "Error: Signature verification failed at Transaction.serialize" - solana

What I'm tryding to achieve is a webpage on which the user connects its phantom wallet and then by a simple press of a button creates a token.
In order to achieve that, I created a simple transaction for now which creates an account for the future token (based on https://www.programcreek.com/typescript/?api=#solana/spl-token.createInitializeMintInstruction)
this is the function which is called when the user press the button:
const onClickCreate = useCallback( async () => {
const lamports = await getMinimumBalanceForRentExemptMint(connection)
const mint = Keypair.generate()
console.log(mint.publicKey.toBase58())
const transaction = new Transaction({feePayer: publicKey})
transaction.add(
SystemProgram.createAccount({
fromPubkey: publicKey,
newAccountPubkey: mint.publicKey,
space: MINT_SIZE,
lamports,
programId: TOKEN_PROGRAM_ID
}),
)
const signature = await sendTransaction(transaction, connection);
await connection.confirmTransaction(signature, 'processed');
}, [publicKey, signTransaction, sendTransaction, connection]);
Executing the code results in the error saying that the signature failed.
I use wallet-adapter to connect the user's wallet (which gives me access to the variable publicKey)
If I replace the createAccount function by SystemProgram.transfer everything works ok :
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: publicKey,
toPubkey: Keypair.generate().publicKey,
lamports: lamportsI,
})
);
Thank you for your time

Ok so I checked a little bit more and found someone on the solana forums in Feb 2022 who had kind of the same problem I have : https://forums.solana.com/t/signature-error-when-create-a-new-account/5348/2
the answer by #zicklag worked out for me.
for short, you simply have to make the mint keypair sign the transaction (which seems logic after you think about it)
final code (inside the function called by pressing the button):
if (!publicKey) throw new WalletNotConnectedError();
const lamports = await getMinimumBalanceForRentExemptMint(connection)
const mint = Keypair.generate()
const transaction = new Transaction({ feePayer: publicKey, recentBlockhash: (await connection.getLatestBlockhash()).blockhash });
transaction.add(
SystemProgram.createAccount({
fromPubkey: publicKey,
newAccountPubkey: mint.publicKey,
space: MINT_SIZE,
lamports,
programId: TOKEN_PROGRAM_ID
})
)
transaction.sign(mint)
const txID = await sendTransaction(transaction, connection)

Related

How to make an auction with the new metaplex js sdk?

I am working on building a solana marketplace and trying to update from the old solana marketplace to auction house.
code:
const authority = metaplex.identity();
const auctionHouse = await metaplex
.auctions()
.createAuctionHouse({
sellerFeeBasisPoints: 200,
requiresSignOff: false,
treasuryMint: WRAPPED_SOL_MINT,
authority: authority.publicKey,
canChangeSalePrice: true,
feeWithdrawalDestination: publicKey,
treasuryWithdrawalDestinationOwner: publicKey,
})
.run();
const listNft = async () => {
try {
const NFTs = await metaplex.nfts().findAllByOwner(publicKey).run();
console.log(NFTs[4].mintAddress.toString(), " nft");
const auctionHouse = await metaplex
.auctions()
.findAuctionHouseByCreatorAndMint(publicKey, WRAPPED_SOL_MINT)
.run();
const listtx = metaplex
.auctions()
.builders()
.createListing({
mintAccount: NFTs[4].mintAddress,
price: lamports(1 * LAMPORTS_PER_SOL),
auctionHouse: auctionHouse,
});
var rpcClient = await metaplex.rpc().sendTransaction(listtx);
console.log(rpcClient, "listed nft");
} catch (error) {
console.log(error);
}
};
const getListing = async () => {
const NFTs = await metaplex.nfts().findAllByOwner(publicKey).run();
const retrievedAuctionHouse = await metaplex
.auctions()
.findAuctionHouseByCreatorAndMint(publicKey, WRAPPED_SOL_MINT)
.run();
console.log(retrievedAuctionHouse?.address?.toString(), "auction pubkey");
const retrieveListing = await metaplex
.auctions()
.for(retrievedAuctionHouse)
.findListingByAddress(NFTs[4].mintAddress)
.run();
console.log(retrieveListing, "retrieving listings");
};
const bidNft = async () => {
const NFTs = await metaplex.nfts().findAllByOwner(publicKey).run();
const auctionHouse = await metaplex
.auctions()
.findAuctionHouseByCreatorAndMint(publicKey, WRAPPED_SOL_MINT)
.run();
const { bid, buyerTradeState } = await metaplex
.auctions()
.for(auctionHouse)
.bid({
mintAccount: NFTs[5].mintAddress,
price: lamports(0.5 * LAMPORTS_PER_SOL),
});
console.log(bid, buyerTradeState, "bidding");
};
errors:
when trying to list nft:
MetaplexError: AuctionHouseProgram > Account is already initialized"
Source: Program > AuctionHouseProgram [hausS13jsjafwWwGqZTUQRmWyvyxn9EQpqMwV1PBBmk]
Problem: The program [AuctionHouseProgram] at address [hausS13jsjafwWwGqZTUQRmWyvyxn9EQpqMwV1PBBmk] raised an error of code [1] that translates to "Account is already initialized"".
Solution: Check the error message provided by the program.
Caused By: TokenLendingError#AlreadyInitialized: Account is already initialized"
at RpcClient.parseProgramError (RpcClient.mjs?e98e:206:1)
at RpcClient.sendTransaction (RpcClient.mjs?e98e:48:1)
When trying to fetch listings:
Account.mjs?9f45:47 Uncaught (in promise) MetaplexError: Account Not Found
Source: SDK
Problem: The account of type [ListingReceipt] was not found at the provided address [3m517hu6UuV5HjdLmb2GxZkttKTtQ8VseHRCPuUTDJmz].
Solution: Ensure the provided address is correct and that an account exists at this address.
at assertAccountExists (Account.mjs?9f45:47:1)
at eval (Account.mjs?9f45:39:1)
at Object.handle (findListingByAddress.mjs?1810:23:44)
at async eval (Task.mjs?1340:58:1)
at async Disposable.run (Disposable.mjs?b308:22:1)
Found a way around
Instead of using metaplex.auction().builder().createListing i used metaplex.auctions().for(auctionHouse).list().
Then funded the auction fee payer account which is a pda.
Finally in findListingByAddress i passed the sellerTradeState
const { listing, sellerTradeState } = await metaplex
.auctions()
.for(auctionHouse)
.list({
mintAccount: NFTs[1].mintAddress,
price: sol(1.5),
})
.run();
For fetching the listed nft
const retrieveListing = await metaplex
.auctions()
.for(retrievedAuctionHouse)
.findListingByAddress(
sellerTradeState
)
.run();
Just from the information provided above, there are two issues:
You might have run the createAuctionHouse instruction already once which created the Auction house and now that you are running the listNft function which also runs createAuctionHouse instruction, it throws an Account already exists error. You can see why this happens here: https://stackoverflow.com/a/72808405/19264629
You are using the NFTs mint address in findListingByAddress instruction, which is wrong. You have to create a ListingReceipt as stated here: https://docs.metaplex.com/guides/auction-house/#auction-house-receipts while creating the listing itself (this needs to be done in one single transaction). Once this is done, you need to plugin the receipt address in the findListingByAddress to fetch the listings.
Got it to work. The trick was to fund the auction house "Fee Account" as explained above and in Auction House CLI: I cannot execute the sale instruction to list my NFT
const ah = await mx
.auctionHouse()
.findByAddress({ address:ahKey })
.run();
const nft = await mx
.nfts()
.create({
name:"TESTNFT",
symbol: "TESTNFT",
uri: "https://blah.com",
sellerFeeBasisPoints : 100,
creators: [
{
address: wallet.publicKey,
share: 100,
}]
})
.run();
const price = sol(0.1);
const { listing, sellerTradeState } = await mx
.auctionHouse()
.list({
auctionHouse: ah,
mintAccount: nft.mintAddress,
price
})
.run()

How to create an account on NEAR protocol?

I would like to learn how to create an account using RPC or REST calls on NEAR protocol.
If you want to create a subaccount (a.frol.near when you own frol.near): Submit a transaction with CREATE_ACCOUNT, TRANSFER, ADD_KEY actions. Here is an example of such a transaction.
If you want to create *.near account, you need to submit a transaction with create_account function call on near contract. Here is an example of such a transaction, and here is a code snippet from the tutorial in the docs using near-api-js JS library:
const HELP = `Please run this script in the following format:
node create-testnet-account.js CREATOR_ACCOUNT.testnet NEW_ACCOUNT.testnet AMOUNT
`;
const { connect, KeyPair, keyStores, utils } = require("near-api-js");
const path = require("path");
const homedir = require("os").homedir();
const CREDENTIALS_DIR = ".near-credentials";
const credentialsPath = path.join(homedir, CREDENTIALS_DIR);
const keyStore = new keyStores.UnencryptedFileSystemKeyStore(credentialsPath);
const config = {
keyStore,
networkId: "testnet",
nodeUrl: "https://rpc.testnet.near.org",
};
if (process.argv.length !== 5) {
console.info(HELP);
process.exit(1);
}
createAccount(process.argv[2], process.argv[3], process.argv[4]);
async function createAccount(creatorAccountId, newAccountId, amount) {
const near = await connect({ ...config, keyStore });
const creatorAccount = await near.account(creatorAccountId);
const keyPair = KeyPair.fromRandom("ed25519");
const publicKey = keyPair.publicKey.toString();
await keyStore.setKey(config.networkId, newAccountId, keyPair);
return await creatorAccount.functionCall({
contractId: "testnet",
methodName: "create_account",
args: {
new_account_id: newAccountId,
new_public_key: publicKey,
},
gas: "300000000000000",
attachedDeposit: utils.format.parseNearAmount(amount),
});
}
If you don't need a named account, you can just generate a new ed25519 key-pair, and the hex representation of the public key will be your account id (it won't be recorded on chain until you/someone transfers some NEAR tokens to it, and so it is called "implicit" account). Example for such an account.
Here is a detailed tutorial on how to construct a transaction. Ultimately, you will submit your transaction via JSON RPC broadcast_tx* endpoints.

Receiving "Phantom - RPC Error: Transaction creation failed" while building solana escrow with #solana/web3.js

I am trying to replicate https://github.com/dboures/solana-random-number-betting-game
Although when I try to initiate my the Escrow I receive the following error:
Phantom - RPC Error: Transaction creation failed.
Uncaught (in promise) {code: -32003, message: 'Transaction creation failed.'}
I am using Phantom Wallet with Solana RPC.
const transaction = new Transaction({ feePayer: initializerKey })
let recentBlockHash = await connection.getLatestBlockhash();
transaction.recentBlockhash = await recentBlockHash.blockhash;
const tempTokenAccount = Keypair.generate();
// Create Temp Token X Account
transaction.add(
SystemProgram.createAccount({
programId: TOKEN_PROGRAM_ID,
fromPubkey: initializerKey,
newAccountPubkey: tempTokenAccount.publicKey,
space: AccountLayout.span,
lamports: await connection.getMinimumBalanceForRentExemption(AccountLayout.span )
})
);
const { signature } = await wallet.signAndSendTransaction(transaction);
let txid = await connection.confirmTransaction(signature);
console.log(txid);
You're trying to create an account without signing with the keypair of that account to prove ownership.
You have to add the keypair as a signer like such:
await wallet.signAndSendTransaction(transaction, {signers: [tempTokenAccount]})
I was able to solve my problem by using the following code:
const signed = await wallet.request({
method: "signTransaction",
params: {
message: bs58.encode(transaction.serializeMessage())
}
});
const signature = bs58.decode(signed.signature)
transaction.addSignature(initializerKey, signature);
transaction.partialSign(...[tempTokenAccount]);
await connection.sendRawTransaction(transaction.serialize())
instead of:
await wallet.signAndSendTransaction(transaction, {signers: [tempTokenAccount]})
Basically at first I was using one simple function to perform all the above steps, however, for some reason it was not working and throwing the subjected error. When I used this breakdown code it worked!. The cause of the error is still mysterious to me.
Thank you.

Getting address of a token account given a Solana wallet address

I've been trying the code below to get the spl-token account address for a specific token in a Solana wallet from the Solana wallet address, but I am having issues getting the result I am looking for. I run:
const web3 = require('#solana/web3.js');
(async () => {
const solana = new web3.Connection("https://api.mainnet-beta.solana.com");
//the public solana address
const accountPublicKey = new web3.PublicKey(
"2B1Uy1UTnsaN1rBNJLrvk8rzTf5V187wkhouWJSApvGT"
);
//mintAccount = the token mint address
const mintAccount = new web3.PublicKey(
"GLmaRDRmYd4u3YLfnj9eq1mrwxa1YfSweZYYZXZLTRdK"
);
console.log(
await solana.getTokenAccountsByOwner(accountPublicKey, {
mint: mintAccount,
})
);
})();
I'm looking for the token account address in the return, 6kRT2kAVsBThd5cz6gaQtomaBwLxSp672RoRPGizikH4. I get:
{ context: { slot: 116402202 }, value: [ { account: [Object],
pubkey: [PublicKey] } ] }
I can drill down through this a bit using .value[0].pubkey or .value[0].account but ultimately can't get to the information i'm looking for, which is a return of 6kRT2kAVsBThd5cz6gaQtomaBwLxSp672RoRPGizikH4
Does anyone know what is going wrong?
(Note I do not want to use the getOrCreateAssociatedAccountInfo() method, i'm trying to get the token account address without handling the wallets keypair)
ISSUE SOLVED:
I needed to grab the correct _BN data and convert, solution below.
const web3 = require('#solana/web3.js');
(async () => {
const solana = new web3.Connection("https://api.mainnet-beta.solana.com");
//the public solana address
const accountPublicKey = new web3.PublicKey(
"2B1Uy1UTnsaN1rBNJLrvk8rzTf5V187wkhouWJSApvGT"
);
//mintAccount = the token mint address
const mintAccount = new web3.PublicKey(
"GLmaRDRmYd4u3YLfnj9eq1mrwxa1YfSweZYYZXZLTRdK"
);
const account = await solana.getTokenAccountsByOwner(accountPublicKey, {
mint: mintAccount});
console.log(account.value[0].pubkey.toString());
})();
That will certainly work as a heavy-handed approach. As an alternative, you can try to just search for the associated token account owned by that wallet without creating it if it doesn't exist.
You would essentially follow the logic at https://github.com/solana-labs/solana-program-library/blob/0a61bc4ea30f818d4c86f4fe1863100ed261c64d/token/js/client/token.js#L539
except without the whole catch block.

How can i solve a type error causing heroku app to crash?

I am getting a weird error from heroku stating: TypeError: OAuth2Strategy requires a clientID option.
I solved one error with this regarding a script task. I have tried removing files and rebuilding the entire apllication
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const mongoose = require('mongoose');
const keys = require('../config/keys');
const googleClientID = keys.googleClientID
const googleClientSecret = keys.googleClientSecret
const User = mongoose.model('users');
//Takes user model and passes it into a cookie
//Use Mongo autogenerated ID
passport.serializeUser((user, done)=>{
done(null, user.id)
});
passport.deserializeUser((id, done)=>{
User.findById(id)
.then((user)=>{
done(null, user);
})
});
// Creates a new instance of google strategy
// Tells Google how to authenticate Google strategies
passport.use(new GoogleStrategy({
clientID: googleClientID,
clientSecret: googleClientSecret,
callbackURL: '/auth/google/callback',
proxy: true
},
//Whenever we reach out to our database it is an asynchronous
async (accessToken, refreshToken, profile, done ) =>{
const existingUser = await User.findOne({googleID: profile.id})
if(existingUser){
console.log(profile.emails[0].value)
done(null, existingUser)
}
const user = await new User({googleID: profile.id, email: profile.emails[0].value, name: profile.displayName}).save()
done(null, user);
}));

Resources