-
Notifications
You must be signed in to change notification settings - Fork 25
Vaulta transition #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Vaulta transition #208
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9c988bb
require usage of core.vaulta for stake, unstake, and claimrewards
1160417
require auth for mock contract
b7dd280
swap to vaulta when calling eosio.bpay::claimrewards
763d57c
Change TST to EOS and ensure all tests use core_sym::from_string
188aea5
vaulta account name variable, vaulta readme docs
2884a97
change XYZ to
6e96374
use action wrappers, and replace TST instead of concat
2312462
Merge branch 'main' into vaulta-transition
nsjames File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,4 @@ | |
build/* | ||
.DS_Store | ||
.vscode | ||
.idea |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
add_contract(core.vaulta core.vaulta ${CMAKE_CURRENT_SOURCE_DIR}/src/core.vaulta.cpp) | ||
|
||
target_include_directories(core.vaulta | ||
PUBLIC | ||
${CMAKE_CURRENT_SOURCE_DIR}/include) | ||
|
||
set_target_properties(core.vaulta | ||
PROPERTIES | ||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") | ||
|
||
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/ricardian/core.vaulta.contracts.md.in ${CMAKE_CURRENT_BINARY_DIR}/ricardian/core.vaulta.contracts.md @ONLY ) | ||
|
||
target_compile_options( core.vaulta PUBLIC -R${CMAKE_CURRENT_SOURCE_DIR}/ricardian -R${CMAKE_CURRENT_BINARY_DIR}/ricardian ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#pragma once | ||
|
||
#include <eosio/eosio.hpp> | ||
#include <eosio/asset.hpp> | ||
#include <eosio/singleton.hpp> | ||
|
||
namespace eosio { | ||
class [[eosio::contract("core.vaulta")]] core : public contract { | ||
public: | ||
using contract::contract; | ||
using name = eosio::name; | ||
using asset = eosio::asset; | ||
using symbol = eosio::symbol; | ||
static constexpr symbol EOS = symbol("EOS", 4); | ||
|
||
struct [[eosio::table("accounts"), eosio::contract("core.vaulta")]] account { | ||
asset balance; | ||
bool released = false; | ||
uint64_t primary_key()const { return balance.symbol.code().raw(); } | ||
}; | ||
|
||
struct [[eosio::table("stat"), eosio::contract("core.vaulta")]] currency_stats { | ||
asset supply; | ||
asset max_supply; | ||
name issuer; | ||
|
||
uint64_t primary_key()const { return supply.symbol.code().raw(); } | ||
}; | ||
|
||
typedef eosio::multi_index< "accounts"_n, account > accounts; | ||
typedef eosio::multi_index< "stat"_n, currency_stats > stats; | ||
|
||
struct [[eosio::table]] config { | ||
symbol token_symbol; | ||
}; | ||
|
||
typedef eosio::singleton<"config"_n, config> config_table; | ||
|
||
|
||
[[eosio::action]] void init(asset maximum_supply); | ||
[[eosio::action]] void transfer(const name& from, const name& to, const asset& quantity, const std::string& memo); | ||
[[eosio::on_notify("eosio.token::transfer")]] | ||
void on_transfer(const name& from, const name& to, const asset& quantity, const std::string& memo); | ||
|
||
[[eosio::action]] void deposit(const name& owner, const asset& amount); | ||
[[eosio::action]] void withdraw(const name& owner, const asset& amount); | ||
[[eosio::action]] void unstaketorex(const name& owner, const name& receiver, const asset& from_net, const asset& from_cpu); | ||
[[eosio::action]] void claimrewards(const name owner); | ||
|
||
using init_action = eosio::action_wrapper<"init"_n, &core::init>; | ||
using transfer_action = eosio::action_wrapper<"transfer"_n, &core::transfer>; | ||
using deposit_action = eosio::action_wrapper<"deposit"_n, &core::deposit>; | ||
using withdraw_action = eosio::action_wrapper<"withdraw"_n, &core::withdraw>; | ||
using unstaketorex_action = eosio::action_wrapper<"unstaketorex"_n, &core::unstaketorex>; | ||
using claimrewards_action = eosio::action_wrapper<"claimrewards"_n, &core::claimrewards>; | ||
|
||
private: | ||
void add_balance(const name& owner, const asset& value, const name& ram_payer); | ||
void sub_balance(const name& owner, const asset& value); | ||
symbol get_token_symbol(); | ||
void credit_eos_to(const name& account, const asset& quantity); | ||
}; | ||
} /// namespace eosio |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
#include <core.vaulta/core.vaulta.hpp> | ||
|
||
namespace eosio { | ||
|
||
void core::init(asset maximum_supply) { | ||
require_auth(get_self()); | ||
config_table _config(get_self(), get_self().value); | ||
check(!_config.exists(), "This system contract is already initialized"); | ||
|
||
auto sym = maximum_supply.symbol; | ||
check(maximum_supply.is_valid(), "invalid supply"); | ||
check(maximum_supply.amount > 0, "max-supply must be positive"); | ||
|
||
_config.set(config{.token_symbol = sym}, get_self()); | ||
|
||
stats statstable(get_self(), sym.code().raw()); | ||
statstable.emplace(get_self(), [&](auto& s) { | ||
s.supply = maximum_supply; | ||
s.max_supply = maximum_supply; | ||
s.issuer = get_self(); | ||
}); | ||
|
||
add_balance(get_self(), maximum_supply, get_self()); | ||
} | ||
|
||
void core::transfer(const name& from, const name& to, const asset& quantity, const std::string& memo) { | ||
check(from != to, "cannot transfer to self"); | ||
require_auth(from); | ||
check(is_account(to), "to account does not exist"); | ||
|
||
auto sym = quantity.symbol.code(); | ||
stats statstable(get_self(), sym.raw()); | ||
const auto& st = statstable.get(sym.raw()); | ||
|
||
check(quantity.is_valid(), "invalid quantity"); | ||
check(quantity.amount > 0, "must transfer positive quantity"); | ||
check(quantity.symbol == st.supply.symbol, "symbol precision mismatch"); | ||
check(memo.size() <= 256, "memo has more than 256 bytes"); | ||
|
||
auto payer = has_auth(to) ? to : from; | ||
|
||
sub_balance(from, quantity); | ||
add_balance(to, quantity, payer); | ||
|
||
require_recipient(from); | ||
require_recipient(to); | ||
|
||
// If `from` is sending $A tokens to this contract | ||
// they are swapping from $A to EOS | ||
if (to == get_self()) { | ||
check(quantity.symbol == get_token_symbol(), "Wrong token used"); | ||
credit_eos_to(from, quantity); | ||
} | ||
} | ||
|
||
void core::add_balance(const name& owner, const asset& value, const name& ram_payer) { | ||
accounts to_acnts(get_self(), owner.value); | ||
auto to = to_acnts.find(value.symbol.code().raw()); | ||
if (to == to_acnts.end()) { | ||
to_acnts.emplace(ram_payer == owner ? owner : get_self(), [&](auto& a) { | ||
a.balance = value; | ||
a.released = ram_payer == owner; | ||
}); | ||
} else { | ||
to_acnts.modify(to, same_payer, [&](auto& a) { a.balance += value; }); | ||
} | ||
} | ||
|
||
void core::sub_balance(const name& owner, const asset& value) { | ||
accounts from_acnts(get_self(), owner.value); | ||
|
||
const auto& from = from_acnts.get(value.symbol.code().raw(), "no balance object found"); | ||
check(from.balance.amount >= value.amount, "overdrawn balance"); | ||
|
||
if(!from.released){ | ||
auto balance = from.balance; | ||
// This clears out the RAM consumed by the scope overhead. | ||
from_acnts.erase( from ); | ||
from_acnts.emplace( owner, [&]( auto& a ){ | ||
a.balance = balance - value; | ||
a.released = true; | ||
}); | ||
} else { | ||
from_acnts.modify( from, owner, [&]( auto& a ) { | ||
a.balance -= value; | ||
}); | ||
} | ||
} | ||
|
||
void core::on_transfer(const name& from, const name& to, const asset& quantity, const std::string& memo) { | ||
if (from == get_self() || to != get_self()) | ||
return; | ||
check(quantity.amount > 0, "Swap amount must be greater than 0"); | ||
|
||
// Ignore for system accounts, otherwise when unstaking or selling ram this will swap EOS for | ||
// $A and credit them to the sending account which will lock those tokens. | ||
if (from == "eosio.ram"_n) | ||
return; | ||
if (from == "eosio.stake"_n) | ||
return; | ||
|
||
check(quantity.symbol == EOS, "Invalid symbol"); | ||
asset swap_amount = asset(quantity.amount, get_token_symbol()); | ||
transfer_action(get_self(), {{get_self(), "active"_n}}).send(get_self(), from, swap_amount, std::string("")); | ||
} | ||
|
||
symbol core::get_token_symbol() { | ||
config_table _config(get_self(), get_self().value); | ||
check(_config.exists(), "Contract is not initialized"); | ||
config cfg = _config.get(); | ||
return cfg.token_symbol; | ||
} | ||
greg7mdp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
void core::credit_eos_to(const name& account, const asset& quantity) { | ||
check(quantity.amount > 0, "Credit amount must be greater than 0"); | ||
|
||
asset swap_amount = asset(quantity.amount, EOS); | ||
transfer_action("eosio.token"_n, {{get_self(), "active"_n}}).send(get_self(), account, swap_amount, std::string("")); | ||
} | ||
|
||
void core::deposit(const name& owner, const asset& amount) { | ||
require_auth(owner); | ||
action( | ||
permission_level{ owner, "active"_n }, | ||
"eosio"_n, | ||
"deposit"_n, | ||
std::make_tuple(owner, amount) | ||
).send(); | ||
} | ||
|
||
void core::withdraw(const name& owner, const asset& amount) { | ||
require_auth(owner); | ||
action( | ||
permission_level{ owner, "active"_n }, | ||
"eosio"_n, | ||
"withdraw"_n, | ||
std::make_tuple(owner, amount) | ||
).send(); | ||
} | ||
|
||
void core::unstaketorex(const name& owner, const name& receiver, const asset& from_net, const asset& from_cpu) { | ||
require_auth(owner); | ||
action( | ||
permission_level{ owner, "active"_n }, | ||
"eosio"_n, | ||
"unstaketorex"_n, | ||
std::make_tuple(owner, receiver, from_net, from_cpu) | ||
).send(); | ||
} | ||
|
||
void core::claimrewards(const name owner) { | ||
require_auth(owner); | ||
action( | ||
permission_level{ owner, "active"_n }, | ||
"eosio"_n, | ||
"claimrewards"_n, | ||
std::make_tuple(owner) | ||
).send(); | ||
} | ||
greg7mdp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
} /// namespace eosio |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.