Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Last active September 2, 2019 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexytiger/45c2e77b30742d751490afe968c05e40 to your computer and use it in GitHub Desktop.
Save alexytiger/45c2e77b30742d751490afe968c05e40 to your computer and use it in GitHub Desktop.
pragma solidity 0.5.11;
import "./HitchensUnorderedKeySet.sol";
contract FleaMarket {
using HitchensUnorderedKeySetLib for HitchensUnorderedKeySetLib.Set;
HitchensUnorderedKeySetLib.Set widgetSet;
struct WidgetStruct {
address contractAddress;
}
mapping(bytes32 => WidgetStruct) widgets;
string public name;
address public lastContractAddress;
constructor() public {
name = "FleaMarket Smart Contract";
}
event logNewPurchaseContract(address contractAddress);
event logRemovePurchaseContract(address sender, bytes32 key);
function createPurchaseContract(bytes32 key, string memory title, string memory ipfsHash)
public payable returns(bool createResult)
{
widgetSet.insert(key); // Note that this will fail automatically if the key already exists.
WidgetStruct storage w = widgets[key];
SafeRemotePurchase c = (new SafeRemotePurchase).value(msg.value)(msg.sender, key, title, ipfsHash);
lastContractAddress = address(c);
w.contractAddress = lastContractAddress;
emit logNewPurchaseContract(lastContractAddress);
return true;
}
function getContractCount() public view returns(uint contractCount) {
return widgetSet.count();
}
function getContractKeyAtIndex(uint index) public view returns(bytes32 key) {
return widgetSet.keyAtIndex(index);
}
function getContractByKey(bytes32 key) public view returns(address contractAddress) {
require(widgetSet.exists(key), "Can't get a widget that doesn't exist.");
WidgetStruct storage w = widgets[key];
return(w.contractAddress);
}
function removeContractByKey(bytes32 key) public {
// Note that this will fail automatically if the key doesn't exist
widgetSet.remove(key);
delete widgets[key];
emit logRemovePurchaseContract(msg.sender, key);
}
}
// based on https://solidity.readthedocs.io/en/latest/solidity-by-example.html
contract SafeRemotePurchase {
// .....
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment