From 7f2d54704b9afaeafa88669f225f19ff29fe393a Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Wed, 22 Nov 2023 19:55:40 +0100 Subject: [PATCH] feat/Node: Native token feature (#1616) * native token feature * unused import * flatten * added asNativeToken fn --------- Co-authored-by: Thibault Martinez --- .../examples/client/05-get-address-balance.ts | 13 +++--- .../consolidate-outputs.ts | 6 +-- .../nodejs/lib/types/block/output/feature.ts | 40 ++++++++++++++++++- .../nodejs/lib/types/block/output/output.ts | 28 ++++++------- 4 files changed, 62 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 1c9b3f6b2d..2146be6a8f 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.id] = + (totalNativeTokens[nativeTokenFeature.id] || + BigInt(0)) + nativeTokenFeature.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 70df3b9842..e897c7e0dc 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 @@ -49,11 +49,11 @@ async function run() { outputs.forEach(({ output, address }, i) => { console.log(`OUTPUT #${i}`); console.log( - '- address: %s\n- amount: %d\n- native tokens: %s', + '- address: %s\n- amount: %d\n- native token: %s', Utils.hexToBech32(address.toString(), 'rms'), output.getAmount(), output instanceof CommonOutput - ? (output as CommonOutput).getNativeTokens() + ? (output as CommonOutput).getNativeToken() ?? [] : [], ); }); @@ -91,7 +91,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 b7860615a5..0ea713e422 100644 --- a/bindings/nodejs/lib/types/block/output/feature.ts +++ b/bindings/nodejs/lib/types/block/output/feature.ts @@ -8,8 +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. @@ -113,6 +115,40 @@ class TagFeature extends Feature { } } +/** + * Native token feature. + */ +class NativeTokenFeature extends Feature { + /** + * Identifier of the native token. + */ + readonly id: HexEncodedString; + /** + * Amount of native tokens of the given Token ID. + */ + readonly amount: u256; + + /** + * Creates a new `NativeTokenFeature`. + * @param nativeToken The native token stored with the feature. + */ + constructor(nativeToken: INativeToken) { + super(FeatureType.NativeToken); + 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, + }; + } +} + /** * Block Issuer feature. */ @@ -178,6 +214,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 }, ], @@ -191,6 +228,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 8d54898472..a75124bc78 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,21 @@ 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(): INativeToken | 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 (const feature of this.features) { + if (feature instanceof NativeTokenFeature) { + return (feature as NativeTokenFeature).asNativeToken(); } } - return this.nativeTokens; + return undefined; } }