From f3ec0f654eea9a0ca85126316120cd4c15415b32 Mon Sep 17 00:00:00 2001 From: Denis Angell Date: Tue, 14 Feb 2023 19:28:17 -0500 Subject: [PATCH] add transactions --- .../xrpl/src/models/transactions/index.ts | 9 ++ .../src/models/transactions/transaction.ts | 30 ++++++ .../src/models/transactions/uriTokenBurn.ts | 60 ++++++++++++ .../src/models/transactions/uriTokenBuy.ts | 83 +++++++++++++++++ .../src/models/transactions/uriTokenClear.ts | 61 ++++++++++++ .../src/models/transactions/uriTokenMint.ts | 92 +++++++++++++++++++ .../src/models/transactions/uriTokenSell.ts | 89 ++++++++++++++++++ 7 files changed, 424 insertions(+) create mode 100644 packages/xrpl/src/models/transactions/uriTokenBurn.ts create mode 100644 packages/xrpl/src/models/transactions/uriTokenBuy.ts create mode 100644 packages/xrpl/src/models/transactions/uriTokenClear.ts create mode 100644 packages/xrpl/src/models/transactions/uriTokenMint.ts create mode 100644 packages/xrpl/src/models/transactions/uriTokenSell.ts diff --git a/packages/xrpl/src/models/transactions/index.ts b/packages/xrpl/src/models/transactions/index.ts index 49944424..5fe363cc 100644 --- a/packages/xrpl/src/models/transactions/index.ts +++ b/packages/xrpl/src/models/transactions/index.ts @@ -45,3 +45,12 @@ export { SetRegularKey } from './setRegularKey' export { SignerListSet } from './signerListSet' export { TicketCreate } from './ticketCreate' export { TrustSetFlagsInterface, TrustSetFlags, TrustSet } from './trustSet' +export { + URITokenMintFlagsInterface, + URITokenMintFlags, + URITokenMint, +} from './uriTokenMint' +export { URITokenBurn } from './uriTokenBurn' +export { URITokenSell } from './uriTokenSell' +export { URITokenBuy } from './uriTokenBuy' +export { URITokenClear } from './uriTokenClear' diff --git a/packages/xrpl/src/models/transactions/transaction.ts b/packages/xrpl/src/models/transactions/transaction.ts index 898e0232..cdd50fcb 100644 --- a/packages/xrpl/src/models/transactions/transaction.ts +++ b/packages/xrpl/src/models/transactions/transaction.ts @@ -51,6 +51,11 @@ import { SetRegularKey, validateSetRegularKey } from './setRegularKey' import { SignerListSet, validateSignerListSet } from './signerListSet' import { TicketCreate, validateTicketCreate } from './ticketCreate' import { TrustSet, validateTrustSet } from './trustSet' +import { URITokenBurn, validateURITokenBurn } from './uriTokenBurn' +import { URITokenBuy, validateURITokenBuy } from './uriTokenBuy' +import { URITokenClear, validateURITokenClear } from './uriTokenClear' +import { URITokenMint, validateURITokenMint } from './uriTokenMint' +import { URITokenSell, validateURITokenSell } from './uriTokenSell' /** * @category Transaction Models @@ -80,6 +85,11 @@ export type Transaction = | SignerListSet | TicketCreate | TrustSet + | URITokenBurn + | URITokenBuy + | URITokenClear + | URITokenMint + | URITokenSell /** * @category Transaction Models @@ -204,6 +214,26 @@ export function validate(transaction: Record): void { validateTrustSet(tx) break + case 'URITokenMint': + validateURITokenMint(tx) + break + + case 'URITokenBurn': + validateURITokenBurn(tx) + break + + case 'URITokenCreateSellOffer': + validateURITokenSell(tx) + break + + case 'URITokenBuy': + validateURITokenBuy(tx) + break + + case 'URITokenCancelSellOffer': + validateURITokenClear(tx) + break + default: throw new ValidationError( `Invalid field TransactionType: ${tx.TransactionType}`, diff --git a/packages/xrpl/src/models/transactions/uriTokenBurn.ts b/packages/xrpl/src/models/transactions/uriTokenBurn.ts new file mode 100644 index 00000000..dbf64b97 --- /dev/null +++ b/packages/xrpl/src/models/transactions/uriTokenBurn.ts @@ -0,0 +1,60 @@ +import { ValidationError } from '../../errors' + +import { BaseTransaction, validateBaseTransaction } from './common' + +/** + * Map of flags to boolean values representing {@link URITokenBurn} transaction + * flags. + * + * @category Transaction Flags + * + * @example + * ```typescript + * const tx: URITokenBurn = { + * Account: 'rhFcpWDHLqpBmX4ezWiA5VLSS4e1BHqhHd', + * URITokenID: '7AFCE32EBA8BD310CC2D00BE10B76E2183337EA20444D4580E4DBDB396C101FB', + * TransactionType: 'URITokenBurn', + * } + * + * // Autofill the tx to see how flags actually look compared to the interface usage. + * const autofilledTx = await client.autofill(tx) + * console.log(autofilledTx) + * // { + * // Account: 'rhFcpWDHLqpBmX4ezWiA5VLSS4e1BHqhHd', + * // URITokenID: '7AFCE32EBA8BD310CC2D00BE10B76E2183337EA20444D4580E4DBDB396C101FB', + * // TransactionType: 'URITokenBurn', + * // Sequence: 21970384, + * // Fee: '12', + * // LastLedgerSequence: 21970404 + * // } + * ``` + */ + +/** + * An URITokenBurn transaction is effectively a limit order . It defines an + * intent to exchange currencies, and creates an Offer object if not completely. + * Fulfilled when placed. Offers can be partially fulfilled. + * + * @category Transaction Models + */ +export interface URITokenBurn extends BaseTransaction { + TransactionType: 'URITokenBurn' + /** + * Identifies the URIToken object to be removed by the transaction. + */ + URITokenID: string +} + +/** + * Verify the form and type of an URITokenBurn at runtime. + * + * @param tx - An URITokenBurn Transaction. + * @throws When the URITokenBurn is Malformed. + */ +export function validateURITokenBurn(tx: Record): void { + validateBaseTransaction(tx) + + if (tx.URITokenID == null) { + throw new ValidationError('NFTokenBurn: missing field URITokenID') + } +} diff --git a/packages/xrpl/src/models/transactions/uriTokenBuy.ts b/packages/xrpl/src/models/transactions/uriTokenBuy.ts new file mode 100644 index 00000000..e59be8b5 --- /dev/null +++ b/packages/xrpl/src/models/transactions/uriTokenBuy.ts @@ -0,0 +1,83 @@ +import { ValidationError } from '../../errors' +import { Amount } from '../common' + +import { BaseTransaction, isAmount, validateBaseTransaction } from './common' + +/** + * Map of flags to boolean values representing {@link URITokenBuy} transaction + * flags. + * + * @category Transaction Flags + * + * @example + * ```typescript + * const tx: URITokenBuy = { + * Account: 'rhFcpWDHLqpBmX4ezWiA5VLSS4e1BHqhHd', + * URITokenID: '7AFCE32EBA8BD310CC2D00BE10B76E2183337EA20444D4580E4DBDB396C101FB', + * Amount: '1000000', + * TransactionType: 'URITokenBuy', + * } + * + * // Autofill the tx to see how flags actually look compared to the interface usage. + * const autofilledTx = await client.autofill(tx) + * console.log(autofilledTx) + * // { + * // Account: 'rhFcpWDHLqpBmX4ezWiA5VLSS4e1BHqhHd', + * // URITokenID: '7AFCE32EBA8BD310CC2D00BE10B76E2183337EA20444D4580E4DBDB396C101FB', + * // Amount: '1000000', + * // TransactionType: 'URITokenBuy', + * // Sequence: 21970384, + * // Fee: '12', + * // LastLedgerSequence: 21970404 + * // } + * ``` + */ + +/** + * An URITokenBuy transaction is effectively a limit order . It defines an + * intent to exchange currencies, and creates an Offer object if not completely. + * Fulfilled when placed. Offers can be partially fulfilled. + * + * @category Transaction Models + */ +export interface URITokenBuy extends BaseTransaction { + TransactionType: 'URITokenBuy' + /** + * Identifies the URITokenID of the NFToken object that the + * offer references. + */ + URITokenID: string + /** + * Indicates the amount expected or offered for the Token. + * + * The amount must be non-zero, except when this is a sell + * offer and the asset is XRP. This would indicate that the current + * owner of the token is giving it away free, either to anyone at all, + * or to the account identified by the Destination field. + */ + Amount: Amount +} + +/** + * Verify the form and type of an URITokenBuy at runtime. + * + * @param tx - An URITokenBuy Transaction. + * @throws When the URITokenBuy is Malformed. + */ +export function validateURITokenBuy(tx: Record): void { + validateBaseTransaction(tx) + + if (tx.Account === tx.Destination) { + throw new ValidationError( + 'URITokenBuy: Destination and Account must not be equal', + ) + } + + if (tx.URITokenID == null) { + throw new ValidationError('URITokenBuy: missing field URITokenID') + } + + if (!isAmount(tx.Amount)) { + throw new ValidationError('URITokenBuy: invalid Amount') + } +} diff --git a/packages/xrpl/src/models/transactions/uriTokenClear.ts b/packages/xrpl/src/models/transactions/uriTokenClear.ts new file mode 100644 index 00000000..1a6acfc0 --- /dev/null +++ b/packages/xrpl/src/models/transactions/uriTokenClear.ts @@ -0,0 +1,61 @@ +import { ValidationError } from '../../errors' + +import { BaseTransaction, validateBaseTransaction } from './common' + +/** + * Map of flags to boolean values representing {@link URITokenClear} transaction + * flags. + * + * @category Transaction Flags + * + * @example + * ```typescript + * const tx: URITokenClear = { + * Account: 'rhFcpWDHLqpBmX4ezWiA5VLSS4e1BHqhHd', + * URITokenID: '7AFCE32EBA8BD310CC2D00BE10B76E2183337EA20444D4580E4DBDB396C101FB', + * TransactionType: 'URITokenCancelSellOffer', + * } + * + * // Autofill the tx to see how flags actually look compared to the interface usage. + * const autofilledTx = await client.autofill(tx) + * console.log(autofilledTx) + * // { + * // Account: 'rhFcpWDHLqpBmX4ezWiA5VLSS4e1BHqhHd', + * // URITokenID: '7AFCE32EBA8BD310CC2D00BE10B76E2183337EA20444D4580E4DBDB396C101FB', + * // TransactionType: 'URITokenCancelSellOffer', + * // Sequence: 21970384, + * // Fee: '12', + * // LastLedgerSequence: 21970404 + * // } + * ``` + */ + +/** + * An URITokenClear transaction is effectively a limit order . It defines an + * intent to exchange currencies, and creates an Offer object if not completely. + * Fulfilled when placed. Offers can be partially fulfilled. + * + * @category Transaction Models + */ +export interface URITokenClear extends BaseTransaction { + TransactionType: 'URITokenCancelSellOffer' + /** + * Identifies the URITokenID of the NFToken object that the + * offer references. + */ + URITokenID: string +} + +/** + * Verify the form and type of an URITokenClear at runtime. + * + * @param tx - An URITokenClear Transaction. + * @throws When the URITokenClear is Malformed. + */ +export function validateURITokenClear(tx: Record): void { + validateBaseTransaction(tx) + + if (tx.URITokenID == null) { + throw new ValidationError('URITokenClear: missing field URITokenID') + } +} diff --git a/packages/xrpl/src/models/transactions/uriTokenMint.ts b/packages/xrpl/src/models/transactions/uriTokenMint.ts new file mode 100644 index 00000000..4ac5bf58 --- /dev/null +++ b/packages/xrpl/src/models/transactions/uriTokenMint.ts @@ -0,0 +1,92 @@ +import { ValidationError } from '../../errors' +import { isHex } from '../utils' + +import { BaseTransaction, GlobalFlags, validateBaseTransaction } from './common' + +/** + * Transaction Flags for an URITokenMint Transaction. + * + * @category Transaction Flags + */ +export enum URITokenMintFlags { + /** + * If set, indicates that the minted token may be burned by the issuer even + * if the issuer does not currently hold the token. The current holder of + * the token may always burn it. + */ + tfBurnable = 0x00000001, +} + +/** + * Map of flags to boolean values representing {@link URITokenMint} transaction + * flags. + * + * @category Transaction Flags + * + * @example + * ```typescript + * const tx: URITokenMint = { + * Account: 'rhFcpWDHLqpBmX4ezWiA5VLSS4e1BHqhHd', + * URI: '697066733A2F2F434944', + * TransactionType: 'URITokenMint', + * Flags: { + * tfBurnable: true, + * }, + * } + * + * // Autofill the tx to see how flags actually look compared to the interface usage. + * const autofilledTx = await client.autofill(tx) + * console.log(autofilledTx) + * // { + * // Account: 'rhFcpWDHLqpBmX4ezWiA5VLSS4e1BHqhHd', + * // URI: '697066733A2F2F434944', + * // TransactionType: 'URITokenMint', + * // Flags: 0, + * // Sequence: 21970384, + * // Fee: '12', + * // LastLedgerSequence: 21970404 + * // } + * ``` + */ +export interface URITokenMintFlagsInterface extends GlobalFlags { + tfBurnable?: boolean +} + +/** + * An URITokenMint transaction is effectively a limit order . It defines an + * intent to exchange currencies, and creates an Offer object if not completely. + * Fulfilled when placed. Offers can be partially fulfilled. + * + * @category Transaction Models + */ +export interface URITokenMint extends BaseTransaction { + TransactionType: 'URITokenMint' + Flags?: number | URITokenMintFlagsInterface + /** + * URI that points to the data and/or metadata associated with the NFT. + * This field need not be an HTTP or HTTPS URL; it could be an IPFS URI, a + * magnet link, immediate data encoded as an RFC2379 "data" URL, or even an + * opaque issuer-specific encoding. The URI is NOT checked for validity, but + * the field is limited to a maximum length of 256 bytes. + * + * This field must be hex-encoded. You can use `convertStringToHex` to + * convert this field to the proper encoding. + */ + URI: string + + Digest?: string +} + +/** + * Verify the form and type of an URITokenMint at runtime. + * + * @param tx - An URITokenMint Transaction. + * @throws When the URITokenMint is Malformed. + */ +export function validateURITokenMint(tx: Record): void { + validateBaseTransaction(tx) + + if (typeof tx.URI === 'string' && !isHex(tx.URI)) { + throw new ValidationError('URITokenMint: URI must be in hex format') + } +} diff --git a/packages/xrpl/src/models/transactions/uriTokenSell.ts b/packages/xrpl/src/models/transactions/uriTokenSell.ts new file mode 100644 index 00000000..2db72ab6 --- /dev/null +++ b/packages/xrpl/src/models/transactions/uriTokenSell.ts @@ -0,0 +1,89 @@ +import { ValidationError } from '../../errors' +import { Amount } from '../common' + +import { BaseTransaction, isAmount, validateBaseTransaction } from './common' + +/** + * Map of flags to boolean values representing {@link URITokenSell} transaction + * flags. + * + * @category Transaction Flags + * + * @example + * ```typescript + * const tx: URITokenSell = { + * Account: 'rhFcpWDHLqpBmX4ezWiA5VLSS4e1BHqhHd', + * URITokenID: '7AFCE32EBA8BD310CC2D00BE10B76E2183337EA20444D4580E4DBDB396C101FB', + * Amount: '1000000', + * TransactionType: 'URITokenCreateSellOffer', + * } + * + * // Autofill the tx to see how flags actually look compared to the interface usage. + * const autofilledTx = await client.autofill(tx) + * console.log(autofilledTx) + * // { + * // Account: 'rhFcpWDHLqpBmX4ezWiA5VLSS4e1BHqhHd', + * // URITokenID: '7AFCE32EBA8BD310CC2D00BE10B76E2183337EA20444D4580E4DBDB396C101FB', + * // Amount: '1000000', + * // TransactionType: 'URITokenCreateSellOffer', + * // Sequence: 21970384, + * // Fee: '12', + * // LastLedgerSequence: 21970404 + * // } + * ``` + */ + +/** + * An URITokenSell transaction is effectively a limit order . It defines an + * intent to exchange currencies, and creates an Offer object if not completely. + * Fulfilled when placed. Offers can be partially fulfilled. + * + * @category Transaction Models + */ +export interface URITokenSell extends BaseTransaction { + TransactionType: 'URITokenCreateSellOffer' + /** + * Identifies the URITokenID of the NFToken object that the + * offer references. + */ + URITokenID: string + /** + * Indicates the amount expected or offered for the Token. + * + * The amount must be non-zero, except when this is a sell + * offer and the asset is XRP. This would indicate that the current + * owner of the token is giving it away free, either to anyone at all, + * or to the account identified by the Destination field. + */ + Amount: Amount + /** + * If present, indicates that this offer may only be + * accepted by the specified account. Attempts by other + * accounts to accept this offer MUST fail. + */ + Destination?: string +} + +/** + * Verify the form and type of an URITokenSell at runtime. + * + * @param tx - An URITokenSell Transaction. + * @throws When the URITokenSell is Malformed. + */ +export function validateURITokenSell(tx: Record): void { + validateBaseTransaction(tx) + + if (tx.Account === tx.Destination) { + throw new ValidationError( + 'URITokenSell: Destination and Account must not be equal', + ) + } + + if (tx.URITokenID == null) { + throw new ValidationError('URITokenSell: missing field URITokenID') + } + + if (!isAmount(tx.Amount)) { + throw new ValidationError('URITokenSell: invalid Amount') + } +}