-
Originally from Stack Overflow. 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 do I achieve this and how do I call it with argument with the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Note: Below you'll find the original answer suggesting that you use module addr::my_module {
/// You are not the publisher of the package.
const CALLER_NOT_PUBLISHER: u64 = 1;
fun initialize(caller: &signer) {
// Ensure only the module publisher can call this.
assert!(signer::address_of(&caller) == @addr, error::permission_denied(CALLER_NOT_PUBLISHER));
// Do initialization.
}
} You might be looking for An example:
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. |
Beta Was this translation helpful? Give feedback.
Note: Below you'll find the original answer suggesting that you use
init_module
. This still works, but these days we often caution againstinit_module
due to some weird quirks with its behavior (for example,init_module
only runs on publish, not upgrade, which confuses some folks). These days we recommend you add your own custom function, e.g.initialize
, and then call it yourself manually after you publish. For example: