Как отключить добавление дополнительного токена SPL с помощью web3?
Я использую 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);
}
});