Skip to content
This repository was archived by the owner on May 11, 2021. It is now read-only.

Adding an indexing contract #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
65 changes: 65 additions & 0 deletions eth/contracts/PoapIndex.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* Copyright 2019 Hadrien Croubois
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

pragma solidity ^0.5.0;

import "./Poap.sol";
import "./Set.sol";

contract PoapIndex
{
using Set for Set.set;

// Poap token address
Poap public poap;

// event to tokens
mapping(uint256 => Set.set) private m_tokensEvent;

event TokenAdded(uint256 indexed tokenId, uint256 indexed eventId);

constructor(Poap _poap)
public
{
poap = _poap;
}

function addToken(uint256 _tokenId) external
{
_addToken(_tokenId);
}

function addTokens(uint256[] calldata _tokenIds) external
{
for (uint256 i = 0; i < _tokenIds.length; ++i)
{
_addToken(_tokenIds[i]);
}
}

function _addToken(uint256 _tokenId) internal
{
uint256 eventId = poap.tokenEvent(_tokenId);
if (eventId != 0 && m_tokensEvent[eventId].add(_tokenId))
{
emit TokenAdded(_tokenId, eventId);
}
}

function viewTokens(uint256 _eventId) external view returns (uint256[] memory)
{
return m_tokensEvent[_eventId].content();
}
}
91 changes: 91 additions & 0 deletions eth/contracts/Set.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* Copyright 2019 Hadrien Croubois
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

pragma solidity ^0.5.0;

library Set
{
struct set
{
uint256[] members;
mapping(uint256 => uint256) indexes;
}

function length(set storage _list)
internal view returns (uint256)
{
return _list.members.length;
}

function at(set storage _list, uint256 _index)
internal view returns (uint256)
{
return _list.members[_index - 1];
}

function indexOf(set storage _list, uint256 _value)
internal view returns (uint256)
{
return _list.indexes[_value];
}

function contains(set storage _list, uint256 _value)
internal view returns (bool)
{
return indexOf(_list, _value) != 0;
}

function content(set storage _list)
internal view returns (uint256[] memory)
{
return _list.members;
}

function add(set storage _list, uint256 _value)
internal returns (bool)
{
if (contains(_list, _value))
{
return false;
}
_list.indexes[_value] = _list.members.push(_value);
return true;
}

function remove(set storage _list, uint256 _value)
internal returns (bool)
{
if (!contains(_list, _value))
{
return false;
}

uint256 i = indexOf(_list, _value);
uint256 last = length(_list);

if (i != last)
{
uint256 swapValue = _list.members[last - 1];
_list.members[i - 1] = swapValue;
_list.indexes[swapValue] = i;
}

delete _list.indexes[_value];
--_list.members.length;

return true;
}

}