Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[on hold till next version] Port getOwnersByPage to an extension #6143

Closed
wants to merge 1 commit into from
Closed
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
51 changes: 51 additions & 0 deletions smart-contract-extensions/contracts/LockInspector.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
pragma solidity ^0.5.0;

import 'unlock-abi-1-3/IPublicLockV6.sol';

/**
* @notice Read-only calls to ease reviewing a lock's details.
*/
contract LockInspector
{
/**
* @notice A function which returns a subset of the keys for this Lock as an array
* @param _lock the address of the lock to check
* @param _page the page of key owners requested when faceted by page size
* @param _pageSize the number of Key Owners requested per page
*/
function getOwnersByPage(
IPublicLock _lock,
uint _page,
uint _pageSize
) external view
returns (address[] memory)
{
uint pageSize = _pageSize;
uint startIndex = _page * pageSize;
uint endOfPageIndex;
uint ownerCount = _lock.numberOfOwners();

if(startIndex + pageSize > ownerCount)
{
endOfPageIndex = ownerCount;
pageSize = ownerCount - startIndex;
}
else
{
endOfPageIndex = (startIndex + pageSize);
}

// new temp in-memory array to hold pageSize number of requested owners:
address[] memory ownersByPage = new address[](pageSize);
uint pageIndex = 0;

// Build the requested set of owners into a new temporary array:
for (uint i = startIndex; i < endOfPageIndex; i++)
{
ownersByPage[pageIndex] = _lock.owners(i);
pageIndex++;
}

return ownersByPage;
}
}
91 changes: 91 additions & 0 deletions smart-contract-extensions/test/lockInspector/getOwnersByPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const { protocols } = require('hardlydifficult-ethereum-contracts')

const LockInspector = artifacts.require('LockInspector.sol')

const keyPrice = web3.utils.toWei('0.01', 'ether')
let lock
let lockInspector

contract('getOwnersByPage', accounts => {
before(async () => {
lock = await protocols.unlock.createTestLock(web3, {
keyPrice,
from: accounts[1], // Lock owner
})
lockInspector = await LockInspector.new()
})

describe('when there are 0 key owners', () => {
it('returns empty results', async () => {
const results = await lockInspector.getOwnersByPage(lock.address, 0, 2, {
from: accounts[5],
})
assert.equal(results.length, 0)
})
})

describe('when there are less owners than the page size', () => {
it('should return all of the key owners', async () => {
await lock.purchase(0, accounts[1], web3.utils.padLeft(0, 40), [], {
value: keyPrice,
from: accounts[1],
})
let result = await lockInspector.getOwnersByPage(lock.address, 0, 2, {
from: accounts[0],
})
assert.equal(result.length, 1)
assert.include(result, accounts[1])
})
})

describe('when there are more owners than the page size', () => {
it('return page size number of key owners', async () => {
await lock.purchase(0, accounts[1], web3.utils.padLeft(0, 40), [], {
value: keyPrice,
from: accounts[1],
})

await lock.purchase(0, accounts[2], web3.utils.padLeft(0, 40), [], {
value: keyPrice,
from: accounts[2],
})

await lock.purchase(0, accounts[3], web3.utils.padLeft(0, 40), [], {
value: keyPrice,
from: accounts[3],
})

let result = await lockInspector.getOwnersByPage(lock.address, 0, 2, {
from: accounts[0],
})
assert.equal(result.length, 2)
assert.include(result, accounts[1])
assert.include(result, accounts[2])
})
})

describe('when requesting a secondary page', () => {
it('return page size number of key owners', async () => {
await lock.purchase(0, accounts[1], web3.utils.padLeft(0, 40), [], {
value: keyPrice,
from: accounts[1],
})

await lock.purchase(0, accounts[2], web3.utils.padLeft(0, 40), [], {
value: keyPrice,
from: accounts[2],
})

await lock.purchase(0, accounts[3], web3.utils.padLeft(0, 40), [], {
value: keyPrice,
from: accounts[3],
})

let result = await lockInspector.getOwnersByPage(lock.address, 1, 2, {
from: accounts[0],
})
assert.equal(result.length, 1)
assert.equal(accounts[3], result[0])
})
})
})
11 changes: 0 additions & 11 deletions smart-contracts/contracts/interfaces/IPublicLock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,6 @@ contract IPublicLock is IERC721Enumerable {
address _account
) external view returns (uint);

/**
* A function which returns a subset of the keys for this Lock as an array
* @param _page the page of key owners requested when faceted by page size
* @param _pageSize the number of Key Owners requested per page
* @dev Throws if there are no key owners yet
*/
function getOwnersByPage(
uint _page,
uint _pageSize
) external view returns (address[] memory);

/**
* Checks if the given address owns the given tokenId.
* @param _tokenId The tokenId of the key to check
Expand Down
35 changes: 0 additions & 35 deletions smart-contracts/contracts/mixins/MixinKeys.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,41 +131,6 @@ contract MixinKeys is
return keyByOwner[_account].tokenId;
}

/**
* A function which returns a subset of the keys for this Lock as an array
* @param _page the page of key owners requested when faceted by page size
* @param _pageSize the number of Key Owners requested per page
*/
function getOwnersByPage(uint _page, uint _pageSize)
public
view
returns (address[] memory)
{
require(owners.length > 0, 'NO_OUTSTANDING_KEYS');
uint pageSize = _pageSize;
uint _startIndex = _page * pageSize;
uint endOfPageIndex;

if (_startIndex + pageSize > owners.length) {
endOfPageIndex = owners.length;
pageSize = owners.length - _startIndex;
} else {
endOfPageIndex = (_startIndex + pageSize);
}

// new temp in-memory array to hold pageSize number of requested owners:
address[] memory ownersByPage = new address[](pageSize);
uint pageIndex = 0;

// Build the requested set of owners into a new temporary array:
for (uint i = _startIndex; i < endOfPageIndex; i++) {
ownersByPage[pageIndex] = owners[i];
pageIndex++;
}

return ownersByPage;
}

/**
* Checks if the given address owns the given tokenId.
*/
Expand Down
124 changes: 0 additions & 124 deletions smart-contracts/test/Lock/getOwnersByPage.js

This file was deleted.