Initialize a contract

更新时间:
复制 MD 格式

To perform specific initialization operations when you deploy a smart contract, implement a dedicated method for the initialization logic. Then, explicitly call this method during deployment. The smart contract platform does not perform a default initialization when a contract is deployed.

#include<mychainlib/contract.h>
using namespace mychain;
class DemoContract : public Contract {
public:
  INTERFACE void DemoInit(int32_t a, std::string b) {
    //do something...
  }
};
INTERFACE_EXPORT(DemoContract,(DemoInit))

When you deploy the contract, use the SDK to specify the DemoInit method as the initialization method and provide values for the a and b parameters. The DemoInit method is also a regular contract method that you can call.

  • If an exception occurs when DemoInit is executed during deployment, the transaction receipt contains the exception information.
  • If DemoInit is executed successfully during deployment, the transaction receipt contains the contract bytecode. Therefore, do not define a return value for DemoInit because you cannot retrieve it.
Note: Each time a transaction calls a contract method, a new contract instance is created and its constructor is invoked. Therefore, do not implement the deployment initialization logic in the contract's constructor. Otherwise, the initialization operation is executed repeatedly.