Deploying to network
Summary: In previous steps we developed and tested our smart contracts ensuring their correctness.
In this part of the guide we will proceed to deployment of previously developed smart contracts and cover interaction with them on-chain.
Address and initial state
We already know that address is unique identifier of smart contract
in network which is used to send transactions and verify their sender upon receive, but we still didn't discussed how it's created. The common formula of smart contract address looks like that:
address=hash(state_init(code, data))
Address of smart contract is a hash of aggregated initial code and data of smart contracts upon deployment. This simple mechanism have few important consequences:
You already know the address
In TON any address that don't have any data is considered in nonexistent
state, nevertheless when we created a wallet using wallet app in Getting started section we still was able to get address of our future wallet smart contract from wallet app before it's deployment and examine it in explorer.
The reason behind that is because creating your private and public key pair through mnemonic phrase, where second key is part of initial data of smart contract makes state_init
of our contract fully determent:
- code is one of the standard wallet implementation, like
v5r1
. - data is
public_key
with other default initialized fields.
Which makes it possible to calculate future wallet smart contract address.
Magic storage member
In previous steps we deliberately didn't explain the purpose of ctxID
and ID
stored in our smart contracts state and remaining untouched in all smart contracts functionality. Now their purpose should start to be more clear.
Since we can't deploy smart contract with the same state_init
the only way to provide same initial code and "same" initial data is to create separate filed in it ensuring additional uniqueness. Which in case of wallet gives you opportunity to have same key pair to several wallet smart contracts.
One to rule them all
If you already considered that ID
field is a must-have for any smart contract, there is another opportunity that could change your mind. Let's examine previously developed CounterInternal
smart contract init section:
init(id: Int, owner: Address) {
self.id = id;
self.counter = 0;
self.owner = owner;
}
If we remove id
field from its initial storage we can ensure that only one CounterInternal
smart-cotnract could exzist for a particular owner.
This mechanism plays cruicial role in Jetton Processing, each non-native(jetton) token requires it's own Jetton Wallet
for a particular owner and therefore provides a calculatable address from it, creating a star scheme with basic wallet in center.
Implementation
Now, when our smart contracts is fully tested, we are ready to deploy them into the TON. In Blueprint SDK
this process is the same for both Mainnet
and Testnet
and any of the presented languages in guide: FunC
, Tact
, Tolk
.
Step 1: Update deployment script
Deploy scripts relays on the same wrappers that you have used in testing scripts, we will use one common script to deploy both of previously deployed smart contracts, update deployHelloWorld.ts
with this code:
import { toNano } from '@ton/core';
import { HelloWorld } from '../wrappers/HelloWorld';
import { CounterInternal } from '../wrappers/CounterInternal';
import { compile, NetworkProvider } from '@ton/blueprint';
export async function run(provider: NetworkProvider) {
const helloWorld = provider.open(
HelloWorld.createFromConfig(
{
id: Math.floor(Math.random() * 10000),
ctxCounter: 0,
ctxCounterExt: 0n,
},
await compile('HelloWorld')
)
);
await helloWorld.sendDeploy(provider.sender(), toNano('0.05'));
await provider.waitForDeploy(helloWorld.address);
const counterInternal = provider.open(
await CounterInternal.fromInit(
BigInt(Math.floor(Math.random() * 10000)),
helloWorld.address
)
);
await counterInternal.send(
provider.sender(),
{ value: toNano('0.05') },
null
);
await provider.waitForDeploy(counterInternal.address);
console.log('ID', await helloWorld.getID());
console.log('ID', (await counterInternal.getId()).toString());
}
Step 2: Run deploy script
You can run scripts by entering following command:
npx blueprint run # deployHelloWorld # optionally directly pass script
Step 3: choose network
After that you will see interactive menu allowing to choose network:
? Which network do you want to use? (Use arrow keys)
mainnet
❯ testnet
custom
Before deployment to the Mainnet
ensure that your smart contracts correspond to Security Measures, as we sad before, HelloWorld
smart contract don't.
Step 4: choose wallet app
Next, choose the way to access your wallet, the easiest way to do that is using TON Connect for most popular wallet apps: TonKeeper
, MyTonWallet
or Tonhub
.
? Which wallet are you using? (Use arrow keys)
❯ TON Connect compatible mobile wallet (example: Tonkeeper)
Create a ton:// deep link
Mnemonic
? Choose your wallet (Use arrow keys)
❯ Tonkeeper
MyTonWallet
Tonhub
Finally scan QR code in the terminal through your wallet app and connect the wallet. After you done that for the first time in project this step will be skipped.
You will recieve a transaction request in your wallet app each time your code require currency for transaction.
Step 5: observe your smart contract in network
After confirming request in your wallet and awaiting for deployment you will see corresponding message with the reference to your newly deployed smart contract view in explorer:
Contract deployed at address EQBrFHgzSwxVYBXjIYAM6g2RHbeFebJA8QUDwg4IX8CPDPug
You can view it at https://testnet.tonscan.org/address/EQBrFHgzSwxVYBXjIYAM6g2RHbeFebJA8QUDwg4IX8CPDPug
Congratulations! Your custom smart contracts
is ready to execute get methods
same way as your wallet in Getting started section and execute transactions
same as in Blockchain Interaction section - consider it's reading to understand how to interact with smart contracts
off-chain if you still don't.
Using Blueprint SDK
and wallet apps is not the only option, you can create message with state_init by yourself, moreover you can do it even through smart contract internal message.