Skip to content

Commit 3f61125

Browse files
nkrishangjoaquim-vergeskumaryash90
authored
Pack: using Chainlink VRF v2 (Direct Funding Method) (#296)
* add chainlink contracts package * Create PackVRFDirect: VRF with Direct payment * Burn packs in the claimRewards function instead * Remove addPackContents to bring down size * Change contract type Pack - Pack_VRF * Move pack test to dir * Bug fix: expect randomWords to initially be empty. * Move PackVRFDirect events to interface * Tests for PackVRFDirect * PackVRFDirect: Update docs * Add benchmark test: fulfillRandomness * Make openerToReqId public * Offer convenience public fn canClaimRewards * pkg update * fix canClaimRewards * pkg release * Add openPackAdnClaimRewards * pkg release * Add tests for openPackAndClaimRewards * allow VRF wrapper to call claim rewards * prevent calling open twice while in flight * Add openPackAndClaim to interface * pkg release * Add tests for not being able to open pack while repvious req is in flight * fulfillRandomWords: low gas failsafe * add WIP comment Co-authored-by: Joaquim Verges <[email protected]> Co-authored-by: Yash <[email protected]>
1 parent ab250a7 commit 3f61125

14 files changed

+3810
-11
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
pragma solidity ^0.8.11;
3+
4+
import "../extension/interface/ITokenBundle.sol";
5+
6+
/**
7+
* The thirdweb `Pack` contract is a lootbox mechanism. An account can bundle up arbitrary ERC20, ERC721 and ERC1155 tokens into
8+
* a set of packs. A pack can then be opened in return for a selection of the tokens in the pack. The selection of tokens distributed
9+
* on opening a pack depends on the relative supply of all tokens in the packs.
10+
*/
11+
12+
interface IPackVRFDirect is ITokenBundle {
13+
/**
14+
* @notice All info relevant to packs.
15+
*
16+
* @param perUnitAmounts Mapping from a UID -> to the per-unit amount of that asset i.e. `Token` at that index.
17+
* @param openStartTimestamp The timestamp after which packs can be opened.
18+
* @param amountDistributedPerOpen The number of reward units distributed per open.
19+
*/
20+
struct PackInfo {
21+
uint256[] perUnitAmounts;
22+
uint128 openStartTimestamp;
23+
uint128 amountDistributedPerOpen;
24+
}
25+
26+
/// @notice Emitted when a set of packs is created.
27+
event PackCreated(uint256 indexed packId, address recipient, uint256 totalPacksCreated);
28+
29+
/// @notice Emitted when the opening of a pack is requested.
30+
event PackOpenRequested(address indexed opener, uint256 indexed packId, uint256 amountToOpen, uint256 requestId);
31+
32+
/// @notice Emitted when Chainlink VRF fulfills a random number request.
33+
event PackRandomnessFulfilled(uint256 indexed packId, uint256 indexed requestId);
34+
35+
/// @notice Emitted when a pack is opened.
36+
event PackOpened(
37+
uint256 indexed packId,
38+
address indexed opener,
39+
uint256 numOfPacksOpened,
40+
Token[] rewardUnitsDistributed
41+
);
42+
43+
/**
44+
* @notice Creates a pack with the stated contents.
45+
*
46+
* @param contents The reward units to pack in the packs.
47+
* @param numOfRewardUnits The number of reward units to create, for each asset specified in `contents`.
48+
* @param packUri The (metadata) URI assigned to the packs created.
49+
* @param openStartTimestamp The timestamp after which packs can be opened.
50+
* @param amountDistributedPerOpen The number of reward units distributed per open.
51+
* @param recipient The recipient of the packs created.
52+
*
53+
* @return packId The unique identifer of the created set of packs.
54+
* @return packTotalSupply The total number of packs created.
55+
*/
56+
function createPack(
57+
Token[] calldata contents,
58+
uint256[] calldata numOfRewardUnits,
59+
string calldata packUri,
60+
uint128 openStartTimestamp,
61+
uint128 amountDistributedPerOpen,
62+
address recipient
63+
) external payable returns (uint256 packId, uint256 packTotalSupply);
64+
65+
/**
66+
* @notice Lets a pack owner request to open a pack.
67+
*
68+
* @param packId The identifier of the pack to open.
69+
* @param amountToOpen The number of packs to open at once.
70+
*/
71+
function openPack(uint256 packId, uint256 amountToOpen) external returns (uint256 requestId);
72+
73+
/// @notice Called by a pack opener to claim rewards from the opened pack.
74+
function claimRewards() external returns (Token[] memory rewardUnits);
75+
76+
/// @notice Called by a pack opener to open a pack in a single transaction, instead of calling openPack and claimRewards separately.
77+
function openPackAndClaimRewards(
78+
uint256 _packId,
79+
uint256 _amountToOpen,
80+
uint32 _callBackGasLimit
81+
) external returns (uint256);
82+
83+
/// @notice Returns whether a pack opener is ready to call `claimRewards`.
84+
function canClaimRewards(address _opener) external view returns (bool);
85+
}

0 commit comments

Comments
 (0)