Last active
March 18, 2025 04:57
-
-
Save Jayke770/e563bad67f3b3614fee2facf28f1821a to your computer and use it in GitHub Desktop.
Ton Sender
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
| /** | |
| * yarn init -y && npx tsc --init | |
| * yarn add -D typescipt ts-node | |
| * yarn add tonweb-mnemonic tonweb @orbs-network/ton-access | |
| */ | |
| import TonWeb from 'tonweb' | |
| import { type Network, getHttpEndpoint } from '@orbs-network/ton-access' | |
| import { mnemonicToKeyPair, generateMnemonic } from 'tonweb-mnemonic' | |
| interface WalletInfo { | |
| wallet: boolean, | |
| balance: number, | |
| account_state: 'uninitialized' | 'active', | |
| last_transaction_id: { | |
| '@type': string, | |
| lt: string, | |
| hash: string | |
| }, | |
| wallet_id?: number | |
| } | |
| class TON { | |
| private network: Network | undefined | |
| /** | |
| * | |
| * @param network = "mainnet" | "testnet" | |
| */ | |
| constructor({ network }: { network: Network }) { | |
| this.network = network | |
| } | |
| /** | |
| * | |
| * @returns "{ mnemonics = string[], address: string, status?: boolean, message?: string}" | |
| */ | |
| generateWallet() { | |
| return new Promise<{ mnemonics?: string[], address?: string, status?: boolean, message?: string }>(async (resolve, reject) => { | |
| try { | |
| const endpoint = await getHttpEndpoint({ network: this.network }) | |
| const tonweb = new TonWeb(new TonWeb.HttpProvider(endpoint)) | |
| const mnemonics = await generateMnemonic() | |
| const keyPair = await mnemonicToKeyPair(mnemonics) | |
| const wallet = new tonweb.wallet.all.v4R2(tonweb.provider, { publicKey: keyPair.publicKey }) | |
| const address = await wallet.getAddress() | |
| resolve({ | |
| address: address.toString(true, true, true), | |
| mnemonics, | |
| status: true, | |
| message: 'Wallet Created' | |
| }) | |
| } catch (e) { | |
| } | |
| }) | |
| } | |
| /** | |
| * | |
| * @param mnemonics string[] | |
| * @param receiver string | |
| * @param amount string | |
| * @returns "{status: boolean, message?: string}" | |
| */ | |
| deploy(mnemonics: string[], receiver: string, amount: string) { | |
| return new Promise<{ status: boolean, message?: string }>(async (resolve, reject) => { | |
| try { | |
| const endpoint = await getHttpEndpoint({ network: this.network }) | |
| const tonweb = new TonWeb(new TonWeb.HttpProvider(endpoint)) | |
| const keyPair = await mnemonicToKeyPair(mnemonics) | |
| const wallet = new tonweb.wallet.all.v4R2(tonweb.provider, { publicKey: keyPair.publicKey }) | |
| const address = await wallet.getAddress() | |
| const balance = await tonweb.getBalance(address.toString(true, true, true)) | |
| if (parseFloat(balance) <= 0) reject("Balance Not Enough") | |
| const seqno = await wallet.methods.seqno().call() || 0 | |
| await wallet.methods.transfer({ | |
| secretKey: keyPair.secretKey, | |
| amount: tonweb.utils.toNano(amount), | |
| seqno, | |
| sendMode: 3, | |
| toAddress: receiver | |
| }).send() | |
| resolve({ status: true, message: 'Success' }) | |
| } catch (e: any) { | |
| resolve({ status: false, message: e.message }) | |
| } | |
| }) | |
| } | |
| /** | |
| * | |
| * @param mnemonics string[] | |
| * @param receiver string | |
| * @param amount string | |
| * @returns "{ status: boolean, message: string, "@extra"?: string }" | |
| */ | |
| sendTon(mnemonics: string[], receiver: string, amount: string) { | |
| return new Promise<{ status: boolean, message: string, "@extra"?: string }>(async (resolve, reject) => { | |
| try { | |
| const endpoint = await getHttpEndpoint({ network: this.network }) | |
| const tonweb = new TonWeb(new TonWeb.HttpProvider(endpoint)) | |
| const keyPair = await mnemonicToKeyPair(mnemonics) | |
| const wallet = new tonweb.wallet.all.v4R2(tonweb.provider, { publicKey: keyPair.publicKey }) | |
| const address = (await wallet.getAddress())?.toString(true, true, true) | |
| const walletInfo: WalletInfo | undefined = await wallet.provider.getWalletInfo(address) | |
| if (walletInfo?.account_state !== 'active') throw new Error("Wallet Not deployed") | |
| const seqno = await wallet.methods.seqno().call() || 0 | |
| const tx: { '@type'?: 'ok', '@extra': string } = await wallet.methods.transfer({ | |
| secretKey: keyPair.secretKey, | |
| amount: tonweb.utils.toNano(amount), | |
| seqno, | |
| sendMode: 3, | |
| toAddress: receiver | |
| }).send() | |
| resolve({ status: tx?.['@type'] === 'ok', message: "Successfully Sent", "@extra": tx?.['@extra'] }) | |
| } catch (e: any) { | |
| resolve({ status: false, message: e.message }) | |
| } | |
| }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment