提交 e6a5ee2c 编写于 作者: D Daniel Larimer

Progress on currency contract

上级 48e0889f
#pragma once
#include <eoslib/singleton.hpp>
#include <eoslib/table.hpp>
#include <eoslib/token.hpp>
#include <eoslib/asset.hpp>
#include <eoslib/dispatcher.hpp>
......@@ -72,28 +73,30 @@ namespace eosio {
};
struct account {
uint64_t symbol = token_type::symbol;
token_type balance;
template<typename DataStream>
friend DataStream& operator << ( DataStream& ds, const account& t ){
return ds << t.balance;
return ds << t.symbol << t.balance;
}
template<typename DataStream>
friend DataStream& operator >> ( DataStream& ds, account& t ){
return ds >> t.balance;
return ds >> t.symbol >> t.balance;
}
};
struct currency_stats {
uint64_t symbol = token_type::symbol;
token_type supply;
template<typename DataStream>
friend DataStream& operator << ( DataStream& ds, const currency_stats& t ){
return ds << t.supply;
return ds << t.symbol << t.supply;
}
template<typename DataStream>
friend DataStream& operator >> ( DataStream& ds, currency_stats& t ){
return ds >> t.supply;
return ds >> t.symbol >> t.supply;
}
};
......@@ -101,22 +104,21 @@ namespace eosio {
* Each user stores their balance in the singleton table under the
* scope of their account name.
*/
typedef singleton<code, accounts_table_name, account> accounts;
typedef singleton<code, stats_table_name, currency_stats> stats;
typedef table64<code, accounts_table_name, account> accounts;
typedef table64<code, stats_table_name, currency_stats> stats;
static token_type get_balance( account_name owner ) {
return accounts::get_or_create( owner ).balance;
return accounts::get_or_create( token_type::symbol, owner ).balance;
}
static void set_balance( account_name owner, token_type balance ) {
accounts::set( account{balance}, owner );
accounts::set( account{token_type::symbol,balance}, owner );
}
static void on( const issue& act ) {
require_auth( code );
auto s = stats::get_or_create();
auto s = stats::get_or_create(token_type::symbol);
s.supply += act.quantity;
stats::set(s);
......
......@@ -15,18 +15,18 @@ namespace eosio {
class singleton
{
public:
static const uint64_t singleton_table_name = N(singleton);
//static const uint64_t singleton_table_name = N(singleton);
static bool exists( scope_name scope = Code ) {
uint64_t key = SingletonName;
auto read = load_i64( scope, Code, singleton_table_name, (char*)&key, sizeof(key) );
auto read = load_i64( scope, Code, key, (char*)&key, sizeof(key) );
return read > 0;
}
static T get( scope_name scope = Code ) {
char temp[1024+8];
*reinterpret_cast<uint64_t *>(temp) = SingletonName;
auto read = load_i64( scope, Code, singleton_table_name, temp, sizeof(temp) );
auto read = load_i64( scope, Code, SingletonName, temp, sizeof(temp) );
assert( read > 0, "singleton does not exist" );
datastream<const char*> ds(temp + sizeof(SingletonName), read);
......@@ -40,7 +40,7 @@ namespace eosio {
*reinterpret_cast<uint64_t *>(temp) = SingletonName;
auto read = load_i64( scope, Code, singleton_table_name, temp, sizeof(temp) );
auto read = load_i64( scope, Code, SingletonName, temp, sizeof(temp) );
if( read < 0 ) {
set( def, scope );
return def;
......@@ -62,7 +62,7 @@ namespace eosio {
ds << SingletonName;
ds << value;
store_i64( scope, singleton_table_name, buf, sizeof(buf) );
store_i64( scope, SingletonName, buf, sizeof(buf) );
}
};
......
......@@ -85,6 +85,7 @@ void apply_context::exec_one()
}
}
std::cerr << _pending_console_output.str();
results.applied_actions.emplace_back(action_trace {receiver, act, _pending_console_output.str(), 0, 0, move(data_access)});
_pending_console_output = std::ostringstream();
_read_locks.clear();
......
......@@ -56,6 +56,10 @@ namespace eosio { namespace testing {
share_type get_balance( const account_name& account ) const;
share_type get_currency_balance( account_name contract,
uint64_t symbol,
const account_name& account ) const;
private:
fc::temp_directory tempdir;
chain_controller::controller_config cfg;
......
......@@ -247,5 +247,23 @@ namespace eosio { namespace testing {
return contracts::get_eosio_balance(db, account);
}
/**
* Reads balance as stored by generic_currency contract
*/
share_type tester::get_currency_balance( account_name code,
uint64_t symbol,
const account_name& account ) const {
/*
const auto& db = control->get_database();
const auto& tbl = db.get<table_id_object, contracts::by_scope_code_table>(boost::make_tuple(account, code, N(account)));
const auto& obj = db.get<key_value_object, contracts::by_scope_primary>( boost::make_tuple( tbl.id, symbol ) );
datastream<const char*> ds( obj.value.data(), obj.value.size() );
share_type result;
fc::raw::unpack( ds, result );
*/
return 0;//result;
}
} } /// eosio::test
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册