#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
#include <eosiolib/asset.hpp>
#include <eosiolib/types.hpp>
#include "company.hpp"
#include "../eosio.company/eosio.company.hpp"
#include "../eosio.contracts/eosio.token/include/eosio.token/eosio.token.hpp"

using namespace eosio;



class company : public eosio::contract {
    using contract::contract;
    typedef eosio::multi_index<N(accounts), account> accounts;

public:
    company( account_name self ) :
        contract(self),
        _cmeta(_self, _self),
        _cusers(_self, _self),
        _cmedia(_self, _self),
        _companies(N(eosio.ccomp), N(eosio.ccomp)),
        _investors(N(eosio.ccomp), N(eosio.ccomp)){}

    [[eosio::action]]
    void init(uint64_t c_pk) {
        auto cmt = _cmeta.find(_self);
        bool c_exist_in_db = (cmt != _cmeta.end());
        eosio_assert(!c_exist_in_db, "company alredy initialised");
        auto cmp = _companies.find(c_pk);
        companies cmpt = *cmp;
        _cmeta.emplace( _self, [&]( auto& row ){
            row.c_aname = _self;
            row.c_name = cmpt.c_meta.c_name;
            row.c_desc = cmpt.c_meta.c_desc;
            row.c_tag = cmpt.c_meta.c_tag;
            row.c_media = cmpt.c_meta.c_media;
            row.c_contact = cmpt.c_meta.c_contact;
            row.c_industry = cmpt.c_meta.c_industry;
        });
        initmedia();
        syncusers();
    }

    [[eosio::action]]
    void changemeta(account_name investor, cmeta cmeta) {
        require_token_auth(investor);
        auto cmt = _cmeta.find(_self);
        bool mt_exist_in_db = (cmt != _cmeta.end());
        eosio_assert(mt_exist_in_db, "coudn't find meta");
        _cmeta.modify( cmt, 0, [&]( auto& row ){
            row.c_name = cmeta.c_name;
            row.c_desc = cmeta.c_desc;
            row.c_tag = cmeta.c_tag;
            row.c_media = cmeta.c_media;
            row.c_logo = cmeta.c_logo;
            row.c_contact = cmeta.c_contact;
            row.c_industry = cmeta.c_industry;
        });
    }

    [[eosio::action]]
    void addmedia(account_name investor, media cmedia) {

        require_token_auth(investor);
        _cmedia.emplace( _self, [&]( auto& row ){
            row.cm_pk = _cmedia.available_primary_key();
            row.cm_type = cmedia.cm_type;
            row.cm_media = cmedia.cm_media;
            row.cm_mpublished = cmedia.cm_mpublished;
        });
    }

    [[eosio::action]]
    void modifymedia(account_name investor, uint64_t cm_pk, media cmedia) {

        require_token_auth(investor);
        auto cmd = _cmedia.find(cm_pk);
        bool md_exist_in_db = (cmd != _cmedia.end());
        eosio_assert(md_exist_in_db, "coudn't find media");
        _cmedia.modify( cmd, 0, [&]( auto& row ){
            row.cm_type = cmedia.cm_type;
            row.cm_media = cmedia.cm_media;
            row.cm_mpublished = cmedia.cm_mpublished;
        });
    }


    [[eosio::action]]
    void syncusers() {

        for(auto itr = _cusers.begin(); itr != _cusers.end();) {
            itr = _cusers.erase(itr);
        }
        for (auto inv : _investors) {
            if(inv.i_caname == _self) {
                auto iter = _cusers.find(inv.i_paname);
                bool exist_in_db = (iter != _cusers.end());
                if(!exist_in_db) {
                    _cusers.emplace( _self, [&]( auto& row ){
                        row.cu_paname = inv.i_paname;
                        row.cu_role = "owner";
                    });
                }
            }
        }
    }

