add AMMVote

This commit is contained in:
Omar Khan
2022-08-19 17:18:46 -04:00
parent 3617487c32
commit b794f909ad
4 changed files with 145 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
import { ValidationError } from '../../errors'
import {
BaseTransaction,
validateBaseTransaction,
} from './common'
/**
* AMMVote 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.
*/
export interface AMMVote extends BaseTransaction {
TransactionType: 'AMMVote'
/**
* A hash that uniquely identifies the AMM instance. This field is required.
*/
AMMID: string
/**
* Specifies the fee, in basis point.
* 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.
*/
FeeVal: number
}
/**
* Verify the form and type of an AMMVote at runtime.
*
* @param tx - An AMMVote Transaction.
* @throws When the AMMVote is Malformed.
*/
export function validateAMMVote(tx: Record<string, unknown>): void {
validateBaseTransaction(tx)
if (tx.AMMID == null) {
throw new ValidationError('AMMVote: missing field AMMID')
}
if (typeof tx.AMMID !== 'string') {
throw new ValidationError('AMMVote: AMMID must be a string')
}
if (tx.FeeVal == null) {
throw new ValidationError('AMMVote: missing field FeeVal')
}
if (typeof tx.FeeVal !== 'number') {
throw new ValidationError('AMMVote: FeeVal must be a number')
}
}

View File

@@ -9,6 +9,7 @@ export {
export { AccountDelete } from './accountDelete'
export { AMMDeposit } from './AMMDeposit'
export { AMMInstanceCreate } from './AMMInstanceCreate'
export { AMMVote } from './AMMVote'
export { AMMWithdraw } from './AMMWithdraw'
export { CheckCancel } from './checkCancel'
export { CheckCash } from './checkCash'

View File

@@ -14,6 +14,7 @@ import {
AMMInstanceCreate,
validateAMMInstanceCreate,
} from './AMMInstanceCreate'
import { AMMVote, validateAMMVote } from './AMMVote'
import { AMMWithdraw, validateAMMWithdraw } from './AMMWithdraw'
import { CheckCancel, validateCheckCancel } from './checkCancel'
import { CheckCash, validateCheckCash } from './checkCash'
@@ -65,6 +66,7 @@ export type Transaction =
| AccountSet
| AMMDeposit
| AMMInstanceCreate
| AMMVote
| AMMWithdraw
| CheckCancel
| CheckCash
@@ -132,6 +134,10 @@ export function validate(transaction: Record<string, unknown>): void {
validateAMMInstanceCreate(tx)
break
case 'AMMVote':
validateAMMVote(tx)
break
case 'AMMWithdraw':
validateAMMWithdraw(tx)
break

View File

@@ -0,0 +1,83 @@
import { assert } from 'chai'
import { validate, ValidationError } from 'xrpl-local'
/**
* AMMVote Transaction Verification Testing.
*
* Providing runtime verification testing for each specific transaction type.
*/
describe('AMMVote', function () {
it(`verifies valid AMMVote`, function () {
const validTx = {
TransactionType: 'AMMVote',
Account: 'rWYkbWkCeg8dP6rXALnjgZSjjLyih5NXm',
AMMID: '24BA86F99302CF124AB27311C831F5BFAA72C4625DDA65B7EDF346A60CC19883',
FeeVal: 25,
Sequence: 1337,
} as any
assert.doesNotThrow(() => validate(validTx))
})
it(`throws w/ missing field AMMID`, function () {
const invalid = {
TransactionType: 'AMMVote',
Account: 'rWYkbWkCeg8dP6rXALnjgZSjjLyih5NXm',
FeeVal: 25,
Sequence: 1337,
} as any
assert.throws(
() => validate(invalid),
ValidationError,
'AMMVote: missing field AMMID',
)
})
it(`throws w/ AMMID must be a string`, function () {
const invalid = {
TransactionType: 'AMMVote',
Account: 'rWYkbWkCeg8dP6rXALnjgZSjjLyih5NXm',
AMMID: 1234,
FeeVal: 25,
Sequence: 1337,
} as any
assert.throws(
() => validate(invalid),
ValidationError,
'AMMVote: AMMID must be a string',
)
})
it(`throws w/ missing field FeeVal`, function () {
const invalid = {
TransactionType: 'AMMVote',
Account: 'rWYkbWkCeg8dP6rXALnjgZSjjLyih5NXm',
AMMID: '24BA86F99302CF124AB27311C831F5BFAA72C4625DDA65B7EDF346A60CC19883',
Sequence: 1337,
} as any
assert.throws(
() => validate(invalid),
ValidationError,
'AMMVote: missing field FeeVal',
)
})
it(`throws w/ FeeVal must be a number`, function () {
const invalid = {
TransactionType: 'AMMVote',
Account: 'rWYkbWkCeg8dP6rXALnjgZSjjLyih5NXm',
AMMID: '24BA86F99302CF124AB27311C831F5BFAA72C4625DDA65B7EDF346A60CC19883',
FeeVal: '25',
Sequence: 1337,
} as any
assert.throws(
() => validate(invalid),
ValidationError,
'AMMVote: FeeVal must be a number',
)
})
})