Move experiments to snippets (or whatever Github's equivalent is)
Closed this issue · 5 comments
ControlxFreak commented
Move experiments to snippets (or whatever Github's equivalent is)
ControlxFreak commented
drop_rates.py
# %%
"""
Rarity Level Statistics
=======================
The probability mass function (PMF) describing their rarity follows a sliding Poisson distribution.
Each week, the mean of the Poisson PMF will change from [1, 2, 6, 16].
The values of each rarity level remain fixed at 2 * [1,2,3,4,5,6,7].
"""
import os
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import poisson
# Initialization
labels = ["Common", "Uncommon", "Rare", "Legendary", "Mythical", "N-F-Tacular", "Christmas Miracle"]
num_labels = len(labels)
ids = np.arange(1, num_labels + 1)
values = 2*ids
mus = [1, 2, 6, 12, 16]
# Compute the PMFs everyday
figi, axi = plt.subplots(nrows=5, ncols=1, sharex=True, figsize=(10, 15))
ipmfs = []
for wi, mu in enumerate(mus):
# Grab the pmf for these IDs
ipmf = poisson.pmf(ids, mu=mu)
# Normalize to maintain distribution
ipmf /= np.sum(ipmf)
ipmfs.append(ipmf)
# Print the values
print(f"### Week {wi} Probabilities")
print("| Level | Rarity (%) |")
print("| ----------- | ----------- |")
for l, p in zip(labels, ipmf):
print(f"| {l} \t\t | {100 * p:.3f} % |")
# Plot
axi[wi].barh(labels, [100*p for p in ipmf])
axi[wi].set_title(f"Week {wi + 1} Rarity Distribution")
axi[wi].set_xlabel("Probability (%)")
axi[wi].invert_yaxis()
os.makedirs("../docs/", exist_ok=True)
plt.tight_layout()
figi.savefig("../docs/rarity_histogram.png")
# %%
# Compute the probability that everyone gets at least 1 of each NFT rarity
probs = np.ones(num_labels)
for wi, ipmf in enumerate(ipmfs):
# Compute the probability
for i in range(num_labels):
# Note there are 7 attempts per week
probs[i] *= (1 - ipmf[i]) ** 7
# The probability of NOT getting it is 1 - probs
probs = 1 - probs
for l, p in zip(labels, probs):
print(f"P(|{l}| > 1) = {100*p:.3f}")
# %%
ControlxFreak commented
estimate_gasfee.py
"""
From Etherscan, it appears that executing "mint4NFTs()" costs about 450k gas (averaged over 5 attempts).
"""
from requests import Session, Request, Response
import dotenv
import os
import json
from web3 import Web3
# Initialize the environment variables
dotenv.load_dotenv()
ALCHEMY_TOKEN = os.getenv("ALCHEMY_TOKEN")
PINATA_API_KEY = os.getenv("PINATA_API_KEY")
PINATA_SECRET_KEY = os.getenv("PINATA_SECRET_KEY")
CONTRACT_ADDRESS = os.getenv("CONTRACT_ADDRESS")
OWNER_ADDRESS = os.getenv("OWNER_ADDRESS")
OWNER_PRIVATE_KEY = os.getenv("OWNER_PRIVATE_KEY")
# Prepare the IPFS url, payload and header
ipfs_url = "https://api.pinata.cloud/pinning/pinFileToIPFS"
ipfs_headers = {
'pinata_api_key': PINATA_API_KEY,
'pinata_secret_api_key': PINATA_SECRET_KEY
}
# Instantiate the web3 client once since this is time-consuming
ALCHEMY_URL = f"https://eth-goerli.g.alchemy.com/v2/{ALCHEMY_TOKEN}"
w3 = Web3(Web3.HTTPProvider(ALCHEMY_URL))
# Create the account object
account = w3.eth.account.from_key(OWNER_PRIVATE_KEY)
# Load the ABI once into memory
with open("contracts/XmasLootBox_abi.json", "r") as f:
CONTRACT_ABI=json.load(f)
# Instantiate the contract class
contract = w3.eth.contract(address=w3.toChecksumAddress(CONTRACT_ADDRESS), abi=CONTRACT_ABI)
# Get the the current nonce of the owner
nonce = w3.eth.get_transaction_count(OWNER_ADDRESS)
# Build the transaction
txn = contract.functions.mint4NFTs(
OWNER_ADDRESS,
["ipfs://QmPSe6THkLiWXC8oz2Z8KFJX4AkKwknKLZTkqAMYHQCVSV/",
"ipfs://QmPSe6THkLiWXC8oz2Z8KFJX4AkKwknKLZTkqAMYHQCVSV/",
"ipfs://QmPSe6THkLiWXC8oz2Z8KFJX4AkKwknKLZTkqAMYHQCVSV/",
"ipfs://QmPSe6THkLiWXC8oz2Z8KFJX4AkKwknKLZTkqAMYHQCVSV/"]
).build_transaction({
'from': account.address,
'nonce': nonce,
'maxPriorityFeePerGas': w3.toWei(10, 'gwei'),
'maxFeePerGas': w3.toWei(200, 'gwei'),
})
print("========================")
print("Transaction: ")
print(txn)
# Sign and send the transaction
signed_txn = w3.eth.account.sign_transaction(txn, account.key)
txn_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
w3.eth.waitForTransactionReceipt(txn_hash)
print("========================")
print("Signed Transaction: ")
print(txn)
ControlxFreak commented
generate.py
# %%
from copy import deepcopy
import json
import os
from dotenv import load_dotenv
from src.generators import generate_dalle_description, generate_erc721_metadata, generate_dalle_art
from src.rarity import sample_rarity_label_uniform, sample_attributes, sample_frame
from src.artists import add_frame
from dalle2 import Dalle2
from PIL import Image
print("DO NOT RUN THIS.")
print("THIS IS JUST A TEST SCRIPT FOR DRAINING MY ACCOUNT OF UNUSED VALUES")
exit()
load_dotenv()
OPENAI_TOKEN = os.getenv("OPENAI_TOKEN")
FRAME_DIR = "assets/frames/"
OUT_DIR = "out/"
# Define the number of NFTs that you want to generate
N = 3
# Construct the dalle-2 object
dalle = Dalle2(OPENAI_TOKEN)
# Get the next NFT ID
with open('next_id', 'r', encoding="utf8") as f:
next_nft_id = int(f.readline())
# Generate some NFTs
for _ in range(N):
print("==========================================")
print("------------------------------------------")
print("------------------------------------------")
print("==========================================")
# Sample a rarity level
rarity_label = sample_rarity_label_uniform()
print(f"Sampled Rarity Level: {rarity_label}")
# Sample the atributes
attributes = sample_attributes(rarity_label)
# Generate the description string and query Dalle-2
description = generate_dalle_description(attributes)
print(f"Description: {description}")
# Generate the dalle images
images = generate_dalle_art(dalle, description)
# Sample the frame
frame_name = sample_frame(rarity_label)
print(f"Frame Name: {frame_name}")
if frame_name is None:
nft_imgs = [image for image in images]
else:
# Add the frame to each image
frame_path = os.path.join(FRAME_DIR, f"{frame_name}.gif")
nft_imgs = [
add_frame(image, frame_path) for image in images
]
# Generate the ERC721-compliant metadata json
metadata = generate_erc721_metadata(attributes)
# Save the images and metadata to disk
for img, nft_img in zip(images, nft_imgs):
print(f"Saving NFT: {next_nft_id}...")
# First, save the image
img.save(os.path.join(OUT_DIR, f"{next_nft_id}_img.png"))
# Second, save the nft
if frame_name is None:
nft_img.save(os.path.join(OUT_DIR, f"{next_nft_id}.png"))
else:
nft_img[0].save(os.path.join(OUT_DIR, f"{next_nft_id}.gif"),
save_all = True, append_images = nft_img[1:],
optimize = False, duration = 10, loop=0)
# Lastly, save the metadata
metadata_cpy = deepcopy(metadata)
metadata_cpy["name"] = metadata_cpy["name"].format(next_nft_id)
metadata_cpy["description"] = description
with open(os.path.join(OUT_DIR, f"{next_nft_id}.json"), "w", encoding="utf8") as outfile:
json.dump(metadata_cpy, outfile)
next_nft_id+=1
# Update the ID
with open('next_id', 'w', encoding="utf8") as f:
f.write(f"{next_nft_id}")
# %%
ControlxFreak commented
test_rarity_border.py
# %%
from os import makedirs
from PIL import Image
# Load the example image
# frame_name = "neon_frame"
# frame_name = "cat_crime_graffiti"
# frame_name = "glitch_unstable"
# frame_name = "speedlines"
# frame_name = "glitch_wave"
# frame_name = "darkage_red"
frame_name = "rain_of_gold"
# frame_name = "christmas_lights"
frame_gif = Image.open(f"../assets/frames/{frame_name}.gif")
image = Image.open("../assets/example/1.png")
width, height = image.size
# Create a black background image
if frame_name.lower() in ["speedlines", "rain_of_gold"]:
OFFSET = 0
else:
OFFSET = 275
base_layer = Image.new(mode="RGB", size=(width + OFFSET, height + OFFSET))
# Paste the target image on the base layer
base_layer.paste(image, (OFFSET//2, OFFSET//2))
# Break apart the frame gif, paste it ontop of the base image, and reconstruct it in a list
gif_imgs = []
try:
while 1:
# Get the next frame, resize it, and add an alpha layer
frame_gif.seek(frame_gif.tell()+1)
frame_img = frame_gif.resize((width + OFFSET, height + OFFSET))
frame_img = frame_img.convert('RGBA')
# Copy the base layer image
temp_img = base_layer.copy()
# Then the frame on top
temp_img.paste(frame_img, (0, 0), frame_img)
# Add this frame to the output list
gif_imgs.append(temp_img.copy())
except EOFError:
pass # end of sequence
makedirs("output/", exist_ok=True)
gif_imgs[0].save('output/example.gif',
save_all = True, append_images = gif_imgs[1:],
optimize = False, duration = 10, loop=0)
# %%
ControlxFreak commented
just dumping them here... If I care at all, I can just go back in the git history..