Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Acala Runtime Design #2

Closed
xlc opened this issue Nov 19, 2019 · 5 comments
Closed

Acala Runtime Design #2

xlc opened this issue Nov 19, 2019 · 5 comments

Comments

@xlc
Copy link
Member

xlc commented Nov 19, 2019

Acala Runtime Design

注:

  • 很多类型的泛型参数我跳过了,实现的时候按需求添加
  • 完全没有考虑事件,这个可以最后根据前端和看护机的需求添加
  • 我们会使用 offchain_worker 来实现看护机,这部分设计会陆续添加
  • 第二版会加入 uniswap 类型的 DEX,作为价钱来源之一,参与竞拍,无缝转换各种手续费
  • 各种需要 root 权限来修改链上变量参数的方法我就不细写了,每个变量提供一个 set_xxx 就好

模块结构:

每个模块都分为这些部分:

  • Types
    • 所有模块定义的 Rust 类型,包括了 trait, struct, enum, type
  • Trait
    • Substrate Runtime Module Trait 部分,所有可以传入的类型参数
  • Storage
    • decl_storage 部分,链上存储
  • Call
    • decl_module 中的 dispatchable call 部分,用户可以调用发起的交易
  • Module
    • impl Module 部分,对其他模块的 Rust 接口,还有 module hooks (on_initialize / on_finalize / offchain_worker)
    • Module: TraitName, TraitNameTwo 的意思是实现了 TraitName 和 TraitNameTwo 这两个接口
  • Event
    • decl_event 部分,定义链上可以发生的事件

Common Modules (ORML)

  • utilities
  • traits
    • 包含了所有通用接口定义
    • Types
      • trait BasicCurrency
        • 简化版单资产管理接口
        • type Balance
        • fn transfer
        • fn deposit
        • fn withdraw
      • trait BasicCurrencyExtended: MultiCurrency
        • 增加了 Amount,支持负数
        • type Amount: TryInto<Balance> + TryFrom<Balance>
        • fn update_balance(who: AccountId, amount: Amount)
      • trait MultiCurrency
        • 多资产管理接口
        • type CurrencyId
        • type Balance
        • fn transfer
        • fn deposit
        • fn withdraw
      • trait MultiCurrencyExtended: MultiCurrency
        • 增加了 Amount,支持负数
        • type Amount: TryInto<Balance>, TryFrom<Balance>
        • fn update_balance(who: AccountId, currency_id: CurrencyId, amount: Amount)
      • trait OnNewData<Key, Value>
        • 通用新数据钩子
        • fn on_new_data(key: Key, value: Value)
      • trait DataProvider<Key, Value>
        • 通用数据读取接口
        • fn get(key: Key) -> Option<Value>
      • trait PriceProvider<CurrencyId, Price>
        • 价钱数据接口
        • fn get_price(base: CurrencyId, quote: CurrencyId): Option<Price>
      • struct AuctionInfo
        • 拍卖信息
        • bid: Option<(AccountId, Balance)>
        • end: Option<BlockNumber>
      • trait Auction
        • 通用拍卖接口
        • type AuctionId
        • type Balance
        • fn auction_info(id: AuctionId): AuctionInfo
        • fn update_auction(id: AuctionId, info: AuctionInfo)
        • fn new_auction(start: BlockNumber, end: Option<BlockNumber>): AuctionId
      • struct OnNewBidResult
        • accept_bid: bool
          • 是否接受出价,比如如果增加金额低于要求
        • auction_end: Option<Option<BlockNumber>>
          • 是否修改拍卖结束时间
      • trait AuctionHandler
        • 拍卖接口钩子
        • fn on_new_bid(now: BlockNumber, id: AuctionId, new_bid: (AccountId, Balance), last_bid: Option<(AccountId, Balance)>): OnNewBidResult
          • 接收到新竞价,通过返回值决定是否接受竞价和修改拍卖截止时间
          • 这里要预先提取当前赢家资金,返回上个赢家资金
        • fn on_auction_ended(id: AuctionId, winner: Option<(AccountId, Balance)>)
          • 拍卖结束
  • tokens
    • 多资产接口实现
    • Trait
      • CurrencyId
    • Call
      • transfer(origin, currency_id: CurrencyId, to: AccountId, value: Balance)
    • Module: MultiCurrencyExtended
  • currencies
    • 用来聚合 srml-balances 和 orml-tokens
    • Trait
      • NativeCurrency: Currency
      • NativeCurrencyId: Get<CurrencyId>
      • MultiCurrency: MultiCurrencyExtended<CurrencyId = Self::MultiCurrencyId>
    • Call
      • transfer(origin, currency_id: CurrencyId, to: AccountId, value: Balance)
    • Module: MultiCurrencyExtended
  • oracle
    • 预言机模块,实现通用数据接口
    • Trait
      • Key
      • Value
      • OperatorOrigin: EnsureOrigin
      • OnNewData: OnNewData<Key, Value>
    • Call
      • feed_data(origin, key: Key, value: Value)
    • Module: DataProvider<Key, Value>
  • prices
    • 价钱模块,价钱数据接口实现
    • Trait
      • CurrencyId
      • Source: DataProvider<CurrencyId, Price>
    • Module: PriceProvider<CurrencyId, Price>
  • auction
    • 通用拍卖接口实现。具体的货品交割由 AuctionHandler 的实现来执行
    • Trait
      • Balance
      • Handler: AuctionHandler
    • Storage
      • Actions: map AuctionId => Option<AuctionInfo>
      • AuctionEndTime: map (BlockNumber, Option<AuctionId>) => Option<LinkedItem<AuctionId>>
        • 每个区块中会截止的拍卖,用链表存储
      • AuctionCounts: AuctionId
    • Call
      • bid(origin, id: AuctionId, value: Balance)
    • Module: Auction
      • on_finalize
        • 遍历 AuctionEndTime ,结束拍卖

