Skip to content

Commit

Permalink
createDrop
Browse files Browse the repository at this point in the history
  • Loading branch information
julesGoullee committed Nov 7, 2021
1 parent 5f2e73d commit 854292f
Show file tree
Hide file tree
Showing 61 changed files with 2,925 additions and 10,206 deletions.
107 changes: 98 additions & 9 deletions packages/contracts/contracts/CyberDropBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,35 @@ contract CyberDropBase is CyberDestinationFactoryBase {
return drop;
}

function mint(
function createDrop(
string memory _uri,
uint256 _timeStart,
uint256 _timeEnd,
uint256 _price,
uint256 _priceStart,
uint256 _priceEnd,
uint256 _stepDuration,
uint256 _amountCap,
uint256 _shareCyber,
bytes memory _signature
) public returns (uint256 _tokenId) {
address sender = _msgSender();
uint256 nonce = minterNonce(sender);
require(_timeEnd > _timeStart, 'IT');
require(_timeEnd - _timeStart > _stepDuration, 'IS');
require(_priceStart > _priceEnd, 'IP');
require(_shareCyber <= 100, 'ISO');
require(_timeStart < _timeEnd, 'IT');

address sender = _msgSender();
uint256 nonce = minterNonce(sender);
bytes memory _message = abi.encodePacked(
_uri,
_timeStart,
_timeEnd,
_price,
_priceStart,
_priceEnd,
_stepDuration,
_amountCap,
_shareCyber,
nonce,
sender
sender,
nonce
);
address recoveredAddress = keccak256(_message)
.toEthSignedMessageHash()
Expand All @@ -63,10 +69,12 @@ contract CyberDropBase is CyberDestinationFactoryBase {
LibDropStorage.Drop memory drop = LibDropStorage.Drop({
timeStart: _timeStart,
timeEnd: _timeEnd,
priceStart: _priceStart,
priceEnd: _priceEnd,
stepDuration: _stepDuration,
amountCap: _amountCap,
shareCyber: _shareCyber,
creator: payable(sender),
price: _price,
minted: 0
});
LibDropStorage.layout().drops[_tokenId] = drop;
Expand All @@ -75,4 +83,85 @@ contract CyberDropBase is CyberDestinationFactoryBase {

return _tokenId;
}

function mint(uint256 _tokenId) public payable returns (bool) {
address sender = _msgSender();
LibDropStorage.Drop storage drop = LibDropStorage.layout().drops[_tokenId];

require(
block.timestamp > drop.timeStart && block.timestamp <= drop.timeEnd,
'OOT'
);

if (drop.amountCap != 0) {
require(drop.minted < drop.amountCap, 'CR');
}

uint256 price = getMintPrice(drop);
require(msg.value >= price, 'IA');

_safeMint(sender, _tokenId, 1, '');
drop.minted += 1;
emit Minted(sender, _tokenId, 1);

uint256 amountOnCyber = (msg.value * drop.shareCyber) / 100;
uint256 amountCreator = msg.value - amountOnCyber;

drop.creator.transfer(amountCreator);
payable(LibAppStorage.layout().oncyber).transfer(amountOnCyber);
return true;
}

function getMintPriceForToken(uint256 _tokenId)
public
view
returns (uint256)
{
LibDropStorage.Drop storage drop = LibDropStorage.layout().drops[_tokenId];

require(
block.timestamp > drop.timeStart && block.timestamp <= drop.timeEnd,
'OOT'
);
if (drop.amountCap != 0) {
require(drop.minted < drop.amountCap, 'CR');
}
return getMintPrice(drop);
}

function getMintPrice(LibDropStorage.Drop memory drop)
public
view
returns (uint256)
{
uint256 timeSpent = block.timestamp - drop.timeStart;
uint256 duration = drop.timeEnd - drop.timeStart;
return
getPriceFor(
timeSpent,
duration,
drop.priceStart,
drop.priceEnd,
drop.stepDuration
);
}

function getPriceFor(
uint256 _timeSpent,
uint256 _duration,
uint256 _priceStart,
uint256 _priceEnd,
uint256 _stepDuration
) public pure returns (uint256) {
// https://www.desmos.com/calculator/ipv4mm7hrh
// f\left(x\right)=\frac{s\ \cdot d\ +\ \operatorname{mod}\left(x,\ g\right)\ \cdot\ \left(s\ -\ l\right)\ -\ x\ \cdot\ \left(s\ -\ l\right)\ \ }{d}
// (s * d + (x % g) * (s - l) - x * (s - l) / d
return
(_duration *
_priceStart +
(_timeSpent % _stepDuration) *
(_priceStart - _priceEnd) -
_timeSpent *
(_priceStart - _priceEnd)) / _duration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,6 @@
pragma solidity 0.8.9;

//import 'hardhat/console.sol';
import '@openzeppelin/contracts/utils/cryptography/ECDSA.sol';
import '@openzeppelin/contracts/utils/Counters.sol';
import '../CyberDropBase.sol';

import '../CyberDestinationFactoryBase.sol';

contract CyberDestinationFactoryFacet is CyberDestinationFactoryBase {
using ECDSA for bytes32;
using Counters for Counters.Counter;

function mint(
string memory _uri,
uint256 _amount,
uint256 _amountOncyber,
bytes memory _signature
) public returns (uint256 _tokenId) {
address sender = _msgSender();
uint256 nonce = minterNonce(sender);

bytes memory _message = abi.encodePacked(
_uri,
_amount,
_amountOncyber,
nonce,
sender
);
address recoveredAddress = keccak256(_message)
.toEthSignedMessageHash()
.recover(_signature);
require(recoveredAddress == LibAppStorage.layout().manager, 'NM');
require(_amount >= _amountOncyber, 'IAO');

// Mint token
_tokenId = LibAppStorage.layout().totalSupply.current();
setTokenURI(_tokenId, _uri);
LibAppStorage.layout().totalSupply.increment();
LibAppStorage.layout().minterNonce[sender].increment();
_safeMint(sender, _tokenId, _amount, '');

if (_amountOncyber > 0) {
_safeTransfer(
sender,
sender,
LibAppStorage.layout().oncyber,
_tokenId,
_amountOncyber,
''
);
}

emit Minted(sender, _tokenId, _amount);

if (!isApprovedForAll(sender, LibAppStorage.layout().opensea)) {
setApprovalForAll(LibAppStorage.layout().opensea, true);
}

return _tokenId;
}
}
contract CyberDestinationFactoryFacet is CyberDropBase {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,6 @@
pragma solidity 0.8.9;

//import 'hardhat/console.sol';
import '@openzeppelin/contracts/utils/cryptography/ECDSA.sol';
import '@openzeppelin/contracts/utils/Counters.sol';

import '../libraries/LibAppStorage.sol';
import '../libraries/LibDropStorage.sol';
import '../CyberDropBase.sol';

contract CyberDestinationUtilityFactoryFacet is CyberDropBase {
using ECDSA for bytes32;
using Counters for Counters.Counter;

function mintEdition(uint256 _tokenId) public payable returns (bool) {
address sender = _msgSender();
LibDropStorage.Drop storage drop = LibDropStorage.layout().drops[_tokenId];

require(
block.timestamp >= drop.timeStart && block.timestamp <= drop.timeEnd,
'OOT'
);

require(msg.value == drop.price, 'IA');

if (drop.amountCap != 0) {
require(drop.minted < drop.amountCap, 'CR');
}

_safeMint(sender, _tokenId, 1, '');
drop.minted += 1;
emit Minted(sender, _tokenId, 1);

uint256 amountOnCyber = (msg.value * drop.shareCyber) / 100;
uint256 amountCreator = msg.value - amountOnCyber;

drop.creator.transfer(amountCreator);
payable(LibAppStorage.layout().oncyber).transfer(amountOnCyber);
return true;
}
}
contract CyberDestinationUtilityFactoryFacet is CyberDropBase {}
8 changes: 5 additions & 3 deletions packages/contracts/contracts/libraries/LibDropStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ library LibDropStorage {
struct Drop {
uint256 timeStart;
uint256 timeEnd;
uint256 shareCyber;
uint256 price;
uint256 priceStart;
uint256 priceEnd;
uint256 stepDuration;
uint256 amountCap;
uint256 minted;
uint256 shareCyber;
address payable creator;
uint256 minted;
}

struct Layout {
Expand Down
82 changes: 34 additions & 48 deletions packages/contracts/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,58 @@
import { BigNumber, Signer, utils } from 'ethers'

export async function signMintingRequest(
uri: string,
amount: string,
amountOncyber: string,
nonce: string,
minter: string,
signer: Signer
): Promise<string> {
const aURI = utils.toUtf8Bytes(uri)
const aAmount = utils.hexZeroPad(BigNumber.from(amount).toHexString(), 32)
const aAmountOncyber = utils.hexZeroPad(
BigNumber.from(amountOncyber).toHexString(),
32
)
const aNonce = utils.hexZeroPad(BigNumber.from(nonce).toHexString(), 32)
const aMinter = utils.arrayify(minter)
const message = utils.concat([aURI, aAmount, aAmountOncyber, aNonce, aMinter])

const hash = utils.keccak256(message)
const aHash = utils.arrayify(hash)

const signature = await signer.signMessage(aHash)
return signature
}

export async function signMintingUtilityRequest(
export async function signCreateDropRequest(
uri: string,
timeStart: number,
timeEnd: number,
price: number,
priceStart: number,
priceEnd: number,
stepDuration: number,
amountCap: number,
shareCyber: number,
nonce: string,
minter: string,
creator: string,
nonce: number,
signer: Signer
): Promise<string> {
const aURI = utils.toUtf8Bytes(uri)
const aTimeStart = utils.hexZeroPad(
const pUri = utils.toUtf8Bytes(uri)
const pTimeStart = utils.hexZeroPad(
BigNumber.from(timeStart).toHexString(),
32
)
const aTimeEnd = utils.hexZeroPad(BigNumber.from(timeEnd).toHexString(), 32)
const aPrice = utils.hexZeroPad(BigNumber.from(price).toHexString(), 32)
const aAmountCap = utils.hexZeroPad(
const pTimeEnd = utils.hexZeroPad(BigNumber.from(timeEnd).toHexString(), 32)
const pPriceStart = utils.hexZeroPad(
BigNumber.from(priceStart).toHexString(),
32
)
const pPriceEnd = utils.hexZeroPad(BigNumber.from(priceEnd).toHexString(), 32)
const pStepDuration = utils.hexZeroPad(
BigNumber.from(stepDuration).toHexString(),
32
)
const pAmountCap = utils.hexZeroPad(
BigNumber.from(amountCap).toHexString(),
32
)
const ashareCyber = utils.hexZeroPad(
const pShareCyber = utils.hexZeroPad(
BigNumber.from(shareCyber).toHexString(),
32
)
const aNonce = utils.hexZeroPad(BigNumber.from(nonce).toHexString(), 32)
const aMinter = utils.arrayify(minter)
const pCreator = utils.arrayify(creator)
const pNonce = utils.hexZeroPad(BigNumber.from(nonce).toHexString(), 32)
const message = utils.concat([
aURI,
aTimeStart,
aTimeEnd,
aPrice,
aAmountCap,
ashareCyber,
aNonce,
aMinter,
pUri,
pTimeStart,
pTimeEnd,
pPriceStart,
pPriceEnd,
pStepDuration,
pAmountCap,
pShareCyber,
pCreator,
pNonce,
])

const hash = utils.keccak256(message)
const aHash = utils.arrayify(hash)
const pHash = utils.arrayify(hash)

const signature = await signer.signMessage(aHash)
return signature
return signer.signMessage(pHash)
}
Loading

0 comments on commit 854292f

Please sign in to comment.