Asset quantities in StarkEx
In the Ethereum blockchain, token quantities are represented by a 256-bit number. However, for the sake of efficiency, quantities in StarkEx are represented by a 64-bit number. The largest number of digits that a 64-bit number can represent is much less than that which a 256-bit number can represent. So in order to support trading in significant volume, the smallest unit that StarkEx can use for an asset is larger than the smallest unit that Ethereum can use.
The inputs to all offchain transactions and some onchain requests are given in units of the offchain asset, so the code that generates the transaction signature must use the correct factor for converting 256-bit quantities of onchain or synthetic ETH or ERC-20 asset quantities to a 64-bit StarkEx representation.
Converting onchain asset quantities |
This factor is called the quantization factor or quantum. The quantum represents the smallest amount of an asset that users can trade offchain, or with which they can pay offchain fees. |
Converting synthetic asset quantities |
This factor is called the resolution factor. The resolution is the total number of the smallest part of a single synthetic token in the StarkEx system. |
You decide the value of the quantum and resolution factor according to the requirements of your application’s business logic.
Balance of a synthetic asset
Every balance is a 64-bit value, where one bit represents the sign indicating if the value is positive or negative, and 63-bits represent the number. So every balance falls within the range (-263, 263).
Quantization
The quantum is the smallest unit of a token in the StarkEx system. It is similar to an exchange rate that you use to find the onchain value of a quantity of a token in StarkEx, according to the following formula:
Consider one possible quantization for ETH, which is an onchain asset:
Onchain asset |
The smallest unit of ETH is 1 WEI, which is \(10^{-18}\) ETH. |
Offchain asset |
The smallest unit with which you can conduct transactions of the offchain asset that corresponds to ETH represents \(10^{-11}\) onchain ETH, or \(10^{7}\) onchain WEI. |
So the operator defines the quantum for ETH to be \(10^{7}\).
This example shows how to find how many units of onchain WEI are represented by 17 units of the corresponding offchain asset.
-
In Ethereum, 1 WEI = \(10^{-18}\)ETH.
-
Let \(\text{quantum}=10^7\).
-
Let \(\text{StarkEx_amount}=17\). This is the number of corresponding assets in a StarkEx offchain position.
Plugging in these values to the formula above gives the following:
170,000,000 is the amount of WEI locked onchain that is associated with the corresponding StarkEx offchain vault, which is the same as \(10^{-11} \cdot 17\) ETH.
The quantum is necessary for every asset that can be represented onchain. For example, in the deposit flow, the onchain deposit transaction gets an unquantized amount of tokens from the user, yet the parameter to the onchain call is a quantized amount. |
Resolution
In contrast to collateral, synthetic assets do not necessarily correspond to onchain assets. For example, a barrel of oil can be a synthetic asset. So quantization, which is specifically for working with onchain assets, does not apply to synthetic assets. However, it is still necessary to work with a representation of the synthetic asset within the StarkEx system that defines the smallest unit of the asset that users can trade.
In StarkEx, synthetic assets are converted to StarkEx units by applying a calculation called resolution that uses a constant called a resolution factor.
For collateral, resolution is also necessary to simplify certain calculations.
Resolution provides the following benefits:
-
Signed external oracle prices are given in round units, such as BTC or ETH, rather than satoshi or WEI, so resolution makes the translation to the price used in the StarkEx system valid.
-
Resolution enables trading in synthetic assets that do not necessarily correspond to ERC-20 tokens, such as barrels of oil.
The resolution is the total number of a single asset in the StarkEx system that equals one synthetic token. For example, if the smallest unit of DOGE that you can trade on StarkEx is 10-6 DOGE, that means that one synthetic DOGE = 106 StarkEx units. This number, 106, is the resolution factor for DOGE in StarkEx.
The formula to find the amount of a synthetic asset is:
Because every balance is within the range (-263, 263), then in the example above, each position can contain amounts of (10-6\(\cdot\)-263, 10-6\(\cdot\)263) DOGE.
Consider synthetic ETH.
Consider that in Ethereum, 1 ETH = \(10^{18}\) WEI.
-
Let \(\text{resolution} = 10^8\). This means that the operator decided that the smallest unit of ETH their application supports is 10-8 synthetic ETH.
-
Alice has 2.25 synthetic ETH in her position.
What is the StarkEx amount of ETH in her position?
Plugging in these values to the formula above gives the following:
225,000,000 is the number of StarkEx units Alice holds that correspond to synthetic ETH in her position.
Prices: Calculating StarkEx internal values
When calculating values to send to the StarkEx gateway, be aware that the StarkEx system represents prices as 64-bit fixed-point unsigned numbers, with 32 bits after the decimal point.
Consider the following values:
-
The quantization of USDC is 1.
-
The resolution of USDC is 106, which means that the smallest unit of USDC in the StarkEx system is 10-6 USDC.
-
The resolution of synthetic ETH is 108.
-
A signature from the oracle provider sets the price of ETH to 383.3456178219832 USDC, which the oracle provider notates as 383345617821983200000.
The internal price of ETH in StarkEx is calculated as follows:
Where \(\text{ROUND_PRICE}\) rounds the quotient to the nearest integer.
Plugging in the values gives the following:
Which simplifies to:
Rounding this price to the nearest integer gives 16464568916
, which is the value that the StarkEx system uses for the price of ETH.
Total value: Calculating StarkEx internal values
-
Collateral and synthetic asset values have resolution applied.
-
Total value is a 96-bit fixed-point signed integer, with 32 bits after the decimal point.
Consider the following applies to Alice’s position:
collateral: |
Alice holds 500 USDC. |
synthetic: |
Alice holds 6 ETH, where 1 ETH = 1,000 USDC. |
The total value of Alice’s position in end-user terms is:
500 + (6 * 1,000) = 6,500
To convert this value to a StarkEx internal value:
Plugging in the values above gives:
So total value, expressed as a 96-bit fixed-point signed integer, with 32 bits after the decimal point, is 12026225000000000000
.
Total risk: Calculating StarkEx internal values
When StarkEx calculates total risk:
-
The risk factor is a positive number between 0 and 1. It is similar to the maintenance margin. For more information, see Margin and Total value and total risk.
-
The risk factor and prices are represented as 64-bit unsigned integers, with 32 bits after the decimal point.
-
Total risk is a 128-bit fixed-point unsigned integer, with 64 bits after the decimal point.
To calculate the internal representation of the risk factor:
For example, if the risk_factor for ETH is 0.1, the internal representation is 0.1 * 232 = 429496729.6
. This number is rounded to 429496730
, which is the value for risk_factor
in the general configuration file.
Calculating total risk results in a 128-bit fixed-point unsigned number, with 64 bits to the right of the decimal point.
The risk factor for ETH is 0.1. The internal StarkEx value is 0.1 * 232= 429496729.6
, which rounds to 429496730
.
The resolution factors for USDC, BTC, and ETH are as follows:
-
USDC: 106
-
BTC: 107
-
ETH: 108
So the internal values of Alice’s USDC, BTC, and ETH positions are as follows:
-
500 USDC * 106 = 500000000
-
4 BTC * 107 = 40000000
-
6 ETH * 108 = 600000000
Consider Alice’s total risk from Example: Total risk is 40,600:
collateral: |
500 USDC |
synthetic: |
4 BTC. 1 BTC = 20,000 USDC. |
(4 * 20,000 * 0.5) + (6 * 1,000 * 0.1) = 40,600
Now convert the risk factors to 32-bit numbers:
-
\(\text{BTC_risk_factor}\) = 0.5 * 232 = 2147483648
-
\(\text{ETH_risk_factor}\) = 0.1 * 232 = 429496730
The internal StarkEx value of Alice’s total risk is calculated as follows:
\(\text{BTC_risk_factor}\) is the internal StarkEx value of this risk factor, so it already has been multiplied by 232. |
Plugging in the values from the example:
( (4 * 20,000) * 107 * 2147483648 ) + ((6 * 1,000) * 108 * 429496730) = 197568495600000000000
So the internal value of Alice’s total risk is 197568495600000000000.