Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added address addition and remove to LinkedList data structure
- Loading branch information
1 parent
f6b3f7b
commit e179d9f
Showing
3 changed files
with
123 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
pragma solidity ^0.6.4; | ||
|
||
import "../prize-pool/MappedSinglyLinkedList.sol"; | ||
|
||
contract MappedSinglyLinkedListExposed { | ||
using MappedSinglyLinkedList for MappedSinglyLinkedList.Mapping; | ||
|
||
MappedSinglyLinkedList.Mapping list; | ||
|
||
constructor (address[] memory addresses) public { | ||
list.initialize(addresses); | ||
} | ||
|
||
function addressArray() external view returns (address[] memory) { | ||
return list.addressArray(); | ||
} | ||
|
||
function addAddress(address newAddress) external { | ||
list.addAddress(newAddress); | ||
} | ||
|
||
function removeAddress(address prevAddress, address addr) external { | ||
list.removeAddress(prevAddress, addr); | ||
} | ||
|
||
function contains(address addr) external view returns (bool) { | ||
return list.contains(addr); | ||
} | ||
|
||
} |
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,77 @@ | ||
const { deployContract, deployMockContract } = require('ethereum-waffle') | ||
const MappedSinglyLinkedListExposed = require('../build/MappedSinglyLinkedListExposed.json') | ||
|
||
const { ethers } = require('./helpers/ethers') | ||
const { expect } = require('chai') | ||
const buidler = require('./helpers/buidler') | ||
const { AddressZero } = require('ethers/constants') | ||
|
||
const toWei = ethers.utils.parseEther | ||
|
||
const debug = require('debug')('ptv3:MappedSinglyLinkedListExposed.test') | ||
|
||
let overrides = { gasLimit: 20000000 } | ||
|
||
const SENTINAL = '0x0000000000000000000000000000000000000001' | ||
|
||
describe('PrizePool contract', function() { | ||
|
||
let list | ||
|
||
beforeEach(async () => { | ||
[wallet, wallet2, wallet3, wallet4] = await buidler.ethers.getSigners() | ||
|
||
list = await deployContract(wallet, MappedSinglyLinkedListExposed, [[wallet2._address]], overrides) | ||
}) | ||
|
||
describe('initialize()', () => { | ||
it('should have initialized with a value', async () => { | ||
expect(await list.contains(wallet2._address)).to.be.true | ||
}) | ||
}) | ||
|
||
describe('addressArray', () =>{ | ||
it('should create an array from addresses', async () => { | ||
expect(await list.addressArray()).to.deep.equal([wallet2._address]) | ||
}) | ||
}) | ||
|
||
describe('addAddress', () => { | ||
it('should not allow adding the SENTINAL address', async () => { | ||
await expect(list.addAddress(SENTINAL)).to.be.revertedWith("Invalid address") | ||
}) | ||
|
||
it('should not allow adding a zero address', async () => { | ||
await expect(list.addAddress(AddressZero)).to.be.revertedWith("Invalid address") | ||
}) | ||
|
||
it('should allow the user to add an address', async () => { | ||
await list.addAddress(wallet._address) | ||
|
||
expect(await list.addressArray()).to.deep.equal([wallet._address, wallet2._address]) | ||
}) | ||
}) | ||
|
||
describe('removeAddress', () => { | ||
it('should not allow removing the SENTINAL address', async () => { | ||
await expect(list.removeAddress(SENTINAL, SENTINAL)).to.be.revertedWith("Invalid address") | ||
}) | ||
|
||
it('should not allow removing an address that does not exist', async () => { | ||
await expect(list.removeAddress(wallet._address, wallet2._address)).to.be.revertedWith("Invalid prevAddress") | ||
}) | ||
|
||
it('should not allow removing a zero address', async () => { | ||
await expect(list.removeAddress(wallet._address, AddressZero)).to.be.revertedWith("Invalid address") | ||
}) | ||
|
||
it('should allow the user to add an address', async () => { | ||
await list.addAddress(wallet._address) | ||
|
||
await list.removeAddress(wallet._address, wallet2._address) | ||
|
||
expect(await list.addressArray()).to.deep.equal([wallet._address]) | ||
expect(await list.contains(wallet2._address)).to.be.false | ||
}) | ||
}) | ||
}); |