Acala Runtime Modules

  • support
    • 辅助模块,包含同样帮助方法和常用类型
    • Types
      • trait RiskManager<CurrencyId, Balance, DebitBalance>
        • 风险评估接口
        • fn requiredCollateralRatio(currency_id: CurrencyId): Ratio
          • 要求抵押率,用户开仓,调整仓位,不能让抵押率低于这个值
        • fn check_position_adjuestment(currency_id: CurrencyId, collaterals: SignedBalance<Balance>, debits: SignedBalance<DebitBalance>): Result
          • 检验这个操作后抵押率是否足够,足够的话 Ok, 不够的话 Err
      • type Price = Fixed128
      • type ExchangeRate = Fixed128
      • type Ratio = Fixed128
  • debits
    • 债务管理模块,负责处理债务单位的增减
    • Trait
      • Currency: BasicCurrencyExtended
        • 稳定币资产
      • DebitBalance
        • 债务资产单位
      • Convert: Convert<(CurrencyId, Balance), DebitBalance> + Convert<(CurrencyId, DebitBalance), Balance>
        • 负责不同资产的债务单位和稳定币直接的转换
    • Module: MultiCurrencyExtended
      • 实现了多资产接口,但所有交易都会根据债务单位和稳定币的汇率转换成稳定币资产,然后交易稳定币资产
  • vaults
    • 负载与资产管理模块
    • Trait
      • Currency: MultiCurrencyExtended
        • 多资产接口
      • DebitCurrency: MultiCurrencyExtended
        • 债务资产接口
      • PriceSource: PriceProvider<CurrencyId, Price>
        • 价钱来源
      • RickManager: RickManager<CurrencyId>
        • 风控接口
    • Storage
      • Debits: double_map AccountId, CurrencyId => DebitBalance
        • 用户对映资产 CDP 的债务金额
      • Collaterals: double_map AccountId, CurrencyId => Balance
        • 用户对映资产的 CDP 的抵押物金额
      • TotalDebits: map CurrencyId => DebitBalance
      • TotalCollaterals: map CurrencyId => Balance
    • Module
      • collateral_ratio(who: AccountId, currency_id: CurrencyId): Option<Fixed128>
        • 用户对映资产的 CDP 的抵押率
      • update_position(who: AccountId, currency_id: CurrencyId, collaterals: SignedBalance<Balance>, debits: SignedBalance<DebitBalance>): Result
        • 修改 CDP。对债务的变动会直接修改用户 aUSD 余额,对抵押物的变动也会直接修改用户抵押物的余额
  • auction_manager
    • 负责管理不同类型的拍卖
    • 需要讨论的几个点
      • 拍卖时长硬顶软顶
      • 大量抵押物同时拍卖的时候是否需要把时间稍微分离,防止对市场过大的冲击
    • 需要添加的功能
      • 盈余拍卖
      • 负债拍卖
    • Types
      • struct AuctionItem
        • 不会变的信息放在这个结构里面,会变的单独放,比如截止时间
        • owenr: AccountId
        • currency_id: CurrencyId
        • amount: Balance
        • target: Balance
        • start_time: BlockNumber
      • trait AuctionManagerHandler
        • on_auction_end(currency_id: CurrencyId, target: Balance, bid: Balance)
          • 反馈拍卖结果,这个主要是用来报告债务的。如果达标了,auction_manager 自己处理相关的所有资产转移
    • Trait
      • Currency: MultiCurrencyExtended
      • Auction: Auction
      • Handler: AuctionManagerHandler
    • Storage
      • MaximumAuctionSize: map CurrencyId => Balance
        • 一次拍卖最大数额,超过这个的会被拆分为多次拍卖
      • Auctions: map AuctionId => Option<AuctionItem>
        • 所有当前拍卖,结束的清空
      • AuctionWinner: map AuctionId => Option<AccountId, Balance>
        • 当前竞拍赢家,拍卖结束后清空
    • Constants
      • MinimumIncrementSize: Permill
        • 每次拍卖最低增加幅度百分比,两次竞拍价格至少要增加 max(target, last_bid) * MinimumIncrementSize
      • AuctionTimeToClose: BlockNumber
        • 每次成功竞拍后,拍卖截止时间调整为当前时间加上这个值
      • AuctionDurationSoftCap: BlockNumber
        • 如果拍卖持续了这么久还未结束,竞价最低增加金额翻倍,截止延长时间减半
        • 这个机制实现前可以详细讨论下,看看需不需要调整
    • Module: AuctionHandler
      • new_collateral_auction(who: AccountId, currency_id: CurrencyId, amount: Balance, target: Balance)
        • 转移用户CDP中抵押品
        • 转移债务
        • 如果数额太大,拆分成多个拍卖
  • cdp_engine
    • CDP 引擎
    • Types
      • struct DebitExchangeRateConvertor: Convert<(CurrencyId, Balance), DebitBalance> + Convert<(CurrencyId, DebitBalance), Balance>
        • 进行不同资产的债务单位和稳定币直接的转换
    • Trait: auction_manager::Trait + vaults::Trait
      • Currency: MultiCurrencyExtended
      • PriceSource: PriceProvider<CurrencyId, Price>
        • 价钱来源
      • CollateralCurrencyIds: Get<static '&[CurrencyId]>
        • 可抵押资产编号
      • StableCurrencyId: Get<CurrencyId>
        • 稳定币编号
      • Auction: Auction
        • 拍卖模块接口
    • Constants
      • GlobalStabilityFee: Permill
        • 全局稳定费率
      • DefaultLiquidationRatio: Ratio
        • 默认触发清算抵押率
    • DefaultLiquidationPenalty: Permill
      • 默认清算惩罚费率
      • DefaultDebitExchangeRate: ExchangeRate
        • 默认起始负债汇率
      • MinimumDebitValue: Balance
        • 债务下限,单位为 aUSD。CDP 通过操作后,要么债务为0,要么 aUSD 价值必须大于这个数值
    • Storage
      • StabilityFee: map CurrencyId => Option<Permill>
        • 对映资产的稳定费率,加上全局稳定费率得到真正费率
      • LiquidationRatio: map CurrencyId => Option<Ratio>
        • 对映资产的触发清算抵押率
      • LiquidationPenalty: map CurrencyId => Option<Permill>
        • 对映资产的清算惩罚费率
      • RequiredCollateralRatio: map CurrencyId => Option<Ratio>
        • 对映资产的要求抵押率,用户无法主动把抵押率降低与这个值
      • DebitExchangeRate: map CurrencyId => Option<ExchangeRate>
        • 对映资产的负债汇率
      • MaximumTotalDebitValue: map CurrencyId => Balance
        • 对映资产的总共债务上限
    • Module: RiskManager, AuctionManagerHandler
      • 实现了风控接口,验证CDP抵押率,债务上限,债务下限
      • 实现了拍卖钩子,结算处理拍卖结果
      • on_finalize
        • 根据当前 StabilityFee 更新每个 DebitExchangeRate
  • honzon
    • Trait: cdp_engine::Trait
    • Storage
      • Authorizations: double_map AccountId, (CurrencyId, AccountId) => bool
        • 仓位权限授权
    • Call
      • update_vault(origin, currency_id: CurrencyId, collateral: Amount, debits: Amount)
        • 修改仓位
        • 添加 / 减少 抵押品会从用户账上 转入 / 转出 抵押品
        • 添加 / 减少 债务会从用户稳定币账户执行对映的 铸币 / 烧币
      • transfer_vault(origin, currency_id: CurrencyId, to: AccountId, collateral: Amount, debits: Amount)
        • 转移仓位,必须有对方授权
      • authorize(origin, currency_id: CurrencyId, to: AccountId)
        • 授权给对方
      • unauthorize(origin, currency_id: CurrencyId, to: AccountId)
        • 取消授权
      • unauthorize_all(origin)
        • 取消全部授权
  • primitives
    • Types
      • enum TokensCurrencyId
        • AUSD = 1
        • DOT
        • XBTC
      • enum CurrencyId
        • ACA = 0
        • AUSD
        • DOT
        • XBTC
  • Runtime
    • Balances
    • Tokens
      • CurrencyId = CurrencyId
    • Currencies
      • NativeCurrency = Balances
      • NativeCurrencyId = Get<CurrencyId::ACA>
      • MultiCurrency = Tokens
    • Oracle
    • Auction
      • Handler = AuctionManager
    • Prices
      • CurrencyId = CurrencyId
      • Source = Oracle
    • Debits
      • Currency = tokens::NativeCurrency
      • DebitBalance = Balance
      • Convert = cdp_engine::DebitExchangeRateConvertor
    • Vaults
      • Currency = Tokens
      • DebitCurrency = Debits
      • PriceSource = Prices
      • RickManager = CDPEngine
    • AuctionManager
      • Auction = Auction
      • Handler = CDPEngine
    • CDPEngine
      • Currency = Currencies
      • PriceSource = Prices
      • CollateralCurrencyIds = [CurrencyId::DOT, CurrencyId::XBTC]
    • StableCurrencyId: Currency::AUSD
      • Auction: Auction
    • Honzon
