Skip to content

Commit

Permalink
Merge pull request #84 from kaleido-io/mint-tokenidxfield
Browse files Browse the repository at this point in the history
token index support for minting
  • Loading branch information
dechdev authored May 5, 2022
2 parents 10e7382 + 0f8e03a commit 7f2caf0
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 52 deletions.
56 changes: 29 additions & 27 deletions server/src/controllers/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export class TokensController {
const mintBody = {
pool: body.pool,
amount: body.amount,
tokenIndex: body.tokenIndex
} as any;
if (body.messagingMethod) {
mintBody.message =
Expand Down Expand Up @@ -268,7 +269,8 @@ export class TokensTemplateController {
return formatTemplate(`
const transfer = await firefly.mintTokens({
pool: <%= ${q('pool')} %>,
amount: <%= ${q('amount')} %>,<% if(messagingMethod && (value||jsonValue)) { %>
amount: <%= ${q('amount')} %>,<% if(tokenIndex) { %>
tokenIndex: <%= ${q('tokenIndex')} %>,<% } %><% if(messagingMethod && (value||jsonValue)) { %>
message: {
header: {
tag: <%= tag ? ${q('tag')} : 'undefined' %>,
Expand Down Expand Up @@ -311,32 +313,32 @@ export class TokensTemplateController {
file.buffer,
<%= ${q('filename')} %>,
);
const transfer = await firefly.mintTokens({
pool: <%= ${q('pool')} %>,
amount: <%= ${q('amount')} %>,<% if (tokenIndex) { %>
<% print('tokenIndex: ' + ${q(
'tokenIndex',
)} + ',') } %>,<% if(messagingMethod === 'broadcast') { %>
message: {
header: {
tag: <%= tag ? ${q('tag')} : 'undefined' %>,
topics: <%= topic ? ('[' + ${q('topic')} + ']') : 'undefined' %>,
},
data: [{ id: data.id }],
}<% } else { %>
message: {
header: {
tag: <%= tag ? ${q('tag')} : 'undefined' %>,
topics: <%= topic ? ('[' + ${q('topic')} + ']') : 'undefined' %>,
},
group: {
members: [<%= recipients.map((r) => '{ identity: ' + ${q('r')} + ' }').join(', ') %>],
},
data: [{ id: data.id }],
}
<%} %>
});
return { type: 'token_transfer', id: transfer.localId };
const transfer = await firefly.mintTokens({
pool: <%= ${q('pool')} %>,
amount: <%= ${q('amount')} %>,<% if (tokenIndex) { %>
<% print('tokenIndex: ' + ${q(
'tokenIndex',
)} + ',') } %><% if(messagingMethod === 'broadcast') { %>
message: {
header: {
tag: <%= tag ? ${q('tag')} : 'undefined' %>,
topics: <%= topic ? ('[' + ${q('topic')} + ']') : 'undefined' %>,
},
data: [{ id: data.id }],
}<% } else { %>
message: {
header: {
tag: <%= tag ? ${q('tag')} : 'undefined' %>,
topics: <%= topic ? ('[' + ${q('topic')} + ']') : 'undefined' %>,
},
group: {
members: [<%= recipients.map((r) => '{ identity: ' + ${q('r')} + ' }').join(', ') %>],
},
data: [{ id: data.id }],
}
<%} %>
});
return { type: 'token_transfer', id: transfer.localId };
`);
}

Expand Down
24 changes: 12 additions & 12 deletions server/test/tokens.template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,18 @@ describe('Templates: Tokens', () => {
file.buffer,
'document.pdf',
);
const transfer = await firefly.mintTokens({
pool: 'pool1',
amount: '10',,
message: {
header: {
tag: 'test-tag',
topics: ['test-topic'],
},
data: [{ id: data.id }],
}
});
return { type: 'token_transfer', id: transfer.localId };
const transfer = await firefly.mintTokens({
pool: 'pool1',
amount: '10',
message: {
header: {
tag: 'test-tag',
topics: ['test-topic'],
},
data: [{ id: data.id }],
}
});
return { type: 'token_transfer', id: transfer.localId };
`),
);
});
Expand Down
4 changes: 2 additions & 2 deletions server/test/tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,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"});
expect(mockFireFly.mintTokens).toHaveBeenCalledWith({"amount": "10000", "message": {"data": [{"id": "data1"}], "header": {"tag": undefined, "topics": undefined}}, "pool": "test-pool", "tokenIndex": ""});
});

test('Mint tokens with private blob', async () => {
Expand Down Expand Up @@ -310,7 +310,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"}], "group": {"members": [{"identity": "alpha"}, {"identity": "beta"}]}, "header": {"tag": undefined, "topics": undefined, "type": "transfer_private"}}, "pool": "test-pool"});
expect(mockFireFly.mintTokens).toHaveBeenCalledWith({"amount": "10000", "message": {"data": [{"id": "data1"}], "group": {"members": [{"identity": "alpha"}, {"identity": "beta"}]}, "header": {"tag": undefined, "topics": undefined, "type": "transfer_private"}}, "pool": "test-pool", "tokenIndex": ""});
});

test('Burn tokens', async () => {
Expand Down
37 changes: 35 additions & 2 deletions ui/src/components/Forms/Tokens/MintForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const MintForm: React.FC = () => {

const [pool, setPool] = useState<ITokenPool>();
const [amount, setAmount] = useState<string>('1');
const [tokenIndex, setTokenIndex] = useState<string>('1');
const [decimalAmount, setDecimalAmount] = useState<string | undefined>(
undefined
);
Expand All @@ -59,11 +60,11 @@ export const MintForm: React.FC = () => {
const body = {
pool: pool?.name,
amount: pool?.type === PoolType.F ? decimalAmount : amount,
tokenIndex: '',
tokenIndex: pool?.type === PoolType.NF ? tokenIndex : '',
messagingMethod: messagingMethod ? messagingMethod : null,
};
setJsonPayload(messagingMethod ? { ...jsonPayload, ...body } : body);
}, [pool, decimalAmount, formID]);
}, [pool, decimalAmount, tokenIndex, formID]);

useEffect(() => {
if (formID !== TUTORIAL_FORMS.MINT) return;
Expand All @@ -89,6 +90,11 @@ export const MintForm: React.FC = () => {
setJsonPayload({ ...jsonPayload, recipients: null, messagingMethod: null });
};

const isFungible = () => {
const selectedPool = tokenPools.find((p) => p.name === pool?.name);
return selectedPool?.type === 'fungible';
};

const handleAmountChange = (event: React.ChangeEvent<HTMLInputElement>) => {
if (pool && pool.type === PoolType.NF) {
setAmount(event.target.value);
Expand All @@ -104,6 +110,18 @@ export const MintForm: React.FC = () => {
setAmount(event.target.value);
};

const handleTokenIndexChange = (
event: React.ChangeEvent<HTMLInputElement>
) => {
if (
!pool ||
pool.type !== PoolType.NF ||
isAmountInvalid(event.target.value)
)
return;
setTokenIndex(event.target.value);
};

return (
<Grid container>
<Grid container spacing={DEFAULT_SPACING}>
Expand Down Expand Up @@ -152,16 +170,31 @@ export const MintForm: React.FC = () => {
label={t('amount')}
placeholder={t('exampleAmount')}
value={amount}
disabled={pool?.type === PoolType.NF}
onChange={handleAmountChange}
/>
</FormControl>
</Grid>
{!isFungible() && (
<Grid item xs={10} mt={2}>
<FormControl fullWidth required>
<TextField
fullWidth
label={t('tokenIndex')}
placeholder={t('exampleTokenIndex')}
value={tokenIndex}
onChange={handleTokenIndexChange}
/>
</FormControl>
</Grid>
)}
</Grid>
<MessageForm
tokenMissingFields={!amount || !pool}
tokenOperationPayload={{
pool: pool?.name,
amount: amount.toString(),
tokenIndex: pool?.type === PoolType.NF ? tokenIndex : '',
}}
label={t('attachAMessage')}
/>
Expand Down
9 changes: 0 additions & 9 deletions ui/src/ff_models/eventTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -638,15 +638,6 @@ export const FF_EVENTS_CATEGORY_MAP: {
/>
),
},
{
label: t('tokenIndex') + ':',
value: (
<FFListText
color="secondary"
text={event.tokenTransfer?.tokenIndex ?? t('---')}
/>
),
},
];

if (event.tokenTransfer?.tokenIndex) {
Expand Down
1 change: 1 addition & 0 deletions ui/src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"exampleAmount": "ex. 10",
"exampleFromAddress": "ex. 0x987....321",
"exampleTag": "ex. new_widget",
"exampleTokenIndex": "ex. 7",
"exampleTopic": "ex. widget_123",
"ffi": "FFI - FireFly Interface",
"ffiShort": "FFI",
Expand Down

0 comments on commit 7f2caf0

Please sign in to comment.