Skip to content

Commit

Permalink
Implement buyramself action
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Jan 25, 2024
1 parent 83f4a1e commit 9dd2717
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
11 changes: 11 additions & 0 deletions contracts/eosio.system/include/eosio.system/eosio.system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,17 @@ namespace eosiosystem {
[[eosio::action]]
void buyrambytes( const name& payer, const name& receiver, uint32_t bytes );

/**
* The buyramself action is designed to enhance the permission security by allowing an account to purchase RAM exclusively for itself.
* This action prevents the potential risk associated with standard actions like buyram and buyrambytes,
* which can transfer EOS tokens out of the account, acting as a proxy for eosio.token::transfer.
*
* @param account - the ram buyer and receiver,
* @param quant - the quantity of tokens to buy ram with.
*/
[[eosio::action]]
void buyramself( const name& account, const asset& quant );

/**
* Logging for buyram & buyrambytes action
*
Expand Down
11 changes: 11 additions & 0 deletions contracts/eosio.system/ricardian/eosio.system.contracts.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ icon: @ICON_BASE_URL@/@RESOURCE_ICON_URI@

{{payer}} buys RAM on behalf of {{receiver}} by paying {{quant}}. This transaction will incur a 0.5% fee out of {{quant}} and the amount of RAM delivered will depend on market rates.

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

---
spec_version: "0.2.0"
title: Buy RAM self
summary: '{{nowrap account}} buys RAM to self by paying {{nowrap quant}}'
icon: @ICON_BASE_URL@/@RESOURCE_ICON_URI@
---

{{account}} buys RAM to self by paying {{quant}}. This transaction will incur a 0.5% fee out of {{quant}} and the amount of RAM delivered will depend on market rates.

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

---
Expand Down
6 changes: 6 additions & 0 deletions contracts/eosio.system/src/delegate_bandwidth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ namespace eosiosystem {
buyram( payer, receiver, asset{ cost_plus_fee, core_symbol() } );
}

/**
* Buy self ram action, ram can only be purchased to itself.
*/
void system_contract::buyramself( const name& account, const asset& quant ) {
buyram( account, account, quant );
}

/**
* When buying ram the payer irreversibly transfers quant to system contract and only
Expand Down
16 changes: 16 additions & 0 deletions tests/eosio.system_ram_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,20 @@ BOOST_FIXTURE_TEST_CASE( ram_burn, eosio_system_tester ) try {

} FC_LOG_AND_RETHROW()


// buyramself
BOOST_FIXTURE_TEST_CASE( buy_ram_self, eosio_system_tester ) try {
const std::vector<account_name> accounts = { "alice"_n };
create_accounts_with_resources( accounts );
const account_name alice = accounts[0];

transfer( config::system_account_name, alice, core_sym::from_string("100.0000"), config::system_account_name );
const uint64_t alice_before = get_total_stake( alice )["ram_bytes"].as_uint64();
BOOST_REQUIRE_EQUAL( success(), buyramself( alice, core_sym::from_string("1.0000")) );
const uint64_t alice_after = get_total_stake( alice )["ram_bytes"].as_uint64();
BOOST_REQUIRE_EQUAL( alice_before + 68375, alice_after );

} FC_LOG_AND_RETHROW()


BOOST_AUTO_TEST_SUITE_END()
4 changes: 4 additions & 0 deletions tests/eosio.system_tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ class eosio_system_tester : public TESTER {
return sellram( account_name(account), numbytes );
}

action_result buyramself( const account_name& account, const asset& quant ) {
return push_action( account, "buyramself"_n, mvo()( "account",account)("quant",quant) );
}

action_result push_action( const account_name& signer, const action_name &name, const variant_object &data, bool auth = true ) {
string action_type_name = abi_ser.get_action_type(name);

Expand Down

0 comments on commit 9dd2717

Please sign in to comment.