Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Last active February 20, 2020 00:47
Show Gist options
  • Save alexytiger/acee93d690aaacf7d90152042eb186e2 to your computer and use it in GitHub Desktop.
Save alexytiger/acee93d690aaacf7d90152042eb186e2 to your computer and use it in GitHub Desktop.
e-book
pragma solidity >=0.4.22 <0.7.0;
import "./HitchensUnorderedKeySet.sol";
import "./SafeRemotePurchase.sol";
import "./Ownable.sol";
contract FleaMarketFactory is Ownable {
using HitchensUnorderedKeySetLib for HitchensUnorderedKeySetLib.Set;
HitchensUnorderedKeySetLib.Set private widgetSet;
string public contractName;
struct WidgetStruct {
// pointer on the child contract
address purchaseContract;
}
mapping(bytes32 => WidgetStruct) private widgets;
constructor() public {
contractName = "FleaMarket Smart Contract";
}
event LogCreatePurchaseContract(
address sender,
bytes32 key,
address contractAddress
);
event LogRemovePurchaseContract(address sender, bytes32 key);
// commissionRate, for example, 350 ==> (350/100) = 3.5%
function createPurchaseContract(
bytes32 key,
string calldata description,
string calldata ipfsImageHash,
uint256 commissionRate
) external payable returns (bool createResult) {
// Note that this will fail automatically if
// the key already exists.
widgetSet.insert(key);
WidgetStruct storage wgt = widgets[key];
// msg.sender would be the seller
SafeRemotePurchase c =
(new SafeRemotePurchase).value(msg.value)(
commissionRate,
msg.sender,
key,
description,
ipfsImageHash
);
/*
When a new children contract is created
the msg.sender value passed to the Ownable
is the address of the parent contract.
So we need to tell the child contract who is the contract manager
*/
c.transferOwnership(owner());
// cast contract pointer to address
address newContract = address(c);
wgt.purchaseContract = newContract;
emit LogCreatePurchaseContract(msg.sender, key, newContract);
return true;
}
function getContractCount() public view returns (uint256 contractCount) {
return widgetSet.count();
}
function getContractKeyAtIndex(uint256 index)
external
view
returns (bytes32 key)
{
return widgetSet.keyAtIndex(index);
}
function getContractByKey(bytes32 key)
external
view
returns (address contractAddress)
{
require(
widgetSet.exists(key),
"Can't get a widget that doesn't exist."
);
WidgetStruct storage w = widgets[key];
return (w.purchaseContract);
}
function removeContractByKey(bytes32 key)
external
onlyOwner
returns (bool result)
{
// Note that this will fail automatically if the key doesn't exist
widgetSet.remove(key);
delete widgets[key];
emit LogRemovePurchaseContract(msg.sender, key);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment