title | actions | requireLogin | material | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Understanding the _deploy Function |
|
true |
|
Now that you've learned how to invoke the Neo Name Service smart contract, we'll move on and look at two key aspects of building on Neo - how contracts are deployed and updated.
To facilitate contract initialization and storage setup, Neo provides a designated entry point in the form a function named _deploy
.
The _deploy
function has the following signature:
public static void _deploy(object data, bool update)
{
// Deployment logic
}
Let's break down the parameters of the _deploy
function:
data
: An optional parameter that can be used to pass additional data to the_deploy
function during contract deployment. It can be any object or information needed for initialization purposes.update
: A boolean parameter that indicates whether the contract is being deployed for the first time or being updated. Ifupdate
istrue
, it means the contract is being updated and not freshly deployed.
Next, when a contract is updated, Neo provides a similar function named Update
. As this function is public
it means that anybody could call it which can become a problem. To solve this issue, we need to save the address of the owner when the contract is deployed and then, the Update
function must ensure that it's invoked by the owner of the contract.
That being said, the first thing the _deploy
function should do is to verify that the value of the update
parameter is false, using something like this:
if (update) return;
Next, you would want to store the address of the owner in the contract storage. As you probably remember from one of the previous chapters, you can only store and retrieve data as key-value pairs, so you'll use the owner key as the key and the owner address as the value.
We went ahead and wrote some boilerplate code that specifies the owner address and determines the owner key. Check out the code we've written and make sure you understand what it does before you continue.
Note: If you want to use this code in your own smart contract, make sure to update the line of code that sets the address of the owner!
Next, you must declare the _invoke
function and invoke the Put
method of the ContractMap
object to store the owner address and key. This method takes two parameters - a key under which the value will be stored and the value to be stored in the storage map.
- Create a new
public static void
function named_deploy
that takes two parameters -data
of type object andupdate
of typebool
. - The first line of your new fucntion should use the example above to ensure that the value of the
update
parameter isfalse
. - Next, you must call the
Put
function of theContractMap
object, passing itownerKey
andowner
.