Skip to content

Commit

Permalink
Merge pull request #96 from AntelopeIO/merge-savanna-tokenonmics
Browse files Browse the repository at this point in the history
[3.4 -> Main] Merge Tokenonmics Updates into Main
  • Loading branch information
arhag authored Aug 12, 2024
2 parents 88f2eed + 3282ef6 commit 7718066
Show file tree
Hide file tree
Showing 31 changed files with 1,746 additions and 288 deletions.
1 change: 1 addition & 0 deletions .cspell/custom-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ setacctram
setalimits
setcode
setinflation
setpayfactor
setparams
setpriv
setram
Expand Down
2 changes: 2 additions & 0 deletions contracts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,7 @@ add_subdirectory(eosio.msig)
add_subdirectory(eosio.system)
add_subdirectory(eosio.token)
add_subdirectory(eosio.wrap)
add_subdirectory(eosio.fees)
add_subdirectory(eosio.bpay)

add_subdirectory(test_contracts)
14 changes: 14 additions & 0 deletions contracts/eosio.bpay/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
add_contract(eosio.bpay eosio.bpay ${CMAKE_CURRENT_SOURCE_DIR}/src/eosio.bpay.cpp)

target_include_directories(eosio.bpay PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/../eosio.system/include
${CMAKE_CURRENT_SOURCE_DIR}/../eosio.token/include)

set_target_properties(eosio.bpay
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")

configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/ricardian/eosio.bpay.contracts.md.in ${CMAKE_CURRENT_BINARY_DIR}/ricardian/eosio.bpay.contracts.md @ONLY )

target_compile_options( eosio.bpay PUBLIC -R${CMAKE_CURRENT_SOURCE_DIR}/ricardian -R${CMAKE_CURRENT_BINARY_DIR}/ricardian )
55 changes: 55 additions & 0 deletions contracts/eosio.bpay/include/eosio.bpay/eosio.bpay.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

#include <eosio/eosio.hpp>
#include <eosio.system/eosio.system.hpp>
#include <eosio.token/eosio.token.hpp>

using namespace std;

namespace eosio {
/**
* The `eosio.bpay` contract handles system bpay distribution.
*/
class [[eosio::contract("eosio.bpay")]] bpay : public contract {
public:
using contract::contract;

/**
* ## TABLE `rewards`
*
* @param owner - block producer owner account
* @param quantity - reward quantity in EOS
*
* ### example
*
* ```json
* [
* {
* "owner": "alice",
* "quantity": "8.800 EOS"
* }
* ]
* ```
*/
struct [[eosio::table("rewards")]] rewards_row {
name owner;
asset quantity;

uint64_t primary_key() const { return owner.value; }
};
typedef eosio::multi_index< "rewards"_n, rewards_row > rewards_table;

/**
* Claim rewards for a block producer.
*
* @param owner - block producer owner account
*/
[[eosio::action]]
void claimrewards( const name owner);

[[eosio::on_notify("eosio.token::transfer")]]
void on_transfer( const name from, const name to, const asset quantity, const string memo );

private:
};
} /// namespace eosio
10 changes: 10 additions & 0 deletions contracts/eosio.bpay/ricardian/eosio.bpay.contracts.md.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1 class="contract">claimrewards</h1>

---
spec_version: "0.2.0"
title: Claim Rewards
summary: '{{nowrap owner}} claims block production rewards'
icon: @ICON_BASE_URL@/@MULTISIG_ICON_URI@
---

{{owner}} claims block production rewards accumulated through network fees.
73 changes: 73 additions & 0 deletions contracts/eosio.bpay/src/eosio.bpay.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <eosio.bpay/eosio.bpay.hpp>

