Skip to content

Instantly share code, notes, and snippets.

@Sajjon
Last active April 9, 2021 12:01
Show Gist options
  • Save Sajjon/3b7537b529d60ff7d64cd301667e21a2 to your computer and use it in GitHub Desktop.
Save Sajjon/3b7537b529d60ff7d64cd301667e21a2 to your computer and use it in GitHub Desktop.
Token Balances with token Info
it('ownedBalanceOfToken', (done) => {
const subs = new Subscription()
const radix = Radix.create().__withAPI(mockedAPI)
const loadKeystore = (): Promise<KeystoreT> =>
Promise.resolve(keystoreForTest.keystore)
radix.login(keystoreForTest.password, loadKeystore)
type BalanceOfToken = Readonly<{
balance: AmountT
token: Token
}>
type OwnedBalanceOfToken = Readonly<{
owner: AddressT
balanceOfTokens: BalanceOfToken[]
}>
const getBalanceOfToken = (tb: TokenBalance): Observable<BalanceOfToken> => {
return radix.ledger.tokenInfo(tb.token).pipe(
map((tokenInfo: Token): BalanceOfToken => ({
balance: tb.amount,
token: tokenInfo
})),
)
}
radix.__wallet
.subscribe((_w) => {
const ownedBalanceOfToken: Observable<OwnedBalanceOfToken> = radix.tokenBalances.pipe(
mergeMap((tokenBalances: TokenBalances): Observable<OwnedBalanceOfToken> => {
const balanceOfTokensObservableList: Observable<BalanceOfToken>[] = tokenBalances.tokenBalances.map(getBalanceOfToken)
return combineLatest(balanceOfTokensObservableList).pipe(
map((balanceOfTokens: BalanceOfToken[]): OwnedBalanceOfToken => {
return {
owner: tokenBalances.owner,
balanceOfTokens,
}
})
)
})
)
type ExpectedValue = { name: string, amount: string }
const expectedValues: ExpectedValue[] = [
{ name: 'Ether (wrapped on Radix)', amount: '1291' },
{ name: 'Bar token', amount: '7785' },
{ name: 'Foo token', amount: '4121' },
{ name: 'Bitcoin (wrapped on Radix)', amount: '2143' },
{ name: 'Gold token', amount: '1674' },
]
ownedBalanceOfToken
.pipe(
map((obot: OwnedBalanceOfToken): ExpectedValue[] => {
return obot.balanceOfTokens.map((bot): ExpectedValue => ({
name: bot.token.name,
amount: bot.balance.toString()
}))
}),
)
.subscribe(values => {
expect(values).toStrictEqual(expectedValues)
done()
})
.add(subs)
}).add(subs)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment