Skip to content

Commit

Permalink
fix: handle tokens without metadata (#209)
Browse files Browse the repository at this point in the history
* fix(token): handle tokens without metadata

* fix: correct check for ABI method existence

* better naming schema for tokens without metadata

* fix: color dropdown width
  • Loading branch information
troggy authored and sunify committed May 15, 2019
1 parent 35ca910 commit de34699
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/components/amountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default class AmountInput extends React.Component<
<Select
value={color}
style={{
width: this.token.isNft ? '120px !important' : '80px !import',
width: '80px',
}}
onChange={this.handleColorChange}
>
Expand Down
30 changes: 26 additions & 4 deletions src/stores/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { EventLog } from 'web3/types';
import Contract from 'web3/eth/contract';
import { isNFT } from '../utils';
import { isNFT, isNST } from '../utils';

import { web3RootStore } from './web3/root';
import { AccountStore } from './account';
Expand All @@ -21,6 +21,18 @@ function hexToAscii(hex) {
return result;
}

const defaultSymbol = (color, addr) => {
if (isNST(color)) {
return `NST${addr.substring(2, 5)}`;
}

if (isNFT(color)) {
return `NFT${addr.substring(2, 5)}`;
}

return `T${addr.substring(2, 5)}`;
};

const bytesTokenABI = [
{
constant: true,
Expand Down Expand Up @@ -56,17 +68,27 @@ const tokenValue = (token: Contract, method: string) => {
.call()
.then(hexToAscii);
}
);
)
.catch(e => {
console.warn(
`Error executing method '${method}'. Probably ABI mismatches the bytecode`
);
return Promise.reject();
});
};

export const tokenInfo = (
token: Contract,
color: number
): Promise<[string, string, string]> => {
return Promise.all([
tokenValue(token, 'symbol'),
tokenValue(token, 'symbol').catch(_ =>
defaultSymbol(color, token.options.address)
),
isNFT(color) ? Promise.resolve(0) : token.methods.decimals().call(),
tokenValue(token, 'name'),
tokenValue(token, 'name').catch(_ =>
defaultSymbol(color, token.options.address)
),
]);
};

Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { range } from './range';
export { toArray } from './toArray';
export { txSuccess } from './txSuccess';
export { nftDisplayValue, NFT_COLOR_BASE, isNFT } from './nft';
export { nstDisplayValue, NST_COLOR_BASE, isNST } from './nst';

export { KNOWN_NETWORKS } from './knownNetworks';

Expand Down
8 changes: 8 additions & 0 deletions src/utils/nst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { BigIntType } from 'jsbi-utils';

export const NST_COLOR_BASE = 49153; // 2^15 + 1 + 2^14

export const isNST = (color: number): boolean => color >= NST_COLOR_BASE;

export const nstDisplayValue = (value: BigIntType): string =>
value.toString(10);

0 comments on commit de34699

Please sign in to comment.