47 const std::string& caller_address,
const std::string& contract_name,
48 const std::string& contract_path,
49 const std::vector<std::string>& init_params) {
50 std::ifstream contract_fstream(contract_path);
51 if (!contract_fstream) {
52 LOG(ERROR) <<
"could not find contract definition. file:" << contract_path
53 <<
" name:" << contract_name;
54 return absl::InvalidArgumentError(
"Contract not exist.");
57 nlohmann::json contracts_definition = nlohmann::json::parse(contract_fstream);
59 const auto all_contracts = contracts_definition[
"contracts"];
61 const std::string contract_code = all_contracts[contract_name][
"bin"];
62 if (contract_code.empty()) {
63 LOG(ERROR) <<
"could not find contract definition. file:" << contract_path
64 <<
" name:" << contract_name;
65 return absl::InvalidArgumentError(
"Contract not exist.");
68 const auto func_hashes = all_contracts[contract_name][
"hashes"];
70 DeployInfo deploy_info;
71 deploy_info.set_contract_bin(contract_code);
72 deploy_info.set_contract_name(contract_name);
74 for (
auto& func : func_hashes.items()) {
75 FuncInfo* new_func = deploy_info.add_func_info();
76 new_func->set_func_name(func.key());
77 new_func->set_hash(func.value());
80 for (
const std::string& param : init_params) {
81 deploy_info.add_init_param(param);
86 request.set_caller_address(caller_address);
87 *request.mutable_deploy_info() = deploy_info;
89 request.set_cmd(Request::DEPLOY);
90 LOG(ERROR) <<
"send request:" << request.DebugString();
92 if (ret != 0 || response.ret() != 0) {
93 return absl::InternalError(
"Deploy contract fail.");
95 return response.contract();
99 const std::string& caller_address,
const std::string& contract_address,
100 const std::string& func_name,
const std::vector<std::string>& func_params) {
103 request.set_caller_address(caller_address);
104 request.set_contract_address(contract_address);
106 request.mutable_func_params()->set_func_name(func_name);
107 for (
const std::string& param : func_params) {
108 request.mutable_func_params()->add_param(param);
111 request.set_cmd(Request::EXECUTE);
112 LOG(ERROR) <<
"send request:" << request.DebugString();
114 if (ret != 0 || response.ret() != 0) {
115 return absl::InternalError(
"Deploy contract fail.");
117 return response.res();