0

我正在使用 solana web3 创建自定义 SPL 令牌,将一些供应放入“主”令牌帐户,然后随着时间的推移从该令牌帐户转移到其他令牌帐户。

我认为通过将所有供应量铸造到一个主令牌帐户中,这样可以更容易地在那个时间点限制供应量。(..如果我不预先铸造整个供应,而是根据需要铸造点滴和单调,那么我想限制供应将成为一项更棘手的任务)

但是我如何停止供应用 web3 呢?

const splToken = require("@solana/spl-token");
const web3 = require("@solana/web3.js");

const test = async () => {
  console.log("solana test");

  const cluster_url = "https://api.testnet.solana.com";

  //create keys
  const mint_authority = web3.Keypair.generate();
  const freeze_authority = web3.Keypair.generate();

  const connection = new web3.Connection(cluster_url, "confirmed");

  //airdrop to mint authority
  const airdropSignature = await connection.requestAirdrop(
    mint_authority.publicKey,
    web3.LAMPORTS_PER_SOL
  );

  await connection.confirmTransaction(airdropSignature);

  console.log("mint authority pub key", mint_authority.publicKey.toString());

  //check balance of mint authority
  console.log(
    "SOL balance of minter",
    await connection.getBalance(mint_authority.publicKey)
  );

  //create (but not mint) a new token
  const token = await splToken.Token.createMint(
    connection,
    mint_authority,
    mint_authority.publicKey,
    null,
    9,
    splToken.TOKEN_PROGRAM_ID
  );

  console.log(
    "new SOL balance of minter after token creation activity",
    await connection.getBalance(mint_authority.publicKey)
  );

  console.log("new spl token address is", token.publicKey.toString());

  //lets mint some of the token INTO the mint authority wallet
  const minter_token_account = await token.getOrCreateAssociatedAccountInfo(
    mint_authority.publicKey
  );
  console.log(
    "minter_token_account address",
    minter_token_account.address.toString()
  );
  console.log("minting some supply into token account we made for the minter");
  console.log(
    "minter_token_account.mint (same as token address)",
    minter_token_account.mint.toString()
  );
  console.log(
    "minter_token_account.owner (same as pub key of token account)",
    minter_token_account.owner.toString()
  );

  // Mint the tokens - how am I allowed to do this just my using mint_authority.publicKey as the authority, shouldnt I need
  // a private key to do this or is it just because I have a 'powerful' reference to the token because of earlier createMint() ????
  await token.mintTo(
    minter_token_account.address,
    mint_authority.publicKey,
    [],
    10000000
  );

  //get balance of sol for minter
  console.log(
    "new SOL balance of minter after token minting activity",
    await connection.getBalance(mint_authority.publicKey)
  );

  //token accounts by owner
  const tokenAccountsByOwner = await connection.getTokenAccountsByOwner(
    mint_authority.publicKey,
    {
      mint: token.publicKey,
    }
  );

  console.log(
    "tokenAccountsByOwner - token account address",
    tokenAccountsByOwner.value[0].pubkey.toString()
  );
  //   console.log(
  //     "tokenAccountsByOwner - token account lamports",
  //     tokenAccountsByOwner.value[0].account.lamports
  //   );

  //get token account balance
  const tokenAccountBalance = await connection.getTokenAccountBalance(
    minter_token_account.address
  );
  console.log(
    "token account balance:",
    tokenAccountBalance.value.uiAmountString
  );

  //now lets create a new solana address
  const bob = web3.Keypair.generate();

  console.log("bob public address:", bob.publicKey.toString());

  console.log(
    "SOL balance of minter just before creating account for bob",
    await connection.getBalance(mint_authority.publicKey)
  );

  //create token account fot this address
  const bob_token_account = await token.getOrCreateAssociatedAccountInfo(
    bob.publicKey
  );

  console.log(
    "SOL balance of minter just after creating account for bob",
    await connection.getBalance(mint_authority.publicKey)
  ); //seems to cost 2044280 lamports .002 SOL

  console.log(
    "bob_token_account address",
    bob_token_account.address.toString()
  );

  console.log(
    "bob_token_account.mint (same as token address)",
    bob_token_account.mint.toString()
  );
  console.log(
    "bob_token_account.owner (same as pub key of token account)",
    bob_token_account.owner.toString()
  );

  //transfer from minter wallet to bob
  var transferTrx = new web3.Transaction().add(
    splToken.Token.createTransferInstruction(
      splToken.TOKEN_PROGRAM_ID,
      minter_token_account.address,
      bob_token_account.address,
      mint_authority.publicKey,
      [],
      1
    )
  );

  let bobTokenAccountBalance = await connection.getTokenAccountBalance(
    bob_token_account.address
  );
  console.log(
    "bob_token_account balance before transfer:",
    bobTokenAccountBalance.value.uiAmountString
  );

  console.log(
    "SOL balance of minter just before transfering to bob",
    await connection.getBalance(mint_authority.publicKey)
  );

  var signature = await web3.sendAndConfirmTransaction(
    connection,
    transferTrx,
    [mint_authority]
  );

  console.log("transfer signature",signature)

  console.log(
    "SOL balance of minter just AFTER transfering to bob",
    await connection.getBalance(mint_authority.publicKey)
  ); // seems to cost 5000 lamports or .000005 SOL

  bobTokenAccountBalance = await connection.getTokenAccountBalance(
    bob_token_account.address
  );
  console.log(
    "bob_token_account balance after transfer:",
    bobTokenAccountBalance.value.uiAmountString
  );
};

return test()
  .then()
  .catch((err) => {
    if (err.logs) {
      console.log(err.logs);
    } else {
      console.error(err.message);
    }
  });

4

0 回答 0