in interface/contract/contract_client.cpp [46:96]
absl::StatusOr<Contract> ContractClient::DeployContract(
const std::string& caller_address, const std::string& contract_name,
const std::string& contract_path,
const std::vector<std::string>& init_params) {
std::ifstream contract_fstream(contract_path);
if (!contract_fstream) {
LOG(ERROR) << "could not find contract definition. file:" << contract_path
<< " name:" << contract_name;
return absl::InvalidArgumentError("Contract not exist.");
}
nlohmann::json contracts_definition = nlohmann::json::parse(contract_fstream);
const auto all_contracts = contracts_definition["contracts"];
const std::string contract_code = all_contracts[contract_name]["bin"];
if (contract_code.empty()) {
LOG(ERROR) << "could not find contract definition. file:" << contract_path
<< " name:" << contract_name;
return absl::InvalidArgumentError("Contract not exist.");
}
const auto func_hashes = all_contracts[contract_name]["hashes"];
DeployInfo deploy_info;
deploy_info.set_contract_bin(contract_code);
deploy_info.set_contract_name(contract_name);
for (auto& func : func_hashes.items()) {
FuncInfo* new_func = deploy_info.add_func_info();
new_func->set_func_name(func.key());
new_func->set_hash(func.value());
}
for (const std::string& param : init_params) {
deploy_info.add_init_param(param);
}
Request request;
Response response;
request.set_caller_address(caller_address);
*request.mutable_deploy_info() = deploy_info;
request.set_cmd(Request::DEPLOY);
LOG(ERROR) << "send request:" << request.DebugString();
int ret = SendRequest(request, &response);
if (ret != 0 || response.ret() != 0) {
return absl::InternalError("Deploy contract fail.");
}
return response.contract();
}