Skip to content

Commit

Permalink
Merge branch '2.0' into feat/wrapper-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez authored Sep 20, 2023
2 parents e70deed + a37e9e9 commit 703caa1
Show file tree
Hide file tree
Showing 20 changed files with 277 additions and 71 deletions.
9 changes: 6 additions & 3 deletions bindings/nodejs/examples/client/08-data-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ async function run() {
const fetchedBlock = await client.getBlock(blockIdAndBlock[0]);
console.log('Block data: ', fetchedBlock);

if (fetchedBlock.payload instanceof TaggedDataPayload) {
const payload = fetchedBlock.payload as TaggedDataPayload;
console.log('Decoded data:', hexToUtf8(payload.data));
if (fetchedBlock.isBasic()) {
const basic = fetchedBlock.asBasic();
if (basic.payload instanceof TaggedDataPayload) {
const payload = basic.payload as TaggedDataPayload;
console.log('Decoded data:', hexToUtf8(payload.data));
}
}
} catch (error) {
console.error('Error: ', error);
Expand Down
11 changes: 7 additions & 4 deletions bindings/nodejs/examples/client/10-mqtt.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright 2021-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { Block, Client, initLogger } from '@iota/sdk';
import { plainToInstance } from 'class-transformer';
import { Client, initLogger, parseBlockWrapper } from '@iota/sdk';

require('dotenv').config({ path: '.env' });

Expand Down Expand Up @@ -34,8 +33,12 @@ async function run() {

const parsed = JSON.parse(data);
if (parsed.topic == 'blocks') {
const block = plainToInstance(Block, JSON.parse(parsed.payload));
console.log('payload:', block.payload);
const block = parseBlockWrapper(JSON.parse(parsed.payload));

if (block.isBasic()) {
const basic = block.asBasic();
console.log('payload:', basic.payload);
}
}
};

Expand Down
1 change: 0 additions & 1 deletion bindings/nodejs/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
},
"dependencies": {
"@iota/sdk": "link:../",
"class-transformer": "^0.5.1",
"dotenv": "^16.0.0",
"ts-node": "^10.9.1"
},
Expand Down
36 changes: 21 additions & 15 deletions bindings/nodejs/lib/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {
UnlockCondition,
Payload,
TransactionPayload,
parseBlockWrapper,
BlockWrapper,
} from '../types/block';
import { HexEncodedString } from '../utils';
import {
Expand Down Expand Up @@ -168,16 +170,16 @@ export class Client {
* @param blockId The corresponding block ID of the requested block.
* @returns The requested block.
*/
async getBlock(blockId: BlockId): Promise<Block> {
async getBlock(blockId: BlockId): Promise<BlockWrapper> {
const response = await this.methodHandler.callMethod({
name: 'getBlock',
data: {
blockId,
},
});

const parsed = JSON.parse(response) as Response<Block>;
return plainToInstance(Block, parsed.payload);
const parsed = JSON.parse(response) as Response<BlockWrapper>;
return parseBlockWrapper(parsed.payload);
}

/**
Expand Down Expand Up @@ -271,15 +273,17 @@ export class Client {
* @param payload The payload to post.
* @returns The block ID followed by the block containing the payload.
*/
async postBlockPayload(payload: Payload): Promise<[BlockId, Block]> {
async postBlockPayload(payload: Payload): Promise<[BlockId, BlockWrapper]> {
const response = await this.methodHandler.callMethod({
name: 'postBlockPayload',
data: {
payload,
},
});
const parsed = JSON.parse(response) as Response<[BlockId, Block]>;
const block = plainToInstance(Block, parsed.payload[1]);
const parsed = JSON.parse(response) as Response<
[BlockId, BlockWrapper]
>;
const block = parseBlockWrapper(parsed.payload[1]);
return [parsed.payload[0], block];
}

Expand Down Expand Up @@ -417,15 +421,15 @@ export class Client {
* @param transactionId The ID of the transaction.
* @returns The included block that contained the transaction.
*/
async getIncludedBlock(transactionId: string): Promise<Block> {
async getIncludedBlock(transactionId: string): Promise<BlockWrapper> {
const response = await this.methodHandler.callMethod({
name: 'getIncludedBlock',
data: {
transactionId,
},
});
const parsed = JSON.parse(response) as Response<Block>;
return plainToInstance(Block, parsed.payload);
const parsed = JSON.parse(response) as Response<BlockWrapper>;
return parseBlockWrapper(parsed.payload);
}

/**
Expand All @@ -434,15 +438,17 @@ export class Client {
* @param transactionId The ID of the transaction.
* @returns The included block that contained the transaction.
*/
async getIncludedBlockMetadata(transactionId: string): Promise<Block> {
async getIncludedBlockMetadata(
transactionId: string,
): Promise<BlockWrapper> {
const response = await this.methodHandler.callMethod({
name: 'getIncludedBlockMetadata',
data: {
transactionId,
},
});
const parsed = JSON.parse(response) as Response<Block>;
return plainToInstance(Block, parsed.payload);
const parsed = JSON.parse(response) as Response<BlockWrapper>;
return parseBlockWrapper(parsed.payload);
}

/**
Expand Down Expand Up @@ -661,15 +667,15 @@ export class Client {
* @param blockIds An array of `BlockId`s.
* @returns An array of corresponding blocks.
*/
async findBlocks(blockIds: BlockId[]): Promise<Block[]> {
async findBlocks(blockIds: BlockId[]): Promise<BlockWrapper[]> {
const response = await this.methodHandler.callMethod({
name: 'findBlocks',
data: {
blockIds,
},
});
const parsed = JSON.parse(response) as Response<Block[]>;
return plainToInstance(Block, parsed.payload);
const parsed = JSON.parse(response) as Response<BlockWrapper[]>;
return parsed.payload.map((p) => parseBlockWrapper(p));
}

/**
Expand Down
32 changes: 0 additions & 32 deletions bindings/nodejs/lib/types/block/block.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { AccountId } from '../..';
import { u16 } from '../../utils/type_aliases';
import { u16 } from '../../utils/type-aliases';
import { SlotCommitmentId } from '../slot';

enum ContextInputType {
Expand Down
38 changes: 38 additions & 0 deletions bindings/nodejs/lib/types/block/core/basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { Payload, PayloadDiscriminator } from '../payload';
import { Type } from 'class-transformer';
import { StrongParents, WeakParents, ShallowLikeParents } from '../parents';
import { Block } from './block';
import { u64 } from '../../utils';

/**
* Basic Block layout.
*/
export class BasicBlock extends Block {
/**
* Blocks that are strongly directly approved.
*/
readonly strongParents!: StrongParents;
/**
* Blocks that are weakly directly approved.
*/
readonly weakParents!: WeakParents;
/**
* Blocks that are directly referenced to adjust opinion.
*/
readonly shallowLikeParents!: ShallowLikeParents;
/**
* The payload contents.
*/
@Type(() => Payload, {
discriminator: PayloadDiscriminator,
})
readonly payload?: Payload;
/**
* The amount of mana the Account identified by IssuerID is at most
* willing to burn for this block.
*/
readonly burnedMana!: u64;
}
43 changes: 43 additions & 0 deletions bindings/nodejs/lib/types/block/core/block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { BasicBlock } from './basic';
/**
* All of the block types.
*/
export enum BlockType {
/// A Basic block.
Basic = 0,
}

export abstract class Block {
readonly type: BlockType;

/**
* @param type The type of Block.
*/
constructor(type: BlockType) {
this.type = type;
}

/**
* Checks whether the block is a `BasicBlock`.
* @returns true if it is, otherwise false
*/
isBasic(): boolean {
return this.type === BlockType.Basic;
}

/**
* Gets the block as an actual `BasicBlock`.
* NOTE: Will throw an error if the block is not a `BasicBlock`.
* @returns The block
*/
asBasic(): BasicBlock {
if (this.isBasic()) {
return this as unknown as BasicBlock;
} else {
throw new Error('invalid downcast of non-BasicBlock');
}
}
}
15 changes: 15 additions & 0 deletions bindings/nodejs/lib/types/block/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { BlockType } from './block';
import { BasicBlock } from './basic';

export * from './block';
export * from './basic';
export * from './wrapper';

// Here because in block.ts it causes a circular dependency
export const BlockDiscriminator = {
property: 'type',
subTypes: [{ value: BasicBlock, name: BlockType.Basic as any }],
};
Loading

0 comments on commit 703caa1

Please sign in to comment.