1
0

Working on rendering of notamons

This commit is contained in:
Jon Michael Aanes 2025-04-04 01:00:54 +02:00
parent f659047f35
commit 2dcb86f610
6 changed files with 92 additions and 17 deletions

3
.gitignore vendored
View File

@ -10,4 +10,7 @@ expanded.rs
# Maven with auto-import, exclude module files # Maven with auto-import, exclude module files
*.iml *.iml
# Python
__pycache__/
*.sqlite

View File

@ -1,7 +1,9 @@
import flask import flask
import pbc_client import pbc_client
import base64
import dataclasses import dataclasses
import requests_cache import requests_cache
from collections.abc import Iterator
HTML_INDEX = """ HTML_INDEX = """
@ -63,6 +65,21 @@ class Notamon:
nickname: str nickname: str
species_name: str species_name: str
@dataclasses.dataclass(frozen=True)
class NotamonAttributes:
species_id: int
skin_id: int
effect_id: int
stat_hp: int
stat_attack: int
stat_defense: int
@dataclasses.dataclass(frozen=True)
class AssetVariable:
variable_id: int
asset_id: int
opened: bytes | None
app = flask.Flask(__name__) app = flask.Flask(__name__)
@ -74,18 +91,71 @@ TEST_NOTAMON = Notamon(
species_name = 'Mudkip', species_name = 'Mudkip',
) )
ADDRESS_NFTS = '' ADDRESS_NFTS = '02b82d36784146bd29415dbaa623a5f1cf6e6e1108'
ADDRESS_ASSETS = '' ADDRESS_ASSETS = '0338d039e3960426d0cfc590efbdbef70b049696a3'
SESSION = requests_cache.CachedSession() SESSION = requests_cache.CachedSession()
def determine_notamons():
def compress(b: bytes) -> bytes:
l = []
for i in range(0, len(b)//8):
derp = 0
for j in range(0, 8):
derp |= b[i*8+j] << j
del j
l.append(derp)
del i, derp
return bytes(l)
def decode_opened_variable(value: str) -> bytes:
return compress(base64.b64decode(value))[4:]
def get_asset_variables(client, address) -> Iterator[AssetVariable]:
asset_contract_state, _ = client.get_contract_state(ADDRESS_ASSETS)
for variable in asset_contract_state['variables']:
print(variable)
asset_id = int.from_bytes(base64.b64decode(variable['value']['information']['data']))
opened = decode_opened_variable(variable['value']['openValue']['data']) if variable['value']['openValue'] else None
yield AssetVariable(
variable_id = variable['key'],
asset_id = asset_id,
opened = opened,
)
def to_base64_png(b: bytes) -> str:
return f'data:image/png;base64,{base64.b64encode(b)}'
def get_notamon_nfts(client: pbc_client.PbcClient):
asset_contract_state, _ = client.get_contract_state(ADDRESS_NFTS)
print(asset_contract_state)
yield from []
def select_notamons():
client = pbc_client.PbcClient(SESSION, pbc_client.HOSTNAME_TESTNET) client = pbc_client.PbcClient(SESSION, pbc_client.HOSTNAME_TESTNET)
asset_contract_state = client.get_contract_state(ADDRESS_ASSETS)
nfts = list(get_notamon_nfts(client))
print(nfts)
print(client)
assets = list(get_asset_variables(client, ADDRESS_ASSETS))
print(assets )
return [TEST_NOTAMON for i in range(100)] return [TEST_NOTAMON for i in range(100)]
@app.route("/") @app.route("/")
def hello_world(): def hello_world():
return flask.render_template_string(HTML_INDEX, notamons = determine_notamons()) return flask.render_template_string(HTML_INDEX, notamons = select_notamons())

View File

@ -1,5 +1,5 @@
[package] [package]
name = "zk-file-share" name = "notamon-asset-contract"
readme = "README.md" readme = "README.md"
version.workspace = true version.workspace = true
description.workspace = true description.workspace = true

View File

@ -1,5 +1,5 @@
[package] [package]
name = "nft-v2-contract" name = "notamon-nft"
readme = "README.md" readme = "README.md"
version.workspace = true version.workspace = true
description.workspace = true description.workspace = true

View File

@ -31,9 +31,9 @@ struct OperatorApproval {
/// ``` /// ```
type TokenId = u128; type TokenId = u128;
/// Unit /// Unit2
#[derive(CreateTypeSpec, ReadWriteState)] #[derive(CreateTypeSpec, ReadWriteState)]
pub struct Unit {} pub struct Unit2 {}
/// State of the contract. /// State of the contract.
#[state] #[state]
@ -57,7 +57,7 @@ pub struct NFTContractState {
/// Containing approved operators of owners. Operators can transfer and change approvals on all tokens owned by owner. /// Containing approved operators of owners. Operators can transfer and change approvals on all tokens owned by owner.
/// ///
/// Required by MPC-721. /// Required by MPC-721.
operator_approvals: AvlTreeMap<OperatorApproval, Unit>, operator_approvals: AvlTreeMap<OperatorApproval, Unit2>,
/// Template which the uri's of the NFTs fit into. /// Template which the uri's of the NFTs fit into.
/// ///
/// Required by MPC-721. /// Required by MPC-721.
@ -220,6 +220,7 @@ pub fn initialize(
name: String, name: String,
symbol: String, symbol: String,
uri_template: String, uri_template: String,
asset_contract: Address,
) -> NFTContractState { ) -> NFTContractState {
NFTContractState { NFTContractState {
name, name,
@ -232,6 +233,7 @@ pub fn initialize(
contract_owner: ctx.sender, contract_owner: ctx.sender,
notamon_attributes: AvlTreeMap::new(), notamon_attributes: AvlTreeMap::new(),
cached_asset_unlocks: AvlTreeSet::new(), cached_asset_unlocks: AvlTreeSet::new(),
asset_contract,
} }
} }
@ -303,7 +305,7 @@ pub fn set_approval_for_all(
operator, operator,
}; };
if approved { if approved {
state.operator_approvals.insert(operator_approval, Unit {}); state.operator_approvals.insert(operator_approval, Unit2 {});
} else { } else {
state.operator_approvals.remove(&operator_approval); state.operator_approvals.remove(&operator_approval);
} }
@ -386,22 +388,22 @@ fn unlock_assets(
NFTContractState, NFTContractState,
Vec<EventGroup>, Vec<EventGroup>,
) { ) {
let asset_ids: Vec<AssetId> = notamon_attributes.asset_ids().into_iter().filter( let ids_of_not_yet_unlocked_assets: Vec<AssetId> = notamon_attributes.asset_ids().into_iter().filter(
|id| state.cached_asset_unlocks.contains(id) |id| !state.cached_asset_unlocks.contains(id)
) .collect(); ) .collect();
if asset_ids.is_empty() { if ids_of_not_yet_unlocked_assets.is_empty() {
return (state, vec![]); return (state, vec![]);
} }
for id in asset_ids.clone() { for id in ids_of_not_yet_unlocked_assets.clone() {
state.cached_asset_unlocks.insert(id) state.cached_asset_unlocks.insert(id)
} }
let mut event_group_builder = EventGroup::builder(); let mut event_group_builder = EventGroup::builder();
event_group_builder event_group_builder
.call(state.asset_contract, Shortname::from_u32(0x07)) .call(state.asset_contract, Shortname::from_u32(0x07))
.argument(asset_ids) .argument(ids_of_not_yet_unlocked_assets)
.done(); .done();
( (

View File

@ -1,5 +1,5 @@
[package] [package]
name = "upgradable-v1" name = "todo-upgradable-v1"
readme = "README.md" readme = "README.md"
version.workspace = true version.workspace = true
description.workspace = true description.workspace = true