47
loading...
This website collects cookies to deliver better user experience
testnet
environment (faucet/airdrop) seems to be having issues. Please select the devnet
on the selector (or just don't change it, since is the default value). Remember to change your wallet to point to the devnet
network.mint
more of the existing tokens to a user and in the second case we will transfer
tokens.Pubkey::create_program_address
generates these addresses. This method will hash the seeds with program ID to create a new 32-byte address. There's a change (50%) that this may be a point on the ed25519 curve. That means there is a private key associated with this address. In such cases, the safety of the Solana programming model would be compromised. The Pubkey::create_program_address
will fail in case the generated address lie on the ed25519 curve.Pubkey::find_program_address
will internally call the create_program_address
as many times as necessary until it finds a valid one for us.const offer = anchor.web3.Keypair.generate();
const [escrowedTokensOfOfferMaker, escrowedTokensOfOfferMakerBump] = await anchor.web3.PublicKey.findProgramAddress(
[offer.publicKey.toBuffer()],
program.programId
)
findProgrammAddress
method and having to pass it down from the frontend.contract / program
this is how we use it (here you'll find the entire file). Here, we're creating an offer:anchor_spl::token::transfer(
CpiContext::new(
ctx.accounts.token_program.to_account_info(),
anchor_spl::token::Transfer {
from: ctx
.accounts
.token_account_from_who_made_the_offer
.to_account_info(),
to: ctx
.accounts
.escrowed_tokens_of_offer_maker
.to_account_info(),
authority: ctx.accounts.who_made_the_offer.to_account_info(),
},
),
im_offering_this_much,
)
// Transfer what's on the escrowed account to the offer reciever.
anchor_spl::token::transfer(
CpiContext::new_with_signer(
ctx.accounts.token_program.to_account_info(),
anchor_spl::token::Transfer {
from: ctx
.accounts
.escrowed_tokens_of_offer_maker
.to_account_info(),
to: ctx
.accounts
.where_the_escrowed_account_was_funded_from
.to_account_info(),
authority: ctx
.accounts
.escrowed_tokens_of_offer_maker
.to_account_info(),
},
&[&[
ctx.accounts.offer.key().as_ref(),
&[ctx.accounts.offer.escrowed_tokens_of_offer_maker_bump],
]],
),
ctx.accounts.escrowed_tokens_of_offer_maker.amount,
)?;
// Close the escrow account
anchor_spl::token::close_account(CpiContext::new_with_signer(
ctx.accounts.token_program.to_account_info(),
anchor_spl::token::CloseAccount {
account: ctx
.accounts
.escrowed_tokens_of_offer_maker
.to_account_info(),
destination: ctx.accounts.who_made_the_offer.to_account_info(),
authority: ctx
.accounts
.escrowed_tokens_of_offer_maker
.to_account_info(),
},
&[&[
ctx.accounts.offer.key().as_ref(),
&[ctx.accounts.offer.escrowed_tokens_of_offer_maker_bump],
]],
))
// Transfer token to who started the offer
anchor_spl::token::transfer(
CpiContext::new(
ctx.accounts.token_program.to_account_info(),
anchor_spl::token::Transfer {
from: ctx
.accounts
.account_holding_what_receiver_will_give
.to_account_info(),
to: ctx
.accounts
.account_holding_what_maker_will_get
.to_account_info(),
authority: ctx.accounts.who_is_taking_the_offer.to_account_info(),
},
),
ctx.accounts.offer.amount_received_if_offer_accepted,
)?;
// Transfer what's on the escrowed account to the offer reciever.
anchor_spl::token::transfer(
CpiContext::new_with_signer(
ctx.accounts.token_program.to_account_info(),
anchor_spl::token::Transfer {
from: ctx
.accounts
.escrowed_tokens_of_offer_maker
.to_account_info(),
to: ctx
.accounts
.account_holding_what_receiver_will_get
.to_account_info(),
authority: ctx
.accounts
.escrowed_tokens_of_offer_maker
.to_account_info(),
},
&[&[
ctx.accounts.offer.key().as_ref(),
&[ctx.accounts.offer.escrowed_tokens_of_offer_maker_bump],
]],
),
ctx.accounts.escrowed_tokens_of_offer_maker.amount,
)?;
// Close the escrow account
anchor_spl::token::close_account(CpiContext::new_with_signer(
ctx.accounts.token_program.to_account_info(),
anchor_spl::token::CloseAccount {
account: ctx
.accounts
.escrowed_tokens_of_offer_maker
.to_account_info(),
destination: ctx.accounts.who_made_the_offer.to_account_info(),
authority: ctx
.accounts
.escrowed_tokens_of_offer_maker
.to_account_info(),
},
&[&[
ctx.accounts.offer.key().as_ref(),
&[ctx.accounts.offer.escrowed_tokens_of_offer_maker_bump],
]],
))
findProgramAddress
to get a PDA:const cowSeed = Buffer.from(anchor.utils.bytes.utf8.encode("cow-mint-faucet"));
const pigSeed = Buffer.from(anchor.utils.bytes.utf8.encode("pig-mint-faucet"));
const [cowMintPda, cowMintPdaBump] = await anchor.web3.PublicKey.findProgramAddress(
[cowSeed],
program.programId);
const [pigMintPda, pigMintPdaBump] = await anchor.web3.PublicKey.findProgramAddress(
[pigSeed],
program.programId);
anchor_spl::token::mint_to(
CpiContext::new_with_signer(
ctx.accounts.token_program.to_account_info(),
anchor_spl::token::MintTo {
mint: ctx.accounts.mint.to_account_info(),
to: ctx.accounts.destination.to_account_info(),
authority: ctx.accounts.mint.to_account_info(),
},
&[&[&mint_seed, &[mint_bump]]],
),
amount,
)?;
devnet
and testnet
have the app deployed. You can use the selector on the page to change between the two (if you do, remember to change what network you're pointing in your walled).