You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/API.md
+15-16Lines changed: 15 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -27,16 +27,15 @@ Generally, users can specify a list of ERC721 token ID to create an ERC721 red p
27
27
28
28
1. Users need to call `setApprovalForAll()` in advance to approve of our red packet operating users' asset.
29
29
30
-
2. Users call `create_red_packet()` in our red packet contract. At this step, users need to specify a list of the NFT tokenID to put in red packet. During creation, the specified NFTs are **NOT** transferred to our red packet since it requires plenty of gas if we transfer these NFTs one by one. At this stage, red packet contract only has the grant to operate the NFTs rather than **own** these NFTs. Thus, we need to design a mechanism to handle the situation where an NFT has already been transferred to another address when a user tries to claim it.
30
+
2. Users call `create_red_packet()` in our red packet contract. At this step, users need to specify a list of the NFT tokenID to be put in red packet. During creation, the specified NFTs are **NOT** transferred to our red packet since it requires plenty of gas if we transfer these NFTs one by one. At this stage, red packet contract only has the grant to operate the NFTs rather than **own** these NFTs. Thus, we need to design a mechanism to handle the situation where an NFT has already been transferred to another address when a user tries to claim it.
31
31
32
-
3. Users can call `claim()` to claim an NFT from the red packet contract.
32
+
3. Users can call `claim()`function to claim an NFT from the red packet.
33
33
- At the beginning, the NFT will be picked randomly.
34
34
- If the picked NFT is still in red packet creators's account, then the user can claim the NFT directly.
35
35
- If the user meets the situation where an NFT has already been transferred to another address, then red packet contract needs to reselect an available NFT:
36
-
-We use a `uint256` variable `bit_status` to record the status of each NFT in red packet. (Therefore, we currently only allow 256 NFTs to be put in red packet at most.)
37
-
- Firstly, we check if the NFT still belongs to the red packet creator. If no, that means the NFT has already been transferred to another address.
38
-
- Secondly, we start to scan `bit_status` from the first bit. If the bit is 1, we skip it; If the bit is 0, we check if the NFT still belongs to the red packet creator.
39
-
- If yes, we pick this NFT.
36
+
- We use a `uint256` variable `bit_status` to record the status of each NFT in red packet. (Therefore, we currently only allow 256 NFTs to be put in red packet at most.)
37
+
- Firstly, we start to scan `bit_status` from the first bit. If the bit is 1, we skip it; If the bit is 0, we check if the NFT still belongs to the red packet creator.
38
+
- If yes, we pick this NFT and stop the NFT selection period.
40
39
- If no, we update the corresponding bit in `bit_status` to 1 and continue to scan `bit_status`.
41
40
- Then we repeat the above steps until we find an available NFT or there is no NFT remain(Will be revert with error).
42
41
@@ -261,15 +260,15 @@ Get the current availability status of all tokens in a red packet.
261
260
Generally, users can specify a list of ERC721 token ID to create an ERC721 red packet with RedPacket_ERC721. Then, users are able to randomly claim one NFT from the red packet.
262
261
263
262
### Workflow in Red Packet
264
-
1. Users need to call `approve()` to approve of our red packet contract operating enough amount of users' token.
263
+
1. Users need to call `approve()` to approve of our red packet contract operating enough amount of users' tokens.
265
264
266
-
2. Users call `create_red_packet()` in our red packet contract. At this step, creators are able to choose the claim method: avarage or random. **Notice**: Different from the design in RedPacket_ERC721, tokens to be put in red packet will be transferred to red packet contract during creation period in fungible token red packet.
265
+
2. Users call `create_red_packet()` in our red packet contract. At this step, creators are able to choose the claim method: average or random. **Notice**: Different from the design in RedPacket_ERC721, tokens to be put in red packet will be transferred to red packet contract during red packet creation period.
267
266
268
-
3. Users can call `claim()` to claim tokens from the red packet contract.
269
-
1. If the claimer is claiming the last packet, the claimer will get all of the remaining token directly.
267
+
3. Users can call `claim()` to claim tokens from the red packet.
268
+
1. If the claimer is claiming the last packet, the claimer will get all of the remaining tokens directly.
270
269
2. Otherwise, if the red packet is set as random, the claimer will get random amount of tokens. If the red packet is set as average, the claimer will get average amount of tokens.
271
270
272
-
4. After the red packet expired, if there are remain tokens in this red packet, the red packet creator is able to withdraw the remain tokens.
271
+
4. After the red packet expired, if there are remaining tokens in this red packet, the red packet creator is able to withdraw the remaining tokens.
273
272
274
273
275
274
### create red packet
@@ -405,7 +404,7 @@ Check red packet info.
405
404
- N/A
406
405
407
406
### refund
408
-
After the redpacket expired, the red packet creator is able to withdraw the remaining token.
407
+
After the red packet expired, the red packet creator is able to withdraw the remaining token.
409
408
410
409
```solidity
411
410
function refund(bytes32 id) public {}
@@ -432,16 +431,16 @@ After the redpacket expired, the red packet creator is able to withdraw the rema
432
431
```
433
432
434
433
## Verification Design
435
-
Both red packet smart contracts contains a verification mechanism which is used to ensure the claimer is claiming red packet through our Mask externsion.
434
+
Both red packet smart contracts contain a verification mechanism which is used to ensure the claimer is claiming red packet through our Mask extension.
436
435
437
436
Verification mechanism:
438
-
1. Front-end randomly generates an Ethereum account A (the account address is the public key) with its private key. While creating the red packet, we need to pass the public key to redpacket smart contract as a parameter.
437
+
1. Front-end randomly generates an Ethereum account A (the account address is the public key) with its private key. While creating the red packet, we need to pass the public key to red packet smart contract as a parameter.
439
438
2. Smart contract will store the public key and front-end will keep the private key.
440
439
3. When a user tries to claim the red packet, front-end will sign a message with the private key of account A. The content of this message is the address of account B who calls the `claim()` function (i.e. `msg.sender`).
441
440
4. The signature will be passed to smart contract as a parameter while calling `claim()`.
442
-
5. With the signature and the hash of `msg.sender`, redpacket smart contract is able to calculate the address of account who signs the message.
441
+
5. With the signature and the hash of `msg.sender`, red packet smart contract is able to calculate the address of account who signs the message.
443
442
6. By comparing the public key stored during red packet creation period with the account address we calculated, we are able to distinguish if the function caller carries the signature signed with the right private key kept in front-end.
444
443
445
444
With this mechanism:
446
445
- Users who don't have the private key, they cannot generate a right signature. So they will be rejected while calling `claim()`.
447
-
- If they listen to Ethereum network, and catch the transaction payload which claimed successfully. They will try to claim with the same payload. They'll still get rejected since our contract will calculate the hash of message with`msg.sender` which cannot be the same with the successful claim case.
446
+
- If they listen to Ethereum network, and catch the transaction payload which claimed successfully. They will try to claim with the same payload. They'll still get rejected since our contract will calculate the hash of message using`msg.sender` which cannot be the same with the successful claim case.
0 commit comments