-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: gather fungible token derived state into one place (#204)
* refactor: gather fungible token derived state into one place and add unit tests * refactor: add visible back and deprecate it so PR core is not breaking change * chore: readability Co-authored-by: Victor Kirov <[email protected]> * fix: move the logic to always support runes tokens to be clearer * chore: add comments and readability * chore: extend manageTokens type to cater for tokens that were not set yet --------- Co-authored-by: Victor Kirov <[email protected]> Co-authored-by: fede erbes <[email protected]>
- Loading branch information
1 parent
67253ca
commit 822ac7e
Showing
14 changed files
with
273 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import BigNumber from 'bignumber.js'; | ||
import { type RuneBalance, type FungibleToken, type FungibleTokenStates } from '../types'; | ||
|
||
export const runeTokenToFungibleToken = (runeBalance: RuneBalance): FungibleToken => ({ | ||
name: runeBalance.runeName, | ||
decimals: runeBalance.divisibility, | ||
principal: runeBalance.id, | ||
balance: runeBalance.amount.toString(), | ||
total_sent: '', | ||
total_received: '', | ||
assetName: runeBalance.runeName, | ||
ticker: '', | ||
runeSymbol: runeBalance.symbol, | ||
runeInscriptionId: runeBalance.inscriptionId, | ||
protocol: 'runes', | ||
supported: true, // all runes are supported | ||
}); | ||
|
||
/** | ||
* Logic for determining the derived UI state of a fungible token | ||
* | ||
* Extend this if any of the business logic changes around when a token shows | ||
* up in spam, in manage tokens, is included in account balance etc. | ||
*/ | ||
export const getFungibleTokenStates = ({ | ||
fungibleToken, | ||
manageTokens, | ||
spamTokens, | ||
showSpamTokens, | ||
}: { | ||
fungibleToken: FungibleToken; | ||
manageTokens?: Record<string, boolean | undefined>; | ||
spamTokens?: string[]; | ||
showSpamTokens?: boolean; | ||
}): FungibleTokenStates => { | ||
const hasBalance = new BigNumber(fungibleToken.balance).gt(0); | ||
const isSpam = showSpamTokens ? false : !!spamTokens?.includes(fungibleToken.principal); | ||
const isUserEnabled = manageTokens?.[fungibleToken.principal]; // true=enabled, false=disabled, undefined=not set | ||
const isDefaultEnabled = fungibleToken.supported && hasBalance && !isSpam; | ||
const isEnabled = isUserEnabled || !!(isUserEnabled === undefined && isDefaultEnabled); | ||
const showToggle = isEnabled || (hasBalance && !isSpam); | ||
|
||
return { | ||
isSpam, | ||
isEnabled, | ||
showToggle, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { getFungibleTokenStates } from '../fungibleTokens'; | ||
import { type FungibleToken } from '../types'; | ||
|
||
const sip10Token: FungibleToken = { | ||
assetName: 'odin-tkn', | ||
balance: '0', | ||
decimals: 6, | ||
image: 'https://SP2X2Z28NXZVJFCJPBR9Q3NBVYBK3GPX8PXA3R83C.odin-tkn/1-thumb.png', | ||
name: 'Odin', | ||
principal: 'SP2X2Z28NXZVJFCJPBR9Q3NBVYBK3GPX8PXA3R83C.odin-tkn', | ||
protocol: 'stacks', | ||
ticker: 'ODIN', | ||
total_received: '', | ||
total_sent: '', | ||
}; | ||
|
||
const brc20Token: FungibleToken = { | ||
name: 'ORDI', | ||
principal: 'ORDI', | ||
balance: '0', | ||
total_sent: '', | ||
total_received: '', | ||
assetName: 'ORDI', | ||
ticker: 'ORDI', | ||
protocol: 'brc-20', | ||
}; | ||
|
||
const runesToken: FungibleToken = { | ||
assetName: 'DOG•GO•TO•THE•MOON', | ||
balance: '3142748244', | ||
decimals: 5, | ||
name: 'DOG•GO•TO•THE•MOON', | ||
principal: '840000:3', | ||
protocol: 'runes', | ||
runeInscriptionId: 'e79134080a83fe3e0e06ed6990c5a9b63b362313341745707a2bff7d788a1375i0', | ||
runeSymbol: '🐕', | ||
ticker: '', | ||
tokenFiatRate: 0.00256291, | ||
total_received: '', | ||
total_sent: '', | ||
}; | ||
|
||
describe('getFungibleTokenStates', () => { | ||
[ | ||
{ | ||
name: 'should default a supported sip10 token with balance to enabled', | ||
inputs: { | ||
fungibleToken: { | ||
...sip10Token, | ||
supported: true, | ||
balance: '100', | ||
}, | ||
manageTokens: {}, | ||
spamTokens: [], | ||
showSpamTokens: false, | ||
}, | ||
expected: { | ||
isSpam: false, | ||
isEnabled: true, | ||
showToggle: true, | ||
}, | ||
}, | ||
{ | ||
name: 'should default an unsupported sip10 token with balance to disabled - scam tokens', | ||
inputs: { | ||
fungibleToken: { | ||
...sip10Token, | ||
supported: false, | ||
balance: '100', | ||
}, | ||
manageTokens: {}, | ||
spamTokens: [], | ||
showSpamTokens: false, | ||
}, | ||
expected: { | ||
isSpam: false, | ||
isEnabled: false, | ||
showToggle: true, | ||
}, | ||
}, | ||
{ | ||
name: 'should disable and hide any token with balance if in spam tokens', | ||
inputs: { | ||
fungibleToken: { | ||
...sip10Token, | ||
supported: true, | ||
balance: '100', | ||
}, | ||
manageTokens: {}, | ||
spamTokens: [sip10Token.principal], | ||
showSpamTokens: false, | ||
}, | ||
expected: { | ||
isSpam: true, | ||
isEnabled: false, | ||
showToggle: false, | ||
}, | ||
}, | ||
{ | ||
name: 'should show toggle if token has balance, in spam tokens, but user pressed show spam tokens', | ||
inputs: { | ||
fungibleToken: { | ||
...sip10Token, | ||
supported: false, | ||
balance: '100', | ||
}, | ||
manageTokens: {}, | ||
spamTokens: [sip10Token.principal], | ||
showSpamTokens: true, | ||
}, | ||
expected: { | ||
isSpam: false, | ||
isEnabled: false, | ||
showToggle: true, | ||
}, | ||
}, | ||
{ | ||
name: 'should default any supported brc20 token with balance to enabled, if not in spam tokens', | ||
inputs: { | ||
fungibleToken: { | ||
...brc20Token, | ||
balance: '100', | ||
supported: true, | ||
}, | ||
manageTokens: {}, | ||
spamTokens: [], | ||
showSpamTokens: false, | ||
}, | ||
expected: { | ||
isSpam: false, | ||
isEnabled: true, | ||
showToggle: true, | ||
}, | ||
}, | ||
{ | ||
name: 'should default any supported runes token with balance to enabled, if not in spam tokens', | ||
inputs: { | ||
fungibleToken: { | ||
...runesToken, | ||
balance: '100', | ||
supported: true, | ||
}, | ||
manageTokens: {}, | ||
spamTokens: [], | ||
showSpamTokens: false, | ||
}, | ||
expected: { | ||
isSpam: false, | ||
isEnabled: true, | ||
showToggle: true, | ||
}, | ||
}, | ||
{ | ||
name: 'should show toggle if token is in spam tokens but user enabled it', | ||
inputs: { | ||
fungibleToken: { | ||
...runesToken, | ||
balance: '100', | ||
}, | ||
manageTokens: { [runesToken.principal]: true }, | ||
spamTokens: [runesToken.principal], | ||
showSpamTokens: false, | ||
}, | ||
expected: { | ||
isSpam: true, | ||
isEnabled: true, | ||
showToggle: true, | ||
}, | ||
}, | ||
].forEach(({ name, inputs, expected }) => { | ||
it(name, () => { | ||
expect(getFungibleTokenStates(inputs)).toEqual(expected); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.