-
Notifications
You must be signed in to change notification settings - Fork 431
/
NounsSeeder.sol
57 lines (50 loc) · 2.49 KB
/
NounsSeeder.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// SPDX-License-Identifier: GPL-3.0
/// @title The NounsToken pseudo-random seed generator
/*********************************
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
* ░░░░░░█████████░░█████████░░░ *
* ░░░░░░██░░░████░░██░░░████░░░ *
* ░░██████░░░████████░░░████░░░ *
* ░░██░░██░░░████░░██░░░████░░░ *
* ░░██░░██░░░████░░██░░░████░░░ *
* ░░░░░░█████████░░█████████░░░ *
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ *
*********************************/
pragma solidity ^0.8.6;
import { INounsSeeder } from './interfaces/INounsSeeder.sol';
import { INounsDescriptorMinimal } from './interfaces/INounsDescriptorMinimal.sol';
contract NounsSeeder is INounsSeeder {
/**
* @notice Generate a pseudo-random Noun seed using the previous blockhash and noun ID.
*/
// prettier-ignore
function generateSeed(uint256 nounId, INounsDescriptorMinimal descriptor) external view override returns (Seed memory) {
uint256 pseudorandomness = uint256(
keccak256(abi.encodePacked(blockhash(block.number - 1), nounId))
);
uint256 backgroundCount = descriptor.backgroundCount();
uint256 bodyCount = descriptor.bodyCount();
uint256 accessoryCount = descriptor.accessoryCount();
uint256 headCount = descriptor.headCount();
uint256 glassesCount = descriptor.glassesCount();
return Seed({
background: uint48(
uint48(pseudorandomness) % backgroundCount
),
body: uint48(
uint48(pseudorandomness >> 48) % bodyCount
),
accessory: uint48(
uint48(pseudorandomness >> 96) % accessoryCount
),
head: uint48(
uint48(pseudorandomness >> 144) % headCount
),
glasses: uint48(
uint48(pseudorandomness >> 192) % glassesCount
)
});
}
}