In Aptos how do you you call initialize a constructor when deploying the contract with Aptos CLI

60 views Asked by At

I want to know what is the constructor function (that is called during deployment) in Aptos move contract and how initialize a string value on the contract level when calling it. How to achieve this and how do I call it with argument with the aptos move publish command?

1

There are 1 answers

2
Daniel Porteous On

You might be looking for init_module. This function runs automatically just when you publish your module.

An example:

fun init_module(sender: &signer) {
    aptos_framework::managed_coin::initialize<MoonCoin>(
        sender,
        b"Moon Coin",
        b"MOON",
        6,
        false,
    );
}

From aptos-move/move-examples/moon_coin/sources/MoonCoin.move in aptos-core.

You can learn more about it here: https://aptos.dev/move/move-on-aptos/modules-on-aptos/.

From the answer to this previous Stack Overflow question: https://stackoverflow.com/a/76367239/3846032.