提交 c62d5256 编写于 作者: B Brian Johnson

Adding infinite contract for checktime test verification

上级 b988ed19
add_subdirectory(currency)
add_subdirectory(exchange)
#add_subdirectory(social)
\ No newline at end of file
add_subdirectory(infinite)
#add_subdirectory(social)
file(GLOB SOURCE_FILES "*.cpp")
add_wast_target(infinite "${SOURCE_FILES}" "${CMAKE_SOURCE_DIR}/contracts" ${CMAKE_CURRENT_SOURCE_DIR})
#include <infinite/infinite.hpp> /// defines transfer struct (abi)
namespace infinite {
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 infinite::Transfer& transfer ) {
requireNotice( transfer.to, transfer.from );
requireAuth( transfer.from );
auto from = getAccount( transfer.from );
auto to = getAccount( transfer.to );
while (from.balance > infinite::CurrencyTokens())
{
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 infinite
using namespace infinite;
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) )
infinite::apply_currency_transfer( currentMessage< infinite::Transfer >() );
}
}
}
#include <eoslib/eos.hpp>
#include <eoslib/token.hpp>
#include <eoslib/db.hpp>
namespace infinite {
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 {
AccountName from;
AccountName to;
CurrencyTokens quantity;
};
/**
* @brief row in Account table stored within each scope
*/
struct Account {
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);
CurrencyTokens balance;
bool isEmpty()const { return balance.quantity == 0; }
};
static_assert( sizeof(Account) == sizeof(uint64_t)+sizeof(CurrencyTokens), "unexpected packing" );
using Accounts = Table<N(currency),N(currency),N(account),Account,uint64_t>;
/**
* Accounts information for owner is stored:
*
* owner/infinite/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.
*/
inline Account getAccount( AccountName owner ) {
Account account;
/// scope, record
Accounts::get( account, owner );
return account;
}
} /// namespace infinite
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册