From ab51167d0adb0cd8dca3620a2c17f2ef3765d719 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Mon, 30 Oct 2023 14:29:05 +0100 Subject: [PATCH 1/4] native token feature --- .../examples/client/05-get-address-balance.ts | 13 +++++---- .../consolidate-outputs.ts | 4 +-- .../nodejs/lib/types/block/output/feature.ts | 27 +++++++++++++++-- .../nodejs/lib/types/block/output/output.ts | 29 +++++++++---------- 4 files changed, 48 insertions(+), 25 deletions(-) diff --git a/bindings/nodejs/examples/client/05-get-address-balance.ts b/bindings/nodejs/examples/client/05-get-address-balance.ts index 9f8491b8cb..b0fd76b600 100644 --- a/bindings/nodejs/examples/client/05-get-address-balance.ts +++ b/bindings/nodejs/examples/client/05-get-address-balance.ts @@ -54,11 +54,14 @@ async function run() { for (const outputResponse of addressOutputs) { const output = outputResponse['output']; if (output instanceof CommonOutput) { - (output as CommonOutput).getNativeTokens()?.forEach((token) => { - totalNativeTokens[token.id] = - (totalNativeTokens[token.id] || BigInt(0)) + - token.amount; - }); + const nativeTokenFeature = ( + output as CommonOutput + ).getNativeToken(); + if (nativeTokenFeature != undefined) { + totalNativeTokens[nativeTokenFeature.nativeToken.id] = + (totalNativeTokens[nativeTokenFeature.nativeToken.id] || + BigInt(0)) + nativeTokenFeature.nativeToken.amount; + } } totalAmount += output.getAmount(); diff --git a/bindings/nodejs/examples/how_tos/accounts_and_addresses/consolidate-outputs.ts b/bindings/nodejs/examples/how_tos/accounts_and_addresses/consolidate-outputs.ts index 50c1e3c330..a0b56f90cd 100644 --- a/bindings/nodejs/examples/how_tos/accounts_and_addresses/consolidate-outputs.ts +++ b/bindings/nodejs/examples/how_tos/accounts_and_addresses/consolidate-outputs.ts @@ -55,7 +55,7 @@ async function run() { Utils.hexToBech32(address.toString(), 'rms'), output.getAmount(), output instanceof CommonOutput - ? (output as CommonOutput).getNativeTokens() + ? (output as CommonOutput).getNativeToken() ?? [] : [], ); }); @@ -93,7 +93,7 @@ async function run() { Utils.hexToBech32(address.toString(), 'rms'), output.getAmount(), output instanceof CommonOutput - ? (output as CommonOutput).getNativeTokens() + ? (output as CommonOutput).getNativeToken() : undefined, ); }); diff --git a/bindings/nodejs/lib/types/block/output/feature.ts b/bindings/nodejs/lib/types/block/output/feature.ts index ba396da321..15665af627 100644 --- a/bindings/nodejs/lib/types/block/output/feature.ts +++ b/bindings/nodejs/lib/types/block/output/feature.ts @@ -10,6 +10,7 @@ import { } from './block-issuer-key'; import { u64 } from '../../utils/type-aliases'; import { EpochIndex } from '../../block/slot'; +import { INativeToken } from '../../models/native-token'; /** * All of the feature block types. @@ -23,8 +24,9 @@ enum FeatureType { Metadata = 2, /** A Tag feature. */ Tag = 3, - BlockIssuer = 4, - Staking = 5, + NativeToken = 4, + BlockIssuer = 5, + Staking = 6, } /** The base class for features. */ @@ -107,6 +109,25 @@ class TagFeature extends Feature { } } +/** + * Native token feature. + */ +class NativeTokenFeature extends Feature { + /** + * The inner native token held by this feature. + */ + readonly nativeToken: INativeToken; + + /** + * Creates a new `NativeTokenFeature`. + * @param nativeToken The native token stored with the feature. + */ + constructor(nativeToken: INativeToken) { + super(FeatureType.NativeToken); + this.nativeToken = nativeToken; + } +} + /** * Block Issuer feature. */ @@ -172,6 +193,7 @@ const FeatureDiscriminator = { { value: IssuerFeature, name: FeatureType.Issuer as any }, { value: MetadataFeature, name: FeatureType.Metadata as any }, { value: TagFeature, name: FeatureType.Tag as any }, + { value: NativeTokenFeature, name: FeatureType.NativeToken as any }, { value: BlockIssuerFeature, name: FeatureType.BlockIssuer as any }, { value: StakingFeature, name: FeatureType.Staking as any }, ], @@ -185,6 +207,7 @@ export { IssuerFeature, MetadataFeature, TagFeature, + NativeTokenFeature, BlockIssuerFeature, StakingFeature, }; diff --git a/bindings/nodejs/lib/types/block/output/output.ts b/bindings/nodejs/lib/types/block/output/output.ts index 95bfdd45af..ab7e757cf3 100644 --- a/bindings/nodejs/lib/types/block/output/output.ts +++ b/bindings/nodejs/lib/types/block/output/output.ts @@ -5,15 +5,15 @@ import { UnlockCondition, UnlockConditionDiscriminator, } from './unlock-condition'; -import { Feature, FeatureDiscriminator } from './feature'; +import { Feature, FeatureDiscriminator, NativeTokenFeature } from './feature'; // Temp solution for not double parsing JSON import { plainToInstance, Type } from 'class-transformer'; -import { HexEncodedString, hexToBigInt, NumericString, u64 } from '../../utils'; +import { HexEncodedString, NumericString, u64 } from '../../utils'; import { TokenScheme, TokenSchemeDiscriminator } from './token-scheme'; -import { INativeToken } from '../../models'; import { AccountId, NftId, AnchorId, DelegationId } from '../id'; import { EpochIndex } from '../../block/slot'; +import { INativeToken } from '../../models/native-token'; export type OutputId = HexEncodedString; @@ -102,9 +102,6 @@ abstract class CommonOutput extends Output { }) readonly unlockConditions: UnlockCondition[]; - // Getter transforms it into nativeTokens with a proper number - private nativeTokens?: INativeToken[]; - /** * The features contained by the output. */ @@ -126,22 +123,22 @@ abstract class CommonOutput extends Output { super(type, amount); this.unlockConditions = unlockConditions; } + /** - * The native tokens held by the output. + * The native token held by the output. */ - getNativeTokens(): INativeToken[] | undefined { - if (!this.nativeTokens) { - return this.nativeTokens; + getNativeToken(): NativeTokenFeature | undefined { + if (!this.features) { + return undefined; } - // Make sure the amount of native tokens are of bigint type. - for (let i = 0; i < this.nativeTokens.length; i++) { - const token = this.nativeTokens[i]; - if (typeof token.amount === 'string') { - this.nativeTokens[i].amount = hexToBigInt(token.amount); + for (let i = 0; i < this.features.length; i++) { + const feature = this.features[i]; + if (feature instanceof NativeTokenFeature) { + return feature as NativeTokenFeature; } } - return this.nativeTokens; + return undefined; } } /** From 85a1bbafc2146cef6e29ac888bf49f2bc5107ffe Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Tue, 14 Nov 2023 14:57:29 +0100 Subject: [PATCH 2/4] unused import --- bindings/nodejs/lib/types/block/output/output.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/bindings/nodejs/lib/types/block/output/output.ts b/bindings/nodejs/lib/types/block/output/output.ts index 9e879d6dcb..f26dcd2428 100644 --- a/bindings/nodejs/lib/types/block/output/output.ts +++ b/bindings/nodejs/lib/types/block/output/output.ts @@ -13,7 +13,6 @@ import { HexEncodedString, NumericString, u64 } from '../../utils'; import { TokenScheme, TokenSchemeDiscriminator } from './token-scheme'; import { AccountId, NftId, AnchorId, DelegationId } from '../id'; import { EpochIndex } from '../../block/slot'; -import { INativeToken } from '../../models/native-token'; export type OutputId = HexEncodedString; From 1958efb2645b73a5af2e4ab7da4f525ca8228d7a Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Mon, 20 Nov 2023 14:07:20 +0100 Subject: [PATCH 3/4] flatten --- .../examples/client/05-get-address-balance.ts | 6 +++--- bindings/nodejs/lib/types/block/output/feature.ts | 14 ++++++++++---- bindings/nodejs/lib/types/block/output/output.ts | 3 +-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/bindings/nodejs/examples/client/05-get-address-balance.ts b/bindings/nodejs/examples/client/05-get-address-balance.ts index fcc1af6c89..2146be6a8f 100644 --- a/bindings/nodejs/examples/client/05-get-address-balance.ts +++ b/bindings/nodejs/examples/client/05-get-address-balance.ts @@ -58,9 +58,9 @@ async function run() { output as CommonOutput ).getNativeToken(); if (nativeTokenFeature != undefined) { - totalNativeTokens[nativeTokenFeature.nativeToken.id] = - (totalNativeTokens[nativeTokenFeature.nativeToken.id] || - BigInt(0)) + nativeTokenFeature.nativeToken.amount; + totalNativeTokens[nativeTokenFeature.id] = + (totalNativeTokens[nativeTokenFeature.id] || + BigInt(0)) + nativeTokenFeature.amount; } } diff --git a/bindings/nodejs/lib/types/block/output/feature.ts b/bindings/nodejs/lib/types/block/output/feature.ts index 15665af627..f914fad7df 100644 --- a/bindings/nodejs/lib/types/block/output/feature.ts +++ b/bindings/nodejs/lib/types/block/output/feature.ts @@ -8,9 +8,10 @@ import { BlockIssuerKey, BlockIssuerKeyDiscriminator, } from './block-issuer-key'; -import { u64 } from '../../utils/type-aliases'; +import { u256, u64 } from '../../utils/type-aliases'; import { EpochIndex } from '../../block/slot'; import { INativeToken } from '../../models/native-token'; +import { HexEncodedString } from '../../utils/hex-encoding'; /** * All of the feature block types. @@ -114,9 +115,13 @@ class TagFeature extends Feature { */ class NativeTokenFeature extends Feature { /** - * The inner native token held by this feature. + * Identifier of the native token. */ - readonly nativeToken: INativeToken; + readonly id: HexEncodedString; + /** + * Amount of native tokens of the given Token ID. + */ + readonly amount: u256; /** * Creates a new `NativeTokenFeature`. @@ -124,7 +129,8 @@ class NativeTokenFeature extends Feature { */ constructor(nativeToken: INativeToken) { super(FeatureType.NativeToken); - this.nativeToken = nativeToken; + this.id = nativeToken.id; + this.amount = nativeToken.amount; } } diff --git a/bindings/nodejs/lib/types/block/output/output.ts b/bindings/nodejs/lib/types/block/output/output.ts index f26dcd2428..026b269a87 100644 --- a/bindings/nodejs/lib/types/block/output/output.ts +++ b/bindings/nodejs/lib/types/block/output/output.ts @@ -131,8 +131,7 @@ abstract class CommonOutput extends Output { return undefined; } - for (let i = 0; i < this.features.length; i++) { - const feature = this.features[i]; + for (const feature of this.features) { if (feature instanceof NativeTokenFeature) { return feature as NativeTokenFeature; } From 1be4f7e2607419fc09ba97b39d127b6d28a918c1 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Wed, 22 Nov 2023 16:13:04 +0100 Subject: [PATCH 4/4] added asNativeToken fn --- bindings/nodejs/lib/types/block/output/feature.ts | 10 ++++++++++ bindings/nodejs/lib/types/block/output/output.ts | 5 +++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/bindings/nodejs/lib/types/block/output/feature.ts b/bindings/nodejs/lib/types/block/output/feature.ts index 3856f427fe..0ea713e422 100644 --- a/bindings/nodejs/lib/types/block/output/feature.ts +++ b/bindings/nodejs/lib/types/block/output/feature.ts @@ -137,6 +137,16 @@ class NativeTokenFeature extends Feature { this.id = nativeToken.id; this.amount = nativeToken.amount; } + + /** + * Returns the native token contained in this feature. + */ + public asNativeToken(): INativeToken { + return { + id: this.id, + amount: this.amount, + }; + } } /** diff --git a/bindings/nodejs/lib/types/block/output/output.ts b/bindings/nodejs/lib/types/block/output/output.ts index dd47796560..a75124bc78 100644 --- a/bindings/nodejs/lib/types/block/output/output.ts +++ b/bindings/nodejs/lib/types/block/output/output.ts @@ -13,6 +13,7 @@ import { HexEncodedString, NumericString, u64 } from '../../utils'; import { TokenScheme, TokenSchemeDiscriminator } from './token-scheme'; import { AccountId, NftId, AnchorId, DelegationId } from '../id'; import { EpochIndex } from '../../block/slot'; +import { INativeToken } from '../../models/native-token'; export type OutputId = HexEncodedString; @@ -126,14 +127,14 @@ abstract class CommonOutput extends Output { /** * The native token held by the output. */ - getNativeToken(): NativeTokenFeature | undefined { + getNativeToken(): INativeToken | undefined { if (!this.features) { return undefined; } for (const feature of this.features) { if (feature instanceof NativeTokenFeature) { - return feature as NativeTokenFeature; + return (feature as NativeTokenFeature).asNativeToken(); } } return undefined;