code.vegaprotocol.io/vega@v0.79.0/core/integration/docs/AMM.md (about) 1 ## Integration test framework for AMM 2 3 ### Creating, amending and submitting AMMs 4 5 To create a new AMM submission: 6 7 ``` 8 When the parties submit the following AMM: 9 | party | market id | amount | slippage | base | lower bound | upper bound | lower leverage | upper leverage | error | 10 | party id | market ID | commitment amount | tolerance as float | uint | min price uint | max price uint | margin ratio at lower bound as float | margin ratio at upper bound as float | OPTIONAL: error expected on submission | 11 ``` 12 13 All fields, except for `error` are required here. 14 15 Once an AMM has been created, we may want to amend it, so to amend an existing AMM: 16 17 ``` 18 Then the parties ammend the following AMM: 19 | party | market id | amount | slippage | base | lower bound | upper bound | lower leverage | upper leverage | error | 20 | party id (REQUIRED) | market ID (REQUIRED) | optional: uint | float (REQUIRED) | optional | optional | optional | optional | optional | optional | 21 ``` 22 23 The only 3 fields that are required are `party`, `market id`, and `slippage`. Any other fields omitted will not be updated. 24 25 Lastly, cancelling an existing AMM can be done through: 26 27 ``` 28 And the parties cancel the following AMM: 29 | party | market id | method | error | 30 | party id | market ID | CancelAMM_Method | OPTIONAL: error expected | 31 ``` 32 33 Details on the [`CancelAMM_Method` type](types.md#Cancel-AMM-Method) 34 35 ### Checking AMM pools 36 37 To see what's going on with an existing AMM, we can check the AMM pool events with the following steps: 38 39 ``` 40 Then the AMM pool status should be: 41 | party | market id | amount | status | reason | base | lower bound | upper bound | lower leverage | upper leverage | 42 | party ID | market ID | commitment amout | AMM_Status | AMM_StatusReason | uint | uint | uint | float | float | 43 ``` 44 45 Required fields are `party`, `market id`, `amount`, and `status`. All others are optional. 46 47 Details on the [`AMM_Status` type](types.md#AMM-Status) 48 Details on the [`AMM_StatusReason` type](types.md#AMM-Status-Reason) 49 50 Checking the status for a given AMM only checks the most recent AMMPool event that was emitted. If we need to check all statuses a given AMM passed through during a scenario, use the following step: 51 52 ``` 53 And the following AMM pool events should be emitted: 54 | party | market id | amount | status | reason | base | lower bound | upper bound | lower leverage | upper leverage | 55 | party ID | market ID | commitment amout | AMM_Status | AMM_StatusReason | uint | uint | uint | float | float | 56 ``` 57 58 The table data is identical to that used in the previous step, with the same optional/required fields. The difference here is that we can check whether the correct events were emitted in a scenario like this: 59 60 Details on the [`AMM_Status` type](types.md#AMM-Status) 61 Details on the [`AMM_StatusReason` type](types.md#AMM-Status-Reason) 62 63 64 ``` 65 When 66 When the parties submit the following AMM: 67 | party | market id | amount | slippage | base | lower bound | upper bound | lower leverage | upper leverage | 68 | party1 | ETH/DEC24 | 10000 | 0.1 | 1000 | 900 | 1100 | 0.2 | 0.15 | 69 Then the parties amend the following AMM: 70 | party | market id | amount | slippage | base | lower bound | upper bound | upper leverage | 71 | party1 | ETH/DEC24 | 20000 | 0.15 | 1010 | 910 | 1110 | 0.2 | 72 # simple status check, only 1 event can be checked, checking for the initial submission will fail 73 And the AMM pool status should be: 74 | party | market id | amount | status | base | lower bound | upper bound | lower leverage | upper leverage | 75 | party1 | ETH/DEC24 | 20000 | STATUS_ACTIVE | 1010 | 910 | 1110 | 0.2 | 0.2 | 76 When the parties cancel the following AMM: 77 | party | market id | method | 78 | party1 | ETH/DEC24 | METHOD_IMMEDIATE | 79 # check all events emitted so far 80 Then the following AMM pool events should be emitted: 81 | party | market id | amount | status | base | lower bound | upper bound | lower leverage | upper leverage | reason | 82 | party1 | ETH/DEC24 | 10000 | STATUS_ACTIVE | 1000 | 900 | 1100 | 0.2 | 0.15 | | 83 | party1 | ETH/DEC24 | 20000 | STATUS_ACTIVE | 1010 | 910 | 1110 | 0.2 | 0.2 | | 84 | party1 | ETH/DEC24 | 20000 | STATUS_CANCELLED | 1010 | 910 | 1110 | 0.2 | 0.2 | STATUS_REASON_CANCELLED_BY_PARTY | 85 ``` 86 87 ### Checking AMM account balances and transfers 88 89 The AMM pool and sub-accounts are assigned derrived ID's, which can't be specified from the integration test scenario. To allow verifying the balances of the accounts, and check whether or not the expected transfers to and from said account happened, it's possible to assign aliases to the derived ID's. 90 91 ``` 92 Then set the following AMM sub account aliases: 93 | party | market id | alias | 94 | party ID | market ID | account owner alias | 95 ``` 96 97 This step _must_ be used _after_ the AMM submission has been made (ie after we've created the AMM pool), otherwise it will fail. 98 99 Once an alias has been created, we can check the balance of the AMM pool account using the following step: 100 101 ``` 102 Then parties have the following AMM account balances: 103 | account alias | asset | balance | 104 | alias set earlier | asset | expected balance | 105 ``` 106 107 The alias set in the first step is mapped to the internally derived ID, and the balance will be checked in the normal way (getting the most recent account balance event, compare the balance to the expected amount). 108 109 Checking transfers is done through the existing step, but a new optional field was added: 110 111 ``` 112 Then the following transfers should happen: 113 | from | from account | to | to account | market id | amount | asset | type | is amm | 114 | from owner | from account type | to owner | to account type | market ID | amount | asset | OPTIONAL: transfer type | OPTIONAL: bool | 115 ``` 116 117 The new field `is amm` should be `true` for transfers involving AMM sub-accounts. An AMM sub-account is defined as being a general account, does not have a market, and the owner is a pre-defined alias (as per above). For example, a transfer from a general account to an AMM pool sub-account would look something like this: 118 119 ``` 120 When the parties submit the following AMM: 121 | party | market id | amount | slippage | base | lower bound | upper bound | lower leverage | upper leverage | 122 | party1 | ETH/DEC24 | 10000 | 0.1 | 1000 | 900 | 1100 | 0.2 | 0.15 | 123 Then set the the following AMM sub account aliases: 124 | party | market id | alias | 125 | party1 | ETH/DEC24 | party1-amm-acc | 126 And the following transfers should happen: 127 | from | from account | to | to account | market id | amount | asset | is amm | 128 | party1 | ACCOUNT_TYPE_GENERAL | party1-amm-acc | ACCOUNT_TYPE_GENERAL | | 10000 | ETH | true | 129 ``` 130 131 It's important to note that a vAMM will be receiving fees from particular markets, and when holding a position, will create a margin account on the given market, transfers for this can be checked using the same alias like so: 132 133 ``` 134 And the following transfers should happen: 135 | from | from account | to | to account | market id | amount | asset | is amm | type | 136 | | ACCOUNT_TYPE_FEES_MAKER | vamm1-id | ACCOUNT_TYPE_GENERAL | ETH/MAR22 | 1 | USD | true | TRANSFER_TYPE_MAKER_FEE_RECEIVE | 137 | | ACCOUNT_TYPE_FEES_MAKER | vamm1-id | ACCOUNT_TYPE_GENERAL | ETH/MAR22 | 2 | USD | true | TRANSFER_TYPE_MAKER_FEE_RECEIVE | 138 | vamm1-id | ACCOUNT_TYPE_GENERAL | | ACCOUNT_TYPE_SETTLEMENT | ETH/MAR22 | 88 | USD | true | TRANSFER_TYPE_MTM_LOSS | 139 | vamm1-id | ACCOUNT_TYPE_GENERAL | vamm1-id | ACCOUNT_TYPE_MARGIN | ETH/MAR22 | 274 | USD | true | TRANSFER_TYPE_MARGIN_LOW | 140 ``` 141 142 For more details on how to check transfer data [see here](transfers.md). 143 144 ### Checking AMM trades 145 146 Because the parties who created the vAMM don't actually trade directly, the derived party ID will appear as the buyer or seller. The account owner alias created above should therefore be used to check the buyer/seller of trades involving the vAMM pools: 147 148 ``` 149 Then the following trades should be executed: 150 | buyer | price | size | seller | is amm | 151 | party5 | 106 | 1 | vamm1-alias | true | 152 | party5 | 110 | 1 | party2 | | 153 | party5 | 128 | 2 | vamm1-alias | true | 154 | party5 | 140 | 1 | party4 | | 155 ``` 156 157 This step has added the `is amm` column (optional) which should be set to `true` to lookup the actual party ID using the vAMM alias created in the step outlined above. 158 159 ### Checking vAMM position 160 161 Like any party, we may want to check the overall position of a vAMM pool. To do this, we can use the existing step to check the profit and loss. Like with the trade-check and transfer checks, a boolean column `is amm` was added to instruct the integration test framewokr to interpret the party ID given as being a vAMM alias. 162 163 ``` 164 Then the parties should have the following profit and loss: 165 | party | volume | unrealised pnl | realised pnl | is amm | 166 | party5 | 5 | 88 | 0 | | 167 | party1 | 1 | 40 | 0 | | 168 | party2 | -2 | -70 | 0 | | 169 | party4 | -1 | 0 | 0 | | 170 | vamm1-id | -3 | -58 | 0 | true | 171 ``` 172 173 ### Checking AMM account balances 174 175 Just like checking trades and positions, checking the `vAMM` account balances can be done using the optional `is amm` column to the table provided to the relevant step. 176 177 ``` 178 Then the parties should have the following account balances: 179 | party | asset | market | general | margin | is amm | 180 | party1 | USD | ETH/MAR22 | 100000 | 0 | | 181 | vamm1-id | USD | ETH/MAR22 | 123 | 456 | true | 182 ``` 183 184 ### DEBUG STEPS 185 186 The debug steps specific to AMMs are simply ways of printing out the AMM pool event data in human-readable form: 187 188 ``` 189 # simply dump all AMM pool events 190 debug all AMM pool events 191 # debug all AMM pool events for a given party 192 debug AMM pool events for party "([^"]+)" 193 # debug all AMM pool events for a given market 194 debug all AMM pool events for market "([^"]+)" 195 # debug all AMM pool events for a given market and party 196 debug all AMM pool events for market "([^"]+)" and party "([^"]+)" 197 ```