TON Cookbook
  • πŸ’ŽTON Fruits smart contract source code
  • βœ…Smart contract code verification
  • πŸ”¨Compilation and deploy of the TON smart contract
  • 🎟️TON smart contract for NFT raffle
Powered by GitBook
On this page

Compilation and deploy of the TON smart contract

How to compile and deploy smart contract on TON Blockchain using javascript?

PreviousSmart contract code verificationNextTON smart contract for NFT raffle

Last updated 3 years ago

After reading this article, you will know how to compile and deploy TON smart contract with the example

βœ… You can find working example

So, first of all let's install dependencies , ΠΈ :

npm i -D ton ton-compiler tweetnacl

Add required imports:

import { promises } from 'fs'
import { Address, Cell, CellMessage, contractAddress, serializeDict, TonClient } from 'ton'
import { compileFunc } from 'ton-compiler'
import nacl from 'tweetnacl'

Lets create a key pair for the smart contract management, and save it into the file:

const { publicKey, secretKey } = nacl.sign.keyPair()
await promises.writeFile('key.pk', Buffer.from(secretKey))

Then we need to compile the source code. We use source code as an example:

// You may find slot.fc content here https://gist.github.com/ton-solutions/c300d0ebb0a3ee920c8e8b310a451e29
const funcSource = (await promises.readFile('slot.fc')).toString('utf-8')
const compilationResult = await compileFunc(funcSource)
const initialCode = Cell.fromBoc(compilationResult.cell)[0]

Prepare initial contract state:

const reelsCount = 5
const symbolsCount = 8
const payTable = new Map([
    ['2111', 50],
    ['221', 100],
    ['311', 200],
    ['32', 250],
    ['41', 2000],
    ['5', 5000]
])

const slotParams = new Cell()
slotParams.bits.writeUint(symbolsCount, 8)
slotParams.bits.writeUint(reelsCount, 8)
slotParams.bits.writeBit(true)
const payTableCell = serializeDict(payTable, 32, (value, cell) => {
    cell.bits.writeUint(value, 32)
})
slotParams.refs.push(payTableCell)

let initialData = new Cell()
 // seq_no
initialData.bits.writeUint(0, 32)
// Owner address
initialData.bits.writeAddress(Address.parse('<YOUR_ADDRESS>'))
// Deployer public key
initialData.bits.writeBuffer(Buffer.from(publicKey))
initialData = initialData.withReference(slotParams)

Then we need to get smart contract address. So we call contractAddress from the ton package, passing initialCode and initialData, that we received earlier:

const contractSource = {
    initialCode,
    initialData,
    workchain: 0,
}

const address = await contractAddress(contractSource)
console.log(`Please topup ${address.toFriendly()} balance and press enter:`)

After topping balance up we can start deploy our contract:

const payload = new Cell()
payload.bits.writeUint(0, 32) // seq_no
const hash = await payload.hash()
const signature = nacl.sign.detached(hash, secretKey)
const body = new Cell()
body.bits.writeBuffer(Buffer.from(signature.buffer))
body.writeCell(payload)

const client = new TonClient({
    // you can choose desired network here (mainnet/testnet)
    endpoint: 'https://testnet.toncenter.com/api/v2/jsonRPC'
})
await client.sendExternalMessage(
    {
        address,
        source: contractSource
    },
    body
)

Well done, contract is deployed. Happy coding! πŸ’Ž

After you need to top up balance of this address (around 0.03 TON). You may just log it into console, and then top up it through or :

πŸ”¨
TON Fruits
here
ton
ton-compiler
tweetnacl
TON Fruits
wallet
@CryptoBot