آدرس

تهران، خیابان شریعتی، بالاتر از سه راه ملک، روبروی آتش نشانی

شماره تماس

۰۹۱۹۳۴۲۶۲۵۱
۰۲۱۹۱۳۰۳۴۲۴

آدرس ایمیل

info@artarasaneh.com
artarasaneh@gmail.com

چگونه یک قرارداد هوشمند را در شبکه TON مستقر کنیم؟

چگونه یک قرارداد هوشمند را در شبکه TON مستقر کنیم؟

TON (شبکه باز تلگرام) یک پلتفرم بلاک چین بود که توسط تلگرام توسعه یافت و هدف آن ارائه برنامه های غیرمتمرکز سریع و مقیاس پذیر بود.

ویژگی‌های منحصربه‌فرد آن شامل مکانیزم اجماع متحمل خطای بیزانسی و پردازش خارج از زنجیره بود. استفاده بالقوه TON در پشتیبانی از تراکنش‌های پرسرعت و کم‌هزینه، فعال کردن DApp‌های پیچیده و قراردادهای هوشمند، و بهبود تأیید هویت دیجیتال است که همه برای برنامه‌های Web 3.0 که به دنبال راه‌حل‌های کارآمد، ایمن و مقیاس‌پذیر هستند، حیاتی است.

در این مقاله ما شما را از طریق ایجاد یک قرارداد هوشمند در شبکه TON راهنمایی می‌کنیم، و اطلاعاتی واضح و مختصر از ابزارهای لازم، تکنیک‌های کدنویسی و روش‌های استقرار ارائه می‌کنیم. در پایان، درک جامعی از نحوه اجرای قرارداد هوشمند خود در این پلتفرم خواهید داشت.

در اینجا مراحل ایجاد یک قرارداد هوشمند در شبکه TON آمده است

برای تعریف اولین قرارداد هوشمند خود در شبکه TON و راه اندازی ماشین محلی خود برای توسعه، مراحل زیر را دنبال کنید:

پیش نیازهای نصب:

مطمئن شوید که یک سیستم عامل سازگار (معمولا لینوکس) دارید.
جاوا اسکریپت را با نسخه اخیر Node JS نصب کنید.
یک ویرایشگر کد مانند Visual Studio Code با FunC برای کدنویسی قرارداد هوشمند خود نصب کنید همچنین افزونه FunC را نصب کنید.

راه اندازی TON SDK:

از مخزن TON SDK در GitHub دیدن کنید.
دستورالعمل های نصب سیستم عامل انتخابی خود را برای نصب TON SDK دنبال کنید.

راه اندازی پروژه:

ایجاد یک دایرکتوری جدید با

npm install ts-node
npm install @ton-community/func-js
npm install ton ton-crypto ton-core
(int) load_data() inline { ;; read function declaration - returns int as result
 var ds = get_data().begin_parse(); ;; load the storage cell and start parsing as a slice
 return (ds~load_uint(64)); ;; read a 64 bit unsigned int from the slice and return it
}
() save_data(int counter) impure inline { ;; write function declaration - takes an int as arg
 set_data(begin_cell() ;; store the storage cell and create it with a builder
 .store_uint(counter, 64) ;; write a 64 bit unsigned int to the builder
 .end_cell()); ;; convert the builder to a cell
    }
() recv_internal(int msg_value, cell in_msg, slice in_msg_body) impure { ;; well known function signature
 if (in_msg_body.slice_empty?()) { ;; check if incoming message is empty (with no body)
 return (); ;; return successfully and accept an empty message
 }
 int op = in_msg_body~load_uint(32); ;; parse the operation type encoded in the beginning of msg body
 var (counter) = load_data(); ;; call our read utility function to load values from storage
 if (op == 1) { ;; handle op #1 = increment
 save_data(counter + 1); ;; call our write utility function to persist values to storage
 }
    }
int counter() method_id { ;; getter declaration - returns int as result
 var (counter) = load_data(); ;; call our read utility function to load value
 return counter;
    }
npx func-js counter.fc --boc counter.cell
import { Contract, ContractProvider, Sender, Address, Cell, contractAddress, beginCell } from ton-core;
export default class Counter implements Contract {
 static createForDeploy(code: Cell, initialCounterValue: number): Counter {
 const data = beginCell()
 .storeUint(initialCounterValue, 64)
 .endCell();
 const workchain = 0; // deploy to workchain 0
 const address = contractAddress(workchain, { code, data });
 return new Counter(address, { code, data });
 }
 constructor(readonly address: Address, readonly init?: { code: Cell, data: Cell }) {}
    }
// export default class Counter implements Contract {
 async sendDeploy(provider: ContractProvider, via: Sender) {
 await provider.internal(via, {
 value: 0.01, // send 0.01 TON to contract for rent
 bounce: false
 });
 }
// }

استقرار قرارداد در زنجیره

ابتدا TON access را با دستور زیر نصب کنید

npm install @orbs-network/ton-access

یک اسکریپت جدید ایجاد کنید که از کلاس رابط (deploy.ts) استفاده کند.