@bette7
Copy link
Member

bette7 commented Nov 20, 2019

Features to be designed, not an exhaustive list

  1. CDP type: the combination of asset type, risk parameters define a loan type
  2. Emergency shutdown mechanism
  3. Basic governance: e.g. council voting mechanism for upgrade, parameter adjustment etc
  4. Additional mechanism to balance supply & demand of aUSD such as money market
  5. ACA network token integration: e.g. for voting, and as capital used in an auction

@xlc
Copy link
Member Author

xlc commented Nov 22, 2019

  • honzon
    • Call
      • liquidate(origin, who: AccountId, currency_id: CurrentyId)
        • 检测仓位是否危险,是的话针对该仓位发起资产拍卖
        • 无需检测 origin
    • Module
      • offchain_worker()
        • 只有 validator 执行
        • 遍历所有债务,计算会出现危险仓位的的资产价钱
          等到资产价钱到危险价位,发送平仓交易
        • 重复
        • 这个需要等到 double_map 支持遍历

@xlc
Copy link
Member Author

xlc commented Dec 5, 2019

可以加个机制根据 TotalCollaterals 提升 RequiredCollateralRatio 和 LiquidationRatio。目的是避免一次拍卖太多资产。如果 LiquidationRatio 和 TotalCollaterals 挂钩,在太多 TotalCollaterals 的情况下,LiquidationRatio 会比较高,当价钱不稳定的时候就可以提前拍卖相对不安全的仓位。在一部分仓位被拍卖掉的情况下,LiquidationRatio 会降低,避免一次拍卖太多的资产。
提升 RequiredCollateralRatio 同时可以作为aUSD发行的 soft cap。在发行量太大的情况下,RequiredCollateralRatio 会非常高,使得借贷出来的比率非常少。

@xlc
Copy link
Member Author

xlc commented Dec 5, 2019

可以链上整合一个DEX。DEX可以作为价钱的(一部分)来源,同时也可以与拍卖机制结合,同时可以让用户无缝切换费用付费货币。

@xlc
Copy link
Member Author

xlc commented Jan 14, 2020

V1 已经完成,其他的在新的issue里追踪

@xlc xlc closed this as completed Jan 14, 2020
jamcarbon added a commit that referenced this issue Jul 12, 2022
xlc pushed a commit that referenced this issue Jul 14, 2022
* testing custom AWS runners

* fix update

* testing custom AWS runners #1

* test custom runners #2

* test#3

* test#4

* final ready
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants