#include <eosiolib/eosio.hpp>
#include <eosiolib/asset.hpp>
#include <../eosio.token/eosio.token.hpp>
#include <string>
#include <vector>
using std::string;
using namespace eosio;

class eco : public eosio::contract {
    using contract::contract;
    public:
        eco(account_name self):
            contract(self),
            _animals(_self, _self), _pois(_self, _self), _pows(_self, _self){};

        // @abi action
        void createpoi(const string& ipfs, const account_name owner) {

            require_auth(owner);
            auto itr = _pois.find(owner);
            eosio_assert(itr == _pois.end(), "Existing owner found in table!");
            stakeEos(owner);

            _pois.emplace(owner, [&](auto& row) {
                row.owner = owner;
                row.ipfs = ipfs;
            });
        }

        // // @abi action
        // void staketheeos(const account_name owner1, const account_name owner2) {
        //     asset tok(10000, S(4,MYNA));
        //     action(
        //         permission_level{ owner1, N(active) },
        //         N(eco.token), N(transfer),
        //         std::make_tuple(owner1, owner2, tok, std::string(""))
        //     ).send();
        // }

        // @abi action
        void validatepoi(const account_name owner, const account_name validator) {
            
            checkTopTokenHolders(validator);
            require_auth(validator);
            auto itr = _pois.find(owner);
            eosio_assert(itr != _pois.end(), "Could not find Proof of ID index!");
            unstakeEos(owner, validator);

            // Consumer Rx +1 REP Token & Witness -1 REP Token
            asset tok(10000, S(4,REP));
            action(
                permission_level{ validator, N(active) },
                N(eco.token), N(transfer),
                std::make_tuple(validator, owner, tok, std::string(""))
            ).send();
        }

        // @abi action
        void createpow(const string& ipfs, const account_name owner, uint64_t number) {

            require_auth(owner);

            _pows.emplace(owner, [&](auto& row){
                 row.powId = _pows.available_primary_key();
                 row.owner = owner;
                 row.ipfs = ipfs;
                 row.number = number;
             });
        }

        // @abi action
        void validatepow(uint64_t powId, const account_name validator) {
            
            checkTopTokenHolders(validator);
            require_auth(validator);
            auto itr = _pows.find(powId);
            eosio_assert(itr != _pows.end(), "Could not find Proof of Work index!");

            uint64_t number = itr->number;
            number = number * (uint64_t)10000;

            // Consumer Rx +number <MYNA> Tokens
            asset tok(number, S(4,MYNA));
            action(
                permission_level{ validator, N(active) },
                N(eco.token), N(transfer),
                std::make_tuple(validator, itr->owner, tok, std::string(""))
            ).send();

            // Consumer Rx +1 REP Token & Witness -1 REP Token
            asset tok2(10000, S(4,REP));
            action(
                permission_level{ validator, N(active) },
                N(eco.token), N(transfer),
                std::make_tuple(validator, itr->owner, tok2, std::string(""))
            ).send();
        }

        void stakeEos( const account_name function_caller ) {
        
            asset tok(200000, S(4,EOS));
            // eosio::asset quantity(20, S(4,EOS));
            require_auth(function_caller);

            eosio_assert( tok.is_valid(), "Invalid QUANTITY!" );
            eosio_assert( tok.amount > 0, "Not enough EOS Tokens!" );

            action(
                permission_level{ function_caller, N(active) },
                N(eco.token), N(transfer),
                std::make_tuple(function_caller, _self, tok, std::string(""))
            ).send();
        }

        void unstakeEos( const account_name owner, const account_name function_caller ) {
        
            asset tok(200000, S(4,EOS));
            require_auth(function_caller);

            eosio_assert( tok.is_valid(), "Invalid QUANTITY!" );
            eosio_assert( tok.amount > 0, "Not enough EOS Tokens!" );

            action(
                permission_level{ _self, N(active) },
                N(eco.token), N(transfer),
                std::make_tuple(_self, owner, tok, std::string(""))
            ).send();
        }

        void checkTopTokenHolders(const account_name tokenHolder) {

            const asset token_balance = token(N(eco.token)).get_balance(tokenHolder, symbol_type(S(4,REP)).name());
            eosio_assert(token_balance.amount > 0, "Not a Top Token Holder");
        }

    private:

        // @abi table animal i64
        struct animal{
            uint64_t animalId;
            string description;
            auto primary_key() const { return animalId; }
            EOSLIB_SERIALIZE( animal, (animalId)(description) )            
        };
        typedef eosio::multi_index<N(animal), animal> animal_table;    

        // @abi table poi i64
        struct poi{
            account_name owner;
            string ipfs;
            auto primary_key() const { return owner; }
            EOSLIB_SERIALIZE( poi, (owner)(ipfs) )
        };
        typedef eosio::multi_index<N(poi), poi> poi_table;

        // @abi table pow i64
        struct pow{
            uint64_t powId;
            account_name owner;
            string ipfs;
            uint64_t number;
            auto primary_key() const { return powId; }
            EOSLIB_SERIALIZE( pow, (powId)(owner)(ipfs)(number) )
        };
        typedef eosio::multi_index<N(pow), pow> pow_table;
        
        poi_table _pois; 
        pow_table _pows;
        animal_table _animals;
};    

EOSIO_ABI( eco, (createpoi) (validatepoi) (createpow) (validatepow) )

  • EOS

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

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

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

    20 引用 • 9 回帖
  • IPFS
    1 引用
感谢    赞同    分享    收藏    关注    反对    举报    ...