-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerativeERC721.rb
95 lines (77 loc) · 2.41 KB
/
GenerativeERC721.rb
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
pragma :rubidity, "1.0.0"
import 'ERC721'
contract :GenerativeERC721, is: :ERC721 do
string :public, :generativeScript
mapping ({ uint256: :uint256 }), :public, :tokenIdToSeed
uint256 :public, :totalSupply
uint256 :public, :maxSupply
uint256 :public, :maxPerAddress
string :public, :description
constructor(
name: :string,
symbol: :string,
generativeScript: :string,
maxSupply: :uint256,
description: :string,
maxPerAddress: :uint256
) {
ERC721.constructor(name: name, symbol: symbol)
s.maxSupply = maxSupply
s.maxPerAddress = maxPerAddress
s.description = description
s.generativeScript = generativeScript
}
function :mint, { amount: :uint256 }, :public, returns: :uint256 do
require(amount > 0, 'Amount must be positive')
require(amount + s._balanceOf[msg.sender] <= s.maxPerAddress, 'Exceeded mint limit')
require(amount + s.totalSupply <= s.maxSupply, 'Exceeded max supply')
hash = blockhash(block.number).cast(:uint256) % (2 ** 48)
amount.times do |id|
tokenId = s.totalSupply + id
seed = hash + tokenId
s.tokenIdToSeed[tokenId] = seed
_mint(to: msg.sender, id: tokenId)
end
s.totalSupply += amount
end
function :tokenURI, { id: :uint256 }, :public, :view, :override, returns: :string do
require(_exists(id: id), 'ERC721Metadata: URI query for nonexistent token')
html = getHTML(seed: s.tokenIdToSeed[id])
html_as_base_64_data_uri = "data:text/html;base64," + esc.base64Encode(html)
json_data = {
name: "#{s.name} ##{string(id)}",
description: s.description,
animation_url: html_as_base_64_data_uri,
}.to_json
return "data:application/json,#{json_data}"
end
function :getHTML, { seed: :uint256 }, :public, :view, returns: :string do
<<~HTML
<html>
<head>
<style>
body,
html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
display: block;
}
#canvas {
position: absolute;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
<script>
window.SEED = #{string(seed)};
#{s.generativeScript}
</script>
</html>
HTML
end
end