Skip to content

Commit

Permalink
Implement Web3BalancesProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
maxima-net committed Aug 18, 2022
1 parent f39ab5b commit 982a085
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 22 deletions.
10 changes: 0 additions & 10 deletions src/ethereum/balancesProviders/ethereumBalancesProvider.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/ethereum/balancesProviders/index.ts

This file was deleted.

9 changes: 4 additions & 5 deletions src/ethereum/config/defaultOptions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { AtomexBlockchainNetworkOptions, AtomexContext, AtomexCurrencyOptions } from '../../atomex/index';
import { Web3BlockchainToolkitProvider } from '../../evm/index';
import { Web3BlockchainToolkitProvider, Web3BalancesProvider } from '../../evm/index';
import { ERC20EthereumWeb3AtomexProtocolV1, EthereumWeb3AtomexProtocolV1 } from '../atomexProtocol/index';
import { EthereumBalancesProvider } from '../balancesProviders/index';
import type { EthereumCurrency } from '../models';
import { EthereumSwapTransactionsProvider } from '../swapTransactionsProviders/index';
import { mainnetEthereumWeb3AtomexProtocolV1Options, testnetEthereumWeb3AtomexProtocolV1Options } from './atomexProtocol';
Expand Down Expand Up @@ -64,10 +63,10 @@ const createCurrencyOptions = (

export const createDefaultEthereumBlockchainOptions = (atomexContext: AtomexContext): AtomexBlockchainNetworkOptions => {
const blockchain = 'ethereum';
const mainnetRpcUrl = 'https://mainnet.infura.io/v3/df01d4ef450640a2a48d9af4c2078eaf/';
const testNetRpcUrl = 'https://goerli.infura.io/v3/df01d4ef450640a2a48d9af4c2078eaf/';
const mainnetRpcUrl = 'https://mainnet.infura.io/v3/df01d4ef450640a2a48d9af4c2078eaf';
const testNetRpcUrl = 'https://goerli.infura.io/v3/df01d4ef450640a2a48d9af4c2078eaf';

const balancesProvider = new EthereumBalancesProvider();
const balancesProvider = new Web3BalancesProvider(atomexContext.providers.blockchainProvider);
const swapTransactionsProvider = new EthereumSwapTransactionsProvider();

const ethereumOptions: AtomexBlockchainNetworkOptions = atomexContext.atomexNetwork === 'mainnet'
Expand Down
1 change: 0 additions & 1 deletion src/ethereum/index.ts
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';
6 changes: 6 additions & 0 deletions src/ethereum/utils/guards.ts
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';
};
2 changes: 2 additions & 0 deletions src/ethereum/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Buffer } from '../../native';
import { converters } from '../../utils';
import { uint8ArrayToHexString } from '../../utils/converters';

export { isEthereumCurrency } from './guards';

let secp256k1Curve: EC | null = null;
const getSecp256k1Curve = () => {
if (!secp256k1Curve)
Expand Down
1 change: 1 addition & 0 deletions src/evm/balancesProviders/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Web3BalancesProvider } from './web3BalancesProvider';
45 changes: 45 additions & 0 deletions src/evm/balancesProviders/web3BalancesProvider.ts
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);
}
}
1 change: 1 addition & 0 deletions src/evm/index.ts
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';
11 changes: 11 additions & 0 deletions src/evm/models/abi.ts
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'
}
];
1 change: 1 addition & 0 deletions src/evm/models/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export type { Web3AtomexProtocolV1Options } from './web3AtomexProtocolV1Options';
export { erc20Abi } from './abi';
8 changes: 3 additions & 5 deletions tests/blockchain/atomexBlockchainProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { AtomexBlockchainNetworkOptions, AtomexCurrencyOptions } from '../../src/atomex/models/atomexOptions';
import { AtomexBlockchainProvider } from '../../src/blockchain/atomexBlockchainProvider';
import type { Currency } from '../../src/common/index';
import {
EthereumBalancesProvider, EthereumSwapTransactionsProvider
} from '../../src/ethereum/index';
import { Web3BlockchainToolkitProvider } from '../../src/evm/index';
import { EthereumSwapTransactionsProvider } from '../../src/ethereum/index';
import { Web3BalancesProvider, Web3BlockchainToolkitProvider } from '../../src/evm/index';
import {
TzktBalancesProvider, TaquitoBlockchainToolkitProvider, TezosCurrency, TezosSwapTransactionsProvider
} from '../../src/tezos/index';
Expand Down Expand Up @@ -52,7 +50,7 @@ describe('Atomex Blockchain Provider', () => {

const ethereumNetworkOptions: AtomexBlockchainNetworkOptions = {
rpcUrl: '',
balancesProvider: new EthereumBalancesProvider(),
balancesProvider: new Web3BalancesProvider(new AtomexBlockchainProvider()),
blockchainToolkitProvider: new Web3BlockchainToolkitProvider('ethereum', ''),
swapTransactionsProvider: new EthereumSwapTransactionsProvider(),
currencies: [ethereumNativeCurrency],
Expand Down

0 comments on commit 982a085

Please sign in to comment.