    [[eosio::action]]
    void transfer(account_name from,
                  account_name to,
                  asset quantity,
                  std::string memo
    ) {
        if(from == N(xdaccrcmpany))
        {

        }
    }
private:
    void initmedia() {
        _cmedia.emplace( _self, [&]( auto& row ){
            row.cm_pk = _cmedia.available_primary_key();
            row.cm_type = "whitepaper";
        });
        _cmedia.emplace( _self, [&]( auto& row ){
            row.cm_pk = _cmedia.available_primary_key();
            row.cm_type = "onepager";
        });
        _cmedia.emplace( _self, [&]( auto& row ){
            row.cm_pk = _cmedia.available_primary_key();
            row.cm_type = "video";
        });
        _cmedia.emplace( _self, [&]( auto& row ){
            row.cm_pk = _cmedia.available_primary_key();
            row.cm_type = "url";
        });
        _cmedia.emplace( _self, [&]( auto& row ){
            row.cm_pk = _cmedia.available_primary_key();
            row.cm_type = "email";
        });
        _cmedia.emplace( _self, [&]( auto& row ){
            row.cm_pk = _cmedia.available_primary_key();
            row.cm_type = "facebook";
        });
        _cmedia.emplace( _self, [&]( auto& row ){
            row.cm_pk = _cmedia.available_primary_key();
            row.cm_type = "twitter";
        });
        _cmedia.emplace( _self, [&]( auto& row ){
            row.cm_pk = _cmedia.available_primary_key();
            row.cm_type = "reddit";
        });
        _cmedia.emplace( _self, [&]( auto& row ){
            row.cm_pk = _cmedia.available_primary_key();
            row.cm_type = "github";
        });
        _cmedia.emplace( _self, [&]( auto& row ){
            row.cm_pk = _cmedia.available_primary_key();
            row.cm_type = "telegram";
        });
        _cmedia.emplace( _self, [&]( auto& row ){
            row.cm_pk = _cmedia.available_primary_key();
            row.cm_type = "bitcointalk";
        });
    }
    void require_token_auth(account_name investor) {
        require_auth(investor);
        eosio::token t(N(eosio.token));

        auto cmp = _companies.find(_self);
        bool exist_in_db = (cmp != _companies.end());

        eosio_assert(exist_in_db, "coudn't find company");
        companies cmpt = *cmp;

        accounts accountstable( N(eosio.token), investor );
        bool investor_exist = false;
        const auto& ac = accountstable.find( cmpt.c_token.t_tsupply.symbol.name()  );
        if((ac != accountstable.end())) {
            if(ac->balance.amount>=0) {
                investor_exist = true;
            }
        }
        if(!investor_exist) {
            for (auto inv : _investors) {
                if (inv.i_caname == _self && inv.i_paname == investor && !inv.i_issued ) {
                    investor_exist = true;
                }
            }
        }

        eosio_assert(investor_exist, "user should own company token");
    }

    multi_index<N(companies), companies> _companies;
    multi_index<N(investors), investors> _investors;
    multi_index<N(cmeta), cmeta> _cmeta;
    multi_index<N(cmedia), cmedia> _cmedia;
    multi_index<N(cusers), cusers> _cusers;

};


#define EOSIO_ABI_EX( TYPE, MEMBERS ) \
extern "C" { \
   void apply( uint64_t receiver, uint64_t code, uint64_t action ) { \
      auto self = receiver; \
      if( action == N(onerror)) { \
         /* onerror is only valid if it is for the "eosio" code account and authorized by "eosio"'s "active permission */ \
         eosio_assert(code == N(eosio), "onerror action's are only valid from the \"eosio\" system account"); \
      } \
      if( code == self || code == N(eosio.token) || action == N(onerror) ) { \
         TYPE thiscontract( self ); \
         switch( action ) { \
            EOSIO_API( TYPE, MEMBERS ) \
         } \
         /* does not allow destructor of thiscontract to run: eosio_exit(0); */ \
      } \
   } \
}



EOSIO_ABI_EX(company, (init)(transfer)(syncusers)(changemeta)(modifymedia)(addmedia))

//EOSIO_ABI( company, (setinfo)(addinfo)(transfer)(approvetrns)(refundtrns)(initowner) )

  • EOS

    第三代区块链到来了?赶紧学习 EOS 智能合约吧

    3 引用 • 3 回帖
  • 智能合约

    智能合约(英语:Smart contract )是一种旨在以信息化方式传播、验证或执行合同的计算机协议。

    20 引用 • 9 回帖
  • 企业管理
    1 引用
  • 去中心化
    1 引用
感谢    赞同    分享    收藏    关注    反对    举报    ...