From a9730af656f633bca04e6452815ddd92f2f2d178 Mon Sep 17 00:00:00 2001 From: Greg Weisbrod Date: Thu, 21 Oct 2021 00:35:13 -0400 Subject: [PATCH] add NFTokenCreateOffer --- src/models/transactions/NFTokenCreateOffer.ts | 116 ++++++++++++++++++ src/models/transactions/index.ts | 5 + src/models/transactions/transaction.ts | 9 ++ 3 files changed, 130 insertions(+) create mode 100644 src/models/transactions/NFTokenCreateOffer.ts diff --git a/src/models/transactions/NFTokenCreateOffer.ts b/src/models/transactions/NFTokenCreateOffer.ts new file mode 100644 index 00000000..b8274273 --- /dev/null +++ b/src/models/transactions/NFTokenCreateOffer.ts @@ -0,0 +1,116 @@ +import { ValidationError } from '../../errors' +import { Amount } from '../common' + +import { + BaseTransaction, + GlobalFlags, + validateBaseTransaction, + isAmount, +} from './common' + +/** + * Transaction Flags for an NFTokenCreateOffer Transaction. + * + * @category Transaction Flags + */ +export enum NFTokenCreateOfferFlags { + /** + * If set, indicates that the offer is a sell offer. + * Otherwise, it is a buy offer. + */ + tfSellToken = 0x00000001, +} + +/** + * Map of flags to boolean values representing {@link NFTokenCreateOffer} transaction + * flags. + * + * @category Transaction Flags + */ +export interface NFTokenCreateOfferFlagsInterface extends GlobalFlags { + tfSellToken?: boolean +} + +/** + * The NFTokenCreateOffer transaction creates an NFToken object and adds it to the + * relevant NFTokenPage object of the minter. If the transaction is + * successful, the newly minted token will be owned by the minter account + * specified by the transaction. + */ +export interface NFTokenCreateOffer extends BaseTransaction { + TransactionType: 'NFTokenCreateOffer' + /** + * Indicates the AccountID of the account that initiated the + * transaction. + */ + Account: string + /** + * Identifies the TokenID of the NFToken object that the + * offer references. + */ + TokenID: string + /** + * Indicates the amount expected or offered for the Token. + * + * The amount must be non-zero, except where this is an + * offer is an offer to sell and the asset is XRP; then it + * is legal to specify an amount of zero, which means that + * the current owner of the token is giving it away, gratis, + * either to anyone at all, or to the account identified by + * the Destination field. + */ + Amount: Amount + /** + * Indicates the AccountID of the account that owns the + * corresponding NFToken. + * + * If the offer is to buy a token, this field must be present + * and it must be different than Account (since an offer to + * buy a token one already holds is meaningless). + * + * If the offer is to sell a token, this field must not be + * present, as the owner is, implicitly, the same as Account + * (since an offer to sell a token one doesn't already hold + * is meaningless). + */ + Owner?: string + /** + * Indicates the time after which the offer will no longer + * be valid. The value is the number of seconds since the + * Ripple Epoch. + */ + Expiration?: number + /** + * 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 + Flags?: number | NFTokenCreateOfferFlagsInterface +} + +/** + * Verify the form and type of an NFTokenCreateOffer at runtime. + * + * @param tx - An NFTokenCreateOffer Transaction. + * @throws When the NFTokenCreateOffer is Malformed. + */ +export function validateNFTokenCreateOffer(tx: Record): void { + validateBaseTransaction(tx) + + if (tx.Account == null) { + throw new ValidationError('NFTokenCreateOffer: missing field Account') + } + + if (tx.TokenID == null) { + throw new ValidationError('NFTokenCreateOffer: missing field TokenID') + } + + if (tx.Amount == null) { + throw new ValidationError('NFTokenCreateOffer: missing field Amount') + } + + if (typeof tx.Amount !== 'string' && !isAmount(tx.Amount)) { + throw new ValidationError('NFTokenCreateOffer: invalid Amount') + } +} diff --git a/src/models/transactions/index.ts b/src/models/transactions/index.ts index 7af68ffc..b9af0cb7 100644 --- a/src/models/transactions/index.ts +++ b/src/models/transactions/index.ts @@ -15,6 +15,11 @@ export { EscrowCancel } from './escrowCancel' export { EscrowCreate } from './escrowCreate' export { EscrowFinish } from './escrowFinish' export { NFTokenBurn } from './NFTokenBurn' +export { + NFTokenCreateOffer, + NFTokenCreateOfferFlags, + NFTokenCreateOfferFlagsInterface, +} from './NFTokenCreateOffer' export { NFTokenMint, NFTokenMintFlags, diff --git a/src/models/transactions/transaction.ts b/src/models/transactions/transaction.ts index 4a899b5b..ea4c6c80 100644 --- a/src/models/transactions/transaction.ts +++ b/src/models/transactions/transaction.ts @@ -18,6 +18,10 @@ import { EscrowCreate, validateEscrowCreate } from './escrowCreate' import { EscrowFinish, validateEscrowFinish } from './escrowFinish' import { TransactionMetadata } from './metadata' import { NFTokenBurn, validateNFTokenBurn } from './NFTokenBurn' +import { + NFTokenCreateOffer, + validateNFTokenCreateOffer, +} from './NFTokenCreateOffer' import { NFTokenMint, validateNFTokenMint } from './NFTokenMint' import { OfferCancel, validateOfferCancel } from './offerCancel' import { OfferCreate, validateOfferCreate } from './offerCreate' @@ -53,6 +57,7 @@ export type Transaction = | EscrowCreate | EscrowFinish | NFTokenBurn + | NFTokenCreateOffer | NFTokenMint | OfferCancel | OfferCreate @@ -132,6 +137,10 @@ export function validate(transaction: Record): void { validateNFTokenBurn(tx) break + case 'NFTokenCreateOffer': + validateNFTokenCreateOffer(tx) + break + case 'NFTokenMint': validateNFTokenMint(tx) break