Skip to content

Commit

Permalink
Merge pull request #76 from kaleido-io/pool
Browse files Browse the repository at this point in the history
Add more token pool details
  • Loading branch information
dechdev authored May 2, 2022
2 parents 4a0d965 + cfdcf61 commit 1d881f9
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 23 deletions.
14 changes: 7 additions & 7 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"homepage": "https://github.com/hyperledger/firefly-sandbox#readme",
"dependencies": {
"@hyperledger/firefly-sdk": "^0.1.0-alpha.10",
"@hyperledger/firefly-sdk": "^0.1.0-alpha.12",
"body-parser": "^1.20.0",
"class-transformer": "^0.3.1",
"class-validator": "^0.12.2",
Expand Down
36 changes: 27 additions & 9 deletions server/src/controllers/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import {
QueryParam,
Req,
UploadedFile,
Param,
} from 'routing-controllers';
import { Request } from 'express';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { plainToClassFromExist } from 'class-transformer';
import { FireFlyTokenPoolResponse } from '@hyperledger/firefly-sdk';
import { firefly } from '../clients/firefly';
import {
formatTemplate,
Expand All @@ -28,7 +31,6 @@ import {
MintBurnBlob,
TransferBlob,
} from '../interfaces';
import { plainToClassFromExist } from 'class-transformer';

/**
* Tokens - API Server
Expand All @@ -41,13 +43,15 @@ export class TokensController {
@OpenAPI({ summary: 'List all token pools' })
async tokenpools(): Promise<TokenPool[]> {
const pools = await firefly.getTokenPools();
return pools.map((p) => ({
id: p.id,
name: p.name,
symbol: p.symbol,
type: p.type,
decimals: p.decimals,
}));
return pools.map((p) => mapPool(p));
}

@Get('/pools/:id')
@ResponseSchema(TokenPool)
@OpenAPI({ summary: 'Look up a token pool by ID' })
async tokenpool(@Param('id') id: string): Promise<TokenPool> {
const pool = await firefly.getTokenPool(id);
return mapPool(pool);
}

@Post('/pools')
Expand Down Expand Up @@ -224,7 +228,7 @@ export class TokensController {
for (const b of balances) {
if (!poolMap.has(b.pool)) {
const pool = await firefly.getTokenPool(b.pool);
poolMap.set(b.pool, pool);
poolMap.set(b.pool, mapPool(pool));
}
}
return balances.map((b) => ({
Expand All @@ -236,6 +240,20 @@ export class TokensController {
}
}

function mapPool(pool: FireFlyTokenPoolResponse) {
// Some contracts (base ERC20/ERC721) do not support passing extra data.
// The UI needs to adjust for this, as some items won't reliably confirm.
const schema: string = pool.info?.schema ?? '';
return {
id: pool.id,
name: pool.name,
symbol: pool.symbol,
type: pool.type,
decimals: pool.decimals ?? 0,
dataSupport: schema.indexOf('NoData') === -1,
};
}

/**
* Tokens - Code Templates
* Allows the frontend to display representative code snippets for backend operations.
Expand Down
29 changes: 25 additions & 4 deletions server/src/controllers/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { nanoid } from 'nanoid';
import { firefly } from '../clients/firefly';
import { WebsocketHandler } from '../utils';
import Logger from '../logger';
import { FF_EVENTS, FF_TX } from '../enums';

/**
* Simple WebSocket Server
Expand All @@ -24,10 +25,30 @@ export class SimpleWebSocket {
};

const ffSocket = firefly.listen(sub, async (socket, event) => {
if (event.type === 'transaction_submitted' && event.transaction?.type === 'batch_pin') {
// Enrich batch_pin transaction events with details on the batch
const batches = await firefly.getBatches({ 'tx.id': event.tx });
event['batch'] = batches[0];
if (event.type === FF_EVENTS.TX_SUBMITTED) {
if (event.transaction?.type === FF_TX.BATCH_PIN) {
// Enrich batch_pin transaction events with details on the batch
const batches = await firefly.getBatches({ 'tx.id': event.tx });
event['batch'] = batches[0];
} else if (event.transaction?.type === FF_TX.TOKEN_TRANSFER) {
// Enrich token_transfer transaction events with pool ID
const operations = await firefly.getOperations({
tx: event.tx,
type: FF_TX.TOKEN_TRANSFER,
});
if (operations.length > 0) {
event['pool'] = operations[0].input?.pool;
}
} else if (event.transaction?.type === FF_TX.TOKEN_APPROVAL) {
// Enrich token_approval transaction events with pool ID
const operations = await firefly.getOperations({
tx: event.tx,
type: FF_TX.TOKEN_APPROVAL,
});
if (operations.length > 0) {
event['pool'] = operations[0].input?.pool;
}
}
}

// Forward the event to the client
Expand Down
36 changes: 36 additions & 0 deletions server/src/enums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export enum FF_EVENTS {
// Blockchain Event
BLOCKCHAIN_EVENT_RECEIVED = 'blockchain_event_received',
BLOCKCHAIN_INVOKE_OP_SUCCEEDED = 'blockchain_invoke_op_succeeded',
BLOCKCHAIN_INVOKE_OP_FAILED = 'blockchain_invoke_op_failed',
CONTRACT_API_CONFIRMED = 'contract_api_confirmed',
CONTRACT_INTERFACE_CONFIRMED = 'contract_interface_confirmed',
DATATYPE_CONFIRMED = 'datatype_confirmed',
IDENTITY_CONFIRMED = 'identity_confirmed',
IDENTITY_UPDATED = 'identity_updated',
NS_CONFIRMED = 'namespace_confirmed',
// Message/Definitions
MSG_CONFIRMED = 'message_confirmed',
MSG_REJECTED = 'message_rejected',
TX_SUBMITTED = 'transaction_submitted',
// Transfers
TOKEN_POOL_CONFIRMED = 'token_pool_confirmed',
TOKEN_POOL_OP_FAILED = 'token_pool_op_failed',
TOKEN_APPROVAL_CONFIRMED = 'token_approval_confirmed',
TOKEN_APPROVAL_OP_FAILED = 'token_approval_op_failed',
TOKEN_TRANSFER_CONFIRMED = 'token_transfer_confirmed',
TOKEN_TRANSFER_OP_FAILED = 'token_transfer_op_failed',
}

export enum FF_TX {
NONE = 'none',
// Blockchain Event
CONTRACT_INVOKE = 'contract_invoke',
//Message/Definitions
BATCH_PIN = 'batch_pin',
UNPINNED = 'unpinned',
// Transfers
TOKEN_APPROVAL = 'token_approval',
TOKEN_POOL = 'token_pool',
TOKEN_TRANSFER = 'token_transfer',
}
8 changes: 8 additions & 0 deletions server/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {
ArrayNotEmpty,
IsBoolean,
IsDefined,
IsEnum,
IsInstance,
IsInt,
IsNumberString,
IsObject,
IsOptional,
Expand Down Expand Up @@ -146,6 +148,12 @@ export class TokenPoolInput {
export class TokenPool extends TokenPoolInput {
@IsUUID()
id: string;

@IsInt()
decimals: number;

@IsBoolean()
dataSupport: boolean;
}

export class TokenMintBurn {
Expand Down
19 changes: 17 additions & 2 deletions server/test/tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ describe('Tokens', () => {
await request(server)
.get('/api/tokens/pools')
.expect(200)
.expect([{ id: 'pool1' }, { id: 'pool2' }]);
.expect([
{ id: 'pool1', decimals: 0, dataSupport: true },
{ id: 'pool2', decimals: 0, dataSupport: true },
]);

expect(mockFireFly.getTokenPools).toHaveBeenCalledWith();
});
Expand Down Expand Up @@ -144,7 +147,19 @@ describe('Tokens', () => {
await request(server)
.get('/api/tokens/balances?pool=poolA&key=0x123')
.expect(200)
.expect([{ key: '0x123', balance: '1', pool: pool }]);
.expect([
{
key: '0x123',
balance: '1',
pool: {
name: 'poolA',
type: 'fungible',
id: 'poolA',
decimals: 0,
dataSupport: true,
},
},
]);

expect(mockFireFly.getTokenBalances).toHaveBeenCalledWith({
pool: 'poolA',
Expand Down

0 comments on commit 1d881f9

Please sign in to comment.