Skip to content

Commit

Permalink
Merge pull request #62 from rdewilder/buyramburn
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere authored Apr 2, 2024
2 parents 550ef3e + 267f784 commit d8a72a2
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
12 changes: 12 additions & 0 deletions contracts/eosio.system/include/eosio.system/eosio.system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,17 @@ namespace eosiosystem {
[[eosio::action]]
action_return_ramtransfer ramburn( const name& owner, int64_t bytes, const std::string& memo );

/**
* Buy RAM and immediately burn RAM.
* An inline transfer from payer to system contract of tokens will be executed.
*
* @param payer - the payer of buy RAM & burn.
* @param quantity - the quantity of tokens to buy RAM & burn with.
* @param memo - the memo string to accompany the transaction.
*/
[[eosio::action]]
action_return_buyram buyramburn( const name& payer, const asset& quantity, const std::string& memo );

/**
* Logging for ram changes
*
Expand Down Expand Up @@ -1522,6 +1533,7 @@ namespace eosiosystem {
using logsellram_action = eosio::action_wrapper<"logsellram"_n, &system_contract::logsellram>;
using ramtransfer_action = eosio::action_wrapper<"ramtransfer"_n, &system_contract::ramtransfer>;
using ramburn_action = eosio::action_wrapper<"ramburn"_n, &system_contract::ramburn>;
using buyramburn_action = eosio::action_wrapper<"buyramburn"_n, &system_contract::buyramburn>;
using logramchange_action = eosio::action_wrapper<"logramchange"_n, &system_contract::logramchange>;
using refund_action = eosio::action_wrapper<"refund"_n, &system_contract::refund>;
using regproducer_action = eosio::action_wrapper<"regproducer"_n, &system_contract::regproducer>;
Expand Down
15 changes: 15 additions & 0 deletions contracts/eosio.system/ricardian/eosio.system.contracts.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,21 @@ Burn {{bytes}} bytes of unused RAM from account {{owner}}.
{{memo}}
{{/if}}

<h1 class="contract">buyramburn</h1>

---
spec_version: "0.2.0"
title: Buy and Burn RAM
summary: 'Buy and immediately Burn {{quantity}} of RAM from {{nowrap payer}}'
icon: @ICON_BASE_URL@/@RESOURCE_ICON_URI@
---

Buy and Burn {{quantity}} of RAM from account {{payer}}.

{{#if memo}}There is a memo attached to the action stating:
{{memo}}
{{/if}}

<h1 class="contract">sellrex</h1>

---
Expand Down
14 changes: 14 additions & 0 deletions contracts/eosio.system/src/delegate_bandwidth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ namespace eosiosystem {
return ramtransfer( owner, null_account, bytes, memo );
}

/**
* This action will buy and then burn the purchased RAM bytes.
*/
action_return_buyram system_contract::buyramburn( const name& payer, const asset& quantity, const std::string& memo ) {
require_auth( payer );
check( quantity.symbol == core_symbol(), "quantity must be core token" );
check( quantity.amount > 0, "quantity must be positive" );

const auto return_buyram = buyram( payer, payer, quantity );
ramburn( payer, return_buyram.bytes_purchased, memo );

return return_buyram;
}

[[eosio::action]]
void system_contract::logramchange( const name& owner, int64_t bytes, int64_t ram_bytes )
{
Expand Down
25 changes: 25 additions & 0 deletions tests/eosio.system_ram_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,31 @@ BOOST_FIXTURE_TEST_CASE( ram_burn, eosio_system_tester ) try {

} FC_LOG_AND_RETHROW()

// buyramburn
BOOST_FIXTURE_TEST_CASE( buy_ram_burn, eosio_system_tester ) try {
const std::vector<account_name> accounts = { "alice"_n };
const account_name alice = accounts[0];
const account_name null_account = "eosio.null"_n;

create_accounts_with_resources( accounts );
transfer( config::system_account_name, alice, core_sym::from_string("100.0000"), config::system_account_name );

BOOST_REQUIRE_EQUAL( success(), buyrambytes( alice, alice, 10000 ) );
BOOST_REQUIRE_EQUAL( success(), buyrambytes( alice, null_account, 10000 ) );

const uint64_t null_before_buyramburn = get_total_stake( null_account )["ram_bytes"].as_uint64();
const uint64_t alice_before_buyramburn = get_total_stake( alice )["ram_bytes"].as_uint64();
const asset initial_alice_balance = get_balance(alice);
const asset ten_core_token = core_sym::from_string("10.0000");

// buy ram burn action
BOOST_REQUIRE_EQUAL( success(), buyramburn( alice, ten_core_token, "burn RAM burn memo" ) );
const uint64_t alice_after_buyramburn = get_total_stake( alice )["ram_bytes"].as_uint64();
const uint64_t null_after_buyramburn = get_total_stake( null_account )["ram_bytes"].as_uint64();
BOOST_REQUIRE_EQUAL( alice_before_buyramburn, alice_after_buyramburn );
BOOST_REQUIRE_EQUAL( true, null_before_buyramburn < null_after_buyramburn );
BOOST_REQUIRE_EQUAL( initial_alice_balance - ten_core_token, get_balance(alice));
} FC_LOG_AND_RETHROW()

// buyramself
BOOST_FIXTURE_TEST_CASE( buy_ram_self, eosio_system_tester ) try {
Expand Down
5 changes: 5 additions & 0 deletions tests/eosio.system_tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ class eosio_system_tester : public TESTER {
return ramburn(account_name(owner), bytes, memo);
}

action_result buyramburn( const name& payer, const asset& quantity, const std::string& memo)
{
return push_action(payer, "buyramburn"_n, mvo()("payer", payer)("quantity", quantity)("memo", memo));
}

void validate_ramburn_return(const account_name& owner, uint32_t bytes, const std::string& memo,
const type_name& type, const std::string& json) {
// create hex return from provided json
Expand Down

0 comments on commit d8a72a2

Please sign in to comment.