From 96379b77908f49a8ee42d18cb9239413c536b511 Mon Sep 17 00:00:00 2001 From: inazarov Date: Fri, 15 Oct 2021 11:28:32 +0300 Subject: [PATCH] union --- src/common.ts | 60 +++++++++++++++++++ src/exchange.ts | 74 +++++++++++++++++++++++ src/index.ts | 58 ++---------------- src/invoke.ts | 72 +++++++++++++++++++++++ src/network.ts | 8 +++ src/parts.ts | 134 +----------------------------------------- transactions/index.ts | 2 +- 7 files changed, 222 insertions(+), 186 deletions(-) create mode 100644 src/common.ts create mode 100644 src/exchange.ts create mode 100644 src/invoke.ts create mode 100644 src/network.ts diff --git a/src/common.ts b/src/common.ts new file mode 100644 index 0000000..11dbc17 --- /dev/null +++ b/src/common.ts @@ -0,0 +1,60 @@ +export type ExchangeTransactionOrderType = 'buy' | 'sell'; +export type Base64Script = string; +export type Base58Bytes = string; +export type Proofs = Array; +export type Long = string | number; +export type AssetDecimals = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; +export type Base64string = string; + +export const GENESIS_TYPE = 1 as 1; +export const PAYMENT_TYPE = 2 as 2; +export const ISSUE_TYPE = 3 as 3; +export const TRANSFER_TYPE = 4 as 4; +export const REISSUE_TYPE = 5 as 5; +export const BURN_TYPE = 6 as 6; +export const EXCHANGE_TYPE = 7 as 7; +export const LEASE_TYPE = 8 as 8; +export const CANCEL_LEASE_TYPE = 9 as 9; +export const ALIAS_TYPE = 10 as 10; +export const MASS_TRANSFER_TYPE = 11 as 11; +export const DATA_TYPE = 12 as 12; +export const SET_SCRIPT_TYPE = 13 as 13; +export const SPONSORSHIP_TYPE = 14 as 14; +export const SET_ASSET_SCRIPT_TYPE = 15 as 15; +export const INVOKE_SCRIPT_TYPE = 16 as 16; +export const UPDATE_ASSET_INFO_TYPE = 17 as 17; + +export const INTEGER_DATA_TYPE = 'integer' as 'integer'; +export const BOOLEAN_DATA_TYPE = 'boolean' as 'boolean'; +export const STRING_DATA_TYPE = 'string' as 'string'; +export const BINARY_DATA_TYPE = 'binary' as 'binary'; + +export const TRANSACTION_TYPE = { + GENESIS: GENESIS_TYPE, + PAYMENT: PAYMENT_TYPE, + ISSUE: ISSUE_TYPE, + TRANSFER: TRANSFER_TYPE, + REISSUE: REISSUE_TYPE, + BURN: BURN_TYPE, + EXCHANGE: EXCHANGE_TYPE, + LEASE: LEASE_TYPE, + CANCEL_LEASE: CANCEL_LEASE_TYPE, + ALIAS: ALIAS_TYPE, + MASS_TRANSFER: MASS_TRANSFER_TYPE, + DATA: DATA_TYPE, + SET_SCRIPT: SET_SCRIPT_TYPE, + SPONSORSHIP: SPONSORSHIP_TYPE, + SET_ASSET_SCRIPT: SET_ASSET_SCRIPT_TYPE, + INVOKE_SCRIPT: INVOKE_SCRIPT_TYPE, + UPDATE_ASSET_INFO: UPDATE_ASSET_INFO_TYPE, +}; + +export const DATA_FIELD_TYPE = { + INTEGER: INTEGER_DATA_TYPE, + BOOLEAN: BOOLEAN_DATA_TYPE, + STRING: STRING_DATA_TYPE, + BINARY: BINARY_DATA_TYPE, +}; + +export type TransactionType = typeof TRANSACTION_TYPE[keyof typeof TRANSACTION_TYPE]; +export type DataFiledType = typeof DATA_FIELD_TYPE[keyof typeof DATA_FIELD_TYPE]; diff --git a/src/exchange.ts b/src/exchange.ts new file mode 100644 index 0000000..91a191b --- /dev/null +++ b/src/exchange.ts @@ -0,0 +1,74 @@ +import { ExchangeTransaction } from '../transactions'; +import { ExchangeTransactionOrderType, Long } from './common'; + +export type ExchangeTransactionOrderData = { + version: number; + orderType: ExchangeTransactionOrderType; + assetPair: { + amountAsset: string | null; + priceAsset: string | null; + }; + price: LONG; + amount: LONG; + timestamp: number; + expiration: number; + matcherFee: LONG; + matcherPublicKey: string; + senderPublicKey: string; +}; + +export type WithVersion< + Target extends Record, + Version extends number +> = Target & { + version: Version; +}; + +type ExchangeOrderWithCustomFee = ExchangeTransactionOrderData & { + matcherFeeAssetId: string | null; +}; + +export type ExchangeTransactionOrderV1 = WithVersion< + ExchangeTransactionOrderData, + 1 +>; +export type ExchangeTransactionOrderV2 = WithVersion< + ExchangeTransactionOrderData, + 2 +>; +export type ExchangeTransactionOrderV3 = WithVersion< + ExchangeOrderWithCustomFee, + 3 +>; +export type ExchangeTransactionOrderV4 = WithVersion< + ExchangeOrderWithCustomFee, + 4 +>; + +export type ExchangeTransactionOrder = + | ExchangeTransactionOrderV1 + | ExchangeTransactionOrderV2 + | ExchangeTransactionOrderV3 + | ExchangeTransactionOrderV4; + +export type SignedIExchangeTransactionOrder< + ORDER extends ExchangeTransactionOrder +> = ORDER & + (ORDER extends { version: 1 } + ? { signature: string } + : { proofs: Array }); + +export type ExchangeTransactionOrderMap = { + 1: ExchangeTransactionOrderV1; + 2: ExchangeTransactionOrderV2; + 3: ExchangeTransactionOrderV3; + 4: ExchangeTransactionOrderV4; +}; + +export type ExchangeTransactionOrderByTx< + TX extends ExchangeTransaction +> = TX extends { version: 1 } + ? ExchangeTransactionOrderMap[1] + : TX extends { version: 2 } + ? ExchangeTransactionOrderMap[1 | 2 | 3] + : ExchangeTransactionOrder; diff --git a/src/index.ts b/src/index.ts index 7baf290..0fbef9d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,55 +1,9 @@ export * from '../transactions'; -export * from './parts'; - -export const GENESIS_TYPE = 1 as 1; -export const PAYMENT_TYPE = 2 as 2; -export const ISSUE_TYPE = 3 as 3; -export const TRANSFER_TYPE = 4 as 4; -export const REISSUE_TYPE = 5 as 5; -export const BURN_TYPE = 6 as 6; -export const EXCHANGE_TYPE = 7 as 7; -export const LEASE_TYPE = 8 as 8; -export const CANCEL_LEASE_TYPE = 9 as 9; -export const ALIAS_TYPE = 10 as 10; -export const MASS_TRANSFER_TYPE = 11 as 11; -export const DATA_TYPE = 12 as 12; -export const SET_SCRIPT_TYPE = 13 as 13; -export const SPONSORSHIP_TYPE = 14 as 14; -export const SET_ASSET_SCRIPT_TYPE = 15 as 15; -export const INVOKE_SCRIPT_TYPE = 16 as 16; -export const UPDATE_ASSET_INFO_TYPE = 17 as 17; - -export const INTEGER_DATA_TYPE = 'integer' as 'integer'; -export const BOOLEAN_DATA_TYPE = 'boolean' as 'boolean'; -export const STRING_DATA_TYPE = 'string' as 'string'; -export const BINARY_DATA_TYPE = 'binary' as 'binary'; -export const TRANSACTION_TYPE = { - GENESIS: GENESIS_TYPE, - PAYMENT: PAYMENT_TYPE, - ISSUE: ISSUE_TYPE, - TRANSFER: TRANSFER_TYPE, - REISSUE: REISSUE_TYPE, - BURN: BURN_TYPE, - EXCHANGE: EXCHANGE_TYPE, - LEASE: LEASE_TYPE, - CANCEL_LEASE: CANCEL_LEASE_TYPE, - ALIAS: ALIAS_TYPE, - MASS_TRANSFER: MASS_TRANSFER_TYPE, - DATA: DATA_TYPE, - SET_SCRIPT: SET_SCRIPT_TYPE, - SPONSORSHIP: SPONSORSHIP_TYPE, - SET_ASSET_SCRIPT: SET_ASSET_SCRIPT_TYPE, - INVOKE_SCRIPT: INVOKE_SCRIPT_TYPE, - UPDATE_ASSET_INFO: UPDATE_ASSET_INFO_TYPE, -}; - -export const DATA_FIELD_TYPE = { - INTEGER: INTEGER_DATA_TYPE, - BOOLEAN: BOOLEAN_DATA_TYPE, - STRING: STRING_DATA_TYPE, - BINARY: BINARY_DATA_TYPE, -}; +export * from './common'; +export * from './parts'; +export * from './network'; -export type TransactionType = typeof TRANSACTION_TYPE[keyof typeof TRANSACTION_TYPE]; -export type DataFiledType = typeof DATA_FIELD_TYPE[keyof typeof DATA_FIELD_TYPE]; +// transactions +export * from './invoke'; +export * from './exchange'; \ No newline at end of file diff --git a/src/invoke.ts b/src/invoke.ts new file mode 100644 index 0000000..5149233 --- /dev/null +++ b/src/invoke.ts @@ -0,0 +1,72 @@ +import { Base64string, Long } from './common'; + +export enum EInvokeArgumentType { + INTEGER = 'integer', + STRING = 'string', + BOOLEAN = 'boolean', + BINARY = 'binary', + UNION = 'union', +} + +export type InvokeScriptCall = { + function: string; + args: Array>; +}; + +export type InvokeScriptPayment = { + assetId: string | null; + amount: LONG; +}; + +export type InvokeScriptCallArgument = + | InvokeScriptCallStringArgument + | InvokeScriptCallBinaryArgument + | InvokeScriptCallBooleanArgument + | InvokeScriptCallIntegerArgument + | InvokeScriptCallUnionArgument + | InvokeScriptCallListArgument< + LONG, + | InvokeScriptCallStringArgument + | InvokeScriptCallBinaryArgument + | InvokeScriptCallBooleanArgument + | InvokeScriptCallIntegerArgument + >; + +export type InvokeScriptCallArgumentGeneric = { + type: Type; + value: Value; +}; + +export type InvokeScriptCallStringArgument = InvokeScriptCallArgumentGeneric< + EInvokeArgumentType.STRING, + string +>; +export type InvokeScriptCallBinaryArgument = InvokeScriptCallArgumentGeneric< + EInvokeArgumentType.BINARY, + Base64string +>; +export type InvokeScriptCallBooleanArgument = InvokeScriptCallArgumentGeneric< + EInvokeArgumentType.BOOLEAN, + boolean +>; +export type InvokeScriptCallIntegerArgument< + LONG = Long +> = InvokeScriptCallArgumentGeneric; +export type InvokeScriptCallUnionArgument = InvokeScriptCallArgumentGeneric< + EInvokeArgumentType.UNION, + boolean | string | Base64string | Long +> & { + valueType: EInvokeArgumentType.BINARY + | EInvokeArgumentType.BOOLEAN + | EInvokeArgumentType.INTEGER + | EInvokeArgumentType.STRING +}; +export type InvokeScriptCallListArgument< + LONG, + ITEMS extends + | InvokeScriptCallStringArgument + | InvokeScriptCallBinaryArgument + | InvokeScriptCallBooleanArgument + | InvokeScriptCallIntegerArgument + | InvokeScriptCallUnionArgument +> = InvokeScriptCallArgumentGeneric<'list', Array>; diff --git a/src/network.ts b/src/network.ts new file mode 100644 index 0000000..73c2642 --- /dev/null +++ b/src/network.ts @@ -0,0 +1,8 @@ +export enum EChainId { + MAINNET = 87, + STAGENET = 83, + TESTNET = 84, + PRIVATE = 80, + DEVNET = 68, + DEVNET_C = 67 +} diff --git a/src/parts.ts b/src/parts.ts index 0ba2b60..16d02ee 100644 --- a/src/parts.ts +++ b/src/parts.ts @@ -1,71 +1,11 @@ -import { DATA_FIELD_TYPE, ExchangeTransaction } from './index'; +import { DATA_FIELD_TYPE, Long, Base64string } from './common'; -export type ExchangeTransactionOrderType = 'buy' | 'sell'; -export type Base64Script = string; -export type Base58Bytes = string; -export type Proofs = Array; -export type Long = string | number; -export type AssetDecimals = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; -export type Base64string = string; export type WithApiMixin = WithId & { sender: string; height: number; }; -export type InvokeScriptCall = { - function: string; - args: Array>; -}; - -export type InvokeScriptPayment = { - assetId: string | null; - amount: LONG; -}; - -export type InvokeScriptCallArgument = - | InvokeScriptCallStringArgument - | InvokeScriptCallBinaryArgument - | InvokeScriptCallBooleanArgument - | InvokeScriptCallIntegerArgument - | InvokeScriptCallListArgument< - LONG, - | InvokeScriptCallStringArgument - | InvokeScriptCallBinaryArgument - | InvokeScriptCallBooleanArgument - | InvokeScriptCallIntegerArgument - >; - -export type InvokeScriptCallArgumentGeneric = { - type: Type; - value: Value; -}; - -export type InvokeScriptCallStringArgument = InvokeScriptCallArgumentGeneric< - 'string', - string ->; -export type InvokeScriptCallBinaryArgument = InvokeScriptCallArgumentGeneric< - 'binary', - Base64string ->; -export type InvokeScriptCallBooleanArgument = InvokeScriptCallArgumentGeneric< - 'boolean', - boolean ->; -export type InvokeScriptCallIntegerArgument< - LONG = Long -> = InvokeScriptCallArgumentGeneric<'integer', LONG>; - -export type InvokeScriptCallListArgument< - LONG, - ITEMS extends - | InvokeScriptCallStringArgument - | InvokeScriptCallBinaryArgument - | InvokeScriptCallBooleanArgument - | InvokeScriptCallIntegerArgument -> = InvokeScriptCallArgumentGeneric<'list', Array>; - export interface WithId { id: string; } @@ -98,78 +38,6 @@ export type DataTransactionEntryBoolean = DataTransactionEntryGeneric< boolean >; -export type ExchangeTransactionOrderData = { - version: number; - orderType: ExchangeTransactionOrderType; - assetPair: { - amountAsset: string | null; - priceAsset: string | null; - }; - price: LONG; - amount: LONG; - timestamp: number; - expiration: number; - matcherFee: LONG; - matcherPublicKey: string; - senderPublicKey: string; -}; - -export type WithVersion< - Target extends Record, - Version extends number -> = Target & { - version: Version; -}; - -type ExchangeOrderWithCustomFee = ExchangeTransactionOrderData & { - matcherFeeAssetId: string | null; -}; - -export type ExchangeTransactionOrderV1 = WithVersion< - ExchangeTransactionOrderData, - 1 ->; -export type ExchangeTransactionOrderV2 = WithVersion< - ExchangeTransactionOrderData, - 2 ->; -export type ExchangeTransactionOrderV3 = WithVersion< - ExchangeOrderWithCustomFee, - 3 ->; -export type ExchangeTransactionOrderV4 = WithVersion< - ExchangeOrderWithCustomFee, - 4 ->; - -export type ExchangeTransactionOrder = - | ExchangeTransactionOrderV1 - | ExchangeTransactionOrderV2 - | ExchangeTransactionOrderV3 - | ExchangeTransactionOrderV4; - -export type SignedIExchangeTransactionOrder< - ORDER extends ExchangeTransactionOrder -> = ORDER & - (ORDER extends { version: 1 } - ? { signature: string } - : { proofs: Array }); - -export type ExchangeTransactionOrderMap = { - 1: ExchangeTransactionOrderV1; - 2: ExchangeTransactionOrderV2; - 3: ExchangeTransactionOrderV3; - 4: ExchangeTransactionOrderV4; -}; - -export type ExchangeTransactionOrderByTx< - TX extends ExchangeTransaction -> = TX extends { version: 1 } - ? ExchangeTransactionOrderMap[1] - : TX extends { version: 2 } - ? ExchangeTransactionOrderMap[1 | 2 | 3] - : ExchangeTransactionOrder; - export type DataTransactionEntry = | DataTransactionEntryInteger | DataTransactionEntryString diff --git a/transactions/index.ts b/transactions/index.ts index 2722708..45b6133 100644 --- a/transactions/index.ts +++ b/transactions/index.ts @@ -163,7 +163,7 @@ export type SetScriptTransactionFields = { export type SponsorshipTransactionFields = { assetId: string; - minSponsoredAssetFee: LONG; + minSponsoredAssetFee: LONG | null; } export type SetAssetScriptTransactionFields = {