-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b701f70
commit 9915137
Showing
11 changed files
with
76 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export { HttpClient, type RequestOptions } from './httpClient'; | ||
export { RestAtomexClient, type RestAtomexClientOptions } from './restAtomexClient'; |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export { TezosBalancesProvider } from './tezosBalancesProvider'; | ||
export { TzktBalancesProvider } from './tzktBalancesProvider'; |
This file was deleted.
Oops, something went wrong.
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,54 @@ | ||
import type BigNumber from 'bignumber.js'; | ||
|
||
import type { BalancesProvider } from '../../blockchain/index'; | ||
import type { Currency } from '../../common/index'; | ||
import { HttpClient } from '../../core/index'; | ||
import { numberToTokensAmount } from '../../utils/converters'; | ||
import type { FA12TezosCurrency, FA2TezosCurrency, NativeTezosCurrency } from '../models/index'; | ||
import { isTezosCurrency } from '../utils/index'; | ||
|
||
export class TzktBalancesProvider implements BalancesProvider { | ||
private readonly httpClient: HttpClient; | ||
|
||
constructor(baseUrl: string) { | ||
this.httpClient = new HttpClient(baseUrl); | ||
} | ||
|
||
async getBalance(address: string, currency: Currency): Promise<BigNumber> { | ||
if (!isTezosCurrency(currency)) | ||
throw new Error('Not tezos blockchain currency provided'); | ||
|
||
switch (currency.type) { | ||
case 'native': | ||
return await this.getNativeTokenBalance(address, currency); | ||
|
||
case 'fa1.2': | ||
case 'fa2': | ||
return await this.getTokenBalance(address, currency); | ||
} | ||
} | ||
|
||
private async getNativeTokenBalance(address: string, currency: NativeTezosCurrency): Promise<BigNumber> { | ||
const urlPath = `/v1/accounts/${address}/balance`; | ||
const balance = await this.httpClient.request<number>({ urlPath }, false); | ||
|
||
return numberToTokensAmount(balance, currency.decimals); | ||
} | ||
|
||
private async getTokenBalance(address: string, currency: FA12TezosCurrency | FA2TezosCurrency): Promise<BigNumber> { | ||
const urlPath = '/v1/tokens/balances'; | ||
const params = { | ||
'account': address, | ||
'token.contract': currency.contractAddress, | ||
'token.tokenId': currency.type === 'fa1.2' ? 0 : currency.tokenId, | ||
'select': 'balance' | ||
}; | ||
|
||
const balances = await this.httpClient.request<number[]>({ urlPath, params }, false); | ||
const balance = balances[0]; | ||
if (balance === undefined) | ||
throw new Error('Invalid response'); | ||
|
||
return numberToTokensAmount(balance, currency.decimals); | ||
} | ||
} |
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