Skip to content

Commit

Permalink
provide message type in *MessageBody utils
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Shorsher <[email protected]>
  • Loading branch information
shorsher committed May 27, 2022
1 parent eeda7ba commit 4b347a9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
25 changes: 13 additions & 12 deletions server/src/controllers/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { plainToClassFromExist } from 'class-transformer';
import { firefly } from '../clients/firefly';
import {
FF_MESSAGES,
formatTemplate,
FormDataSchema,
getBroadcastMessageBody,
Expand Down Expand Up @@ -85,8 +86,8 @@ export class TokensController {
if (body.messagingMethod) {
mintBody.message =
body.messagingMethod === 'broadcast'
? getBroadcastMessageBody(body)
: getPrivateMessageBody(body);
? getBroadcastMessageBody(body, undefined, FF_MESSAGES.TRANSFER_BROADCAST)
: getPrivateMessageBody(body, undefined, FF_MESSAGES.TRANSFER_PRIVATE);
}
const transfer = await firefly.mintTokens(mintBody);
return { type: 'token_transfer', id: transfer.localId };
Expand All @@ -112,8 +113,8 @@ export class TokensController {
if (body.messagingMethod) {
mintBody.message =
body.messagingMethod === 'broadcast'
? getBroadcastMessageBody(body, data.id)
: getPrivateMessageBody(body, data.id);
? getBroadcastMessageBody(body, data.id, FF_MESSAGES.TRANSFER_BROADCAST)
: getPrivateMessageBody(body, data.id, FF_MESSAGES.TRANSFER_PRIVATE);
}
const transfer = await firefly.mintTokens(mintBody);
return { type: 'token_transfer', id: transfer.localId };
Expand All @@ -133,8 +134,8 @@ export class TokensController {
if (body.messagingMethod) {
burnBody.message =
body.messagingMethod === 'broadcast'
? getBroadcastMessageBody(body)
: getPrivateMessageBody(body);
? getBroadcastMessageBody(body, undefined, FF_MESSAGES.TRANSFER_BROADCAST)
: getPrivateMessageBody(body, undefined, FF_MESSAGES.TRANSFER_PRIVATE);
}
const transfer = await firefly.burnTokens(burnBody);
return { type: 'token_transfer', id: transfer.localId };
Expand All @@ -160,8 +161,8 @@ export class TokensController {
if (body.messagingMethod) {
burnBody.message =
body.messagingMethod === 'broadcast'
? getBroadcastMessageBody(body, data.id)
: getPrivateMessageBody(body, data.id);
? getBroadcastMessageBody(body, data.id, FF_MESSAGES.TRANSFER_BROADCAST)
: getPrivateMessageBody(body, data.id, FF_MESSAGES.TRANSFER_PRIVATE);
}
const transfer = await firefly.burnTokens(burnBody);
return { type: 'token_transfer', id: transfer.localId };
Expand All @@ -182,8 +183,8 @@ export class TokensController {
if (body.messagingMethod) {
transferBody.message =
body.messagingMethod === 'broadcast'
? getBroadcastMessageBody(body)
: getPrivateMessageBody(body);
? getBroadcastMessageBody(body, undefined, FF_MESSAGES.TRANSFER_BROADCAST)
: getPrivateMessageBody(body, undefined, FF_MESSAGES.TRANSFER_PRIVATE);
}
const transfer = await firefly.transferTokens(transferBody);
return { type: 'token_transfer', id: transfer.localId };
Expand All @@ -210,8 +211,8 @@ export class TokensController {
if (body.messagingMethod) {
transferBody.message =
body.messagingMethod === 'broadcast'
? getBroadcastMessageBody(body, data.id)
: getPrivateMessageBody(body, data.id);
? getBroadcastMessageBody(body, data.id, FF_MESSAGES.TRANSFER_BROADCAST)
: getPrivateMessageBody(body, data.id, FF_MESSAGES.TRANSFER_PRIVATE);
}
const transfer = await firefly.transferTokens(transferBody);
return { type: 'token_transfer', id: transfer.localId };
Expand Down
7 changes: 4 additions & 3 deletions server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,25 @@ export function quoteAndEscape(varName: string, options?: QuoteOptions) {
return result;
}

export function getBroadcastMessageBody(body: BroadcastValue, blobId?: string) {
export function getBroadcastMessageBody(body: BroadcastValue, blobId?: string, messageType?: FF_MESSAGES) {
const dataBody = blobId ? { id: blobId } : getMessageBody(body);
return {
header: {
tag: body.tag || undefined,
topics: body.topic ? [body.topic] : undefined,
type: messageType || undefined
},
data: [dataBody],
};
}

export function getPrivateMessageBody(body: PrivateValue, blobId?: string) {
export function getPrivateMessageBody(body: PrivateValue, blobId?: string, messageType?: FF_MESSAGES) {
const dataBody = blobId ? { id: blobId } : getMessageBody(body);
return {
header: {
tag: body.tag || undefined,
topics: body.topic ? [body.topic] : undefined,
type: FF_MESSAGES.TRANSFER_PRIVATE,
type: messageType || undefined
},
group: {
members: body.recipients.map((r) => ({ identity: r })),
Expand Down
4 changes: 2 additions & 2 deletions server/test/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('Messages', () => {

expect(mockFireFly.uploadDataBlob).not.toHaveBeenCalled();
expect(mockFireFly.sendPrivateMessage).toHaveBeenCalledWith({
header: { tag: 'test-tag', topics: ['test-topic'], type: 'transfer_private' },
header: { tag: 'test-tag', topics: ['test-topic']},
group: {
members: [{ identity: 'alpha' }, { identity: 'beta' }],
},
Expand Down Expand Up @@ -109,7 +109,7 @@ describe('Messages', () => {

expect(mockFireFly.uploadDataBlob).toHaveBeenCalledWith(expect.any(Buffer), 'simple-file.txt');
expect(mockFireFly.sendPrivateMessage).toHaveBeenCalledWith({
header: { tag: 'test-tag', topics: undefined, type: 'transfer_private' },
header: { tag: 'test-tag', topics: undefined },
group: {
members: [{ identity: 'alpha' }, { identity: 'beta' }],
},
Expand Down
12 changes: 9 additions & 3 deletions server/test/tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ describe('Tokens', () => {
header: {
tag: 'test',
topics: ['one'],
type: "transfer_broadcast"
},
},
pool: 'my-pool',
Expand Down Expand Up @@ -160,6 +161,7 @@ describe('Tokens', () => {
header: {
tag: 'test',
topics: ['one'],
type: "transfer_broadcast"
},
},
pool: 'my-pool',
Expand Down Expand Up @@ -281,7 +283,7 @@ describe('Tokens', () => {
.expect({ type: 'token_transfer', id: 'transfer1' });

expect(mockFireFly.uploadDataBlob).toHaveBeenCalledWith(expect.any(Buffer), 'simple-file.txt');
expect(mockFireFly.mintTokens).toHaveBeenCalledWith({"amount": "10000", "message": {"data": [{"id": "data1"}], "header": {"tag": undefined, "topics": undefined}}, "pool": "test-pool", "tokenIndex": ""});
expect(mockFireFly.mintTokens).toHaveBeenCalledWith({"amount": "10000", "message": {"data": [{"id": "data1"}], "header": {"tag": undefined, "topics": undefined, "type": "transfer_broadcast"}}, "pool": "test-pool", "tokenIndex": ""});
});

test('Mint tokens with private blob', async () => {
Expand Down Expand Up @@ -372,6 +374,7 @@ describe('Tokens', () => {
header: {
tag: 'test',
topics: ['one'],
type: "transfer_broadcast"
},
},
pool: 'my-pool',
Expand Down Expand Up @@ -413,6 +416,7 @@ describe('Tokens', () => {
header: {
tag: 'test',
topics: ['one'],
type: "transfer_broadcast"
},
},
pool: 'my-pool',
Expand Down Expand Up @@ -442,7 +446,7 @@ describe('Tokens', () => {
.expect({ type: 'token_transfer', id: 'transfer1' });

expect(mockFireFly.uploadDataBlob).toHaveBeenCalledWith(expect.any(Buffer), 'simple-file.txt');
expect(mockFireFly.burnTokens).toHaveBeenCalledWith({"amount": "10000", "message": {"data": [{"id": "data1"}], "header": {"tag": undefined, "topics": undefined}}, "pool": "test-pool"});
expect(mockFireFly.burnTokens).toHaveBeenCalledWith({"amount": "10000", "message": {"data": [{"id": "data1"}], "header": {"tag": undefined, "topics": undefined, "type": "transfer_broadcast"}}, "pool": "test-pool"});
});

test('Burn tokens with private blob', async () => {
Expand Down Expand Up @@ -624,6 +628,7 @@ describe('Tokens', () => {
header: {
tag: 'test',
topics: ['one'],
type: "transfer_broadcast",
},
},
pool: 'my-pool',
Expand Down Expand Up @@ -667,6 +672,7 @@ describe('Tokens', () => {
header: {
tag: 'test',
topics: ['one'],
type: "transfer_broadcast",
},
},
pool: 'my-pool',
Expand Down Expand Up @@ -792,7 +798,7 @@ describe('Tokens', () => {
.expect({ type: 'token_transfer', id: 'transfer1' });

expect(mockFireFly.uploadDataBlob).toHaveBeenCalledWith(expect.any(Buffer), 'simple-file.txt');
expect(mockFireFly.transferTokens).toHaveBeenCalledWith({"amount": "10000", "message": {"data": [{"id": "data1"}], "header": {"tag": undefined, "topics": undefined}}, "pool": "test-pool", "to": "0x111", "tokenIndex": undefined});
expect(mockFireFly.transferTokens).toHaveBeenCalledWith({"amount": "10000", "message": {"data": [{"id": "data1"}], "header": {"tag": undefined, "topics": undefined, "type": "transfer_broadcast"}}, "pool": "test-pool", "to": "0x111", "tokenIndex": undefined});
});

test('Transfer tokens with private blob', async () => {
Expand Down

0 comments on commit 4b347a9

Please sign in to comment.