提交 eec8b9f5 编写于 作者: U Ubuntu

Issue 246 - eoscpp changes and readme.md fixes

上级 f8a25a67
......@@ -175,6 +175,7 @@ add_subdirectory( libraries )
add_subdirectory( programs )
add_subdirectory( plugins )
add_subdirectory( tests )
add_subdirectory( tools )
if (ENABLE_INSTALLER)
......
......@@ -399,6 +399,12 @@ Before uploading a contract, you can verify that there is no current contract:
code hash: 0000000000000000000000000000000000000000000000000000000000000000
```
Before you can upload currency contract you need to import its private key that you generated using `create key` command:
```bash
./eosc wallet import PRIVATE_KEY_2
```
With an account for a contract created, you can upload a sample contract:
```bash
......
{
"types": [{
"newTypeName": "AccountName",
"type": "Name"
}
],
"structs": [{
"name": "transfer",
"base": "",
"fields": {
"from": "AccountName",
"to": "AccountName",
"amount": "UInt64"
}
},{
"name": "account",
"base": "",
"fields": {
"account": "Name",
"balance": "UInt64"
}
}
],
"actions": [{
"action": "transfer",
"type": "transfer"
}
],
"tables": [{
"table": "account",
"type": "account",
"indextype": "i64",
"keynames" : ["account"],
"keytypes" : ["Name"]
}
]
}
#include <currency/currency.hpp> /// defines transfer struct (abi)
namespace TOKEN_NAME {
using namespace eos;
/// When storing accounts, check for empty balance and remove account
void storeAccount( AccountName account, const Account& a ) {
if( a.isEmpty() ) {
/// value, scope
Accounts::remove( a, account );
} else {
/// value, scope
Accounts::store( a, account );
}
}
void apply_currency_transfer( const TOKEN_NAME::Transfer& transfer ) {
requireNotice( transfer.to, transfer.from );
requireAuth( transfer.from );
auto from = getAccount( transfer.from );
auto to = getAccount( transfer.to );
from.balance -= transfer.quantity; /// token subtraction has underflow assertion
to.balance += transfer.quantity; /// token addition has overflow assertion
storeAccount( transfer.from, from );
storeAccount( transfer.to, to );
}
} // namespace TOKEN_NAME
using namespace currency;
extern "C" {
void init() {
storeAccount( N(currency), Account( CurrencyTokens(1000ll*1000ll*1000ll) ) );
}
/// The apply method implements the dispatch of events to this contract
void apply( uint64_t code, uint64_t action ) {
if( code == N(currency) ) {
if( action == N(transfer) )
currency::apply_currency_transfer( currentMessage< TOKEN_NAME::Transfer >() );
}
}
}
#include <eoslib/eos.hpp>
#include <eoslib/token.hpp>
#include <eoslib/db.hpp>
/**
* Make it easy to change the account name the currency is deployed to.
*/
#ifndef TOKEN_NAME
#define TOKEN_NAME currency
#endif
namespace TOKEN_NAME {
/**
* @defgroup currencyapi Currency Contract API
* @brief Defines the curency contract
* @ingroup contractapi
*
* @{
*/
/**
* Defines a currency token
*/
typedef eos::token<uint64_t,N(currency)> CurrencyTokens;
/**
* Transfer requires that the sender and receiver be the first two
* accounts notified and that the sender has provided authorization.
*/
struct Transfer {
/**
* Account to transfer from
*/
AccountName from;
/**
* Account to transfer to
*/
AccountName to;
/**
* quantity to transfer
*/
CurrencyTokens quantity;
};
/**
* @brief row in Account table stored within each scope
*/
struct Account {
/**
Constructor with default zero quantity (balance).
*/
Account( CurrencyTokens b = CurrencyTokens() ):balance(b){}
/**
* The key is constant because there is only one record per scope/currency/accounts
*/
const uint64_t key = N(account);
/**
* Balance number of tokens in account
**/
CurrencyTokens balance;
/**
Method to check if accoutn is empty.
@return true if account balance is zero.
**/
bool isEmpty()const { return balance.quantity == 0; }
};
/**
Assert statement to verify structure packing for Account
**/
static_assert( sizeof(Account) == sizeof(uint64_t)+sizeof(CurrencyTokens), "unexpected packing" );
/**
Defines the database table for Account information
**/
using Accounts = Table<N(currency),N(currency),N(account),Account,uint64_t>;
/**
* Accounts information for owner is stored:
*
* owner/TOKEN_NAME/account/account -> Account
*
* This API is made available for 3rd parties wanting read access to
* the users balance. If the account doesn't exist a default constructed
* account will be returned.
* @param owner The account owner
* @return Account instance
*/
inline Account getAccount( AccountName owner ) {
Account account;
/// scope, record
Accounts::get( account, owner );
return account;
}
} /// @} /// currencyapi
configure_file("eoscpp" "${CMAKE_CURRENT_BINARY_DIR}" @ONLY)
#!/bin/bash -e
function copy_skeleton {
cp -r /home/ubuntu/eos/contracts/skeleton/. $newname
for file in $(find ./$newname -name 'skeleton.*')
do
mv "${file}" "`echo $file | sed 's/skeleton\./'"$newname"'./'`"
done
echo "skeleton $newname contract created"
}
function build_contract {
output=$1
shift
workdir=`mktemp -d`
mkdir $workdir/built
for file in $@; do
name=`basename $file`
filePath=`dirname $file`
@WASM_CLANG@ -emit-llvm -O3 --std=c++14 --target=wasm32 -ffreestanding -nostdlib -fno-threadsafe-statics -fno-rtti -fno-exceptions -I @CMAKE_SOURCE_DIR@/contracts -I $filePath -c $file -o $workdir/built/$name
done
@WASM_LLVM_LINK@ -o $workdir/linked.bc $workdir/built/*
@WASM_LLC@ --asm-verbose=false -o $workdir/assembly.s $workdir/linked.bc
@BINARYEN_BIN@/s2wasm -o $output -s 16384 $workdir/assembly.s
rm -rf $workdir
}
function print_help {
echo "Usage: $0 output.wast contract.cpp [other.cpp ...]"
echo " OR"
echo " $0 -n mycontract"
echo
echo "Options:"
echo " -n | --newcontract [name]"
echo " Create a new contract in the [name] folder, based on the example contract"
}
OPTIONS=$(getopt --options=hn: --longoptions=help,newcontract: --name "$0" -- "$@")
if [[ $? -ne 0 ]]; then
# getopt failed
exit 2
fi
eval set -- "$OPTIONS"
while true; do
case "$1" in
-n|--newcontract)
newname=$2
shift 2
;;
-h|--help)
print_help
exit 1
;;
--)
shift
break
;;
*)
echo "Unrecognized option: $1"
exit 1
;;
esac
done
if [[ "x" == "x$newname" ]]; then
if [[ $# -le 1 ]]; then
print_help
exit 1
fi
build_contract $@
else
if [[ $# -ne 0 ]]; then
print_help
exit 1
fi
copy_skeleton $newname
fi
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册