-
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
f39ab5b
commit 982a085
Showing
12 changed files
with
74 additions
and
22 deletions.
There are no files selected for viewing
10 changes: 0 additions & 10 deletions
10
src/ethereum/balancesProviders/ethereumBalancesProvider.ts
This file was deleted.
Oops, something went wrong.
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
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,4 +1,3 @@ | ||
export { EthereumWeb3AtomexProtocolV1, ERC20EthereumWeb3AtomexProtocolV1 } from './atomexProtocol/index'; | ||
export { EthereumBalancesProvider } from './balancesProviders/index'; | ||
export { EthereumSwapTransactionsProvider } from './swapTransactionsProviders/index'; | ||
export { ethereumMainnetCurrencies, ethereumTestnetCurrencies, createDefaultEthereumBlockchainOptions } from './config/index'; |
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,6 @@ | ||
import type { Currency } from '../../common'; | ||
import type { EthereumCurrency } from '../models'; | ||
|
||
export const isEthereumCurrency = (currency: Currency): currency is EthereumCurrency => { | ||
return currency.blockchain === 'ethereum'; | ||
}; |
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 @@ | ||
export { Web3BalancesProvider } from './web3BalancesProvider'; |
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,45 @@ | ||
import type BigNumber from 'bignumber.js'; | ||
import type Web3 from 'web3'; | ||
|
||
import type { AtomexBlockchainProvider, BalancesProvider } from '../../blockchain/index'; | ||
import type { Currency } from '../../common/index'; | ||
import type { ERC20EthereumCurrency, NativeEthereumCurrency } from '../../ethereum/models'; | ||
import { isEthereumCurrency } from '../../ethereum/utils/index'; | ||
import { numberToTokensAmount } from '../../utils/converters'; | ||
import { erc20Abi } from '../models/index'; | ||
|
||
export class Web3BalancesProvider implements BalancesProvider { | ||
constructor( | ||
private readonly blockchainProvider: AtomexBlockchainProvider | ||
) { } | ||
|
||
async getBalance(address: string, currency: Currency): Promise<BigNumber> { | ||
if (!isEthereumCurrency(currency)) | ||
throw new Error('Not ethereum blockchain currency provided'); | ||
|
||
const toolkit = await this.blockchainProvider.getReadonlyToolkit<Web3>('web3'); | ||
if (!toolkit) | ||
throw new Error('Readonly web3 toolkit not found'); | ||
|
||
switch (currency.type) { | ||
case 'native': | ||
return await this.getNativeTokenBalance(address, currency, toolkit); | ||
|
||
case 'erc-20': | ||
return await this.getTokenBalance(address, currency, toolkit); | ||
} | ||
} | ||
|
||
private async getNativeTokenBalance(address: string, currency: NativeEthereumCurrency, toolkit: Web3): Promise<BigNumber> { | ||
const balance = await toolkit.eth.getBalance(address); | ||
|
||
return numberToTokensAmount(+balance, currency.decimals); | ||
} | ||
|
||
private async getTokenBalance(address: string, currency: ERC20EthereumCurrency, toolkit: Web3): Promise<BigNumber> { | ||
const contract = await new toolkit.eth.Contract(erc20Abi, currency.contractAddress); | ||
const balance = await contract.methods.balanceOf(address).call() as string; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export { Web3AtomexProtocolV1 } from './atomexProtocol/index'; | ||
export { Web3BlockchainWallet } from './wallets/index'; | ||
export { Web3BlockchainToolkitProvider } from './blockchainToolkitProviders/index'; | ||
export { Web3BalancesProvider } from './balancesProviders/index'; | ||
|
||
export type { Web3AtomexProtocolV1Options } from './models/index'; |
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,11 @@ | ||
import type { AbiItem } from 'web3-utils'; | ||
|
||
export const erc20Abi: AbiItem[] = [ | ||
{ | ||
constant: true, | ||
inputs: [{ name: '_owner', type: 'address' }], | ||
name: 'balanceOf', | ||
outputs: [{ name: 'balance', type: 'uint256' }], | ||
type: 'function' | ||
} | ||
]; |
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,2 @@ | ||
export type { Web3AtomexProtocolV1Options } from './web3AtomexProtocolV1Options'; | ||
export { erc20Abi } from './abi'; |
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