智能合约 智能合约 空参数函数调用

OuterST · July 24, 2018 · Last by dnbstarz replied at August 22, 2018 · 14192 hits

1. 新建文件

在 eos/contrants下新建文件夹 eosio.voidtest

在文件夹里新建eosio.voidtest.hpp文件

/**
* @file
* @copyright defined in eos/LICENSE.txt
*/
#pragma
once

#include <eosiolib/eosio.hpp>
#include <eosiolib/asset.hpp>
#include <string>

namespace eosio {

class voidtest : public contract {
public:
voidtest(account_name self) : contract(self) {}
void test();
};
} /// namespace eosio

在文件夹里新建 eosio.voidtest.cpp文件

/**
* @file
* @copyright defined in eos/LICENSE.txt
*/
#include
"eosio.voidtest.hpp"

namespace eosio {
// @abi action
void voidtest::test()
{
print("aaaa");
}


EOSIO_ABI( voidtest, (test) )

} /// namespace eosio

新建CMakeLists.txt 文件

file(GLOB ABI_FILES "*.abi")
configure_file("${ABI_FILES}" "${CMAKE_CURRENT_BINARY_DIR}" COPYONLY)

add_wast_executable(TARGET eosio.voidtest
INCLUDE_FOLDERS "${STANDARD_INCLUDE_FOLDERS}"
LIBRARIES libc++ libc eosiolib
DESTINATION_FOLDER ${CMAKE_CURRENT_BINARY_DIR}
)

在父文件夹的CMakeLists.txt文件里添加下面一句

add_subdirectory(eosio.voidtest)

2 生成abi和wasm

到 eos/contracts/eosio.voidtest文件夹里执行

eosiocpp -g eosio.voidtest.abi eosio.voidtest.cpp

eosiocpp -o eosio.voidtest.wasm eosio.voidtest.cpp

在 eos目录

sudo ./eos_build.sh

3 空函数调用

首先修改~/.local/share/eosio/nodeos/config、config.ini 文件,让print能输出到终端。

contracts-console = true

合约调用

~/opt/mongodb/bin/mongod -f ~/opt/mongodb/mongod.conf &

rm -rf ~/.local/share/eosio/nodeos/data/ ~/eosio-wallet/default.wallet

nodeos -e -p eosio --plugin eosio::wallet_api_plugin --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --plugin eosio::net_api_plugin   --plugin eosio::history_plugin 
cleos wallet create

cleos wallet import --private-key 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3

cleos wallet import --private-key 5Js9i3oFfp9jsYBPZoZBZtcux5hke8ySxwBtwERvP51u6thM34j

cleos create account eosio ost EOS8kZRKTXpLozY182AtdvjZyEEwz96vvFvJttj7nbKc3N27s35wg EOS8kZRKTXpLozY182AtdvjZyEEwz96vvFvJttj7nbKc3N27s35wg

cleos set contract ost build/contracts/eosio.voidtest/ -p ost

cleos push action ost test '[]' -p ost

最后合约调用结果

cleos push action ost test '[]' -p ost
executed transaction: e14c7eb92bb5a435cfe3cbf303115b72e9ddc57e2c7ed052e8c401cdee542566 96 bytes 383 us
# ost <= ost::test ""
>> aaaa
2018-07-24T06:08:32.631 thread-0 main.cpp:391 print_result warning: transaction executed locally, but may not be confirmed by the network yet

@lome

共收到 44 条回复
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up