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

feat: add AMM support #2071

Merged
merged 121 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
25bdb69
update definitions.json
khancode Aug 16, 2022
2680c34
add AMMInstanceCreate
khancode Aug 18, 2022
6bcfc2b
renamed LPTokens to LPToken in definitions.json
khancode Aug 18, 2022
ff60b54
update HISTORY.md
khancode Aug 18, 2022
3461fcc
add amm_info RPC command
khancode Aug 18, 2022
e64a668
add AMMDeposit
khancode Aug 19, 2022
35fef65
use null check for missing fields
khancode Aug 19, 2022
3617487
add AMMWithdraw
khancode Aug 19, 2022
b794f90
add AMMVote
khancode Aug 19, 2022
e79dcb5
fix lint error
khancode Aug 19, 2022
db6d32f
add max trading fee check to AMMVote
khancode Aug 20, 2022
78e0a86
refactor MAX_TRADING_FEE to be in one place
khancode Aug 20, 2022
7a035af
add AMMBid
khancode Aug 20, 2022
4b18f06
add AuthAccount interface to AMMBid
khancode Aug 20, 2022
835dc67
refactor tests
khancode Aug 20, 2022
c95f8af
add AMMID to AMMInfoResponse
khancode Aug 28, 2022
b8e4f24
update amm_info docstrings
khancode Aug 30, 2022
ed7760c
fix EPrice type to be Amount
khancode Aug 30, 2022
0de755b
update EPrice validation error message and add missing tests
khancode Aug 30, 2022
e13b27e
update definitions to fix AMM in LEDGER_ENTRY_TYPES
khancode Aug 30, 2022
2796c2a
add missing test case Asset1In and Asset2In valid
khancode Aug 30, 2022
6150d33
add missing test case Asset1Out and Asset2Out valid
khancode Aug 30, 2022
8ef2483
add negative FeeVal check
khancode Aug 30, 2022
e9778eb
update HISTORY.md to specify XLS-30
khancode Aug 30, 2022
6a06412
update wording on AMMDeposit & AMMWithdraw validation errors
khancode Aug 30, 2022
85b8a39
add negative TradingFee check
khancode Aug 30, 2022
a174d09
fix ammInfo response
khancode Sep 2, 2022
533c8ab
add AMMID as optional param in ammInfo response
khancode Sep 2, 2022
c0e0c15
fix EPrice validation checks in AMMDeposit & AMMWithdraw
khancode Sep 2, 2022
56b70aa
add VoteSlots as optional param in AMMInfoResponse
khancode Sep 5, 2022
aed4353
update VoteEntry interface
khancode Sep 5, 2022
25efafa
Merge branch 'develop' into amm
khancode Sep 13, 2022
220d9fb
fix deposit and withdraw tests
khancode Sep 13, 2022
a80f159
fix AMMBid ValidationError
khancode Sep 15, 2022
a2bce8f
update definitions.json with different AuthAccounts number
khancode Sep 15, 2022
f040378
Merge branch 'main' into amm
khancode Oct 31, 2022
a76aeb2
Change amm_info asset parameters to Currency type
khancode Oct 31, 2022
acdfbd5
API name changes with updated definitions.json
khancode Nov 9, 2022
54b86bc
rename amm_info param asset1 -> asset
khancode Nov 9, 2022
c99aa04
fix typo
khancode Nov 9, 2022
3120abc
change AMM_MAX_TRADING_FEE to 1%
khancode Nov 9, 2022
25752d7
Use Asset/Asset2 instead of AMMID for Deposit/Withdraw/Bid/Vote
khancode Nov 9, 2022
6a67715
Merge branch 'main' into amm
khancode Nov 9, 2022
64d8a90
add Deposit/Withdraw flags
khancode Nov 9, 2022
bcdd8fe
rename FeeVal -> TradingFee in VoteEntry
khancode Nov 9, 2022
eba8bd2
rename MinBidPrice -> BidMin and MaxBidPrice -> BidMax
khancode Nov 9, 2022
1c26e8b
update definitions to change Asset & Asset2 nth values to 3 & 4
khancode Nov 9, 2022
007717a
Merge branch 'main' into amm
khancode Nov 16, 2022
40388cc
update definitions
khancode Nov 16, 2022
0574fab
add Issue type and tests for Asset/Asset2
khancode Nov 17, 2022
64e4242
remove AMMID from amm_info and use Issue type
khancode Nov 17, 2022
e32db9d
update amm_info fields
khancode Nov 17, 2022
ff65441
fix lint errors
khancode Nov 17, 2022
7f2b407
Merge branch 'main' into amm
khancode Nov 17, 2022
434ad71
update unit tests
khancode Nov 17, 2022
0970753
add AMM codec-fixtures
khancode Nov 17, 2022
feafc0c
update Issue type
khancode Nov 17, 2022
98f8223
add one asset and withdraw all tests
khancode Nov 17, 2022
2bb4d77
Merge branch 'main' into amm
khancode Dec 15, 2022
ec1d8b6
refactor amm_info response fields to match AMMDevnet
khancode Dec 15, 2022
218ca00
update definitions.json with refactored error codes
khancode Jan 9, 2023
e634db3
update ammInfo.ts response model
khancode Jan 9, 2023
f61cf02
Merge branch 'main' into amm
khancode Jan 9, 2023
f3d6bd4
remove invalid fields from ammInfo.ts response model
khancode Jan 9, 2023
8cdeb2b
update time_interval description
khancode Jan 9, 2023
88b8211
Merge branch 'main' into amm
khancode Feb 15, 2023
287dc1a
rename test model names and fix lint errors
khancode Feb 15, 2023
7e84277
add Owner Reserve Fee for AMMCreate transaction
khancode Feb 15, 2023
d2f7fe6
add missing asset_frozen field
khancode Feb 15, 2023
df75b4a
replace Issue with IssuedCurrency
khancode Feb 16, 2023
d495903
Merge branch 'main' into amm
khancode Feb 16, 2023
651780f
refactor: convert flags to number
khancode Feb 16, 2023
d43fd78
update asset pair to use Currency type
khancode Feb 21, 2023
5c7e315
Merge branch 'main' into amm
khancode Feb 22, 2023
f9f2107
refactor isIssue to isCurrency
khancode Feb 23, 2023
bad598f
add AMM ledger entry object, lsfAMM flag, amm fields to LedgerEntryRe…
khancode May 16, 2023
94bb54a
Merge branch 'main' into amm
khancode May 16, 2023
aa37dfe
update definitions.json
khancode May 16, 2023
30b4406
Merge branch 'main' into amm
khancode May 23, 2023
a2f5b57
WIP defintions
khancode May 23, 2023
b80a196
Merge branch 'main' into amm
khancode May 23, 2023
8c34754
update codec-fixtures
khancode May 23, 2023
70609e0
fix definitions test
khancode May 24, 2023
5324829
Merge branch 'main' into amm
khancode May 24, 2023
85bc5b7
Merge branch 'main' into amm
khancode May 25, 2023
5a38651
Merge branch 'main' into amm
khancode May 31, 2023
b289350
Merge branch 'main' into amm
khancode Jun 2, 2023
426aefb
Merge branch 'main' into amm
khancode Jun 4, 2023
7664b62
update DiscountedFee definition
khancode Jun 6, 2023
143f860
update definitions
khancode Jun 7, 2023
f8eb832
update codec-fixtures
khancode Jun 7, 2023
f52ea8e
Merge branch 'main' into amm
khancode Jun 9, 2023
d7c4361
update definitions
khancode Jun 9, 2023
c1ee18e
Merge branch 'main' into amm
khancode Jun 9, 2023
be76ffd
Merge branch 'main' into amm
khancode Jul 10, 2023
2287313
Merge branch 'main' into amm
ckniffen Jul 17, 2023
e1fcc99
update unit tests
khancode Jul 17, 2023
41e0ee9
update amm_info response
khancode Jul 17, 2023
41ca08f
sort imports/exports
khancode Jul 17, 2023
8cb4a1e
update jsdoc
khancode Jul 17, 2023
1a9e842
update amm_info jsdoc
khancode Jul 17, 2023
08bd26a
Merge branch 'main' into amm
khancode Jul 17, 2023
a28198c
update jsdoc
khancode Jul 17, 2023
a3070db
Merge branch 'main' into amm
khancode Jul 25, 2023
5878433
convert caution to all caps
khancode Jul 25, 2023
34b667d
add validation for AuthAccounts
khancode Jul 27, 2023
70d180c
refactor and export interfaces
khancode Jul 27, 2023
2f5b634
use Currency type
khancode Aug 1, 2023
17e7e86
Merge branch 'main' into amm
khancode Aug 7, 2023
a7b3cf4
Merge branch 'main' into amm
khancode Aug 8, 2023
29e5348
update definitions
khancode Aug 8, 2023
b5a8b73
add AMMDelete
khancode Aug 8, 2023
32e431e
Merge branch 'main' into amm
khancode Aug 12, 2023
5e098f1
Merge branch 'main' into amm
khancode Aug 16, 2023
440ef57
rename Issue to Currency in error message
khancode Aug 16, 2023
434754c
mark asset frozen as optional fields
khancode Aug 16, 2023
bffce20
refactor isAuthAccounts
khancode Aug 16, 2023
760eb9a
add AMMDelete jsdoc
khancode Aug 16, 2023
1e00753
rename to validateAuthAccounts
khancode Aug 17, 2023
14f6829
fix typo
khancode Aug 17, 2023
e463631
fix typo in unit test
khancode Aug 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
319 changes: 318 additions & 1 deletion packages/ripple-binary-codec/src/enums/definitions.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions packages/xrpl/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Subscribe to [the **xrpl-announce** mailing list](https://groups.google.com/g/xr
* `Wallet.fromMnemonic` detects when an invalid encoding is provided, and throws an error
* Made unexpected errors in `submitAndWait` more verbose to make them easier to debug.

### Added
* Support for Automated Market Maker (AMM).
khancode marked this conversation as resolved.
Show resolved Hide resolved

## 2.3.1 (2022-06-27)
### Fixed
* Signing tx with standard currency codes with lowercase and allowed symbols causing an error on decode.
Expand Down
4 changes: 4 additions & 0 deletions packages/xrpl/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import {
AccountOffersResponse,
AccountTxRequest,
AccountTxResponse,
// AMM methods
AMMInfoRequest,
AMMInfoResponse,
khancode marked this conversation as resolved.
Show resolved Hide resolved
GatewayBalancesRequest,
GatewayBalancesResponse,
NoRippleCheckRequest,
Expand Down Expand Up @@ -298,6 +301,7 @@ class Client extends EventEmitter {
): Promise<AccountObjectsResponse>
public async request(r: AccountOffersRequest): Promise<AccountOffersResponse>
public async request(r: AccountTxRequest): Promise<AccountTxResponse>
public async request(r: AMMInfoRequest): Promise<AMMInfoResponse>
public async request(r: BookOffersRequest): Promise<BookOffersResponse>
public async request(r: ChannelVerifyRequest): Promise<ChannelVerifyResponse>
public async request(
Expand Down
81 changes: 81 additions & 0 deletions packages/xrpl/src/models/methods/ammInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Amount, IssuedCurrencyAmount } from '../common'

import { BaseRequest, BaseResponse } from './baseMethod'

/**
* The `amm_info` command retrieves information about an AMM instance.
* Returns an {@link AMMInfoResponse}.
*
* @category Requests
*/
export interface AMMInfoRequest extends BaseRequest {
command: 'amm_info'

/**
* A hash that uniquely identifies the AMM instance.
*/
amm_id?: string

/**
* Specifies one of the pool assets (XRP or token) of the AMM instance.
*/
asset1?: Amount

/**
* Specifies the other pool asset of the AMM instance.
*/
asset2?: Amount
}
khancode marked this conversation as resolved.
Show resolved Hide resolved

/**
* Response expected from an {@link AMMInfoRequest}.
*
* @category Responses
*/
export interface AMMInfoResponse extends BaseResponse {
result: {
/**
* The account that tracks the balance of LPTokens between the AMM instance via Trustline.
*/
AMMAccount: string
khancode marked this conversation as resolved.
Show resolved Hide resolved

/**
* Specifies one of the pool assets (XRP or token) of the AMM instance.
*/
Asset1: Amount

/**
* Specifies the other pool asset of the AMM instance.
*/
Asset2: Amount

/**
* Represents the liquidity providers' shares of the AMM instance's pools.
* LPTokens are tokens on XRPL. Each LPToken represents a proportional share of each pool of the AMM instance.
* The AMM instance account issues the LPTokens to LPs upon liquidity provision.
* LPTokens are balanced in the LPs trustline upon liquidity removal.
khancode marked this conversation as resolved.
Show resolved Hide resolved
*/
LPToken: IssuedCurrencyAmount

/**
* Specifies the fee, in basis point, to be charged to the traders for the trades
* executed against the AMM instance. Trading fee is a percentage of the trading volume.
* Valid values for this field are between 0 and 65000 inclusive.
* A value of 1 is equivalent to 1/10 bps or 0.001%, allowing trading fee
* between 0% and 65%. This field is required.
*/
TradingFee: number

/**
* The ledger index of the current in-progress ledger, which was used when
* retrieving this information.
*/
ledger_current_index?: number

/**
* True if this data is from a validated ledger version; if omitted or set
* to false, this data is not final.
*/
validated?: boolean
}
}
5 changes: 5 additions & 0 deletions packages/xrpl/src/models/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
AccountOffersResponse,
} from './accountOffers'
import { AccountTxRequest, AccountTxResponse } from './accountTx'
import { AMMInfoRequest, AMMInfoResponse } from './ammInfo'
import { ErrorResponse } from './baseMethod'
import { BookOffersRequest, BookOffer, BookOffersResponse } from './bookOffers'
import { ChannelVerifyRequest, ChannelVerifyResponse } from './channelVerify'
Expand Down Expand Up @@ -87,6 +88,7 @@ type Request =
| AccountObjectsRequest
| AccountOffersRequest
| AccountTxRequest
| AMMInfoRequest
khancode marked this conversation as resolved.
Show resolved Hide resolved
| GatewayBalancesRequest
| NoRippleCheckRequest
// ledger methods
Expand Down Expand Up @@ -137,6 +139,7 @@ type Response =
| AccountObjectsResponse
| AccountOffersResponse
| AccountTxResponse
| AMMInfoResponse
| GatewayBalancesResponse
| NoRippleCheckResponse
// ledger methods
Expand Down Expand Up @@ -195,6 +198,8 @@ export {
AccountOffersResponse,
AccountTxRequest,
AccountTxResponse,
AMMInfoRequest,
AMMInfoResponse,
GatewayBalancesRequest,
GatewayBalancesResponse,
NoRippleCheckRequest,
Expand Down
85 changes: 85 additions & 0 deletions packages/xrpl/src/models/transactions/AMMBid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { ValidationError } from '../../errors'
import { Amount } from '../common'

import { BaseTransaction, isAmount, validateBaseTransaction } from './common'

const MAX_AUTH_ACCOUNTS = 4

interface AuthAccount {
AuthAccount: {
Account: string
}
}

/**
* AMMBid is used for submitting a vote for the trading fee of an AMM Instance.
*
* Any XRPL account that holds LPToken for an AMM instance may submit this
* transaction to vote for the trading fee for that instance.
*/
mvadari marked this conversation as resolved.
Show resolved Hide resolved
export interface AMMBid extends BaseTransaction {
TransactionType: 'AMMBid'

/**
* A hash that uniquely identifies the AMM instance. This field is required.
*/
AMMID: string

/**
* This field represents the minimum price that the bidder wants to pay for the slot.
* It is specified in units of LPToken. If specified let MinSlotPrice be X and let
* the slot-price computed by price scheduling algorithm be Y, then bidder always pays
* the max(X, Y).
khancode marked this conversation as resolved.
Show resolved Hide resolved
*/
MinSlotPrice?: Amount

/**
* This field represents the maximum price that the bidder wants to pay for the slot.
* It is specified in units of LPToken.
*/
MaxSlotPrice?: Amount

/**
* This field represents an array of XRPL account IDs that are authorized to trade
* at the discounted fee against the AMM instance.
* A maximum of four accounts can be provided.
*/
AuthAccounts?: AuthAccount[]
}

/**
* Verify the form and type of an AMMBid at runtime.
*
* @param tx - An AMMBid Transaction.
* @throws When the AMMBid is Malformed.
*/
export function validateAMMBid(tx: Record<string, unknown>): void {
validateBaseTransaction(tx)

if (tx.AMMID == null) {
throw new ValidationError('AMMBid: missing field AMMID')
}

if (typeof tx.AMMID !== 'string') {
throw new ValidationError('AMMBid: AMMID must be a string')
}

if (tx.MinSlotPrice != null && !isAmount(tx.MinSlotPrice)) {
throw new ValidationError('AMMBid: MinSlotPrice must be an Amount')
}

if (tx.MaxSlotPrice != null && !isAmount(tx.MaxSlotPrice)) {
throw new ValidationError('AMMBid: MaxSlotPrice must be an Amount')
}

if (tx.AuthAccounts != null) {
if (!Array.isArray(tx.AuthAccounts)) {
throw new ValidationError(`AMMBid: AuthAccounts must be a string array`)
}
if (tx.AuthAccounts.length > MAX_AUTH_ACCOUNTS) {
khancode marked this conversation as resolved.
Show resolved Hide resolved
throw new ValidationError(
`AMMBid: AuthAccounts length must not be greater than ${MAX_AUTH_ACCOUNTS}`,
)
}
}
}
99 changes: 99 additions & 0 deletions packages/xrpl/src/models/transactions/AMMDeposit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* eslint-disable complexity -- required for validateAMMDeposit */
khancode marked this conversation as resolved.
Show resolved Hide resolved
import { ValidationError } from '../../errors'
import { Amount, IssuedCurrencyAmount } from '../common'

import {
BaseTransaction,
isAmount,
isIssuedCurrency,
validateBaseTransaction,
} from './common'

/**
* AMMDeposit is the deposit transaction used to add liquidity to the AMM instance pool,
* thus obtaining some share of the instance's pools in the form of LPToken.
*
* The following are the recommended valid combinations:
* - LPToken
* - Asset1In
* - Asset1In and Asset2In
* - Asset1In and LPToken
* - Asset1In and EPrice
*/
khancode marked this conversation as resolved.
Show resolved Hide resolved
export interface AMMDeposit extends BaseTransaction {
TransactionType: 'AMMDeposit'

/**
* A hash that uniquely identifies the AMM instance. This field is required.
*/
AMMID: string

/**
* Specifies the amount of shares of the AMM instance pools that the trader
* wants to redeem or trade in.
*/
LPToken?: IssuedCurrencyAmount

/**
* Specifies one of the pool assets (XRP or token) of the AMM instance to
* deposit more of its value.
*/
Asset1In?: Amount

/**
* Specifies the other pool asset of the AMM instance to deposit more of
* its value.
*/
Asset2In?: Amount

/**
* Specifies the maximum effective-price that LPToken can be traded out.
*/
EPrice?: number
khancode marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Verify the form and type of an AMMDeposit at runtime.
*
* @param tx - An AMMDeposit Transaction.
* @throws When the AMMDeposit is Malformed.
*/
export function validateAMMDeposit(tx: Record<string, unknown>): void {
validateBaseTransaction(tx)

if (tx.AMMID == null) {
throw new ValidationError('AMMDeposit: missing field AMMID')
}

if (typeof tx.AMMID !== 'string') {
throw new ValidationError('AMMDeposit: AMMID must be a string')
}

if (tx.Asset2In != null && tx.Asset1In == null) {
throw new ValidationError('AMMDeposit: must set Asset1In with Asset2In')
} else if (tx.EPrice != null && tx.Asset1In == null) {
throw new ValidationError('AMMDeposit: must set Asset1In with EPrice')
} else if (tx.LPToken == null && tx.Asset1In == null) {
throw new ValidationError(
'AMMDeposit: must set either or both LPToken with Asset1In',
khancode marked this conversation as resolved.
Show resolved Hide resolved
)
}

if (tx.LPToken != null && !isIssuedCurrency(tx.LPToken)) {
throw new ValidationError(
'AMMDeposit: LPToken must be an IssuedCurrencyAmount',
)
}

if (tx.Asset1In != null && !isAmount(tx.Asset1In)) {
throw new ValidationError('AMMDeposit: Asset1In must be an Amount')
}

if (tx.Asset2In != null && !isAmount(tx.Asset2In)) {
throw new ValidationError('AMMDeposit: Asset2In must be an Amount')
}

if (tx.EPrice != null && typeof tx.EPrice !== 'string') {
throw new ValidationError('AMMDeposit: EPrice must be a string')
khancode marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading