From 2bbdcbb365d8882c169d12d4aa3b9d0795265eca Mon Sep 17 00:00:00 2001 From: Kevin Davis Date: Thu, 15 Jul 2021 17:34:16 -0500 Subject: [PATCH] update swap spec: add msgs, state, events (#966) --- x/swap/spec/02_state.md | 29 +++++++++++++++++- x/swap/spec/03_messages.md | 60 ++++++++++++++++++++++++++++++++++++++ x/swap/spec/04_events.md | 50 +++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 1 deletion(-) diff --git a/x/swap/spec/02_state.md b/x/swap/spec/02_state.md index 41a0502d..b0dbbe23 100644 --- a/x/swap/spec/02_state.md +++ b/x/swap/spec/02_state.md @@ -30,6 +30,33 @@ type AllowedPools []AllowedPool ```go // GenesisState is the state that must be provided at genesis. type GenesisState struct { - Params Params `json:"params" yaml:"params"` + Params Params `json:"params" yaml:"params"` + PoolRecords `json:"pool_records" yaml:"pool_records"` + ShareRecords `json:"share_records" yaml:"share_records"` } + +// PoolRecord represents the state of a liquidity pool +// and is used to store the state of a denominated pool +type PoolRecord struct { + // primary key + PoolID string `json:"pool_id" yaml:"pool_id"` + ReservesA sdk.Coin `json:"reserves_a" yaml:"reserves_a"` + ReservesB sdk.Coin `json:"reserves_b" yaml:"reserves_b"` + TotalShares sdk.Int `json:"total_shares" yaml:"total_shares"` +} + +// PoolRecords is a slice of PoolRecord +type PoolRecords []PoolRecord + +// ShareRecord stores the shares owned for a depositor and pool +type ShareRecord struct { + // primary key + Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"` + // secondary / sort key + PoolID string `json:"pool_id" yaml:"pool_id"` + SharesOwned sdk.Int `json:"shares_owned" yaml:"shares_owned"` +} + +// ShareRecords is a slice of ShareRecord +type ShareRecords []ShareRecord ``` diff --git a/x/swap/spec/03_messages.md b/x/swap/spec/03_messages.md index dbab134f..aee48464 100644 --- a/x/swap/spec/03_messages.md +++ b/x/swap/spec/03_messages.md @@ -3,3 +3,63 @@ order: 3 --> # Messages + + +MsgDeposit adds liquidity to a pool: + +```go +// MsgDeposit deposits liquidity into a pool +type MsgDeposit struct { + Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"` + TokenA sdk.Coin `json:"token_a" yaml:"token_a"` + TokenB sdk.Coin `json:"token_b" yaml:"token_b"` + Slippage sdk.Dec `json:"slippage" yaml:"slippage"` + Deadline int64 `json:"deadline" yaml:"deadline"` +} +``` + +The first deposit to a pool results in a `PoolRecord` being created. For each deposit, a `ShareRecord` is created or updated, depending on if the depositor has an existing deposit. The deposited tokens are converted to shares. For the first deposit to a pool, shares are equal to the geometric mean of the deposited amount. For example, depositing 200 TokenA and 100 TokenB will create `sqrt(100 * 200) = 141` shares. For subsequent deposits, shares are issued equal to the current conversion between tokens and shares in that pool. + +MsgWithdraw removes liquidity from a pool: + +```go +// MsgWithdraw deposits liquidity into a pool +type MsgWithdraw struct { + From sdk.AccAddress `json:"from" yaml:"from"` + Shares sdk.Int `json:"shares" yaml:"shares"` + MinTokenA sdk.Coin `json:"min_token_a" yaml:"min_token_a"` + MinTokenB sdk.Coin `json:"min_token_b" yaml:"min_token_b"` + Deadline int64 `json:"deadline" yaml:"deadline"` +} +``` +When withdrawing from a pool, the user specifies the amount of shares they want to withdraw, as well as the minimum amount of tokenA and tokenB that they must receive for the transaction to succeed. When withdrawing, the `ShareRecord` of the user will be decremented by the corresponding amount of shares, or deleted in the case that all liquidity has been withdrawn. If all shares of a pool have been withdrawn from a pool, the `PoolRecord` will be deleted. + +MsgSwapExactForTokens trades an exact amount of input tokens for a variable amount of output tokens, with a specified maximum slippage tolerance. + +```go +// MsgSwapExactForTokens trades an exact coinA for coinB +type MsgSwapExactForTokens struct { + Requester sdk.AccAddress `json:"requester" yaml:"requester"` + ExactTokenA sdk.Coin `json:"exact_token_a" yaml:"exact_token_a"` + TokenB sdk.Coin `json:"token_b" yaml:"token_b"` + Slippage sdk.Dec `json:"slippage" yaml:"slippage"` + Deadline int64 `json:"deadline" yaml:"deadline"` +} +``` + +When trading exact inputs for variable outputs, the swap fee is removed from TokenA and added to the pool, then slippage is calculated based on the actual amount of TokenB received compared to the desired amount of TokenB. If the realized slippage of the trade is greater than the specified slippage tolerance, the transaction fails. + +MsgSwapForExactTokens trades a variable amount of input tokens for an exact amount of output tokens, with a specified maximum slippage tolerance. + +```go +// MsgSwapForExactTokens trades coinA for an exact coinB +type MsgSwapForExactTokens struct { + Requester sdk.AccAddress `json:"requester" yaml:"requester"` + TokenA sdk.Coin `json:"token_a" yaml:"token_a"` + ExactTokenB sdk.Coin `json:"exact_token_b" yaml:"exact_token_b"` + Slippage sdk.Dec `json:"slippage" yaml:"slippage"` + Deadline int64 `json:"deadline" yaml:"deadline"` +} +``` + +When trading variable inputs for exact outputs, the fee swap fee is removed from TokenA and added to the pool, then slippage is calculated based on the actual amount of TokenA required to acquire the exact TokenB amount versus the desired TokenA required. If the realized slippage of the trade is greater than the specified slippage tolerance, the transaction fails. diff --git a/x/swap/spec/04_events.md b/x/swap/spec/04_events.md index 32c53e3e..75aa8ea2 100644 --- a/x/swap/spec/04_events.md +++ b/x/swap/spec/04_events.md @@ -7,3 +7,53 @@ order: 4 The swap module emits the following events: ## Handlers + +### MsgDeposit + +| Type | Attribute Key | Attribute Value | +| ------------ | ------------- | --------------------- | +| message | module | swap | +| message | sender | `{sender address}` | +| swap_deposit | pool_id | `{poolID}` | +| swap_deposit | depositor | `{depositor address}` | +| swap_deposit | amount | `{amount}` | +| swap_deposit | shares | `{shares}` | + +### MsgWithdraw + +| Type | Attribute Key | Attribute Value | +| ------------- | ------------- | --------------------- | +| message | module | swap | +| message | sender | `{sender address}` | +| swap_withdraw | pool_id | `{poolID}` | +| swap_withdraw | owner | `{owner address}` | +| swap_withdraw | amount | `{amount}` | +| swap_withdraw | shares | `{shares}` | + + +### MsgSwapExactForTokens + +| Type | Attribute Key | Attribute Value | +| ------------- | ------------- | ------------------------ | +| message | module | swap | +| message | sender | `{sender address}` | +| swap_trade | pool_id | `{poolID}` | +| swap_trade | requester | `{requester address}` | +| swap_trade | swap_input | `{input amount}` | +| swap_trade | swap_output | `{output amount}` | +| swap_trade | fee_paid | `{fee amount}` | +| swap_trade | exact | `{exact trade direction}`| + + +### MsgSwapForExactTokens + +| Type | Attribute Key | Attribute Value | +| ------------- | ------------- | ------------------------ | +| message | module | swap | +| message | sender | `{sender address}` | +| swap_trade | pool_id | `{poolID}` | +| swap_trade | requester | `{requester address}` | +| swap_trade | swap_input | `{input amount}` | +| swap_trade | swap_output | `{output amount}` | +| swap_trade | fee_paid | `{fee amount}` | +| swap_trade | exact | `{exact trade direction}`|