├── .gitignore ├── ricardian_contracts ├── resource_exchange.cycle_rc.md ├── resource_exchange.calcost_rc.md ├── resource_exchange.withdraw_rc.md ├── resource_exchange.buystake_rc.md ├── resource_exchange.sellstake_rc.md └── resource_exchange_rc.md ├── .vscode └── tasks.json ├── README.md ├── src ├── state_manager.cpp ├── accounts.cpp ├── resource_exchange.cpp ├── bandwidth.cpp ├── pricing.cpp ├── stake.cpp └── resource_exchange.hpp └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.wast 3 | *.wasm 4 | bin/ 5 | node_modules/ 6 | webapp/package-lock.json 7 | .vs/slnx.sqlite 8 | -------------------------------------------------------------------------------- /ricardian_contracts/resource_exchange.cycle_rc.md: -------------------------------------------------------------------------------- 1 | # CONTRACT FOR resource_exchange::cycle 2 | 3 | ## ACTION NAME: cycle 4 | 5 | ### Intent 6 | INTENT. The intention of the author and the invoker of this contract is to execute queued actions, deduct from accounts the market price for their used resources and pay dividends on their balances. 7 | 8 | ### Term 9 | TERM. This Contract expires at the conclusion of code execution. -------------------------------------------------------------------------------- /ricardian_contracts/resource_exchange.calcost_rc.md: -------------------------------------------------------------------------------- 1 | # CONTRACT FOR resource_exchange::calcost 2 | 3 | ## ACTION NAME: calcost 4 | 5 | ### Parameters 6 | 7 | Implied parameters: 8 | 9 | * `asset` (amount of resources the party wish to get the current price of) 10 | 11 | ### Intent 12 | INTENT. The intention of the author and the invoker of this contract is to get a quote of the price given the current contract state. 13 | 14 | ### Term 15 | TERM. This Contract expires at the conclusion of code execution. -------------------------------------------------------------------------------- /ricardian_contracts/resource_exchange.withdraw_rc.md: -------------------------------------------------------------------------------- 1 | # CONTRACT FOR resource_exchange::withdraw 2 | 3 | ## ACTION NAME: withdraw 4 | 5 | ### Parameters 6 | 7 | Implied parameters: 8 | 9 | * `account_name` (name of the party invoking and signing the contract) 10 | * `asset` (amount of tokens the party whish to withdraw from the contract) 11 | 12 | ### Intent 13 | INTENT. The intention of the author and the invoker of this contract is to get withdraw tokens from the users account balance. 14 | 15 | ### Term 16 | TERM. This Contract expires at the conclusion of code execution. -------------------------------------------------------------------------------- /ricardian_contracts/resource_exchange.buystake_rc.md: -------------------------------------------------------------------------------- 1 | # CONTRACT FOR resource_exchange::buystake 2 | 3 | ## ACTION NAME: buystake 4 | ### Parameters 5 | 6 | Implied parameters: 7 | 8 | * `account_name` (name of the party invoking and signing the contract) 9 | * `asset` (amount of net stake the party whish to purchase) 10 | * `asset` (amount of cpu stake the party whish to purchase) 11 | 12 | ### Intent 13 | INTENT. The intention of the author and the invoker of this contract is to queue the purchase and delegation of {parameter} resources. 14 | 15 | ### Term 16 | TERM. This Contract expires at the conclusion of code execution. -------------------------------------------------------------------------------- /ricardian_contracts/resource_exchange.sellstake_rc.md: -------------------------------------------------------------------------------- 1 | # CONTRACT FOR resource_exchange::sellstake 2 | 3 | ## ACTION NAME: sellstake 4 | 5 | ### Parameters 6 | 7 | Implied parameters: 8 | 9 | * `account_name` (name of the party invoking and signing the contract) 10 | * `asset` (amount of net stake the party whish to stop consuming) 11 | * `asset` (amount of cpu stake the party whish to stop consuming) 12 | 13 | ### Intent 14 | INTENT. The intention of the author and the invoker of this contract is to reduce the resources to be billed to the account on the following cycle. 15 | 16 | ### Term 17 | TERM. This Contract expires at the conclusion of code execution. -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "label": "Build", 6 | "group": "build", 7 | "type": "shell", 8 | "command": 9 | "eosiocpp -o bin/resource_exchange/resource_exchange.wast src/resource_exchange.cpp && eosiocpp -g bin/resource_exchange/resource_exchange.abi src/resource_exchange.cpp", 10 | "problemMatcher": [] 11 | }, 12 | { 13 | "label": "create build folder", 14 | "type": "shell", 15 | "command": 16 | "mkdir -p bin/resource_exchange", 17 | "problemMatcher": [] 18 | }, 19 | { 20 | "label": "deploy", 21 | "type": "shell", 22 | "command": "cleos --wallet-url=http://localhost:8899/ -u http://localhost:8888/ set contract exchangeres ./bin/resource_exchange", 23 | "problemMatcher": [] 24 | }, 25 | { 26 | "label": "build & deploy", 27 | "type": "shell", 28 | "command": "eosiocpp -o bin/resource_exchange/resource_exchange.wast src/resource_exchange.cpp && eosiocpp -g bin/resource_exchange/resource_exchange.abi src/resource_exchange.cpp && cleos --wallet-url=http://localhost:8899/ -u http://localhost:8888/ set contract exchangeres ./bin/resource_exchange", 29 | "problemMatcher": [] 30 | }, 31 | { 32 | "label": "set_eosio_code", 33 | "type": "shell", 34 | "command": "cleos --wallet-url=http://localhost:8899/ -u http://localhost:8888/ set account permission exchangeres active '{\"threshold\": 1,\"keys\": [{\"key\": \"EOS5cMgd9hqvgaXEnzEEbX2tVr6FkpgvxpPYRgsxm8q3RAC3rJ2zH\",\"weight\": 1}],\"accounts\": [{\"permission\":{\"actor\":\"exchangeres\",\"permission\":\"eosio.code\"},\"weight\":1}]}' owner -p exchangeres", 35 | "problemMatcher": [] 36 | }, 37 | { 38 | "label": "run webapp", 39 | "type": "shell", 40 | "command": "cd webapp && node ./main.js", 41 | "problemMatcher": [] 42 | } 43 | ] 44 | } 45 | 46 | 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Resources exchange for EOS blockchain 2 | 3 | ### The aim of this contract is to create a self regulated market for CPU and Bandwidth on the eos blockchain 4 | 5 | ##### This contract allows: 6 | - Leasing of EOS 7 | - Renting of EOS 8 | - Auto-renewals of the rent agreement 9 | - Automatic repricing based on market state 10 | - Timely withdraw of funds 11 | - Generation of income from leasing 12 | 13 | 14 | ## Actions: 15 | - deposit *(automatic when doing a transfer to contract) 16 | - withdraw: get fund out from exchange 17 | - buystake: stake net and cpu to your account 18 | - sellstake: cancel or reduce stake consumption 19 | 20 | ## How to Lease tokens: 21 | - Send the tokens you want to lease to the exchange contract 22 | - You will be automatically earning dividends based on your amount and the current market price 23 | - Withdraw them using the withdraw function, there is a 75 hours delay 24 | # How to Rent tokens: 25 | - Deposit tokens in the contract 26 | - Execute buystake action with amount of tokens for net and cpu you want to rent 27 | - Wait for next cycle (you will be billed every cycle) 28 | - You will have tokens delegated to your account 29 | 30 | ## How the exchange works 31 | 32 | Users of this exchange shall deposit EOS in it, they will get an account inside the exchange which they can use. 33 | This account holds the owner's name, balance and resources consuming. 34 | 35 | Each N days the contract will execute the function cycle(). This will bill the users consuming resources or the new users that want to get resources the price for the next N days. During this period the user can use the resources without any additional charge. After the N days the user will be billed again. 36 | 37 | In between this cycles the user may wish to cancel its resource plan (or change the amount of resources they want to rent), this action will be stored and queued to be performed on the next cycle. 38 | 39 | Actions to withdraw* and deposit will be executed immediately. 40 | 41 | The pricing of the resources is done dynamically based on the exchange capacity and usage and will increase exponentially as the liquid funds of the exchange run out 42 | 43 | Users who want to profit from renting EOS may do so by depositing in the exchange. After each cycle the profits from the fees will be awarded accordingly to the users balance, this also affects users renting resources from the network. Effectively incentivising renters to store resources on the exchange instead of staking them. 44 | 45 | > For any question ask: @alepacheco on telegram 46 | -------------------------------------------------------------------------------- /src/state_manager.cpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "resource_exchange.hpp" 3 | 4 | namespace eosio { 5 | void resource_exchange::state_init() { 6 | if (!contract_state.exists()) { 7 | contract_state.set( 8 | state_t{asset(0), asset(0), time_point_sec(0), asset(0), asset(0)}, 9 | _contract); 10 | } 11 | } 12 | 13 | void resource_exchange::state_change(asset liquid, asset staked) { 14 | auto state = contract_state.get(); 15 | contract_state.set( 16 | state_t{state.liquid_funds + liquid, state.total_stacked + staked, 17 | state.timestamp, state.to_be_refunding, state.refunding}, 18 | _self); 19 | } 20 | 21 | void resource_exchange::state_unstake_delayed(asset amount) { 22 | eosio_assert(amount >= asset(0), "must use positive amount"); 23 | auto state = contract_state.get(); 24 | contract_state.set( 25 | state_t{state.liquid_funds, state.total_stacked - amount, state.timestamp, 26 | state.to_be_refunding + amount, state.refunding}, 27 | _self); 28 | } 29 | 30 | void resource_exchange::state_cycle() { 31 | auto state = contract_state.get(); 32 | contract_state.set( 33 | state_t{state.liquid_funds + state.refunding, state.total_stacked, 34 | state.timestamp, asset(0), state.to_be_refunding}, 35 | _self); 36 | } 37 | 38 | void resource_exchange::state_on_deposit(asset quantity) { 39 | state_change(quantity, asset(0)); 40 | } 41 | 42 | void resource_exchange::state_on_withdraw(asset quantity) { 43 | state_change(-quantity, asset(0)); 44 | } 45 | 46 | void resource_exchange::reset_delayed_tx(pendingtx tx) { 47 | asset tx_amount = asset(tx.net.amount + tx.cpu.amount); 48 | state_change(tx_amount, -tx_amount); 49 | } 50 | 51 | void resource_exchange::state_set_timestamp(time_point_sec this_time) { 52 | auto state = contract_state.get(); 53 | contract_state.set(state_t{state.liquid_funds, state.total_stacked, this_time, 54 | state.to_be_refunding, state.refunding}, 55 | _self); 56 | } 57 | 58 | void resource_exchange::state_on_undelegate_unknown(asset delegated) { 59 | state_unstake_delayed(delegated); 60 | } 61 | 62 | void resource_exchange::state_on_reset_account(asset account_res) { 63 | state_unstake_delayed(account_res); 64 | } 65 | 66 | void resource_exchange::state_on_buystake(asset stake) { 67 | state_change(-stake, stake); 68 | } 69 | 70 | void resource_exchange::state_on_sellstake(asset stake_from_account, 71 | asset stake_from_tx) { 72 | state_change(stake_from_tx, -stake_from_tx); 73 | state_unstake_delayed(stake_from_account); 74 | } 75 | 76 | } // namespace eosio -------------------------------------------------------------------------------- /src/accounts.cpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "resource_exchange.hpp" 3 | #include "state_manager.cpp" 4 | 5 | namespace eosio { 6 | /** 7 | * When a tx is received, the exchange will create an account or find an 8 | *existing one and add the amount to the account and to the liquid state 9 | **/ 10 | void resource_exchange::deposit(currency::transfer tx) { 11 | eosio_assert(tx.quantity.is_valid(), "invalid quantity"); 12 | eosio_assert(tx.quantity.symbol == asset().symbol, 13 | "asset must be system token"); 14 | 15 | auto itr = accounts.find(tx.from); 16 | 17 | if (itr == accounts.end()) { 18 | itr = accounts.emplace(tx.from, [&](auto& acnt) { acnt.owner = tx.from; }); 19 | } 20 | 21 | accounts.modify(itr, 0, [&](auto& acnt) { acnt.balance += tx.quantity; }); 22 | 23 | state_on_deposit(tx.quantity); 24 | } 25 | 26 | /** 27 | * Withdraw deducts the amount from the user account and from the liquid state 28 | * this funds will not be considered as part of the exchange, on the next cycle 29 | * the price will increase, reducing the resource consuption and the tokens will 30 | * be unstaked and accesible on 3 days 31 | **/ 32 | void resource_exchange::withdraw(account_name to, asset quantity) { 33 | // TODO cancel buy tx if cant pay for it 34 | eosio_assert(quantity.is_valid(), "invalid quantity"); 35 | eosio_assert(quantity.amount > 0, "must withdraw positive quantity"); 36 | auto state = contract_state.get(); 37 | 38 | if (quantity > state.liquid_funds) { 39 | if (quantity > state.get_unstaked()) { 40 | // TODO: force overdraft 41 | eosio_assert(false, "cannot withdraw"); 42 | } else { 43 | // retry next cycle 44 | eosio::transaction out; 45 | out.actions.emplace_back(permission_level(to, N(active)), _self, 46 | N(withdraw), std::make_tuple(to, quantity)); 47 | 48 | out.delay_sec = CYCLE_TIME + 100; 49 | out.send(to, _contract); 50 | return; 51 | } 52 | } 53 | 54 | auto itr = accounts.find(to); 55 | eosio_assert(itr != accounts.end(), "unknown account"); 56 | 57 | accounts.modify(itr, 0, [&](auto& acnt) { 58 | eosio_assert(acnt.balance >= quantity, "insufficient balance"); 59 | acnt.balance -= quantity; 60 | }); 61 | 62 | state_on_withdraw(quantity); 63 | // schedule wiithdraw after cycle to prevent abuse 64 | eosio::transaction out; 65 | out.actions.emplace_back( 66 | permission_level(_contract, N(active)), N(eosio.token), N(transfer), 67 | std::make_tuple(_contract, to, quantity, std::string(""))); 68 | 69 | out.delay_sec = CYCLE_TIME + 100; 70 | out.send(to, _contract, true); 71 | 72 | if (itr->is_empty()) { 73 | accounts.erase(itr); 74 | } 75 | } 76 | 77 | } // namespace eosio -------------------------------------------------------------------------------- /src/resource_exchange.cpp: -------------------------------------------------------------------------------- 1 | #include "resource_exchange.hpp" 2 | #include "accounts.cpp" 3 | #include "bandwidth.cpp" 4 | #include "pricing.cpp" 5 | #include "stake.cpp" 6 | #include "state_manager.cpp" 7 | 8 | namespace eosio { 9 | void resource_exchange::apply(account_name contract, account_name act) { 10 | state_init(); 11 | 12 | switch (act) { 13 | case N(transfer): { 14 | eosio_assert(contract == N(eosio.token), 15 | "invalid contract, use eosio.token"); 16 | auto tx = unpack_action_data(); 17 | if (tx.from != _contract) { 18 | require_auth(tx.from); 19 | deposit(tx); 20 | } 21 | break; 22 | } 23 | case N(withdraw): { 24 | auto tx = unpack_action_data(); 25 | require_auth(tx.user); 26 | withdraw(tx.user, tx.quantity); 27 | break; 28 | } 29 | case N(buystake): { 30 | auto tx = unpack_action_data(); 31 | require_auth(tx.user); 32 | buystake(tx.user, tx.net, tx.cpu); 33 | break; 34 | } 35 | case N(sellstake): { 36 | auto tx = unpack_action_data(); 37 | require_auth(tx.user); 38 | sellstake(tx.user, tx.net, tx.cpu); 39 | break; 40 | } 41 | case N(cycle): { 42 | require_auth(_contract); 43 | cycle(); 44 | break; 45 | } 46 | case N(calcosttoken): { 47 | calcosttoken(); 48 | break; 49 | } 50 | } 51 | } 52 | 53 | void resource_exchange::cycle() { 54 | print("Run cycle\n"); 55 | auto secs_to_next = time_point_sec(CYCLE_TIME); 56 | auto secs_flexibility = time_point_sec(5); 57 | auto state = contract_state.get(); 58 | time_point_sec this_time = time_point_sec(now()); 59 | eosio::transaction out; 60 | out.actions.emplace_back(permission_level(_contract, N(active)), _contract, 61 | N(cycle), _contract); 62 | 63 | docycle(); 64 | 65 | out.delay_sec = secs_to_next.utc_seconds; 66 | out.send(this_time.utc_seconds, _contract); 67 | 68 | state_set_timestamp(this_time); 69 | } 70 | 71 | void resource_exchange::docycle() { 72 | double cost_per_token = calcosttoken(); 73 | asset fees_collected = asset(0); 74 | for (auto acnt = accounts.begin(); acnt != accounts.end(); ++acnt) { 75 | fees_collected += billaccount(acnt->owner, cost_per_token); 76 | matchbandwidth(acnt->owner); 77 | } 78 | asset fees_devs = asset(fees_collected.amount * 0.0); 79 | for (auto acnt = accounts.begin(); acnt != accounts.end(); ++acnt) { 80 | payreward(acnt->owner, fees_collected - fees_devs); 81 | } 82 | unstakeunknown(); 83 | 84 | // TODO paydevs 85 | state_cycle(); 86 | 87 | print("Total fees: ", fees_collected, " "); 88 | } 89 | 90 | } // namespace eosio 91 | 92 | extern "C" { 93 | [[noreturn]] void apply(uint64_t receiver, uint64_t code, uint64_t action) { 94 | eosio::resource_exchange ex(receiver); 95 | ex.apply(code, action); 96 | eosio_exit(0); 97 | } 98 | } -------------------------------------------------------------------------------- /src/bandwidth.cpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "resource_exchange.hpp" 3 | 4 | namespace eosio { 5 | 6 | /** 7 | * Delegatebw is a shortcut for the delegatebw action 8 | **/ 9 | void resource_exchange::delegatebw(account_name receiver, 10 | asset stake_net_quantity, 11 | asset stake_cpu_quantity) { 12 | action(permission_level(_contract, N(active)), N(eosio), N(delegatebw), 13 | std::make_tuple(_contract, receiver, stake_net_quantity, 14 | stake_cpu_quantity, false)) 15 | .send(); 16 | } 17 | 18 | /** 19 | * Unelegatebw is a shortcut for the unelegatebw action 20 | **/ 21 | void resource_exchange::undelegatebw(account_name receiver, 22 | asset stake_net_quantity, 23 | asset stake_cpu_quantity) { 24 | action(permission_level(_contract, N(active)), N(eosio), N(undelegatebw), 25 | std::make_tuple(_contract, receiver, stake_net_quantity, 26 | stake_cpu_quantity)) 27 | .send(); 28 | } 29 | 30 | void resource_exchange::unstakeunknown() { 31 | if (delegated_table.begin() == delegated_table.end()) { 32 | return; 33 | } 34 | for (auto delegated = delegated_table.begin(); 35 | delegated != delegated_table.end(); ++delegated) { 36 | if (accounts.find(delegated->to) == accounts.end() && 37 | delegated->to != _contract) { 38 | undelegatebw(delegated->to, delegated->net_weight, delegated->cpu_weight); 39 | asset undelegating = delegated->net_weight + delegated->cpu_weight; 40 | state_on_undelegate_unknown(undelegating); 41 | } 42 | } 43 | } 44 | 45 | void resource_exchange::matchbandwidth(account_name owner) { 46 | auto user = accounts.find(owner); 47 | auto delegated = delegated_table.find(user->owner); 48 | 49 | asset net_delegated = asset(0); 50 | asset cpu_delegated = asset(0); 51 | 52 | if (delegated != delegated_table.end()) { 53 | net_delegated = delegated->net_weight; 54 | cpu_delegated = delegated->cpu_weight; 55 | } 56 | asset net_account = user->resource_net; 57 | asset cpu_account = user->resource_cpu; 58 | asset net_to_delegate = asset(0); 59 | asset net_to_undelegate = asset(0); 60 | asset cpu_to_delegate = asset(0); 61 | asset cpu_to_undelegate = asset(0); 62 | if (net_account > net_delegated) { 63 | net_to_delegate += (net_account - net_delegated); 64 | } else if (net_account < net_delegated) { 65 | net_to_undelegate += (net_delegated - net_account); 66 | } 67 | 68 | if (cpu_account > cpu_delegated) { 69 | cpu_to_delegate += (cpu_account - cpu_delegated); 70 | } else if (cpu_account < cpu_delegated) { 71 | cpu_to_undelegate += (cpu_delegated - cpu_account); 72 | } 73 | if ((net_to_delegate + cpu_to_delegate) > asset(0)) { 74 | delegatebw(user->owner, net_to_delegate, cpu_to_delegate); 75 | } 76 | if ((net_to_undelegate + cpu_to_undelegate) > asset(0)) { 77 | undelegatebw(user->owner, net_to_undelegate, cpu_to_undelegate); 78 | } 79 | } 80 | 81 | } // namespace eosio -------------------------------------------------------------------------------- /src/pricing.cpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "resource_exchange.hpp" 3 | 4 | namespace eosio { 5 | /** 6 | * Function to calculate aproximate cost of resources taking into account state 7 | * changes with purchase 8 | **/ 9 | asset resource_exchange::calcost(asset resources) { 10 | if (resources <= asset(0)) { 11 | return asset(0); 12 | } 13 | eosio_assert(contract_state.exists(), "No contract state available"); 14 | auto state = contract_state.get(); 15 | double_t liquid = state.liquid_funds.amount - resources.amount; 16 | double_t total = state.get_total().amount; 17 | double_t cost_per_token = cost_function(total, liquid); 18 | asset price = asset(cost_per_token * resources.amount); 19 | print("price: ", price); 20 | return price; 21 | } 22 | 23 | /** 24 | * Returns cost per Larimer 25 | **/ 26 | double resource_exchange::calcosttoken() { 27 | eosio_assert(contract_state.exists(), "No contract state available"); 28 | auto state = contract_state.get(); 29 | double liquid = state.liquid_funds.amount; 30 | double total = state.get_total().amount; 31 | eosio_assert(liquid > 0 && total > 0, "No funds to price"); 32 | double cost_per_token = cost_function(total, liquid); 33 | print(cost_per_token); 34 | return cost_per_token; 35 | } 36 | 37 | double resource_exchange::cost_function(double total, double liquid) { 38 | double used = total - liquid; 39 | double price = 1.0 / (-used + (total * PRICE_GAP)); 40 | return price / PRICE_TUNE; 41 | } 42 | 43 | void resource_exchange::payreward(account_name user, asset fee_collected) { 44 | auto state = contract_state.get(); 45 | double reward_per_token = fee_collected.amount / state.get_total().amount; 46 | auto acnt = accounts.find(user); 47 | double reward = acnt->balance.amount * reward_per_token; 48 | accounts.modify(acnt, 0, 49 | [&](auto& account) { account.balance += asset(reward); }); 50 | } 51 | 52 | asset resource_exchange::billaccount(account_name owner, 53 | double cost_per_token) { 54 | auto acnt = accounts.find(owner); 55 | auto pending_itr = pendingtxs.find(acnt->owner); 56 | 57 | auto cost_all = asset(cost_per_token * acnt->get_all().amount); 58 | asset extra_net = asset(0); 59 | asset extra_cpu = asset(0); 60 | asset fee_collected = asset(0); 61 | 62 | if (pending_itr != pendingtxs.end()) { 63 | cost_all += asset(cost_per_token * pending_itr->get_all().amount); 64 | extra_net += pending_itr->net; 65 | extra_cpu += pending_itr->cpu; 66 | } 67 | 68 | eosio_assert(cost_all.amount >= 0, "cost negative"); 69 | if (acnt->balance >= cost_all) { 70 | accounts.modify(acnt, 0, [&](auto& account) { 71 | account.balance -= cost_all; 72 | account.resource_net += extra_net; 73 | account.resource_cpu += extra_cpu; 74 | }); 75 | fee_collected += cost_all; 76 | } else { 77 | // Cancel purchase stake tx, pay just account 78 | if (pending_itr != pendingtxs.end()) { 79 | reset_delayed_tx(*pending_itr); 80 | } 81 | asset cost_account = asset(cost_per_token * acnt->get_all().amount); 82 | if (acnt->balance >= cost_account) { 83 | accounts.modify(acnt, 0, 84 | [&](auto& account) { account.balance -= cost_account; }); 85 | fee_collected += cost_account; 86 | } else { 87 | // can't pay for account, reset account 88 | state_on_reset_account(acnt->resource_net + acnt->resource_cpu); 89 | accounts.modify(acnt, 0, [&](auto& account) { 90 | account.resource_net = asset(0); 91 | account.resource_cpu = asset(0); 92 | }); 93 | } 94 | } 95 | if (pending_itr != pendingtxs.end()) { 96 | pendingtxs.erase(pending_itr); 97 | } 98 | return fee_collected; 99 | } 100 | 101 | } // namespace eosio -------------------------------------------------------------------------------- /src/stake.cpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "resource_exchange.hpp" 3 | #include "state_manager.cpp" 4 | 5 | namespace eosio { 6 | /** 7 | * Buystake will schedule a purchase of stake for the next cycle, 8 | * it will update an existing scheduled purchase or create a new one 9 | * and will set the funds as staked 10 | **/ 11 | void resource_exchange::buystake(account_name from, asset net, asset cpu) { 12 | eosio_assert(net.is_valid() && cpu.is_valid(), "invalid quantity"); 13 | eosio_assert(net.symbol == asset().symbol && cpu.symbol == asset().symbol, 14 | "asset must be system token"); 15 | eosio_assert(net >= asset(0) && cpu >= asset(0) && (net + cpu) > asset(0), 16 | "must buy positive stake"); 17 | 18 | auto itr = accounts.find(from); 19 | eosio_assert(itr != accounts.end(), "account not found"); 20 | 21 | auto pending_itr = pendingtxs.find(from); 22 | if (pending_itr == pendingtxs.end()) { 23 | pending_itr = pendingtxs.emplace(from, [&](auto& tx) { 24 | tx.user = from; 25 | tx.net = asset(0); 26 | tx.cpu = asset(0); 27 | }); 28 | } 29 | 30 | asset adj_net = net + pending_itr->net; 31 | asset adj_cpu = cpu + pending_itr->cpu; 32 | 33 | auto state = contract_state.get(); 34 | eosio_assert(state.liquid_funds.amount * PRICE_GAP >= 35 | (adj_net.amount + adj_cpu.amount), 36 | "not enough resources in exchange"); 37 | 38 | asset cost = calcost(adj_net + adj_cpu); 39 | eosio_assert(itr->balance >= cost, "not enough funds on account"); 40 | 41 | print("Queing purchase of: ", net, " and ", cpu, " in stake for*: ", cost, 42 | "\n"); 43 | pendingtxs.modify(pending_itr, 0, [&](auto& tx) { 44 | tx.net = adj_net; 45 | tx.cpu = adj_cpu; 46 | }); 47 | 48 | state_on_buystake(net + cpu); 49 | } 50 | 51 | /** 52 | * Sellstake will remove resources used from a delayed tx if any 53 | * or sell the remove resources used from the account 54 | **/ 55 | void resource_exchange::sellstake(account_name user, asset net, asset cpu) { 56 | // to sell reduce account resources, in next cycle he will pay the new usage 57 | eosio_assert(net.is_valid() && cpu.is_valid(), "invalid quantity"); 58 | eosio_assert(net >= asset(0) && cpu >= asset(0) && (net + cpu) > asset(0), 59 | "must sell positive stake amount"); 60 | eosio_assert(net.symbol == asset().symbol && cpu.symbol == asset().symbol, 61 | "asset must be system token"); 62 | 63 | auto itr = accounts.find(user); 64 | auto pending_itr = pendingtxs.find(user); 65 | eosio_assert(itr != accounts.end(), "unknown account"); 66 | asset usr_cpu = itr->resource_cpu; 67 | asset usr_net = itr->resource_net; 68 | 69 | if (pending_itr != pendingtxs.end()) { 70 | usr_cpu += pending_itr->cpu; 71 | usr_net += pending_itr->net; 72 | } 73 | 74 | eosio_assert(usr_cpu >= cpu && usr_net >= net && (net + cpu) > asset(0), 75 | "not enough to sell"); 76 | 77 | // resourses removed from where 78 | asset net_from_account = asset(0); 79 | asset cpu_from_account = asset(0); 80 | asset net_from_tx = asset(0); 81 | asset cpu_from_tx = asset(0); 82 | 83 | // reduce first in tx then in account 84 | if (pending_itr == pendingtxs.end()) { 85 | accounts.modify(itr, 0, [&](auto& acnt) { 86 | acnt.resource_net -= usr_net; 87 | acnt.resource_cpu -= usr_cpu; 88 | }); 89 | net_from_account += usr_net; 90 | cpu_from_account += usr_cpu; 91 | } else { 92 | auto amount_tx_net = pending_itr->net - net; 93 | auto amount_tx_cpu = pending_itr->cpu - cpu; 94 | 95 | if (amount_tx_cpu >= asset(0)) { 96 | // if enough to cover with tx 97 | pendingtxs.modify(pending_itr, 0, 98 | [&](auto& tx) { tx.cpu = amount_tx_cpu; }); 99 | cpu_from_tx += cpu; 100 | } else { // split with tx and account 101 | pendingtxs.modify(pending_itr, 0, [&](auto& tx) { tx.cpu = asset(0); }); 102 | cpu_from_tx += pending_itr->cpu; 103 | accounts.modify(itr, 0, 104 | [&](auto& acnt) { acnt.resource_cpu -= -amount_tx_cpu; }); 105 | cpu_from_account += -amount_tx_cpu; 106 | } 107 | 108 | if (amount_tx_net >= asset(0)) { 109 | pendingtxs.modify(pending_itr, 0, 110 | [&](auto& tx) { tx.net = amount_tx_net; }); 111 | net_from_tx += net; 112 | } else { 113 | pendingtxs.modify(pending_itr, 0, [&](auto& tx) { tx.net = asset(0); }); 114 | net_from_tx += pending_itr->net; 115 | accounts.modify(itr, 0, 116 | [&](auto& acnt) { acnt.resource_net -= -amount_tx_net; }); 117 | net_from_account += -amount_tx_net; 118 | } 119 | 120 | if (pending_itr->is_empty()) { 121 | pendingtxs.erase(pending_itr); 122 | } 123 | } 124 | 125 | eosio_assert((net + cpu) == (net_from_account + cpu_from_account + 126 | net_from_tx + cpu_from_tx), 127 | "sold stake calculation error"); 128 | state_on_sellstake(net_from_account + cpu_from_account, 129 | net_from_tx + cpu_from_tx); 130 | } 131 | 132 | } // namespace eosio -------------------------------------------------------------------------------- /ricardian_contracts/resource_exchange_rc.md: -------------------------------------------------------------------------------- 1 | ### CLAUSE NAME: Warranty 2 | WARRANTY. The invoker of the contract action shall uphold its Obligations under this Contract in a timely and workmanlike manner, using knowledge and recommendations for performing the services which meet generally acceptable standards set forth by EOS.IO Blockchain Block Producers. 3 | 4 | ### CLAUSE NAME: Default 5 | DEFAULT. The occurrence of any of the following shall constitute a material default under this Contract: 6 | 7 | ### CLAUSE NAME: Remedies 8 | REMEDIES. In addition to any and all other rights a party may have available according to law, if a party defaults by failing to substantially perform any provision, term or condition of this Contract, the other party may terminate the Contract by providing written notice to the defaulting party. This notice shall describe with sufficient detail the nature of the default. The party receiving such notice shall promptly be removed from being a Block Producer and this Contract shall be automatically terminated. 9 | 10 | ### CLAUSE NAME: Force Majeure 11 | FORCE MAJEURE. If performance of this Contract or any obligation under this Contract is prevented, restricted, or interfered with by causes beyond either party's reasonable control ("Force Majeure"), and if the party unable to carry out its obligations gives the other party prompt written notice of such event, then the obligations of the party invoking this provision shall be suspended to the extent necessary by such event. The term Force Majeure shall include, without limitation, acts of God, fire, explosion, vandalism, storm or other similar occurrence, orders or acts of military or civil authority, or by national emergencies, insurrections, riots, or wars, or strikes, lock-outs, work stoppages, or supplier failures. The excused party shall use reasonable efforts under the circumstances to avoid or remove such causes of non-performance and shall proceed to perform with reasonable dispatch whenever such causes are removed or ceased. An act or omission shall be deemed within the reasonable control of a party if committed, omitted, or caused by such party, or its employees, officers, agents, or affiliates. 12 | 13 | ### CLAUSE NAME: Dispute Resolution 14 | DISPUTE RESOLUTION. Any controversies or disputes arising out of or relating to this Contract will be resolved by binding arbitration under the default rules set forth by the EOS.IO Blockchain. The arbitrator's award will be final, and judgment may be entered upon it by any court having proper jurisdiction. 15 | 16 | ### CLAUSE NAME: Entire Agreement 17 | ENTIRE AGREEMENT. This Contract contains the entire agreement of the parties, and there are no other promises or conditions in any other agreement whether oral or written concerning the subject matter of this Contract. This Contract supersedes any prior written or oral agreements between the parties. 18 | 19 | ### CLAUSE NAME: Severability 20 | SEVERABILITY. If any provision of this Contract will be held to be invalid or unenforceable for any reason, the remaining provisions will continue to be valid and enforceable. If a court finds that any provision of this Contract is invalid or unenforceable, but that by limiting such provision it would become valid and enforceable, then such provision will be deemed to be written, construed, and enforced as so limited. 21 | 22 | ### CLAUSE NAME: Amendment 23 | AMENDMENT. This Contract may be modified or amended in writing by mutual agreement between the parties, if the writing is signed by the party obligated under the amendment. 24 | 25 | ### CLAUSE NAME: Governing Law 26 | GOVERNING LAW. This Contract shall be construed in accordance with the Maxims of Equity. 27 | 28 | ### CLAUSE NAME: Notice 29 | NOTICE. Any notice or communication required or permitted under this Contract shall be sufficiently given if delivered to a verifiable email address or to such other email address as one party may have publicly furnished in writing, or published on a broadcast contract provided by this blockchain for purposes of providing notices of this type. 30 | ### CLAUSE NAME: Waiver of Contractual Right 31 | WAIVER OF CONTRACTUAL RIGHT. The failure of either party to enforce any provision of this Contract shall not be construed as a waiver or limitation of that party's right to subsequently enforce and compel strict compliance with every provision of this Contract. 32 | 33 | ### CLAUSE NAME: Arbitrator's Fees to Prevailing Party 34 | ARBITRATOR'S FEES TO PREVAILING PARTY. In any action arising hereunder or any separate action pertaining to the validity of this Agreement, both sides shall pay half the initial cost of arbitration, and the prevailing party shall be awarded reasonable arbitrator's fees and costs. 35 | 36 | ### CLAUSE NAME: Construction and Interpretation 37 | CONSTRUCTION AND INTERPRETATION. The rule requiring construction or interpretation against the drafter is waived. The document shall be deemed as if it were drafted by both parties in a mutual effort. 38 | 39 | ### CLAUSE NAME: In Witness Whereof 40 | IN WITNESS WHEREOF, the parties hereto have caused this Agreement to be executed by themselves or their duly authorized representatives as of the date of execution, and authorized as proven by the cryptographic signature on the transaction that invokes this contract. -------------------------------------------------------------------------------- /src/resource_exchange.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | namespace eosio { 10 | class resource_exchange : public eosio::contract { 11 | private: 12 | account_name _contract; 13 | const uint32_t CYCLE_TIME = 60 * 60 * 25 * 3; // 3 days and three hours 14 | const double PRICE_TUNE = 0.000001; 15 | const double PRICE_GAP = 1.0; 16 | 17 | struct stake_trade { 18 | account_name user; 19 | asset net; 20 | asset cpu; 21 | }; 22 | 23 | struct withdraw_tx { 24 | account_name user; 25 | asset quantity; 26 | }; 27 | 28 | //@abi table pendingtx i64 29 | struct pendingtx { 30 | pendingtx(account_name o = account_name()) : user(o) {} 31 | account_name user; 32 | asset net; 33 | asset cpu; 34 | asset get_all() const { return cpu + net; } 35 | bool is_empty() const { return !(net.amount | cpu.amount); } 36 | 37 | uint64_t primary_key() const { return user; } 38 | EOSLIB_SERIALIZE(pendingtx, (user)(net)(cpu)) 39 | }; 40 | 41 | //@abi table state i64 42 | struct state_t { 43 | asset liquid_funds; 44 | asset total_stacked; 45 | time_point_sec timestamp; 46 | asset to_be_refunding; 47 | asset refunding; 48 | 49 | asset get_total() const { 50 | return liquid_funds + total_stacked + to_be_refunding + refunding; 51 | } 52 | asset get_unstaked() const { 53 | return liquid_funds + to_be_refunding + refunding; 54 | } 55 | EOSLIB_SERIALIZE(state_t, (liquid_funds)(total_stacked)(timestamp)( 56 | to_be_refunding)(refunding)) 57 | }; 58 | 59 | //@abi table account i64 60 | struct account_t { 61 | account_t(account_name o = account_name()) : owner(o) {} 62 | account_name owner; 63 | asset balance = asset(0); 64 | asset resource_net = asset(0); 65 | asset resource_cpu = asset(0); 66 | asset get_all() const { return resource_cpu + resource_net; } 67 | 68 | bool is_empty() const { 69 | return !(balance.amount | resource_net.amount | resource_cpu.amount); 70 | } 71 | 72 | uint64_t primary_key() const { return owner; } 73 | EOSLIB_SERIALIZE(account_t, (owner)(balance)(resource_net)(resource_cpu)) 74 | }; 75 | 76 | struct delegated_bandwidth { 77 | account_name from; 78 | account_name to; 79 | asset net_weight; 80 | asset cpu_weight; 81 | uint64_t primary_key() const { return to; } 82 | EOSLIB_SERIALIZE(delegated_bandwidth, (from)(to)(net_weight)(cpu_weight)) 83 | }; 84 | 85 | typedef eosio::multi_index 86 | del_bandwidth_table; 87 | 88 | struct account_balance { 89 | asset balance; 90 | uint64_t primary_key() const { return balance.symbol.name(); } 91 | }; 92 | 93 | typedef eosio::multi_index account_balances; 94 | 95 | typedef singleton state_index; 96 | state_index contract_state; 97 | 98 | typedef eosio::multi_index account_index; 99 | account_index accounts; 100 | 101 | typedef eosio::multi_index pendingtx_index; 102 | pendingtx_index pendingtxs; 103 | 104 | void delegatebw(account_name receiver, asset stake_net_quantity, 105 | asset stake_cpu_quantity); 106 | void undelegatebw(account_name receiver, asset stake_net_quantity, 107 | asset stake_cpu_quantity); 108 | 109 | void dobuystake(account_name user, asset net, asset cpu); 110 | 111 | void reset_delayed_tx(pendingtx tx); 112 | asset billaccount(account_name account, double cost_per_token); 113 | void matchbandwidth(account_name user); 114 | void payreward(account_name user, asset fee_collected); 115 | double cost_function(double total, double liquid); 116 | void unstakeunknown(); 117 | 118 | void state_on_deposit(asset quantity); 119 | void state_on_withdraw(asset quantity); 120 | void state_set_timestamp(time_point_sec this_time); 121 | void state_on_sellstake(asset stake_from_account, asset stake_from_tx); 122 | void state_on_buystake(asset stake); 123 | void state_on_reset_account(asset account_res); 124 | void state_on_undelegate_unknown(asset delegated); 125 | void state_unstake_delayed(asset amount); 126 | void state_change(asset liquid, asset staked); 127 | void state_cycle(); 128 | void state_init(); 129 | 130 | void docycle(); 131 | 132 | public: 133 | resource_exchange(account_name self) 134 | : _contract(self), 135 | eosio::contract(self), 136 | accounts(_self, _self), 137 | pendingtxs(_self, _self), 138 | delegated_table(N(eosio), _self), 139 | contract_balance(N(eosio.token), _self), 140 | contract_state(_self, _self) {} 141 | 142 | del_bandwidth_table delegated_table; 143 | account_balances contract_balance; 144 | 145 | void apply(account_name contract, account_name act); 146 | void deposit(currency::transfer tx); 147 | 148 | /// @abi action 149 | void buystake(account_name user, asset net, asset cpu); 150 | 151 | /// @abi action 152 | void sellstake(account_name user, asset net, asset cpu); 153 | 154 | /// @abi action 155 | void withdraw(account_name user, asset quantity); 156 | 157 | asset calcost(asset res); 158 | 159 | /// @abi action 160 | double calcosttoken(); 161 | 162 | /// @abi action 163 | void cycle(); 164 | }; 165 | } // namespace eosio 166 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Attribution-NonCommercial-NoDerivatives 4.0 International 2 | 3 | ======================================================================= 4 | 5 | Creative Commons Corporation ("Creative Commons") is not a law firm and 6 | does not provide legal services or legal advice. Distribution of 7 | Creative Commons public licenses does not create a lawyer-client or 8 | other relationship. Creative Commons makes its licenses and related 9 | information available on an "as-is" basis. Creative Commons gives no 10 | warranties regarding its licenses, any material licensed under their 11 | terms and conditions, or any related information. Creative Commons 12 | disclaims all liability for damages resulting from their use to the 13 | fullest extent possible. 14 | 15 | Using Creative Commons Public Licenses 16 | 17 | Creative Commons public licenses provide a standard set of terms and 18 | conditions that creators and other rights holders may use to share 19 | original works of authorship and other material subject to copyright 20 | and certain other rights specified in the public license below. The 21 | following considerations are for informational purposes only, are not 22 | exhaustive, and do not form part of our licenses. 23 | 24 | Considerations for licensors: Our public licenses are 25 | intended for use by those authorized to give the public 26 | permission to use material in ways otherwise restricted by 27 | copyright and certain other rights. Our licenses are 28 | irrevocable. Licensors should read and understand the terms 29 | and conditions of the license they choose before applying it. 30 | Licensors should also secure all rights necessary before 31 | applying our licenses so that the public can reuse the 32 | material as expected. Licensors should clearly mark any 33 | material not subject to the license. This includes other CC- 34 | licensed material, or material used under an exception or 35 | limitation to copyright. More considerations for licensors: 36 | wiki.creativecommons.org/Considerations_for_licensors 37 | 38 | Considerations for the public: By using one of our public 39 | licenses, a licensor grants the public permission to use the 40 | licensed material under specified terms and conditions. If 41 | the licensor's permission is not necessary for any reason--for 42 | example, because of any applicable exception or limitation to 43 | copyright--then that use is not regulated by the license. Our 44 | licenses grant only permissions under copyright and certain 45 | other rights that a licensor has authority to grant. Use of 46 | the licensed material may still be restricted for other 47 | reasons, including because others have copyright or other 48 | rights in the material. A licensor may make special requests, 49 | such as asking that all changes be marked or described. 50 | Although not required by our licenses, you are encouraged to 51 | respect those requests where reasonable. More_considerations 52 | for the public: 53 | wiki.creativecommons.org/Considerations_for_licensees 54 | 55 | ======================================================================= 56 | 57 | Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 58 | International Public License 59 | 60 | By exercising the Licensed Rights (defined below), You accept and agree 61 | to be bound by the terms and conditions of this Creative Commons 62 | Attribution-NonCommercial-NoDerivatives 4.0 International Public 63 | License ("Public License"). To the extent this Public License may be 64 | interpreted as a contract, You are granted the Licensed Rights in 65 | consideration of Your acceptance of these terms and conditions, and the 66 | Licensor grants You such rights in consideration of benefits the 67 | Licensor receives from making the Licensed Material available under 68 | these terms and conditions. 69 | 70 | 71 | Section 1 -- Definitions. 72 | 73 | a. Adapted Material means material subject to Copyright and Similar 74 | Rights that is derived from or based upon the Licensed Material 75 | and in which the Licensed Material is translated, altered, 76 | arranged, transformed, or otherwise modified in a manner requiring 77 | permission under the Copyright and Similar Rights held by the 78 | Licensor. For purposes of this Public License, where the Licensed 79 | Material is a musical work, performance, or sound recording, 80 | Adapted Material is always produced where the Licensed Material is 81 | synched in timed relation with a moving image. 82 | 83 | b. Copyright and Similar Rights means copyright and/or similar rights 84 | closely related to copyright including, without limitation, 85 | performance, broadcast, sound recording, and Sui Generis Database 86 | Rights, without regard to how the rights are labeled or 87 | categorized. For purposes of this Public License, the rights 88 | specified in Section 2(b)(1)-(2) are not Copyright and Similar 89 | Rights. 90 | 91 | c. Effective Technological Measures means those measures that, in the 92 | absence of proper authority, may not be circumvented under laws 93 | fulfilling obligations under Article 11 of the WIPO Copyright 94 | Treaty adopted on December 20, 1996, and/or similar international 95 | agreements. 96 | 97 | d. Exceptions and Limitations means fair use, fair dealing, and/or 98 | any other exception or limitation to Copyright and Similar Rights 99 | that applies to Your use of the Licensed Material. 100 | 101 | e. Licensed Material means the artistic or literary work, database, 102 | or other material to which the Licensor applied this Public 103 | License. 104 | 105 | f. Licensed Rights means the rights granted to You subject to the 106 | terms and conditions of this Public License, which are limited to 107 | all Copyright and Similar Rights that apply to Your use of the 108 | Licensed Material and that the Licensor has authority to license. 109 | 110 | g. Licensor means the individual(s) or entity(ies) granting rights 111 | under this Public License. 112 | 113 | h. NonCommercial means not primarily intended for or directed towards 114 | commercial advantage or monetary compensation. For purposes of 115 | this Public License, the exchange of the Licensed Material for 116 | other material subject to Copyright and Similar Rights by digital 117 | file-sharing or similar means is NonCommercial provided there is 118 | no payment of monetary compensation in connection with the 119 | exchange. 120 | 121 | i. Share means to provide material to the public by any means or 122 | process that requires permission under the Licensed Rights, such 123 | as reproduction, public display, public performance, distribution, 124 | dissemination, communication, or importation, and to make material 125 | available to the public including in ways that members of the 126 | public may access the material from a place and at a time 127 | individually chosen by them. 128 | 129 | j. Sui Generis Database Rights means rights other than copyright 130 | resulting from Directive 96/9/EC of the European Parliament and of 131 | the Council of 11 March 1996 on the legal protection of databases, 132 | as amended and/or succeeded, as well as other essentially 133 | equivalent rights anywhere in the world. 134 | 135 | k. You means the individual or entity exercising the Licensed Rights 136 | under this Public License. Your has a corresponding meaning. 137 | 138 | 139 | Section 2 -- Scope. 140 | 141 | a. License grant. 142 | 143 | 1. Subject to the terms and conditions of this Public License, 144 | the Licensor hereby grants You a worldwide, royalty-free, 145 | non-sublicensable, non-exclusive, irrevocable license to 146 | exercise the Licensed Rights in the Licensed Material to: 147 | 148 | a. reproduce and Share the Licensed Material, in whole or 149 | in part, for NonCommercial purposes only; and 150 | 151 | b. produce and reproduce, but not Share, Adapted Material 152 | for NonCommercial purposes only. 153 | 154 | 2. Exceptions and Limitations. For the avoidance of doubt, where 155 | Exceptions and Limitations apply to Your use, this Public 156 | License does not apply, and You do not need to comply with 157 | its terms and conditions. 158 | 159 | 3. Term. The term of this Public License is specified in Section 160 | 6(a). 161 | 162 | 4. Media and formats; technical modifications allowed. The 163 | Licensor authorizes You to exercise the Licensed Rights in 164 | all media and formats whether now known or hereafter created, 165 | and to make technical modifications necessary to do so. The 166 | Licensor waives and/or agrees not to assert any right or 167 | authority to forbid You from making technical modifications 168 | necessary to exercise the Licensed Rights, including 169 | technical modifications necessary to circumvent Effective 170 | Technological Measures. For purposes of this Public License, 171 | simply making modifications authorized by this Section 2(a) 172 | (4) never produces Adapted Material. 173 | 174 | 5. Downstream recipients. 175 | 176 | a. Offer from the Licensor -- Licensed Material. Every 177 | recipient of the Licensed Material automatically 178 | receives an offer from the Licensor to exercise the 179 | Licensed Rights under the terms and conditions of this 180 | Public License. 181 | 182 | b. No downstream restrictions. You may not offer or impose 183 | any additional or different terms or conditions on, or 184 | apply any Effective Technological Measures to, the 185 | Licensed Material if doing so restricts exercise of the 186 | Licensed Rights by any recipient of the Licensed 187 | Material. 188 | 189 | 6. No endorsement. Nothing in this Public License constitutes or 190 | may be construed as permission to assert or imply that You 191 | are, or that Your use of the Licensed Material is, connected 192 | with, or sponsored, endorsed, or granted official status by, 193 | the Licensor or others designated to receive attribution as 194 | provided in Section 3(a)(1)(A)(i). 195 | 196 | b. Other rights. 197 | 198 | 1. Moral rights, such as the right of integrity, are not 199 | licensed under this Public License, nor are publicity, 200 | privacy, and/or other similar personality rights; however, to 201 | the extent possible, the Licensor waives and/or agrees not to 202 | assert any such rights held by the Licensor to the limited 203 | extent necessary to allow You to exercise the Licensed 204 | Rights, but not otherwise. 205 | 206 | 2. Patent and trademark rights are not licensed under this 207 | Public License. 208 | 209 | 3. To the extent possible, the Licensor waives any right to 210 | collect royalties from You for the exercise of the Licensed 211 | Rights, whether directly or through a collecting society 212 | under any voluntary or waivable statutory or compulsory 213 | licensing scheme. In all other cases the Licensor expressly 214 | reserves any right to collect such royalties, including when 215 | the Licensed Material is used other than for NonCommercial 216 | purposes. 217 | 218 | 219 | Section 3 -- License Conditions. 220 | 221 | Your exercise of the Licensed Rights is expressly made subject to the 222 | following conditions. 223 | 224 | a. Attribution. 225 | 226 | 1. If You Share the Licensed Material, You must: 227 | 228 | a. retain the following if it is supplied by the Licensor 229 | with the Licensed Material: 230 | 231 | i. identification of the creator(s) of the Licensed 232 | Material and any others designated to receive 233 | attribution, in any reasonable manner requested by 234 | the Licensor (including by pseudonym if 235 | designated); 236 | 237 | ii. a copyright notice; 238 | 239 | iii. a notice that refers to this Public License; 240 | 241 | iv. a notice that refers to the disclaimer of 242 | warranties; 243 | 244 | v. a URI or hyperlink to the Licensed Material to the 245 | extent reasonably practicable; 246 | 247 | b. indicate if You modified the Licensed Material and 248 | retain an indication of any previous modifications; and 249 | 250 | c. indicate the Licensed Material is licensed under this 251 | Public License, and include the text of, or the URI or 252 | hyperlink to, this Public License. 253 | 254 | For the avoidance of doubt, You do not have permission under 255 | this Public License to Share Adapted Material. 256 | 257 | 2. You may satisfy the conditions in Section 3(a)(1) in any 258 | reasonable manner based on the medium, means, and context in 259 | which You Share the Licensed Material. For example, it may be 260 | reasonable to satisfy the conditions by providing a URI or 261 | hyperlink to a resource that includes the required 262 | information. 263 | 264 | 3. If requested by the Licensor, You must remove any of the 265 | information required by Section 3(a)(1)(A) to the extent 266 | reasonably practicable. 267 | 268 | 269 | Section 4 -- Sui Generis Database Rights. 270 | 271 | Where the Licensed Rights include Sui Generis Database Rights that 272 | apply to Your use of the Licensed Material: 273 | 274 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right 275 | to extract, reuse, reproduce, and Share all or a substantial 276 | portion of the contents of the database for NonCommercial purposes 277 | only and provided You do not Share Adapted Material; 278 | 279 | b. if You include all or a substantial portion of the database 280 | contents in a database in which You have Sui Generis Database 281 | Rights, then the database in which You have Sui Generis Database 282 | Rights (but not its individual contents) is Adapted Material; and 283 | 284 | c. You must comply with the conditions in Section 3(a) if You Share 285 | all or a substantial portion of the contents of the database. 286 | 287 | For the avoidance of doubt, this Section 4 supplements and does not 288 | replace Your obligations under this Public License where the Licensed 289 | Rights include other Copyright and Similar Rights. 290 | 291 | 292 | Section 5 -- Disclaimer of Warranties and Limitation of Liability. 293 | 294 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE 295 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS 296 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF 297 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, 298 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, 299 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR 300 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, 301 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT 302 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT 303 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. 304 | 305 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE 306 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, 307 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, 308 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, 309 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR 310 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN 311 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR 312 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR 313 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. 314 | 315 | c. The disclaimer of warranties and limitation of liability provided 316 | above shall be interpreted in a manner that, to the extent 317 | possible, most closely approximates an absolute disclaimer and 318 | waiver of all liability. 319 | 320 | 321 | Section 6 -- Term and Termination. 322 | 323 | a. This Public License applies for the term of the Copyright and 324 | Similar Rights licensed here. However, if You fail to comply with 325 | this Public License, then Your rights under this Public License 326 | terminate automatically. 327 | 328 | b. Where Your right to use the Licensed Material has terminated under 329 | Section 6(a), it reinstates: 330 | 331 | 1. automatically as of the date the violation is cured, provided 332 | it is cured within 30 days of Your discovery of the 333 | violation; or 334 | 335 | 2. upon express reinstatement by the Licensor. 336 | 337 | For the avoidance of doubt, this Section 6(b) does not affect any 338 | right the Licensor may have to seek remedies for Your violations 339 | of this Public License. 340 | 341 | c. For the avoidance of doubt, the Licensor may also offer the 342 | Licensed Material under separate terms or conditions or stop 343 | distributing the Licensed Material at any time; however, doing so 344 | will not terminate this Public License. 345 | 346 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public 347 | License. 348 | 349 | 350 | Section 7 -- Other Terms and Conditions. 351 | 352 | a. The Licensor shall not be bound by any additional or different 353 | terms or conditions communicated by You unless expressly agreed. 354 | 355 | b. Any arrangements, understandings, or agreements regarding the 356 | Licensed Material not stated herein are separate from and 357 | independent of the terms and conditions of this Public License. 358 | 359 | 360 | Section 8 -- Interpretation. 361 | 362 | a. For the avoidance of doubt, this Public License does not, and 363 | shall not be interpreted to, reduce, limit, restrict, or impose 364 | conditions on any use of the Licensed Material that could lawfully 365 | be made without permission under this Public License. 366 | 367 | b. To the extent possible, if any provision of this Public License is 368 | deemed unenforceable, it shall be automatically reformed to the 369 | minimum extent necessary to make it enforceable. If the provision 370 | cannot be reformed, it shall be severed from this Public License 371 | without affecting the enforceability of the remaining terms and 372 | conditions. 373 | 374 | c. No term or condition of this Public License will be waived and no 375 | failure to comply consented to unless expressly agreed to by the 376 | Licensor. 377 | 378 | d. Nothing in this Public License constitutes or may be interpreted 379 | as a limitation upon, or waiver of, any privileges and immunities 380 | that apply to the Licensor or You, including from the legal 381 | processes of any jurisdiction or authority. 382 | 383 | ======================================================================= 384 | 385 | Creative Commons is not a party to its public 386 | licenses. Notwithstanding, Creative Commons may elect to apply one of 387 | its public licenses to material it publishes and in those instances 388 | will be considered the “Licensor.” The text of the Creative Commons 389 | public licenses is dedicated to the public domain under the CC0 Public 390 | Domain Dedication. Except for the limited purpose of indicating that 391 | material is shared under a Creative Commons public license or as 392 | otherwise permitted by the Creative Commons policies published at 393 | creativecommons.org/policies, Creative Commons does not authorize the 394 | use of the trademark "Creative Commons" or any other trademark or logo 395 | of Creative Commons without its prior written consent including, 396 | without limitation, in connection with any unauthorized modifications 397 | to any of its public licenses or any other arrangements, 398 | understandings, or agreements concerning use of licensed material. For 399 | the avoidance of doubt, this paragraph does not form part of the 400 | public licenses. 401 | 402 | Creative Commons may be contacted at creativecommons.org. 403 | --------------------------------------------------------------------------------