import * as fs from fs;
import { getHttpEndpoint } from @orbs-network/ton-access;
import { mnemonicToWalletKey } from ton-crypto;
import { TonClient, Cell, WalletContractV4 } from ton;
import Counter from ./counter; // this is the interface class from step 7
async function deploy() {
 // initialize ton rpc client on testnet
 const endpoint = await getHttpEndpoint({ network: testnet });
 const client = new TonClient({ endpoint });
 // prepare Counter's initial code and data cells for deployment
 const counterCode = Cell.fromBoc(fs.readFileSync(counter.cell))[0]; // compilation output from step 6
 const initialCounterValue = Date.now(); // to avoid collisions use current number of milliseconds since epoch as initial value
 const counter = Counter.createForDeploy(counterCode, initialCounterValue);
 // exit if contract is already deployed
 console.log(contract address:, counter.address.toString());
 if (await client.isContractDeployed(counter.address)) {
 return console.log(Counter already deployed);
 }
 // open wallet v4 (notice the correct wallet version here)
 const mnemonic = unfold sugar water ...; // your 24 secret words (replace ... with the rest of the words)
 const key = await mnemonicToWalletKey(mnemonic.split( ));
 const wallet = WalletContractV4.create({ publicKey: key.publicKey, workchain: 0 });
 if (!await client.isContractDeployed(wallet.address)) {
 return console.log(wallet is not deployed);
 }
 // open wallet and read the current seqno of the wallet
 const walletContract = client.open(wallet);
 const walletSender = walletContract.sender(key.secretKey);
 const seqno = await walletContract.getSeqno();
 // send the deploy transaction
 const counterContract = client.open(counter);
 await counterContract.sendDeploy(walletSender);
 // wait until confirmed
 let currentSeqno = seqno;
 while (currentSeqno == seqno) {
 console.log(waiting for deploy transaction to confirm...);
 await sleep(1500);
 currentSeqno = await walletContract.getSeqno();
 }
 console.log(deploy transaction confirmed!);
}
deploy();
function sleep(ms: number) {
 return new Promise(resolve => setTimeout(resolve, ms));
}

آن را با دستور زیر در ترمینال خود اجرا کنید

npx ts-node step5.ts

آدرس قرارداد استقرار خود را یادداشت کنید.

کد زیر را به counter.ts اضافه کنید

// export default class Counter implements Contract {
 async getCounter(provider: ContractProvider) {
 const { stack } = await provider.get(counter, []);
 return stack.readBigNumber();
 }
// }

اجرای مرحله بعدی (step5.ts):

import { getHttpEndpoint } from @orbs-network/ton-access;
import { mnemonicToWalletKey } from ton-crypto;
import { TonClient, WalletContractV4, Address } from ton;
import Counter from ./counter; // this is the interface class we just implemented
async function main() {
 // initialize ton rpc client on testnet
 const endpoint = await getHttpEndpoint({ network: testnet });
 const client = new TonClient({ endpoint });
 // open wallet v4 (notice the correct wallet version here)
 const mnemonic = unfold sugar water ...; // your 24 secret words (replace ... with the rest of the words)
 const key = await mnemonicToWalletKey(mnemonic.split( ));
 const wallet = WalletContractV4.create({ publicKey: key.publicKey, workchain: 0 });
 if (!await client.isContractDeployed(wallet.address)) {
 return console.log(wallet is not deployed);
 }
 // open wallet and read the current seqno of the wallet
 const walletContract = client.open(wallet);
 const walletSender = walletContract.sender(key.secretKey);
 const seqno = await walletContract.getSeqno();
 // open Counter instance by address
 const counterAddress = Address.parse(EQBYLTm4nsvoqJRvs_L-IGNKwWs5RKe19HBK_lFadf19FUfb); // replace with your address from step 8
 const counter = new Counter(counterAddress);
 const counterContract = client.open(counter);
 // send the increment transaction
 await counterContract.sendIncrement(walletSender);
 // wait until confirmed
 let currentSeqno = seqno;
 while (currentSeqno == seqno) {
 console.log(waiting for transaction to confirm...);
 await sleep(1500);
 currentSeqno = await walletContract.getSeqno();
 }
 console.log(transaction confirmed!);
}
main();
function sleep(ms: number) {
 return new Promise(resolve => setTimeout(resolve, ms));
}

کد را با دستور زیر اجرا کنید

npx ts-node step6.ts

نتیجه:

در اینجا در این وبلاگ، یک قرارداد هوشمند در شبکه TON ایجاد کرده ایم.

برای مدیریت تمام این فرآیندهای دستی به طور خودکار از ابزار توسعه به نام Blueprint استفاده کنید.

در دنیای پویای بلاک چین و فناوری های غیرمتمرکز، شبکه TON به عنوان یک پلتفرم انقلابی با پتانسیل بسیار زیاد برجسته می شود. یادگیری در اکوسیستم شبکه TON مزایای منحصر به فردی را ارائه می دهد که می تواند به طور قابل توجهی به رشد شغلی شما کمک کند.

FABC اولین شرکت جهانی web3.0 در جهان است، شورای ما تجسم جامعه است که طیفی از مبتکران، ایجادکنندگان تغییر و پیشگامان را در بر می گیرد. ما متعهد هستیم که هسته بلاک چین را با دروس عملی کشف کنیم و مفاهیم پیچیده را برای همه در دسترس قرار دهیم. زمینه را برای اکتشاف مطمئن در حوزه بلاک چین فراهم کنید. درباره ما در FABC بیشتر بیاموزید و کشف کنید که چگونه به صنعت کمک می کنیم / مشکلات را حل می کنیم / ایجاد تفاوت می کنیم.

دز صورت تمایل به آموزش بلاکچین می توانید از دوره برنامه نویسی بلاکچین استفاده کنید .

اشتراک گذاری :
مریم گوهرزاد
نویسنده

مریم گوهرزاد

مدرس و بنیانگذار هلدینگ آرتا رسانه. برنامه نویس و محقق حوزه بلاکچین

https://t.me/artarasaneh
tel:09193426251
https://wa.me/+989193426251
https://instagram.com/artarasaneh_com