To make the resources on an EOSIO network more easily available to developers who need them, Block.one wrote the Resource Exchange (REX) smart contract. While there are many great tutorials demonstrating how to use REX, a developer still needs to understand the underlying code. Here’s our technical guide to understanding REX. 

There are eight terms that are worth first defining, as they will be mentioned in most discussions about REX. 
 

MatureWhen purchasing REX tokens, a user will be unable to trade them back into EOS tokens for at least four days. During this period, those tokens are referred to as maturing. You can have up to four different maturity dates (often referred to as buckets) at any given moment. Further, if you use a savings bucket (see below), when you decide to move REX tokens out of your savings, it will take four days from that point until they have matured.

Four daysAll calculations are based off of 00:00 UTC of the following day. So if a user purchases REX tokens at 16:00 UTC today, they will only reach maturity four days and eight hours from now. So whenever you see “four days”, it’s actually four days from the next 00:00 UTC.

30 daysLoans that are taken out to borrow either CPU or Network resources by developers are open for 30 days. If you want this rental to renew after 30 days, you’ll need to use either the fundcpuloan or fundnetloan action. It will renew at the market rate at time of renewal.

SavingsTo add a layer of security, a savings bucket was added. This allows users to hold REX tokens without them maturing. An example of where this would be useful is if your account has separate active and owner permissions and the keys controlling your active permission became compromised. If your REX tokens were already matured, the person who has control over your active permission will be able to immediately trade your REX tokens into EOS and make off with those as well. If they were in your savings account, you’d be able to utilize your owner permission to change your active permission, and cancel the pending maturity of your savings bucket.

REX fundTo interact with REX, you’ll need to first have EOS tokens in your REX fund. EOS tokens are held in your REX fund, not REX tokens.

Voting requirementOne participation requirement that was placed on EOS Mainnet users who want to rent out their resources through REX is that they must either be voting for at least 21 Block Producers or have delegated their vote to a proxy.

Liquidity crunchIf there isn’t enough EOS tokens in the unlent pool to cover a requested withdrawal, then a “liquidity crunch” occurs. This means that all withdrawal orders will be queued, waiting for either new EOS tokens to enter the REX pool, or for resource loans to expire. There is no risk of a user not getting back their EOS tokens, they may just need to wait up to 30 days (at most) to get them.

Market ratesREX is powered by a Bancor relay, which means that the price is not determined by users setting their own pricing, rather by the system itself calculating a market rate for the given ratios of EOS and REX tokens in the pool. This is why it is impossible to determine a rate of return, or a renewal price for a loan, as everything is determined at the time of purchase and the time of sale based on the current conditions.

The method that the value of EOS-to-REX is determined means that the amount of EOS that you put in will always be equal to, or lower than the amount of EOS returned when you choose to trade your REX tokens. This means that you can never lose EOS by holding REX, you can only gain (or at worst break even).

Airdrops can choose to respect your REX balance or not when it comes time to take a snapshot of accounts. This means that there is no guarantee that the tokens you hold in REX will be included, and will be dependent on the developers of each airdrop.

Below is a list of all actions that you might call while interacting with REX.

buyrex Once you have EOS tokens in your REX fund, you may use the buyrex action to trade your EOS tokens for REX tokens.
closerex This action will free up any RAM being used to store data for an account in the eosio.rex contract. This action will first check to see if your account has any REX tokens, any open loans for either CPU or Network bandwidth, or any EOS tokens in your REX fund. If those checks all come back empty, the action may execute.
cnclrexorder This will cancel an unfilled sellrex order for your account.
consolidate Since REX tokens require 4 days before they mature, you can have up to 4 buckets open at a time, each consuming their own amount of RAM. If you would like to free up RAM and have them all consolidated into a single bucket that will then mature 4 days from the current point, you could use the consolidate action.
defcpuloan If you had put aside some EOS tokens to fund the renewal of an open CPU loan before it expires and wish to remove some or all of those tokens, you would call this action.
defnetloan If you had put aside some EOS tokens to fund the renewal of an open Network loan before it expires and wish to remove some or all of those tokens, you would call this action.
deposit This action allows you to deposit EOS tokens into your REX fund.
fundcpuloan If you have an open CPU loan and would like to renew it at its time of expiry, you need to allocate EOS from your REX fund towards that loan using the loan ID.
fundnetloan If you have an open Network loan and would like to renew it at its time of expiry, you need to allocate EOS from your REX fund towards that loan using the loan ID.
mvfrsavings If you have placed REX tokens into your savings bucket using the mvtosavings action, you will need to call this action to begin the 4 day countdown before being able to trade them for EOS tokens.
mvtosavings This action will move some (or all) of your REX tokens into the savings bucket. It will check to ensure that you have enough REX tokens not transferring into your savings bucket to complete any sellrex orders you may have open. You do not need to wait for REX tokens to mature before moving them into your savings bucket.
rentcpu This action should be called only by developers/users who wish to pay to rent CPU bandwidth from the REX pool. You can assign the received CPU bandwidth to any account of your choosing, and can also immediately add EOS tokens to fund a renewal of that loan upon expiration within that same action.
rentnet This action should be called only by developers/users who wish to rent Network resources from the REX pool. You can assign the received Network bandwidth to any account of your choosing, and can also immediately add EOS tokens to fund a renewal of that loan upon expiration within that same action.
rexexec Whenever most actions associated to REX are called, they also run 2 “maintenance actions”. These are actions where the contract updates the REX pool, processes any sell orders, processes any loans, moves tokens from eosio.names into eosio.rex, etc… All of these are found in the runrex action (which cannot be called directly by a user).
sellrex Users would use this action to sell their REX tokens for EOS tokens. If there is currently a liquidity crunch, then your sell order would be placed in queue. If you have previously opened a sell order, calling this action again will combine the two sell orders.
unstaketorex This action allows users to purchase REX using their tokens which are currently staked to either CPU or Network resources.
updaterex This action allows a user to update their vote weight to include any proceeds to which they are entitled if they were to sell their REX into EOS at this moment. A user can achieve the same result by just recasting their vote.
withdraw To remove EOS tokens in a user’s REX fund, they will use the withdraw action.

 

eosio.rex is very handy smart contract for developers who wish to take advantage of cheap and temporary resources. We hope that this helps with your EOSIO education, opening up ideas to what is possible. If you have any questions, please join us in the dfuse Telegram channel.

This article has been updated from its original version found here