Skip to content

Commit

Permalink
Merge pull request #4 from Alien-Worlds/add-repair-to-wax-contract
Browse files Browse the repository at this point in the history
Add `repairrec` action to wax contract
  • Loading branch information
dallasjohnson authored Feb 10, 2022
2 parents 2fa4cbd + 91b5a0a commit 383ecc2
Show file tree
Hide file tree
Showing 22 changed files with 4,696 additions and 437 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
oracle/node_modules
config.js
contracts/teleporteos/node_modules
teleporteos.ts
eosio.token.ts
contracts/teleporteos/artifacts
5 changes: 5 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# .prettierrc or .prettierrc.yaml
trailingComma: 'es5'
tabWidth: 2
semi: true
singleQuote: true
16 changes: 16 additions & 0 deletions contracts/teleporteos/.lamingtonrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"cdt": "https://github.com/EOSIO/eosio.cdt/releases/download/v1.8.1/eosio.cdt_1.8.1-1-ubuntu-18.04_amd64.deb",
"eos": "https://github.com/EOSIO/eos/releases/download/v2.0.13/eosio_2.0.13-1-ubuntu-18.04_amd64.deb",
"contracts": "v1.9.2",
"debug": 0,
"debugTransactions": false,
"bailOnFailure": true,
"keepAlive": true,
"reporter": "List",
"outDir": "artifacts",
"skipSystemContracts": true,
"include": [
"teleporteos.cpp",
"eosio.token.cpp"
]
}
5 changes: 5 additions & 0 deletions contracts/teleporteos/.prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# .prettierrc or .prettierrc.yaml
trailingComma: 'es5'
tabWidth: 2
semi: true
singleQuote: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<h1 class="contract">close</h1>

---
spec_version: "0.2.0"
title: Close Token Balance
summary: 'Close {{nowrap owner}}’s zero quantity balance'
icon: @ICON_BASE_URL@/@TOKEN_ICON_URI@
---

{{owner}} agrees to close their zero quantity balance for the {{symbol_to_symbol_code symbol}} token.

RAM will be refunded to the RAM payer of the {{symbol_to_symbol_code symbol}} token balance for {{owner}}.

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

---
spec_version: "0.2.0"
title: Create New Token
summary: 'Create a new token'
icon: @ICON_BASE_URL@/@TOKEN_ICON_URI@
---

{{$action.account}} agrees to create a new token with symbol {{asset_to_symbol_code maximum_supply}} to be managed by {{issuer}}.

This action will not result any any tokens being issued into circulation.

{{issuer}} will be allowed to issue tokens into circulation, up to a maximum supply of {{maximum_supply}}.

RAM will deducted from {{$action.account}}’s resources to create the necessary records.

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

---
spec_version: "0.2.0"
title: Issue Tokens into Circulation
summary: 'Issue {{nowrap quantity}} into circulation and transfer into {{nowrap to}}’s account'
icon: @ICON_BASE_URL@/@TOKEN_ICON_URI@
---

The token manager agrees to issue {{quantity}} into circulation, and transfer it into {{to}}’s account.

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

If {{to}} does not have a balance for {{asset_to_symbol_code quantity}}, or the token manager does not have a balance for {{asset_to_symbol_code quantity}}, the token manager will be designated as the RAM payer of the {{asset_to_symbol_code quantity}} token balance for {{to}}. As a result, RAM will be deducted from the token manager’s resources to create the necessary records.

This action does not allow the total quantity to exceed the max allowed supply of the token.

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

---
spec_version: "0.2.0"
title: Open Token Balance
summary: 'Open a zero quantity balance for {{nowrap owner}}'
icon: @ICON_BASE_URL@/@TOKEN_ICON_URI@
---

{{ram_payer}} agrees to establish a zero quantity balance for {{owner}} for the {{symbol_to_symbol_code symbol}} token.

If {{owner}} does not have a balance for {{symbol_to_symbol_code symbol}}, {{ram_payer}} will be designated as the RAM payer of the {{symbol_to_symbol_code symbol}} token balance for {{owner}}. As a result, RAM will be deducted from {{ram_payer}}’s resources to create the necessary records.

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

---
spec_version: "0.2.0"
title: Remove Tokens from Circulation
summary: 'Remove {{nowrap quantity}} from circulation'
icon: @ICON_BASE_URL@/@TOKEN_ICON_URI@
---

The token manager agrees to remove {{quantity}} from circulation, taken from their own account.

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

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

---
spec_version: "0.2.0"
title: Transfer Tokens
summary: 'Send {{nowrap quantity}} from {{nowrap from}} to {{nowrap to}}'
icon: @ICON_BASE_URL@/@TRANSFER_ICON_URI@
---

{{from}} agrees to send {{quantity}} to {{to}}.

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

If {{from}} is not already the RAM payer of their {{asset_to_symbol_code quantity}} token balance, {{from}} will be designated as such. As a result, RAM will be deducted from {{from}}’s resources to refund the original RAM payer.

If {{to}} does not have a balance for {{asset_to_symbol_code quantity}}, {{from}} will be designated as the RAM payer of the {{asset_to_symbol_code quantity}} token balance for {{to}}. As a result, RAM will be deducted from {{from}}’s resources to create the necessary records.
245 changes: 245 additions & 0 deletions contracts/teleporteos/contracts/eosio.token/eosio.token.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
#include "eosio.token.hpp"

namespace eosio
{

void token::create(const name &issuer,
const asset &maximum_supply)
{
require_auth(get_self());

auto sym = maximum_supply.symbol;
check(sym.is_valid(), "invalid symbol name");
check(maximum_supply.is_valid(), "invalid supply");
check(maximum_supply.amount > 0, "max-supply must be positive");

stats statstable(get_self(), sym.code().raw());
auto existing = statstable.find(sym.code().raw());
check(existing == statstable.end(), "token with symbol already exists");

statstable.emplace(get_self(), [&](auto &s) {
s.supply.symbol = maximum_supply.symbol;
s.max_supply = maximum_supply;
s.issuer = issuer;
});
}

void token::issue(const name &to, const asset &quantity, const string &memo)
{
auto sym = quantity.symbol;
check(sym.is_valid(), "invalid symbol name");
check(memo.size() <= 256, "memo has more than 256 bytes");

stats statstable(get_self(), sym.code().raw());
auto existing = statstable.find(sym.code().raw());
check(existing != statstable.end(), "token with symbol does not exist, create token before issue");
const auto &st = *existing;
check(to == st.issuer, "tokens can only be issued to issuer account");

require_auth(st.issuer);
check(quantity.is_valid(), "invalid quantity");
check(quantity.amount > 0, "must issue positive quantity");

check(quantity.symbol == st.supply.symbol, "symbol precision mismatch");
check(quantity.amount <= st.max_supply.amount - st.supply.amount, "quantity exceeds available supply");

statstable.modify(st, same_payer, [&](auto &s) {
s.supply += quantity;
});

add_balance(st.issuer, quantity, st.issuer);
}

void token::burn(const name &from, const asset &quantity, const string &memo)
{
auto sym = quantity.symbol;
check(sym.is_valid(), "invalid symbol name");
check(memo.size() <= 256, "memo has more than 256 bytes");

stats statstable(get_self(), sym.code().raw());
auto existing = statstable.find(sym.code().raw());
check(existing != statstable.end(), "token with symbol does not exist");
const auto &st = *existing;

require_auth(from);
check(quantity.is_valid(), "invalid quantity");
check(quantity.amount > 0, "must retire positive quantity");

check(quantity.symbol == st.supply.symbol, "symbol precision mismatch");

statstable.modify(st, same_payer, [&](auto &s) {
s.supply -= quantity;
});

sub_balance(from, quantity);
}

/*void token::retire(const asset &quantity, const string &memo)
{
auto sym = quantity.symbol;
check(sym.is_valid(), "invalid symbol name");
check(memo.size() <= 256, "memo has more than 256 bytes");
stats statstable(get_self(), sym.code().raw());
auto existing = statstable.find(sym.code().raw());
check(existing != statstable.end(), "token with symbol does not exist");
const auto &st = *existing;
require_auth(st.issuer);
check(quantity.is_valid(), "invalid quantity");
check(quantity.amount > 0, "must retire positive quantity");
check(quantity.symbol == st.supply.symbol, "symbol precision mismatch");
statstable.modify(st, same_payer, [&](auto &s) {
s.supply -= quantity;
});
sub_balance(st.issuer, quantity);
}*/

void token::transfer(const name &from,
const name &to,
const asset &quantity,
const 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());

require_recipient(from);
require_recipient(to);

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);
}

