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
DemoInitis executed during deployment, the transaction receipt contains the exception information. - If
DemoInitis executed successfully during deployment, the transaction receipt contains the contract bytecode. Therefore, do not define a return value forDemoInitbecause 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.
该文章对您有帮助吗?