import { Address, beginCell, internal, toNano } from "@ton/core";
import { TonClient, WalletContractV5R1, SendMode } from "@ton/ton";
import { mnemonicToPrivateKey } from "@ton/crypto";
const collectionAddress = Address.parse("<COLLECTION_ADDRESS>");
const recipientAddress = Address.parse("<RECIPIENT_ADDRESS>");
const itemContent = "<ITEM_CONTENT>";
async function main() {
// Toncenter endpoint (Testnet)
const client = new TonClient({
endpoint: "https://testnet.toncenter.com/api/v2/jsonRPC",
});
// Obtain the next item index
const nextItemIndex = (
await client.runMethod(
collectionAddress,
"get_collection_data",
)
).stack.readBigNumber();
// Read the next item index. See the explanation after the code.
// individual content
const content = beginCell()
.storeStringTail(itemContent)
.endCell();
const body = beginCell()
// deploy opcode
.storeUint(1, 32)
// query id
.storeUint(0, 64)
.storeUint(nextItemIndex, 64)
// Forwarded to the new item as its initial balance.
// Ensure `value` >= this amount + all fees.
.storeCoins(toNano("0.005"))
.storeRef(
beginCell()
.storeAddress(recipientAddress)
.storeRef(content)
.endCell(),
)
.endCell();
// Compose deploy message
const msg = internal({
to: collectionAddress,
// Total attached to the collection. Must cover
// the forwarded amount below (0.005) plus
// execution/storage fees;
// keep a safety margin (e.g., 0.01-0.02 TON)
value: toNano("0.01"),
bounce: true,
body,
});
// Initialize wallet
const mnemonic = process.env.MNEMONIC;
if (!mnemonic) {
throw new Error("Set MNEMONIC");
}
const keyPair = await mnemonicToPrivateKey(
mnemonic.split(" ")
);
const walletContract = client.open(
WalletContractV5R1.create({
workchain: 0, // basechain
publicKey: keyPair.publicKey,
}),
);
// Send the mint message through the wallet
const seqno = await walletContract.getSeqno();
await walletContract.sendTransfer({
seqno: seqno,
secretKey: keyPair.secretKey,
// Good practice to use these modes for
// regular wallet transfers
sendMode: SendMode.IGNORE_ERRORS |
SendMode.PAY_GAS_SEPARATELY,
messages: [msg],
});
}
void main();