Is it possible to burn specific amount of solana token occasionally on some specific web hook event? For example: On a blog site when a new post is published on specific tag, then n
amount of token will be burned. Also there should a max amount limit of token that can burn on a single day? How do I achieve this?
1 Answer
To burn tokens you can for use the burn function in js. Like this for example to burn 1 token every second:
Setup:
import { getKeypairFromFile } from "@solana-developers/helpers";
import { TOKEN_2022_PROGRAM_ID, burn, createAccount, createMint, mintTo } from "@solana/spl-token";
import { Connection, Keypair } from "@solana/web3.js";
let connection = new Connection("http://127.0.0.1:8899", "confirmed");
const keyPair = await getKeypairFromFile();
const mintkeyPair = Keypair.generate();
console.log(keyPair.publicKey.toString());
console.log(mintkeyPair.publicKey.toString());
let confirmOptions = {
preflightCommitment: "confirmed",
commitment: "confirmed",
};
const mint = await createMint(
connection,
keyPair,
keyPair.publicKey,
keyPair.publicKey,
9,
mintkeyPair,
confirmOptions,
TOKEN_2022_PROGRAM_ID,
);
console.log("mint", mint);
const tokenAccount = await createAccount(
connection,
keyPair,
mintkeyPair.publicKey,
keyPair.publicKey,
undefined,
confirmOptions,
TOKEN_2022_PROGRAM_ID);
const sig = await mintTo(
connection,
keyPair,
mintkeyPair.publicKey,
tokenAccount,
keyPair.publicKey,
5000000000,
undefined,
undefined,
TOKEN_2022_PROGRAM_ID
);
console.log("sig", sig);
Burn one token every second:
var x = 5;
var interval = 1000;
for (var i = 0; i < x; i++) {
setTimeout(async function () {
const burnSig = await burn(
connection,
keyPair,
tokenAccount,
mintkeyPair.publicKey,
keyPair.publicKey,
1000000000,
undefined,
undefined,
TOKEN_2022_PROGRAM_ID
);
console.log("burnSig", burnSig);
}, i * interval)
}
You can then run this in vercel for example or trigger it via a cron job.
If you want to burn it every time another account changes you can use onAccountChange web socket connection for example like so:
connection.onAccountChange(
accountThatYouWantToListenTo,
(updatedAccountInfo, context) =>
yourBurnHere(),
"confirmed"
);
If you have your token authority in your program you can also use on chain automation using clock work: https://github.com/open-clockwork/clockwork
There are also RPC providers that offer webhook solutions. Helius for example: https://docs.helius.dev/webhooks-and-websockets/what-are-webhooks