Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions lib/l2/property/getBalances.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { BigNumber } from 'ethers'
import { createGetBalancesCaller } from './getBalances'

describe('GetBalances.spec.ts', () => {
describe('createGetBalancesCaller', () => {
it('call success', async () => {
const value = [
{ account: '0x0', balance: BigNumber.from(1) },
{ account: '0x1', balance: BigNumber.from(2) },
{ account: '0x2', balance: BigNumber.from(3) },
]

const propertyContract = {
getBalances: jest
.fn()
.mockImplementation(async () => Promise.resolve(value)),
}

const expected = [
{ account: '0x0', balance: '1' },
{ account: '0x1', balance: '2' },
{ account: '0x2', balance: '3' },
]

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const caller = createGetBalancesCaller(propertyContract as any)

const result = await caller()

expect(result).toEqual(expected)
})

it('call failure', async () => {
const error = 'error'

const propertyContract = {
getBalances: jest
.fn()
.mockImplementation(async () => Promise.reject(error)),
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const caller = createGetBalancesCaller(propertyContract as any)

const result = await caller().catch((err) => err)

expect(result).toEqual(error)
})
})
})
22 changes: 22 additions & 0 deletions lib/l2/property/getBalances.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ethers } from 'ethers'
import { always } from 'ramda'
import { execute, QueryOption } from '../../common/utils/execute'

export type PropertyBalance = Readonly<{
readonly account: string
readonly balance: string
}>

export type CreateGetBalancesCaller = (
contract: ethers.Contract
) => () => Promise<readonly PropertyBalance[]>

export const createGetBalancesCaller: CreateGetBalancesCaller =
(contract: ethers.Contract) =>
always(
execute<QueryOption, readonly PropertyBalance[]>({
contract,
method: 'getBalances',
mutation: false,
})
)
4 changes: 4 additions & 0 deletions lib/l2/property/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { createTransferFromCaller } from '../../common/erc20/transferFrom'
import { createBalanceOfCaller } from './../../common/erc20/balanceOf'
import { createApproveCaller } from './../../common/erc20/approve'
import { createAllowanceCaller } from './../../common/erc20/allowance'
import { createGetBalancesCaller } from './getBalances'

jest.mock('./../../ethereum/property/author')
jest.mock('./../../ethereum/property/changeName')
Expand All @@ -26,6 +27,7 @@ jest.mock('../../common/erc20/transferFrom')
jest.mock('./../../common/erc20/balanceOf')
jest.mock('./../../common/erc20/approve')
jest.mock('./../../common/erc20/allowance')
jest.mock('./getBalances')

describe('property/index.ts', () => {
;(createAuthorCaller as jest.Mock).mockImplementation((contract) => contract)
Expand Down Expand Up @@ -56,6 +58,7 @@ describe('property/index.ts', () => {
;(createAllowanceCaller as jest.Mock).mockImplementation(
(contract) => contract
)
;(createGetBalancesCaller as jest.Mock).mockImplementation((contract) => contract)
describe('createPropertyContract', () => {
it('check return object', () => {
const host = 'localhost'
Expand Down Expand Up @@ -83,6 +86,7 @@ describe('property/index.ts', () => {
author: createAuthorCaller(contract),
changeName: createChangeNameCaller(contract),
changeSymbol: createChangeSymbolCaller(contract),
getBalances: createGetBalancesCaller(contract),
contract: () => contract,
}
}
Expand Down
6 changes: 6 additions & 0 deletions lib/l2/property/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { propertyAbi } from './abi'
import { createAuthorCaller } from './../../ethereum/property/author'
import { createChangeNameCaller } from './../../ethereum/property/changeName'
import { createChangeSymbolCaller } from './../../ethereum/property/changeSymbol'
import {
createGetBalancesCaller,
PropertyBalance,
} from './getBalances'
import { createTransferCaller } from './../../common/erc20/transfer'
import { createNameCaller } from './../../common/erc20/name'
import { createSymbolCaller } from './../../common/erc20/symbol'
Expand Down Expand Up @@ -50,6 +54,7 @@ export type PropertyContract = {
nextSymbol: string,
overrides?: FallbackableOverrides
) => Promise<TransactionResponse>
readonly getBalances: () => Promise<readonly PropertyBalance[]>
readonly contract: () => ethers.Contract
}

Expand All @@ -75,6 +80,7 @@ export const createPropertyContract: CreatePropertyContract =
author: createAuthorCaller(contract),
changeName: createChangeNameCaller(contract),
changeSymbol: createChangeSymbolCaller(contract),
getBalances: createGetBalancesCaller(contract),
contract: always(contract),
}
}