# Creating & Redeeming FXRP

This post is intended as a simplified walkthough of the creation and redemption of FXRP on Flare. It is designed to give an intuitive understanding of the system and its safety mechanisms. It does not highlight every element. The full whitepaper can be found here.

## The Set Up

### Originator

Alice owns a farm which grows oats. She is worried about the price of oats falling and her farm's income being affected. She would like to use some of the XRP she owns to trade with a decentralized derivatives application on Flare so that she can alleviate the risk to her business of the price of oats falling. Alice is going to mint FXRP on Flare from her XRP tokens on the XRP Ledger. In the FXRP system Alice is called an originator.

### Agents

Bob and Charlie have Spark tokens on which they would like to earn a return. They provide Spark tokens as collateral to the FXRP system. Bob and Charlie are termed agents.

### FXRP System Parameters

The Flare Time Series Oracle (FTSO) - referred to as the Flare Oracle in the following -  provides the XRP/Spark rate to the FXRP System, i.e. how many Spark can be purchased with 1 XRP. Together with the amount of collateral in the system and the collateral ratio this defines how many FXRP can be issued by the system.

In this example, let's say, the parameters are as follows:

FXRP system parameters
XRP/Spark rate 10
Required collateral ratio 2.5
System collateral (Spark) 5000

The amount of FXRP that can be issued is termed the issuance capacity. It is determined by the amount of available collateral divided by the XRP/Spark rate and then further divided by the collateral ratio:

$$\small{\texttt{issuance capacity} =\frac{\texttt{available collateral}} {\texttt{XRP/Spark rate } \times \texttt{collateral ratio}}}$$

Plugging in the numbers from the table above into the equation, we get:

$$\small{\texttt{issuance capacity}=\frac{ \texttt{5000}}{ \texttt{10} \times\texttt{2.5}} = \texttt{200}}$$

This means that the FXRP system has sufficient collateral to issue 200 FXRP.

### Collateral Reservation Transaction

In order to safely use the FXRP system Alice must reserve collateral against the amount of FXRP she wishes to originate. This is called collateral reservation. Alice also has an address on Flare and completes a transaction with the FXRP system specifying: 1) the amount of FXRP she would like to create, (in this case 100 FXRP), 2) the originating account(s) on the XRP ledger, 3) the Flare account to receive the FXRP and including 4) the collateral reservation fee. The collateral reservation fee is a 0.1% fee payable in Spark calculated on the value of the XRP being minted into FXRP.

Collateral needs to be reserved so that from the moment the collateral reservation transaction is finalized, there is 2.5 times value of the XRP being minted in to FXRP, locked in Spark tokens. A fee needs to be paid because if the originator (Alice) doesn't complete the transaction, the collateral could instead have been used against a successful origination and the agents could have earned a creation fee.

### Transaction Formula

When Alice completes the collateral reservation transaction she is returned a Transaction Formula. The transaction formula specifies the transaction that Alice needs to make on the XRP Ledger to create the FXRP. Importantly, the transaction formula specifies a last ledger index number on the XRP Ledger by which point Alice's XRP must reach the agents. The last ledger index number is useful because when Alice goes to make the transaction on the XRP Ledger she can use a function called last ledger index. This function means that if for some reason the transaction on the XRP Ledger doesn't finalize by that last ledger index number then Alice retains her XRP and her total loss is the 0.1% fee that she paid to reserve collateral.

### Creation Transaction

On the XRP Ledger, Alice sends to the agent(s) listed in the Transaction Formula the amount of XRP that she wishes to mint into FXRP + the pre-specified creation fee. In this case Alice wants to mint 100 FXRP and the creation fee is set at 5%. Therefore Alice sends a total of 105 XRP to the agents. In this case the transaction formula specifies that the origination is set equally between the agents. This will not always be the case.

### The State Connector System

The FXRP system receives the state of the XRP Ledger from the state connector system. State connector nodes are run independently by every Flare Network validator, and they observe the state of an external system in a safe and decentralized manner. Alice's creation transaction is input to the FXRP system via the state connector system.

When the state connector system reports to the FXRP system that Alice's transaction has been finalized on the XRP Ledger, FXRP is minted and sent to the Flare address Alice specified in the collateral reservation transaction.

### XRP/Spark rate increases

The current FXRP system looks as follows:

FXRP System Parameters
Current XRP/Spark rate 10
Required Collateral Ratio 2.5

FXRP System State
Total Collateral (Spark) 5000
Locked Collateral (Spark) 2500
Available Collateral (Spark) 2500
FXRP Issued 100
FXRP Issuance Capacity 100

The XRP/Spark rate now increases from 10 to 12.

FXRP System Parameters
Current XRP/Spark rate 12
Required Collateral Ratio 2.5

100 FXRP are issued, the amount of collateral need to secure the FXRP at the new rate is:

$$\small{\texttt{100} \times \texttt{12} \times \texttt{2.5} = \texttt{3000}}$$

2500 Spark are already locked against the 100 issued FXRP so the FXRP system now locks 500 more Spark tokens in response to the price increase.

FXRP System State
Total Collateral (Spark) 5000
Locked Collateral (Spark) 3000
Available Collateral (Spark) 2000
FXRP Issued 100
FXRP Issuance Capacity 66.66

### Alice uses the derivatives application

Alice spends 50 FXRP buying put options from Dana on the price of oats. A put option is a financial instrument that allows the owner to benefit if the price of something falls in a given time. The price of oats goes up, which is great for Alice's farm income but means that the put options Alice bought from Dana expire worthless. Alice doesn't mind that she lost 50 FXRP to Dana to protect her income.

### FXRP redemption for XRP

Dana made 50 FXRP in her trade with Alice. Dana would now like to redeem this to the XRP ledger.

Dana sends her 50 FXRP back to the FXRP system, together with; the address to be credited on XRPL and an address on Flare to credit (in Spark) incase of redemption failure. (This example assumes zero redemption fee is charged. The redemption fee rate is controlled by Spark governance and should be zero or very low.)

Once Dana's transaction to the FXRP system is completed, the agents are sent an Agent Redemption Formula (ARF) which specifies the transaction they need to make on the XRP Ledger and by which ledger index number the transaction needs to have finalized.

### Successful Redemption

The agents send 50 XRP to Dana by the ledger index number specified in the ARF. When observed by the FXRP System through the state connector system this then unlocks collateral:

$$\small{\texttt{50} \times \texttt{12} \times \texttt{2.5} = \texttt{1500 Spark}}$$

Which as the agents are equal in this example unlocks 750 Spark each for Bob and Charlie.

The FXRP system now looks as follows:

FXRP System Parameters
Current XRP/Spark rate 12
Required Collateral Ratio 2.5

FXRP System State
Total Collateral (Spark) 5000
Locked Collateral (Spark) 1500
Available Collateral (Spark) 3500
FXRP Issued 50
FXRP Issuance Capacity 116.66

In the table above previously 3000 Spark were locked against 100 issued FXRP, now only 50 FXRP are issued with the other 50 having been successfully redeemed so only 1500 Spark remain locked.

### Redemption Failure

Now let's say that Bob has been spending too much time scuba diving recently and has neglected his duties as an agent. Instead of receiving the full 50 XRP Dana only receives 25 XRP by the specified ledger index number.

To allow Dana to replace her missing XRP, the FXRP system sends her the value of the missing XRP in Spark (at the prevailing rate of 12 Spark to 1 XRP) and a 1% compensation fee to account for transaction costs (the compensation fee is set by Spark governance). This comes to:

$$\small{\texttt{25} \times \texttt{1.01} \times \texttt{12} = \texttt{303 Spark}}$$

Bob is penalized heavily for his failure as an agent. Bob has 1500 Spark locked in the FXRP system. Against this collateral, 50 FXRP were previously issued. The 25 XRP that Dana is missing is secured by half of Bob's collateral, i.e. 750 Spark. Drawn from Bob's collateral is the compensation of 303 Spark that is sent to Dana and a further 223.5 Spark are burned as a penalty. This leaves Bob with 1973.5 Spark in the FXRP system of which 750 Spark remain locked to secure 25 FXRP.

Dana may now go to an exchange with her 303 Spark to buy the missing 25 XRP or, staying in a purely decentralized environment, she could use her Spark tokens to buy 25 FXRP from Alice and redeem these back to the FXRP system. (The second option makes more sense in an environment where there are many agents and a lively decentralized market for FXRP.)

Bob's financial loss here is the Spark tokens that have been paid out in lieu of XRP, plus the Spark tokens that have been burned, plus the compensation fee (Spark), less the XRP he keeps and the creation fee he received in XRP. Bob loses the equivalent of 18.125 more XRP than he retains. Therefore Bob is highly financially incentivized to honor redemptions. Dana has had a frustrating experience but is not out of pocket.

Bob's Loss XRP Gain Spark Loss XRP equivalent @ prevailing rate
Origination amount 25 25
Creation fee retained 1.25 1.25
XRP Replacement 300 -25
Compensation Fee 3 -0.75
Penalty 223.5 -18.625
Sum 26.125 526.5 -18.125

### Bob leaves the FXRP System

Before the redemption failure there was 5000 Spark tokens in the FXRP system of which 3000 were securing 100 issued FXRP. After the redemption failure the total system collateral amount is reduced to:

$$\small{\texttt{5000} - \texttt{303 (compensation)} - \texttt{223.5 (penalty)} = \texttt{4473.5 Spark}}$$

This leaves Bob with the following collateral in the system:

$$\small{\texttt{2500 (Bob's original Spark)} - \texttt{303} - \texttt{223.5} = \texttt{1973.5 Spark}}$$

50 FXRP remain issued of which Bob's pro rata share is 25 FXRP. Therefore Bob had the following collateral locked in the system:

$$\small{\texttt{25} \times \texttt{12} \times \texttt{2.5} = \texttt{750 Spark}}$$

The FXRP system currently looks as follows:

FXRP System Parameters
Current XRP/Spark rate 12
Required Collateral Ratio 2.5

FXRP System State
Total Collateral (Spark) 4473.5
Locked Collateral (Spark) 1500
Available Collateral (Spark) 2973.5
FXRP Issued 50
FXRP Issuance Capacity 99.11

Dana now decides she wants to become an agent, sending 5000 Spark to the FXRP system.

Dana's additional 5000 Spark increases the issuing capacity of the FXRP system by:

$$\small{\texttt{5000} \div \texttt{12} \div \texttt{2.5} = \texttt{166.66 FXRP}}$$

The total issuing capacity is now:

$$\small{ \texttt{99.11 (previous)} + \texttt{166.66 (Dana's new collateral)} = \texttt{265.78 FXRP (rounded)}}$$

The FXRP System now looks as follows:

FXRP System Parameters
Current XRP/Spark rate 12
Required Collateral Ratio 2.5

FXRP System State
Total Collateral (Spark) 9473.5
Locked Collateral (Spark) 1500
Available Collateral (Spark) 7973.5
FXRP Issued 50
FXRP Issuance Capacity 265.78

Bob now wants to quit as an agent to take up scuba diving full time. Bob has 52.5 XRP in his XRP Ledger address that Alice used to originate her 100 FXRP (of which he was penalized for not sending 25 to Dana). Bob now indicates to the FXRP system that he wishes to originate new FXRP whilst excluding himself as an agent in this process (so that he may immediately redeem the FXRP). Bob completes the process and sends 26.25 XRP to the agents on the XRP Ledger (Charlie & Dana) to originate 25 FXRP on Flare.

He then redeems this 25 FXRP, which brings his FXRP position in the System to zero and makes available all his collateral (1973.5 Spark) to withdraw from the system which he does.

Bob is now out of the system and is no longer an agent. He walks away with the 1973.5 Spark tokens returned to him from the FXRP system (diminished by penalization for his previous redemption failure) and the remaining 26.25 XRP tokens on the XRP Ledger from Alice's origination.

When Alice comes, at some point in the future, to redeem her remaining 50 FXRP back to the XRP ledger, all of the agents may have changed and huge sums of Spark may have turned over issuing and redeeming FXRP, but to redeem her FXRP Alice doesn't need to know or keep track of any of this. Alice just needs to send her FXRP to the FXRP system specifying the XRP Ledger address to credit, a Flare address incase of redemption failure and the redemption fee if there is one.

## Collateral Ratio Breach

Here we will describe what happens if the value of an agent's collateral available to the FXRP system, apportioned against an amount of issued FXRP,  falls below the required collateral level. Let us define a new scenario:

FXRP System Parameters
Current XRP/Spark rate 10
Required Collateral Ratio 2.5

In the new scenario We now have three agents, Charlie, Dana & Eve who have collectively issued 1200 FXRP and have the following amounts of collateral in the system. The system looks as follows:

Collateral in FXRP System (Spark) FXRP Issuance Apportionment Collateral Ratio
Charlie 5000 200 2.5
Dana 40000 500 8.0
Eve 15000 500 3.0

The collateral ratio can be calculated as follows:

$$\small{ \texttt{collateral ratio} = \frac{\texttt{collateral in FXRP System (Spark)}} { \texttt{(FXRP issuance apportionment} \times \texttt{XRP/Spark rate})}}$$

For example for Charlie this is:

$$\small{\texttt{5000} \div (\texttt{200} \times \texttt{10}) = \texttt{2.5}}$$

On the XRP Ledger each agent also holds the following XRP positions which occurred through origination:

XRP Held
Charlie 200
Dana 500
Eve 500

### Successful Collateral Breach Rectification

The XRP/Spark rate now has a shock price move to 15 i.e. one XRP buys 15 Spark. This is an instantaneous 50% increase in XRP against Spark.

The position of each of the agents is now:

Collateral in FXRP System (Spark) FXRP Issuance Apportionment Collateral Ratio
Charlie 5000 200 1.66
Dana 40000 500 5.33
Eve 15000 500 2.0

(Notice that before the shock price move Charlie maintains the minimum absolute minimum collateral of 2.5 time the issued value. With the instantaneous 50% price move Charlie still has a greater value of collateral locked up than FXRP issued.)

Charles and Eve are in breach of the collateral requirements (i.e. their collateral ratio has fallen below 2.5) and must take action within a limited time period called the collateral call band. The following table shows their position: they must either reduce their FXRP position by the amount shown in the column Excess FXRP or increase their collateral by the amount shown in the column Collateral Deficit.

Collateral required is the collateral required to support the FXRP Apportioned at the new XRP/Spark price and conforming to the collateral ratio requirement of 2.5.

Collateral (Spark) FXRP Apportioned Collateral Required (Spark) Collateral Deficit (Spark) FXRP Supported Excess FXRP
Charlie 5000 200 7500 2500 133.33 66.67
Eve 15000 500 18750 3750 400 100

Charlie opts to reduce his FXRP position by 70 FXRP. Remember that Charlie has 200 XRP on the XRP Ledger. So Charlie originates 70 FXRP, paying 73.5 XRP, which is the desired amount of FXRP + the preset 5% creation fee. Charlie then immediately redeems the FXRP against his own account reducing his apportionment of FXRP issued from 200 to 130 and bringing his collateral ratio above 2.5. If there is no collateral in the system for Charlie to originate new FXRP he can instead buy FXRP using XRP through an exchange.

Eve on the other hand decides to increase the collateral she has committed to the system. Eve adds 5000 Spark to the FXRP system bringing her collateral ratio back above 2.5.

### Collateral Breach Rectification Failure

If either Charlie or Eve do not rectify their collateral ratio breach within the allotted time, their Spark collateral will be used by the FXRP System to repurchase the entire amount of FXRP apportioned against their collateral.

To see this let's look at Eve. Eve has 15000 Spark as collateral in the FXRP System, the XRP/Spark rate is 15 and her collateral ratio is:

$$\small{\texttt{15000}\div (\texttt{500} \times \texttt{15}) = \texttt{2}}$$

Which is below the required ratio but still double the value of the issued FXRP apportioned against Eve's account.

Collateral (Spark) FXRP Apportioned FXRP Value of Collateral
Eve 15000 500 1000

The system will use Eve's collateral to place a bid to buy 500 FXRP using Spark tokens. The first bid is at the prevailing XRP/Spark rate, if any part of this bid is not taken by the end of n blocks (where n is a system parameter) the bid price increases by 10%. This process is repeated until the entire 500 FXRP has been repurchased by the system. The FXRP are then burned and the remainder of Eve's collateral is returned to her.

The basis for the safety of the FXRP collateral system is centered on two required elements:

1) The agent always has more to lose from defaulting on a required action than by performing the required action.

2) If an agent does default, the system creates a strong profit incentive for others to rectify the situation.

The collateral ratio is key to the maintenance of the safety of the system.

In the collateral breach analysis, we have looked at an instantaneous 50% change in price. To look at what this example means, it is important to remember that crypto markets are open 24 hours per day, 365 days per year and that the FXRP system will be recalculating all parameters (and giving instructions to agents) on a highly frequent (sub minute) basis. Therefore, for the scenario documented above to actually occur, the 50% change in the XRP/Spark rate would need to occur between FXRP recalculation periods, i.e. inside one minute.

It is generally expected that the vast majority of XRP/Spark rate changes between FXRP recalculations will be of far lesser magnitude. It is important to note that even in this extreme analysis, where the XRP/Spark rate has increased by 50% in less than a minute, all the incentives to economic safety have remained intact.  In periods of higher volatility, the FXRP system parameters can be adjusted to increase the frequency of recalculations, the system can then enforce actions more swiftly, reducing the possibility of any degradation to the economic incentives underpinning the safety of the system.

## Subscribe to the Flare Blog

* indicates required