-
Notifications
You must be signed in to change notification settings - Fork 12
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
Showing
9 changed files
with
323 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export * from './ether-js-fetcher' | ||
export * from './types' | ||
export * from './useEtherSWR' | ||
export * from './useBalance' | ||
export * from './useBalanceOf' | ||
import { default as useEtherSWR } from './useEtherSWR' | ||
export default useEtherSWR |
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,33 @@ | ||
import useEtherSWR from './useEtherSWR' | ||
import { useWeb3React } from '@web3-react/core' | ||
import { Web3Provider } from './types' | ||
import { BigNumber } from 'ethers' | ||
|
||
function sayName({ | ||
first, | ||
last = 'Smith' | ||
}: { | ||
first: string | ||
last?: string | ||
}): void { | ||
const name = first + ' ' + last | ||
console.log(name) | ||
} | ||
|
||
type Options = { | ||
block: string | number | ||
} | ||
export function useBalance( | ||
address: string, | ||
{ block }: Options = { block: 'latest' } | ||
) { | ||
return useEtherSWR(['getBalance', address, block]) | ||
} | ||
|
||
export function useBalances( | ||
addresses: string[], | ||
{ block }: Options = { block: 'latest' } | ||
) { | ||
const keys = addresses.map(address => ['getBalance', address, block]) | ||
return useEtherSWR<BigNumber[]>(keys) | ||
} |
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,37 @@ | ||
import useEtherSWR from './useEtherSWR' | ||
import { useWeb3React } from '@web3-react/core' | ||
import { Web3Provider } from './types' | ||
import { BigNumber } from 'ethers' | ||
import { useMemo, useState } from 'react' | ||
|
||
export function useBalanceOf<T = BigNumber>( | ||
contractOrContracts: string | string[], | ||
ownerOrOwners: string | string[] | ||
) { | ||
if (Array.isArray(ownerOrOwners) && Array.isArray(contractOrContracts)) { | ||
throw new Error('Either you pass multiple contracts or multiple owners') | ||
} | ||
const key = useMemo(() => { | ||
const owners = | ||
Array.isArray(ownerOrOwners) && typeof contractOrContracts === 'string' | ||
? ownerOrOwners.map(own => [contractOrContracts, 'balanceOf', own]) | ||
: undefined | ||
|
||
const contracts = | ||
Array.isArray(contractOrContracts) && typeof ownerOrOwners === 'string' | ||
? contractOrContracts.map(cntr => [cntr, 'balanceOf', ownerOrOwners]) | ||
: undefined | ||
|
||
const keys = owners || contracts || [] | ||
|
||
const singleKey: [string, any, any] = | ||
ownerOrOwners && | ||
typeof ownerOrOwners === 'string' && | ||
typeof contractOrContracts === 'string' | ||
? [contractOrContracts as string, 'balanceOf', ownerOrOwners] | ||
: undefined | ||
|
||
return keys.length > 0 ? keys : singleKey | ||
}, [contractOrContracts, ownerOrOwners]) | ||
return useEtherSWR<T>(key) | ||
} |
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,80 @@ | ||
import { mockFetcher, mockMultipleFetch, mockUseWeb3React } from './util/utils' | ||
import useEtherSWR, { etherJsFetcher } from '../src' | ||
import { render, waitFor } from '@testing-library/react' | ||
import { DefaultContainer } from './util/components/DefaultContainer' | ||
import * as React from 'react' | ||
import { useWeb3React } from '@web3-react/core' | ||
import { Contract } from '@ethersproject/contracts' | ||
import { useBalance, useBalances } from '../src/useBalance' | ||
import { BigNumber } from 'ethers' | ||
|
||
jest.mock('../src/ether-js-fetcher') | ||
jest.mock('@web3-react/core') | ||
jest.mock('@ethersproject/contracts') | ||
|
||
const mockedEthFetcher = etherJsFetcher as jest.Mock | ||
const mockeduseWeb3React = useWeb3React as jest.Mock | ||
|
||
describe('useBalance', () => { | ||
const account = '0x001' | ||
|
||
beforeEach(() => { | ||
mockedEthFetcher.mockReset() | ||
mockUseWeb3React(mockeduseWeb3React, { account }) | ||
}) | ||
|
||
it('returns the ether balance of an account', async () => { | ||
const account = '0x002' | ||
const mockData = 10 | ||
const fetcher = mockFetcher(mockedEthFetcher, mockData) | ||
|
||
function Page() { | ||
const { data } = useBalance(account) | ||
return <div>Balance, {data}</div> | ||
} | ||
|
||
const { container } = render( | ||
<DefaultContainer fetcher={mockedEthFetcher}> | ||
<Page /> | ||
</DefaultContainer> | ||
) | ||
|
||
await waitFor(() => { | ||
expect(container.firstChild.textContent).toEqual(`Balance, ${mockData}`) | ||
expect(fetcher).toBeCalledWith('getBalance', account, 'latest') | ||
}) | ||
}) | ||
|
||
it('returns the ether balances of a list of account', async () => { | ||
const account = '0x002' | ||
const mockData = BigNumber.from(10) | ||
const fetcher = mockFetcher(mockedEthFetcher, [mockData]) | ||
|
||
function Page() { | ||
const { data: balances } = useBalances([account]) | ||
if (!balances) { | ||
return <div>Loading</div> | ||
} | ||
return ( | ||
<> | ||
{balances.map((balance, index) => { | ||
return <div key={index}>Balance, {balance.toString()}</div> | ||
})} | ||
</> | ||
) | ||
} | ||
|
||
const { container } = render( | ||
<DefaultContainer fetcher={mockedEthFetcher}> | ||
<Page /> | ||
</DefaultContainer> | ||
) | ||
|
||
await waitFor(() => { | ||
expect(container.textContent).toEqual(`Balance, ${mockData}`) | ||
expect(fetcher).toBeCalledWith( | ||
JSON.stringify([['getBalance', account, 'latest']]) | ||
) | ||
}) | ||
}) | ||
}) |
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,131 @@ | ||
import { | ||
mockContract, | ||
mockFetcher, | ||
mockMultipleFetch, | ||
mockUseWeb3React | ||
} from './util/utils' | ||
import { etherJsFetcher } from '../src' | ||
import { render, waitFor } from '@testing-library/react' | ||
import { DefaultContainer } from './util/components/DefaultContainer' | ||
import * as React from 'react' | ||
import { useWeb3React } from '@web3-react/core' | ||
import { Contract } from '@ethersproject/contracts' | ||
import { BigNumber } from 'ethers' | ||
import { useBalanceOf } from '../src/useBalanceOf' | ||
import { contracts } from '../src/utils' | ||
|
||
jest.mock('../src/ether-js-fetcher') | ||
jest.mock('@web3-react/core') | ||
jest.mock('@ethersproject/contracts') | ||
|
||
const mockedEthFetcher = etherJsFetcher as jest.Mock | ||
const mockeduseWeb3React = useWeb3React as jest.Mock | ||
const mockedContract = (Contract as unknown) as jest.Mock | ||
|
||
describe('useBalanceOf', () => { | ||
const account = '0x001' | ||
const contractAddr = '0x6126A4C0Eb7822C12Bea32327f1706F035b414bf' | ||
let contractInstance | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
mockUseWeb3React(mockeduseWeb3React, { account }) | ||
contractInstance = mockContract(mockedContract) | ||
contracts.clear() | ||
}) | ||
|
||
it('returns the balanceOf from an owner', async () => { | ||
const mockData = BigNumber.from(10) | ||
const fetcher = mockFetcher(mockedEthFetcher, mockData) | ||
const anotherAccount = '0x003' | ||
|
||
function Page() { | ||
const { data } = useBalanceOf(contractAddr, anotherAccount) | ||
if (!data) { | ||
return <div>Loading</div> | ||
} | ||
return <div>Balance, {data.toString()}</div> | ||
} | ||
|
||
const { container } = render( | ||
<DefaultContainer contractAddr={contractAddr} fetcher={mockedEthFetcher}> | ||
<Page /> | ||
</DefaultContainer> | ||
) | ||
|
||
await waitFor(() => { | ||
expect(container.firstChild.textContent).toEqual(`Balance, ${mockData}`) | ||
expect(fetcher).toBeCalledWith(contractAddr, 'balanceOf', anotherAccount) | ||
}) | ||
}) | ||
|
||
it('returns the balanceOf from multiple owners', async () => { | ||
const mockData = BigNumber.from(10) | ||
const fetcher = mockFetcher(mockedEthFetcher, [mockData]) | ||
|
||
function Page() { | ||
const { data: balances } = useBalanceOf<BigNumber[]>(contractAddr, [ | ||
account | ||
]) | ||
if (!balances) { | ||
return <div>Loading</div> | ||
} | ||
return ( | ||
<> | ||
{balances.map((balance, index) => { | ||
return <div key={index}>Balance, {balance.toString()}</div> | ||
})} | ||
</> | ||
) | ||
} | ||
|
||
const { container } = render( | ||
<DefaultContainer contractAddr={contractAddr} fetcher={mockedEthFetcher}> | ||
<Page /> | ||
</DefaultContainer> | ||
) | ||
|
||
await waitFor(() => { | ||
expect(container.textContent).toEqual(`Balance, ${mockData}`) | ||
expect(fetcher).toBeCalledWith( | ||
JSON.stringify([[contractAddr, 'balanceOf', account]]) | ||
) | ||
}) | ||
}) | ||
|
||
it('returns the balanceOf from multiple contract of the same owner', async () => { | ||
const anotherAccount = '0x003' | ||
const mockData = BigNumber.from(10) | ||
const fetcher = mockFetcher(mockedEthFetcher, [mockData]) | ||
|
||
function Page() { | ||
const { data: balances } = useBalanceOf<BigNumber[]>( | ||
[contractAddr], | ||
anotherAccount | ||
) | ||
if (!balances) { | ||
return <div>Loading</div> | ||
} | ||
return ( | ||
<> | ||
{balances.map((balance, index) => { | ||
return <div key={index}>Balance, {balance.toString()}</div> | ||
})} | ||
</> | ||
) | ||
} | ||
|
||
const { container } = render( | ||
<DefaultContainer contractAddr={contractAddr} fetcher={mockedEthFetcher}> | ||
<Page /> | ||
</DefaultContainer> | ||
) | ||
|
||
await waitFor(() => { | ||
expect(container.textContent).toEqual(`Balance, ${mockData}`) | ||
expect(fetcher).toBeCalledWith( | ||
JSON.stringify([[contractAddr, 'balanceOf', anotherAccount]]) | ||
) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.