Last active
January 27, 2025 01:19
-
-
Save gleba/0591780563957aa6fc8a6946aafc4943 to your computer and use it in GitHub Desktop.
TON Jetton example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const TonWeb = require("tonweb"); | |
const utils = require("tonweb/src/utils"); | |
const NftUtils = require("tonweb/src/contract/token/nft/NftUtils"); | |
const Cell = TonWeb.boc.Cell; | |
const { JettonMinter, JettonWallet } = TonWeb.token.jetton; | |
const jettonContentUri = 'https://files.raevskyschool.ru/coin.json'; | |
async function doit() { | |
const tonweb = new TonWeb(new TonWeb.HttpProvider('https://testnet.toncenter.com/api/v2/jsonRPC', { apiKey: "get here: https://t.me/tontestnetapibot" })); | |
const WalletClass = tonweb.wallet.all['v3R2']; | |
async function tonWalletFromBase64(bytes) { | |
const seed = TonWeb.utils.base64ToBytes(bytes); | |
const keyPair = TonWeb.utils.nacl.sign.keyPair.fromSeed(seed); | |
const wallet = new WalletClass(tonweb.provider, { | |
publicKey: keyPair.publicKey, | |
}); | |
const address = await wallet.getAddress(); | |
const logBalance = async (suffix = "") => { | |
const balance = await tonweb.getBalance(admin.address); | |
console.log(suffix + " address", admin.address.toString(true, true, true)); | |
console.log(suffix + " balance", balance); | |
}; | |
return { keyPair, wallet, address, logBalance }; | |
} | |
const admin = await tonWalletFromBase64('vt78J2v6FaSuXFGcyGtqT5elpVxcZ+I1zgu/GUfA5uY='); | |
const bank = await tonWalletFromBase64('vt73J2v6FaSuXFGcyGtqT5elpVxcZ+I1zgu/GUfA5uY='); | |
const minter = new JettonMinter(tonweb.provider, { | |
adminAddress: admin.address, | |
jettonContentUri, | |
jettonWalletCodeHex: JettonWallet.codeHex | |
}); | |
const minterAddress = await minter.getAddress(); | |
console.log("адрес тон-кошелька админа:", admin.address.toString(true, true, true)); | |
console.log("адрес минтера:", minterAddress.toString(true, true, true)); | |
async function createJetton() { | |
const deployMinter = async () => { | |
const seqno = (await admin.wallet.methods.seqno().call()) || 0; | |
console.log(await admin.wallet.methods.transfer({ | |
secretKey: admin.keyPair.secretKey, | |
toAddress: minterAddress.toString(true, true, true), | |
amount: TonWeb.utils.toNano(0.05), | |
seqno, | |
payload: null, | |
sendMode: 3, | |
stateInit: (await minter.createStateInit()).stateInit | |
}).send()); | |
}; | |
// await deployMinter() | |
const mint = async () => { | |
const seqno = (await admin.wallet.methods.seqno().call()) || 0; | |
console.log(await admin.wallet.methods.transfer({ | |
secretKey: admin.keyPair.secretKey, | |
toAddress: minterAddress.toString(true, true, true), | |
amount: TonWeb.utils.toNano('0.5'), | |
seqno: seqno, | |
payload: await minter.createMintBody({ | |
jettonAmount: TonWeb.utils.toNano(721), | |
destination: admin.address, | |
amount: TonWeb.utils.toNano('0.04') | |
}), | |
sendMode: 3, | |
}).send()); | |
}; | |
await mint() | |
const getMinterInfo = async () => { | |
const data = await minter.getJettonData(); | |
data.totalSupply = data.totalSupply.toString(); | |
data.adminAddress = data.adminAddress.toString(true, true, true); | |
console.log("MinterInfo.totalSupply:", data.totalSupply); | |
}; | |
await getMinterInfo(); | |
} | |
// await createJetton() | |
// return | |
async function toJettonWallet(ownerAddress) { | |
const cell = new Cell(); | |
cell.bits.writeAddress(ownerAddress); | |
const result = await tonweb.provider.call2(minterAddress.toString(), 'get_wallet_address', [['tvm.Slice', (0, utils.bytesToBase64)(await cell.toBoc(false))]]); | |
const address = (0, NftUtils.parseAddress)(result); | |
return new JettonWallet(tonweb.provider, { | |
address | |
}); | |
} | |
const checkJettonBalance = async (jettonWallet) => { | |
console.log(":: проверка жетон-баланса жетон-кошелька:", jettonWallet.address.toString(true, true, true)); | |
const data = await jettonWallet.getData(); | |
data.ownerAddress = | |
data.jettonMinterAddress = data.jettonMinterAddress.toString(true, true, true); | |
console.log(":: жетон-баланс:", data.balance.toString()); | |
// console.log(":: ownerAddress :", data.ownerAddress.toString(true, true, true)); | |
// console.log(":: jettonMinterAddress:", data.jettonMinterAddress.toString(true, true, true)); | |
}; | |
const bankJW = await toJettonWallet(bank.address) | |
console.log("bank", bank.address.toString(true, true, true)) | |
console.log("bankJW", bankJW.address.toString(true, true, true)) | |
await checkJettonBalance(bankJW); | |
const transfer = async (targetTonAddres) => { | |
const seqno = (await bank.wallet.methods.seqno().call()) || 0; | |
console.log("перевод жетона") | |
console.log(await bank.wallet.methods.transfer({ | |
secretKey: bank.keyPair.secretKey, | |
toAddress: bankJW.address, | |
amount: TonWeb.utils.toNano(0.4), | |
seqno, | |
payload: await bankJW.createTransferBody({ | |
jettonAmount: TonWeb.utils.toNano('1'), | |
toAddress: targetTonAddres, | |
forwardAmount: TonWeb.utils.toNano(0.1), | |
forwardPayload: new TextEncoder().encode('gift'), | |
responseAddress: bank.address | |
}), | |
sendMode: 3, | |
}).send()); | |
}; | |
const newWallet = await tonWalletFromBase64('vt51J2v6FaSuXFGcyGtqT5elpVxcZ+I1zgu/GUfA5uY='); | |
console.log("тон-адресс нового кошелька", newWallet.address.toString(true, true, true)); | |
await transfer(newWallet.address) | |
const newJettonWallet = await toJettonWallet(newWallet.address); | |
console.log("жетон-адресс нового кошелька", newJettonWallet.address.toString(true, true, true)); | |
console.log("ждём 15 сек") | |
await new Promise(done=>setTimeout(done, 15000)) | |
await checkJettonBalance(bankJW); | |
await checkJettonBalance(newJettonWallet); | |
} | |
doit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
const err = new Error('http provider parse response error')
^
Error: http provider parse response error
result: 'Network not allowed'