Skip to content

Commit b6f0162

Browse files
Merge pull request #777 from dev-protocol/get_balances
getBalances in Property.sol
2 parents 1b71f72 + 9798dc1 commit b6f0162

4 files changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { BigNumber } from 'ethers'
2+
import { createGetBalancesCaller } from './getBalances'
3+
4+
describe('GetBalances.spec.ts', () => {
5+
describe('createGetBalancesCaller', () => {
6+
it('call success', async () => {
7+
const value = [
8+
{ account: '0x0', balance: BigNumber.from(1) },
9+
{ account: '0x1', balance: BigNumber.from(2) },
10+
{ account: '0x2', balance: BigNumber.from(3) },
11+
]
12+
13+
const propertyContract = {
14+
getBalances: jest
15+
.fn()
16+
.mockImplementation(async () => Promise.resolve(value)),
17+
}
18+
19+
const expected = [
20+
{ account: '0x0', balance: '1' },
21+
{ account: '0x1', balance: '2' },
22+
{ account: '0x2', balance: '3' },
23+
]
24+
25+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26+
const caller = createGetBalancesCaller(propertyContract as any)
27+
28+
const result = await caller()
29+
30+
expect(result).toEqual(expected)
31+
})
32+
33+
it('call failure', async () => {
34+
const error = 'error'
35+
36+
const propertyContract = {
37+
getBalances: jest
38+
.fn()
39+
.mockImplementation(async () => Promise.reject(error)),
40+
}
41+
42+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
43+
const caller = createGetBalancesCaller(propertyContract as any)
44+
45+
const result = await caller().catch((err) => err)
46+
47+
expect(result).toEqual(error)
48+
})
49+
})
50+
})

lib/l2/property/getBalances.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ethers } from 'ethers'
2+
import { always } from 'ramda'
3+
import { execute, QueryOption } from '../../common/utils/execute'
4+
5+
export type PropertyBalance = Readonly<{
6+
readonly account: string
7+
readonly balance: string
8+
}>
9+
10+
export type CreateGetBalancesCaller = (
11+
contract: ethers.Contract
12+
) => () => Promise<readonly PropertyBalance[]>
13+
14+
export const createGetBalancesCaller: CreateGetBalancesCaller =
15+
(contract: ethers.Contract) =>
16+
always(
17+
execute<QueryOption, readonly PropertyBalance[]>({
18+
contract,
19+
method: 'getBalances',
20+
mutation: false,
21+
})
22+
)

lib/l2/property/index.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { createTransferFromCaller } from '../../common/erc20/transferFrom'
1313
import { createBalanceOfCaller } from './../../common/erc20/balanceOf'
1414
import { createApproveCaller } from './../../common/erc20/approve'
1515
import { createAllowanceCaller } from './../../common/erc20/allowance'
16+
import { createGetBalancesCaller } from './getBalances'
1617

1718
jest.mock('./../../ethereum/property/author')
1819
jest.mock('./../../ethereum/property/changeName')
@@ -26,6 +27,7 @@ jest.mock('../../common/erc20/transferFrom')
2627
jest.mock('./../../common/erc20/balanceOf')
2728
jest.mock('./../../common/erc20/approve')
2829
jest.mock('./../../common/erc20/allowance')
30+
jest.mock('./getBalances')
2931

3032
describe('property/index.ts', () => {
3133
;(createAuthorCaller as jest.Mock).mockImplementation((contract) => contract)
@@ -56,6 +58,7 @@ describe('property/index.ts', () => {
5658
;(createAllowanceCaller as jest.Mock).mockImplementation(
5759
(contract) => contract
5860
)
61+
;(createGetBalancesCaller as jest.Mock).mockImplementation((contract) => contract)
5962
describe('createPropertyContract', () => {
6063
it('check return object', () => {
6164
const host = 'localhost'
@@ -83,6 +86,7 @@ describe('property/index.ts', () => {
8386
author: createAuthorCaller(contract),
8487
changeName: createChangeNameCaller(contract),
8588
changeSymbol: createChangeSymbolCaller(contract),
89+
getBalances: createGetBalancesCaller(contract),
8690
contract: () => contract,
8791
}
8892
}

lib/l2/property/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { propertyAbi } from './abi'
55
import { createAuthorCaller } from './../../ethereum/property/author'
66
import { createChangeNameCaller } from './../../ethereum/property/changeName'
77
import { createChangeSymbolCaller } from './../../ethereum/property/changeSymbol'
8+
import {
9+
createGetBalancesCaller,
10+
PropertyBalance,
11+
} from './getBalances'
812
import { createTransferCaller } from './../../common/erc20/transfer'
913
import { createNameCaller } from './../../common/erc20/name'
1014
import { createSymbolCaller } from './../../common/erc20/symbol'
@@ -50,6 +54,7 @@ export type PropertyContract = {
5054
nextSymbol: string,
5155
overrides?: FallbackableOverrides
5256
) => Promise<TransactionResponse>
57+
readonly getBalances: () => Promise<readonly PropertyBalance[]>
5358
readonly contract: () => ethers.Contract
5459
}
5560

@@ -75,6 +80,7 @@ export const createPropertyContract: CreatePropertyContract =
7580
author: createAuthorCaller(contract),
7681
changeName: createChangeNameCaller(contract),
7782
changeSymbol: createChangeSymbolCaller(contract),
83+
getBalances: createGetBalancesCaller(contract),
7884
contract: always(contract),
7985
}
8086
}

0 commit comments

Comments
 (0)