ERC721 extension for efficient batch minting (#3311)
Co-authored-by: Francisco <frangio.1@gmail.com>
This commit is contained in:
@ -32,14 +32,6 @@ struct ${opts.checkpointTypeName} {
|
||||
|
||||
/* eslint-disable max-len */
|
||||
const operations = opts => `\
|
||||
/**
|
||||
* @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
|
||||
*/
|
||||
function latest(${opts.historyTypeName} storage self) internal view returns (${opts.valueTypeName}) {
|
||||
uint256 pos = self.${opts.checkpointFieldName}.length;
|
||||
return pos == 0 ? 0 : _unsafeAccess(self.${opts.checkpointFieldName}, pos - 1).${opts.valueFieldName};
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Pushes a (\`key\`, \`value\`) pair into a ${opts.historyTypeName} so that it is stored as the checkpoint.
|
||||
*
|
||||
@ -73,14 +65,6 @@ function upperLookup(${opts.historyTypeName} storage self, ${opts.keyTypeName} k
|
||||
`;
|
||||
|
||||
const legacyOperations = opts => `\
|
||||
/**
|
||||
* @dev Returns the value in the latest checkpoint, or zero if there are no checkpoints.
|
||||
*/
|
||||
function latest(${opts.historyTypeName} storage self) internal view returns (uint256) {
|
||||
uint256 pos = self.${opts.checkpointFieldName}.length;
|
||||
return pos == 0 ? 0 : _unsafeAccess(self.${opts.checkpointFieldName}, pos - 1).${opts.valueFieldName};
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the value at a given block number. If a checkpoint is not available at that block, the closest one
|
||||
* before it is returned, or zero otherwise.
|
||||
@ -147,7 +131,44 @@ function push(
|
||||
}
|
||||
`;
|
||||
|
||||
const helpers = opts => `\
|
||||
const common = opts => `\
|
||||
/**
|
||||
* @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
|
||||
*/
|
||||
function latest(${opts.historyTypeName} storage self) internal view returns (${opts.valueTypeName}) {
|
||||
uint256 pos = self.${opts.checkpointFieldName}.length;
|
||||
return pos == 0 ? 0 : _unsafeAccess(self.${opts.checkpointFieldName}, pos - 1).${opts.valueFieldName};
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
|
||||
* in the most recent checkpoint.
|
||||
*/
|
||||
function latestCheckpoint(${opts.historyTypeName} storage self)
|
||||
internal
|
||||
view
|
||||
returns (
|
||||
bool exists,
|
||||
${opts.keyTypeName} ${opts.keyFieldName},
|
||||
${opts.valueTypeName} ${opts.valueFieldName}
|
||||
)
|
||||
{
|
||||
uint256 pos = self.${opts.checkpointFieldName}.length;
|
||||
if (pos == 0) {
|
||||
return (false, 0, 0);
|
||||
} else {
|
||||
${opts.checkpointTypeName} memory ckpt = _unsafeAccess(self.${opts.checkpointFieldName}, pos - 1);
|
||||
return (true, ckpt.${opts.keyFieldName}, ckpt.${opts.valueFieldName});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the number of checkpoint.
|
||||
*/
|
||||
function length(${opts.historyTypeName} storage self) internal view returns (uint256) {
|
||||
return self.${opts.checkpointFieldName}.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Pushes a (\`key\`, \`value\`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
|
||||
* or by updating the last one.
|
||||
@ -266,12 +287,12 @@ module.exports = format(
|
||||
// Legacy types & functions
|
||||
types(LEGACY_OPTS),
|
||||
legacyOperations(LEGACY_OPTS),
|
||||
helpers(LEGACY_OPTS),
|
||||
common(LEGACY_OPTS),
|
||||
// New flavors
|
||||
...OPTS.flatMap(opts => [
|
||||
types(opts),
|
||||
operations(opts),
|
||||
helpers(opts),
|
||||
common(opts),
|
||||
]),
|
||||
],
|
||||
'}',
|
||||
|
||||
@ -18,6 +18,14 @@ contract CheckpointsMock {
|
||||
return _totalCheckpoints.latest();
|
||||
}
|
||||
|
||||
function latestCheckpoint() public view returns (bool, uint256, uint256) {
|
||||
return _totalCheckpoints.latestCheckpoint();
|
||||
}
|
||||
|
||||
function length() public view returns (uint256) {
|
||||
return _totalCheckpoints.length();
|
||||
}
|
||||
|
||||
function push(uint256 value) public returns (uint256, uint256) {
|
||||
return _totalCheckpoints.push(value);
|
||||
}
|
||||
@ -29,10 +37,6 @@ contract CheckpointsMock {
|
||||
function getAtProbablyRecentBlock(uint256 blockNumber) public view returns (uint256) {
|
||||
return _totalCheckpoints.getAtProbablyRecentBlock(blockNumber);
|
||||
}
|
||||
|
||||
function length() public view returns (uint256) {
|
||||
return _totalCheckpoints._checkpoints.length;
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
@ -46,6 +50,14 @@ contract Checkpoints${length}Mock {
|
||||
return _totalCheckpoints.latest();
|
||||
}
|
||||
|
||||
function latestCheckpoint() public view returns (bool, uint${256 - length}, uint${length}) {
|
||||
return _totalCheckpoints.latestCheckpoint();
|
||||
}
|
||||
|
||||
function length() public view returns (uint256) {
|
||||
return _totalCheckpoints.length();
|
||||
}
|
||||
|
||||
function push(uint${256 - length} key, uint${length} value) public returns (uint${length}, uint${length}) {
|
||||
return _totalCheckpoints.push(key, value);
|
||||
}
|
||||
@ -57,10 +69,6 @@ contract Checkpoints${length}Mock {
|
||||
function upperLookup(uint${256 - length} key) public view returns (uint${length}) {
|
||||
return _totalCheckpoints.upperLookup(key);
|
||||
}
|
||||
|
||||
function length() public view returns (uint256) {
|
||||
return _totalCheckpoints._checkpoints.length;
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user