rSwap V1 Technical Architecture and Swap Module
The entire architecture can be described by the picture below, which mainly includes the rSwap module of StaFi Chain and the rToken-swap service:
This module is the main logic for the user to exchange native tokens. The user initiates a transaction and calls the
swap_rtoken_for_native_token
method. The chain will perform a series of legality checks (rToken balance, exchange rate, minimum exchange amount, etc.) according to the parameters provided by the user. If the verification is correct, the transInfo data corresponding to the transaction will be automatically generated for the user and stored on the chain. This information will be processed by the rToken-swap service.transInfo mainly contains the following information:
```rust
pub struct SwapTransactionInfo<AccountId> {
/// account
pub account: AccountId,
/// receiver
pub receiver: Vec<u8>,
/// value
pub value: u128,
/// deal state
pub is_deal: bool,
}
When the user exchanges,
`
real exchange rate = rToken exchange rate * exchange rate
`
, the rToken exchange rate changes in real time, and the exchange rate changes with different conversion gears. Different gears correspond to different unlocking time and exchange rate. The longer the unlocking time, the higher the exchange rate. When the unlocking time gradually converges to the time required for redemption through liquidityUnbond
, the exchange rate at this time should converge to 1.Since the rToken exchange rate on the chain is constantly changing, the real exchange rate seen when the user initiates the exchange will be different from when the transaction is actually processed. If the difference is large, it may violate the original intention of the user's exchange.
To solve this issue, we have introduced min_out_amount. When the user exchanges tokens, the rates will be calculated according to the slippage selected by the user,
`
min_out_amount = rtoken_amount * rToken exchange rate * exchange rate * (1-slippage)
`
. If the chain is processing the transaction and finds that `
real_out_amount < min_out_amount
`
, the transaction will be terminated to prevent the loss of user tokens.In order to prevent spam attacks, an extra swap_fee will be charged for each transaction. The specific number is currently set up by the StaFi Foundation, and will be gradually opened to community governance in the future.
rToken-swap is an independent service, maintained and operated by acceptors. The main responsibility of the service is to observe the transInfos information on the StaFi Chain. When there is new transInfo information, it will trigger a multi-signature transfer to the user. Each time a sum is processed, the processing result is reported to the StaFi Chain so that StaFi can record relevant information to prevent repeated payments.
The native multi-signature address is jointly managed by acceptors, and the transfer transaction to the user can only be executed after the threshold number is reached. The use of multi-signature technology can prevent malicious transfers caused by some acceptors' dishonest conduct. It can also prevent transaction failures caused by single node failures, and improve the fault tolerance of the entire system.
The acceptor, by running the rToken-swap service, provides convenience for the quick redemption of the user's native token, and at the same time obtains the corresponding incentives through the difference between the rToken exchange rate and the real exchange rate.
At present, the key system parameters of ERD solution, such as the admission of acceptors and the information of exchange stalls, are set by the StaFi Foundation. This will be gradually opened to the community for management in the later stage, to realize the real DAO.
The native address currently adopts a relatively mature multi-signature scheme, which has also been running in the rToken-relay service in a stable manner, and has undergone online testing over a long period of time. In the later stages, it will follow other advanced solutions in the industry (such as threshold signatures, etc.) and gradually replace them.
The report of each processing result is reported through a proposal. Only when a certain number of acceptors report the same information will it be finally written into the blockchain, which can prevent some acceptors from performing malicious actions. If any malicious node is found, the StaFi Foundation will immediately remove it and enforce strict punishments.
Last modified 1yr ago