Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decimals #74

Merged
merged 6 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: '16.x'
node-version: "16.x"
- run: npm ci
working-directory: ./server
- run: npm run test
working-directory: ./server
- run: npm ci
working-directory: ./ui
- run: npm run lint
working-directory: ./ui
- run: npm run test
working-directory: ./ui
- run: npm run build
working-directory: ./ui
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.9",
"@hyperledger/firefly-sdk": "^0.1.0-alpha.10",
"body-parser": "^1.20.0",
"class-transformer": "^0.3.1",
"class-validator": "^0.12.2",
Expand Down
14 changes: 10 additions & 4 deletions server/src/controllers/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ 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 }));
return pools.map((p) => ({
id: p.id,
name: p.name,
symbol: p.symbol,
type: p.type,
decimals: p.decimals,
}));
}

@Post('/pools')
Expand Down Expand Up @@ -213,17 +219,17 @@ export class TokensController {
@QueryParam('pool') pool: string,
@QueryParam('key') key: string,
): Promise<TokenBalance[]> {
const poolMap = new Map<string, string>();
const poolMap = new Map<string, TokenPool>();
const balances = await firefly.getTokenBalances({ pool, key, balance: '>0' });
for (const b of balances) {
if (!poolMap.has(b.pool)) {
const pool = await firefly.getTokenPool(b.pool);
poolMap.set(b.pool, pool.name);
poolMap.set(b.pool, pool);
}
}
return balances.map((b) => ({
pool: b.pool,
poolName: poolMap.get(b.pool),
poolObject: poolMap.get(b.pool),
dechdev marked this conversation as resolved.
Show resolved Hide resolved
key: b.key,
balance: b.balance,
tokenIndex: b.tokenIndex,
Expand Down
4 changes: 2 additions & 2 deletions server/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ export class TokenBalance {
@IsUUID()
pool: string;

@IsString()
poolName: string;
@IsInstance(TokenPool)
poolObject: TokenPool;

@IsString()
key: string;
Expand Down
19 changes: 11 additions & 8 deletions server/test/tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import FireFly, {
} from '@hyperledger/firefly-sdk';
import server from '../src/server';
import { firefly } from '../src/clients/firefly';
import { TokenMintBurn, TokenPoolInput, TokenTransfer } from '../src/interfaces';
import { TokenMintBurn, TokenPool, TokenPoolInput, TokenTransfer } from '../src/interfaces';

jest.mock('@hyperledger/firefly-sdk');
const mockFireFly = firefly as jest.MockedObject<FireFly>;
Expand Down Expand Up @@ -130,20 +130,23 @@ describe('Tokens', () => {
});

test('Get balances', async () => {
const balances = [{ key: '0x123', balance: '1', pool: 'pool1' } as FireFlyTokenBalanceFilter];
const pool = {
name: 'poolA',
type: 'fungible',
id: 'poolA',
} as FireFlyTokenPoolResponse;
const balances = [{ key: '0x123', balance: '1', pool: 'poolA' } as FireFlyTokenBalanceFilter];
dechdev marked this conversation as resolved.
Show resolved Hide resolved

mockFireFly.getTokenPool.mockResolvedValueOnce({
name: 'pool-name',
} as FireFlyTokenPoolResponse);
mockFireFly.getTokenPool.mockResolvedValueOnce(pool);
mockFireFly.getTokenBalances.mockResolvedValueOnce(balances);

await request(server)
.get('/api/tokens/balances?pool=pool1&key=0x123')
.get('/api/tokens/balances?pool=poolA&key=0x123')
.expect(200)
.expect([{ key: '0x123', balance: '1', pool: 'pool1', poolName: 'pool-name' }]);
.expect([{ key: '0x123', balance: '1', pool: 'poolA', poolObject: pool }]);

expect(mockFireFly.getTokenBalances).toHaveBeenCalledWith({
pool: 'pool1',
pool: 'poolA',
key: '0x123',
balance: '>0',
});
Expand Down
13 changes: 8 additions & 5 deletions ui/src/components/Boxes/TokenStateBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ApplicationContext } from '../../contexts/ApplicationContext';
import { SnackbarContext } from '../../contexts/SnackbarContext';
import { ITokenBalance } from '../../interfaces/api';
import { DEFAULT_BORDER_RADIUS } from '../../theme';
import { decimalToAmount } from '../../utils/decimals';
import { fetchCatcher } from '../../utils/fetches';
import { getShortHash, jsNumberForAddress } from '../../utils/strings';

Expand Down Expand Up @@ -50,10 +51,10 @@ export const TokenStateBox: React.FC = () => {
if (b.key !== selfIdentity?.ethereum_address) {
return;
}
const balanceArr = balanceMap[b.poolName]?.balances ?? [];
const balanceArr = balanceMap[b.poolObject.name]?.balances ?? [];
balanceMap = {
...balanceMap,
[b.poolName]: {
[b.poolObject.name]: {
balances: [...balanceArr, b],
},
};
Expand Down Expand Up @@ -165,9 +166,11 @@ export const TokenStateBox: React.FC = () => {
(b) => b.tokenIndex !== undefined
)
? makeNonFungibleString(tokenBalanceMap[poolIDKey].balances)
: `${t('---')} ${t('total')}: ${
tokenBalanceMap[poolIDKey].balances[0].balance
}`}
: `${t('---')} ${t('total')}: ${decimalToAmount(
tokenBalanceMap[poolIDKey].balances[0].balance,
tokenBalanceMap[poolIDKey].balances[0].poolObject
.decimals
)}`}
</Typography>
</Grid>
</Grid>
Expand Down
25 changes: 24 additions & 1 deletion ui/src/components/Forms/Contracts/DefineInterfaceForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
FormControl,
FormHelperText,
Grid,
InputLabel,
MenuItem,
Expand All @@ -10,6 +11,7 @@ import {
import { useContext, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { MAX_FORM_ROWS } from '../../../App';
import { ResourceUrls } from '../../../constants/ResourceUrls';
import { TUTORIAL_FORMS } from '../../../constants/TutorialSections';
import { ApplicationContext } from '../../../contexts/ApplicationContext';
import { FormContext } from '../../../contexts/FormContext';
Expand Down Expand Up @@ -118,10 +120,31 @@ export const DefineInterfaceForm: React.FC = () => {
>
{CONTRACT_INTERFACE_FORMATS.map((f, idx) => (
<MenuItem key={idx} value={f}>
<Typography color="primary">{t(`${f}`)}</Typography>
<Typography color="primary">{t(f)}</Typography>
</MenuItem>
))}
</Select>
<FormHelperText>
{t('either')}&nbsp;
<a
href={ResourceUrls.fireflyFFI}
target="_blank"
style={{ color: 'white', textDecoration: 'none' }}
>
{t('ffiShort')}
</a>
&nbsp;
{t('or')}&nbsp;
<a
href={ResourceUrls.solidityABI}
target="_blank"
style={{ color: 'white', textDecoration: 'none' }}
>
{t('solidityABI')}
</a>
&nbsp;
{t('format')}
</FormHelperText>
</FormControl>
</Grid>
</Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,20 @@ export const RegisterContractApiListenerForm: React.FC = () => {
<FormControl fullWidth required>
<TextField
fullWidth
label={t('name')}
onChange={(e) => setName(e.target.value)}
required
label={t('topic')}
onChange={(e) => setTopic(e.target.value)}
helperText={t('contractTopicHelperText')}
/>
</FormControl>
</Grid>
<Grid item xs={12}>
<FormControl fullWidth required>
<TextField
fullWidth
required
label={t('topic')}
onChange={(e) => setTopic(e.target.value)}
label={t('name')}
onChange={(e) => setName(e.target.value)}
helperText={t('contractListenerNameHelperText')}
/>
</FormControl>
</Grid>
Expand Down
3 changes: 1 addition & 2 deletions ui/src/components/Forms/Messages/DatatypeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,13 @@ export const DatatypeForm: React.FC = () => {

<Grid item xs={12}>
<TextField
label={t('schema')}
label={t('jsonSchema')}
multiline
required
fullWidth
maxRows={MAX_FORM_ROWS}
value={schemaString}
onChange={(e) => setSchemaString(e.target.value)}
helperText={t('datatypeSchemaHelperText')}
/>
</Grid>
</Grid>
Expand Down
42 changes: 20 additions & 22 deletions ui/src/components/Forms/Messages/PrivateForm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
Checkbox,
FormControl,
FormHelperText,
Grid,
InputLabel,
ListItemText,
Expand Down Expand Up @@ -161,6 +160,26 @@ export const PrivateForm: React.FC<Props> = ({
setDatatype(dt);
}}
/>
<Grid container item>
{/* Recipient Select box */}
<FormControl fullWidth required>
<InputLabel>{t('recipients')}</InputLabel>
<Select
multiple
value={recipients}
onChange={handleRecipientChange}
input={<OutlinedInput label={t('recipients')} />}
renderValue={(selected) => selected.join(', ')}
>
{identities.map((identity, idx) => (
<MenuItem key={idx} value={identity.did}>
<Checkbox checked={recipients.indexOf(identity.did) > -1} />
<ListItemText primary={identity.did} />
</MenuItem>
))}
</Select>
</FormControl>
</Grid>
<Grid container item justifyContent="space-between" spacing={1}>
{/* Tag */}
<Grid item xs={6}>
Expand All @@ -183,27 +202,6 @@ export const PrivateForm: React.FC<Props> = ({
/>
</Grid>
</Grid>
<Grid container item>
{/* Recipient Select box */}
<FormControl fullWidth required>
<InputLabel>{t('recipients')}</InputLabel>
<Select
multiple
value={recipients}
onChange={handleRecipientChange}
input={<OutlinedInput label={t('recipients')} />}
renderValue={(selected) => selected.join(', ')}
>
{identities.map((identity, idx) => (
<MenuItem key={idx} value={identity.did}>
<Checkbox checked={recipients.indexOf(identity.did) > -1} />
<ListItemText primary={identity.did} />
</MenuItem>
))}
</Select>
<FormHelperText>{t('recipientsHelperText')}</FormHelperText>
</FormControl>
</Grid>
</Grid>
</Grid>
);
Expand Down
Loading