void token::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");

auto remaining_balance = from.balance.amount - value.amount;
// Check vesting if applicable
vestings vesting_table(get_self(), get_self().value);
auto vest = vesting_table.find(owner.value);
if (vest != vesting_table.end() && vest->vesting_quantity.symbol.code().raw() == value.symbol.code().raw()){
// check we are beyond vesting start (no vesting possible before then)
uint32_t time_now = current_time_point().sec_since_epoch();
uint32_t vesting_seconds = 0;
if (time_now >= vest->vesting_start.sec_since_epoch()){
vesting_seconds = time_now - vest->vesting_start.sec_since_epoch();
}
// print("\nvesting seconds ", vesting_seconds);
uint64_t vest_per_second_sats = (vest->vesting_quantity.amount * 10'000) / vest->vesting_length;
// print("\nvest_per_second_sats ", vest_per_second_sats);
uint64_t vested_total = (vesting_seconds * vest_per_second_sats) / 10'000; // amount they can withdraw
// print("\nvested_total ", vested_total);
if (vested_total < vest->vesting_quantity.amount){
uint64_t min_balance = vest->vesting_quantity.amount - vested_total;
// print("\nmin_balance ", min_balance);
// print("\ncurrent balance ", from.balance.amount);
check(remaining_balance >= min_balance, "Cannot transfer this amount due to vesting locks");
}
}

from_acnts.modify(from, owner, [&](auto &a) {
a.balance -= value;
});
}

void token::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, [&](auto &a) {
a.balance = value;
});
}
else
{
to_acnts.modify(to, same_payer, [&](auto &a) {
a.balance += value;
});
}
}

void token::open(const name &owner, const symbol &symbol, const name &ram_payer)
{
require_auth(ram_payer);

check(is_account(owner), "owner account does not exist");

auto sym_code_raw = symbol.code().raw();
stats statstable(get_self(), sym_code_raw);
const auto &st = statstable.get(sym_code_raw, "symbol does not exist");
check(st.supply.symbol == symbol, "symbol precision mismatch");

accounts acnts(get_self(), owner.value);
auto it = acnts.find(sym_code_raw);
if (it == acnts.end())
{
acnts.emplace(ram_payer, [&](auto &a) {
a.balance = asset{0, symbol};
});
}
}

void token::close(const name &owner, const symbol &symbol)
{
require_auth(owner);
accounts acnts(get_self(), owner.value);
auto it = acnts.find(symbol.code().raw());
check(it != acnts.end(), "Balance row already deleted or never existed. Action won't have any effect.");
check(it->balance.amount == 0, "Cannot close because the balance is not zero.");
acnts.erase(it);
}

void token::addvesting(const name &account, const time_point_sec &vesting_start, const uint32_t &vesting_length, const asset &vesting_quantity)
{
require_auth(get_self());
vestings vesting_table(get_self(), get_self().value);
auto existing = vesting_table.find(account.value);

if (existing == vesting_table.end()){
vesting_table.emplace(get_self(), [&](auto &v){
v.account = account;
v.vesting_start = vesting_start;
v.vesting_length = vesting_length;
v.vesting_quantity = vesting_quantity;
});
}
else {
vesting_table.modify(*existing, get_self(), [&](auto &v){
v.vesting_start = vesting_start;
v.vesting_length = vesting_length;
v.vesting_quantity = vesting_quantity;
});
}

}
/*void token::clearvesting()
{
require_auth(get_self());
vestings vesting_table(get_self(), get_self().value);
auto vest = vesting_table.begin();
while (vest != vesting_table.end()){
vest = vesting_table.erase(vest);
}
}*/

} // namespace eosio
Loading

0 comments on commit 383ecc2

Please sign in to comment.