Created
February 4, 2023 04:25
-
-
Save jwinterm/3b9c284922a3a4250be9b5a31ddc50fa to your computer and use it in GitHub Desktop.
Script to update rectangular regions on moonplace.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import json | |
import web3 | |
from PIL import Image | |
import base64 | |
from io import BytesIO | |
# Setup web3 things | |
nova_provider = web3.Web3(web3.HTTPProvider('https://nova.arbitrum.io/rpc')) | |
chain_id = 42170 | |
moonplace_address = '0x934095513c1ff89592A4b8490e263da7a6a4CEAc' | |
with open('moonplace.json') as f: | |
abi = json.load(f) | |
moonplace = nova_provider.eth.contract(address=moonplace_address, abi=abi) | |
# Your private key exported from Metamask (if you want to send transactions) | |
pk = "YOUR PRIVATE KEY HERE IF YOU ARE USING WEB3 TO UPDATE TILES" | |
data = [] | |
txtout, b64out = '', '' | |
x,y=0,0 | |
# This section defines the upper left corner of the image by tile position | |
xpos = 41 # X position of upper left corner of image | |
ypos = 82 # Y position of upper left corner of image | |
# This section defines the size of the image in tiles | |
xsize = 7 # X size of image in tiles | |
ysize = 4 # Y size of image in tiles | |
with Image.open("image.png") as im: | |
ypos_init = ypos | |
while x < xsize: | |
while y < ysize: | |
img = im.crop((x*10, y*10, (x+1)*10, (y+1)*10)) | |
buffered = BytesIO() | |
img.save(buffered, format="PNG") | |
#img.save(f"imgout{xpos}{ypos}.png", format="PNG") # Uncomment this line to save the images to disk | |
img_str = str(base64.b64encode(buffered.getvalue()), 'utf-8') | |
tmp_dict = { | |
"title": "Moon Pixel Map", | |
"description": f"Block: ({xpos}, {ypos})", | |
"image": f"data:image/png;base64,{img_str}" | |
} | |
txtout += json.dumps(tmp_dict)+'\n' | |
base64_str = 'data:application/json;base64,'+str(base64.b64encode(json.dumps(tmp_dict).encode('utf-8')), 'utf-8') | |
b64out += base64_str+'\n' | |
data.append([xpos, ypos, base64_str]) | |
y += 1 | |
ypos += 1 | |
y = 0 | |
ypos = ypos_init | |
x += 1 | |
xpos += 1 | |
with open('txtout.txt', 'w') as f: | |
f.write(txtout) | |
with open('b64out.txt', 'w') as f: | |
f.write(b64out) | |
# This section makes txs to update the moonplace contract | |
# Comment this section out or delete it if you don't want to send the transactions | |
for i in data: | |
print(f"*********************************************\n Processing x: {i[0]}, y: {i[1]}") | |
tx = moonplace.functions.update(i[0], i[1], i[2]).buildTransaction({ | |
'chainId': chain_id, | |
'gas': 1000000, | |
'gasPrice': nova_provider.toWei('0.0000000001', 'ether'), | |
'nonce': nova_provider.eth.getTransactionCount(nova_provider.eth.account.from_key(pk).address) | |
}) | |
signed_tx = nova_provider.eth.account.sign_transaction(tx, private_key=pk) | |
tx_hash = nova_provider.eth.sendRawTransaction(signed_tx.rawTransaction) | |
print(nova_provider.toHex(tx_hash)) | |
tx_receipt = nova_provider.eth.waitForTransactionReceipt(tx_hash) | |
print(tx_receipt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment