Deploy
Find out how to deploy a contract to Acki Nacki Blockchain with SDK
Core api is more flexible than AppKit, and you can perform a lot of complex logic using it. But you will need to write more code with it as well:)
You need to define the contract in your node.js application before deploy.
See the "Add Contract to your App" guide to find out how to do it.
About deploy
Deploy operation means that you upload contract code and initial data to the blockchain.
The address of the contract can be calculated before deploy and it depends on code and data.
To deploy a contract you need to sponsor its address, so that deploy fee will be paid out of these funds.
Deploy steps
Below is the sequence of steps you need to complete:
Observe deploy parameters
Prepare a pair of keys
[optional] Prepare Initial contract data
Calculate future address of the contract
Transfer funds to the future address
Deploy
Let's take a look at every step.
Observe deploy parameters
Here is the structure, that is used to prepare initial contract state for deploy. It will define the future contract address.
tvc
- As we discussed at the previous step, contract compilation artifact, converted into base64.
workchain_is
- Target workchain where the contract will be deployed. By default is 0.
initial_data
- structure that contains public contract data fields with assigned values that need to be included into the initial state.
initial_pubkey
- owner pubkey. If not specified, pubkey is taken from Signer
. If you manage to specify contract owner directly in tvc
, then it will have priority above both initial_pubkey
and Signer
.
Prepare a pair of keys
To deploy you will need a pair of keys to sign the deployment message. The keys are used during address generation, so the future contract address depends partially on the key as well.
In fact, keys are optional for deploy, but, if you want to be the contract owner then specify Signer object for that. Read more about Signer object in Reference.
If you want to make another person owner of the contract then specify their pubkey via DeploySet.initial_pubkey
.
In this guide we will use crypto.generate_random_sign_keys
function to generate a key pair.
Note: Please store these keys in a secure environment as you will need them to calculate the future address and deploy the contract. You might also need it to call contract functions in the future.
[Optional] Prepare initial contract data
Placing data into storage always affects the future contract address.
This is useful for scenarios when you need to deploy several contracts with identical contract code (e.g. wallets) to different addresses.
If each contract is deployed with its own unique pair of keys, you do not need to use DeploySet.initial_data
as the addresses will be generated using the corresponding key pair each time.
If, however, all the contracts need to have the same contract code and have to be deployed with the same pair of keys but to different addresses, you may use placing data into storage through DeploySet.initial_data
to vary the addresses during address calculation.
To be used as initParams
variables must be declared as static
in the contract.
Specify constructor
Constructor - is any contract function that will be called upon deploy. It will not influence contract address.
It, together with its parameters, is specified in the CallSet structure:
function_name
- function name that will be called upon deploy. Usually, it is called constructor
.
header
- optional, can be specified for some complex use cases. Read more here.
input
- object with function parameters.
Calculate the future contract address
Acki Nacki blockchain requires every contract to have a positive token balance before deployment. The contract pays for the initial deployment message reducing account balance.
We will create deploy message and get future contract address from it.
You can either specify or not specify call_set here, it will not influence the address.
Note: Deployment requires the same pair of keys used for address calculation.
Note:
constructorParams
do not affect the address. OnlyinitParams
and the keys do. If you only plan to calculate address, you can omit this parameter.
Transfer funds to the future address
Now that you know the address, you need to transfer the initial funds to it from your wallet or the Giver.
Note: Evernode SE offers a pre-deployed giver. When in real networks, you need to use your wallet for this or deploy your own giver. We have separated guides of deployment and usage of your own giver.
Deploy
At this point, you're all set for deployment.
Note: Insufficient balance will result in a failed deployment.
Note: Acki Nacki Blockchain does not guarantee a 100% external inbound messages success rate. There is a possibility that deployment will fail. To ensure success, please check for a completed transaction.
Pattern 1. Deploy in 1 step: process_message
method
process_message
methodThis is an easy to implement pattern to deploy for server-side development and tests, but we do not recommend to use it in web and mobile applications because of network inconsistency and application crash probability. For more reliable approach - use the Pattern 2.
Method `process_message performs all the deploy steps sequentially in one method: create message, send it, wait for transaction and decode external outbound message generated by contract RETURN operator. The time limit can be set up in Client configuration. The default time setting is 40 seconds. You can learn more here.
See the full example in sdk samples repository:
https://github.com/tonlabs/sdk-samples/blob/master/core-examples/node-js/hello-wallet/index.js
Check out how to run contract's methods in the next section.
Pattern 2. Deploy in 3 steps: encode_message
-> send_message
-> wait_for_transaction
encode_message
-> send_message
-> wait_for_transaction
Any interaction with and within the Acki Nacki blockchain is implemented via messages.
To deploy a contract, first you need to create a deploy message that will include all the initial data:
Now the message should be sent. send_message
method returns the the last block created in the shard before the message was sent.
send_events: false
flag specifies that we will not monitor message delivery events. Soon there will be a guide about message processing monitoring.
After the message was sent we need to wait for the transaction:
If wait_for_transaction
fails with 507 error - you can perform a retry. In all other cases retry may end up with double spends.
See the full example in sdk samples repository:
Check out how to run contract's methods in the next section.
Deploy a contract from another contract
Often developers need to deploy one contract from another contract and try to pass tvc
file as a cell parameter for contract to use.
This is not a correct way.
To be able to deploy a contract from another contract you need to pass the contract's code as a cell parameter.
You can get it several ways:
You can use SDK function get_code_from_tvc of
boc
module and retrieve the code's boc from tvc file.You can download another account with the same
code_hash
'sboc
from graphql, using blockchain API and retrieve the 'code' from the resultboc
field.
Sample Source Code
Full sample: https://github.com/tonlabs/sdk-samples/blob/master/core-examples/node-js/hello-wallet/
Check out AppKit documentation for this use case.
Last updated