namespace eosio {

void bpay::claimrewards( const name owner ) {
require_auth( owner );

rewards_table _rewards( get_self(), get_self().value );

const auto& row = _rewards.get( owner.value, "no rewards to claim" );

eosio::token::transfer_action transfer( "eosio.token"_n, { get_self(), "active"_n });
transfer.send( get_self(), owner, row.quantity, "producer block pay" );

_rewards.erase(row);
}

void bpay::on_transfer( const name from, const name to, const asset quantity, const string memo ) {
if (from == get_self() || to != get_self()) {
return;
}

// ignore eosio system incoming transfers (caused by bpay income transfers eosio => eosio.bpay => producer)
if ( from == "eosio"_n) return;

symbol system_symbol = eosiosystem::system_contract::get_core_symbol();

check( quantity.symbol == system_symbol, "only core token allowed" );

rewards_table _rewards( get_self(), get_self().value );
eosiosystem::producers_table _producers( "eosio"_n, "eosio"_n.value );

eosiosystem::global_state_singleton _global("eosio"_n, "eosio"_n.value);
check( _global.exists(), "global state does not exist");
uint16_t producer_count = _global.get().last_producer_schedule_size;

// get producer with the most votes
// using `by_votes` secondary index
auto idx = _producers.get_index<"prototalvote"_n>();
auto prod = idx.begin();

// get top n producers by vote, excluding inactive
std::vector<name> top_producers;
while (true) {
if (prod == idx.end()) break;
if (prod->is_active == false) continue;

top_producers.push_back(prod->owner);

if (top_producers.size() == producer_count) break;

prod++;
}

asset reward = quantity / top_producers.size();

// distribute rewards to top producers
for (auto producer : top_producers) {
auto row = _rewards.find( producer.value );
if (row == _rewards.end()) {
_rewards.emplace( get_self(), [&](auto& row) {
row.owner = producer;
row.quantity = reward;
});
} else {
_rewards.modify(row, get_self(), [&](auto& row) {
row.quantity += reward;
});
}
}
}

} /// namespace eosio
11 changes: 11 additions & 0 deletions contracts/eosio.fees/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
add_contract(eosio.fees eosio.fees ${CMAKE_CURRENT_SOURCE_DIR}/src/eosio.fees.cpp)

target_include_directories(eosio.fees PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/../eosio.system/include)

set_target_properties(eosio.fees
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")

target_compile_options( eosio.fees PUBLIC )
35 changes: 35 additions & 0 deletions contracts/eosio.fees/include/eosio.fees/eosio.fees.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <eosio/asset.hpp>
#include <eosio/eosio.hpp>
#include <eosio.system/eosio.system.hpp>

#include <string>

namespace eosiosystem {
class system_contract;
}

namespace eosio {

using std::string;
/**
* The eosio.fees smart contract facilitates the collection of transaction fees from system accounts and their subsequent distribution to the Resource Exchange (REX) pool.
*
* This contract serves as an essential component for inclusion in system-level unit tests.
*
* A comprehensive implementation of the eosio.fees contract can be accessed at EOS Network Foundation GitHub repository.
* https://github.com/eosnetworkfoundation/eosio.fees
*/
class [[eosio::contract("eosio.fees")]] fees : public contract {
public:
using contract::contract;

[[eosio::on_notify("eosio.token::transfer")]]
void on_transfer( const name from, const name to, const asset quantity, const string memo );

[[eosio::action]]
void noop();
};

}
21 changes: 21 additions & 0 deletions contracts/eosio.fees/src/eosio.fees.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <eosio.fees/eosio.fees.hpp>

namespace eosio {

void fees::on_transfer( const name from, const name to, const asset quantity, const string memo )
{
if ( to != get_self() ) {
return;
}
if (eosiosystem::system_contract::rex_available()) {
eosiosystem::system_contract::donatetorex_action donatetorex( "eosio"_n, { get_self(), "active"_n });
donatetorex.send(get_self(), quantity, memo);
}
}

void fees::noop()
{
require_auth( get_self() );
}

} /// namespace eosio
Loading

0 comments on commit 7718066

Please sign in to comment.