Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add map ui #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions contracts/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ version = "1.0.0"
authors = []

[addresses]
std = "0x1"
aptos_std = "0x1"
aptos_framework = "0x1"
my_addr = "0x61b96051f553d767d7e6dfcc04b04c28d793c8af3d07d3a43b4e2f8f4ca04c9f"
supermap = "_"

[dev-addresses]
Expand All @@ -29,3 +25,6 @@ rev = "mainnet"
subdir = "aptos-move/framework/aptos-stdlib"

[dev-dependencies]


# aptos move publish --named-addresses supermap=default
50 changes: 22 additions & 28 deletions contracts/sources/map_manager.move
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
module supermap::map_manager {
use aptos_framework::account::{Self, SignerCapability};
use aptos_framework::aptos_account;
use aptos_framework::event;
use aptos_framework::object::{Self, ConstructorRef, Object};
use aptos_framework::object::{Self, Object};
use aptos_framework::timestamp;
use aptos_std::string_utils::{Self};
use aptos_std::simple_map::{Self, SimpleMap};
use aptos_token_objects::collection;
use aptos_token_objects::token;
use aptos_token_objects::property_map;
use std::option;
use std::signer::address_of;
use std::signer;
use std::string::{Self, String};

// coin
use aptos_framework::coin::Coin;
use aptos_framework::coin;
use aptos_framework::aptos_coin::AptosCoin;

// vector
use std::vector;

Expand Down Expand Up @@ -62,8 +54,8 @@ module supermap::map_manager {
struct Map has key {
// it should be a square.
map_size: u64,
// use vector<u8> to represent the map
map: vector<u8>,
// use vector<u64> to represent the map
map: vector<vector<u64>>,
property_mutator_ref: property_map::MutatorRef,
}

Expand Down Expand Up @@ -103,15 +95,16 @@ module supermap::map_manager {
// see the hero guide:
// > https://mp.weixin.qq.com/s/P7VogEWxp-qGpIfaUPPARQ
// mint a map
public entry fun mint_map(owner: &signer, name: String, description: String, uri: String, map_size: u64, map: vector<u8>) acquires State {
public entry fun mint_map(owner: &signer, name: String, description: String, uri: String, map_size: u64, map: vector<vector<u64>>) acquires State {
// TODO: mint a map here
// * map is an nft
// * set uri as the nft's uri.
// * set size and map as the properties of the nft.
// * also: there should be place to set 2d example uri of the map and 3d example uri of the map.

// generate resource acct
let state = borrow_global_mut<State>(@supermap);
let state = borrow_global_mut<State>(get_resource_account_address());
let map_id = state.last_map_id + 1;
let resource_account = account::create_signer_with_capability(&state.signer_cap);

let constructor_ref = token::create_named_token(
Expand All @@ -135,11 +128,6 @@ module supermap::map_manager {
string::utf8(b"size"),
map_size,
);
property_map::add_typed<vector<u8>>(
&property_mutator_ref,
string::utf8(b"map"),
map,
);
// create properties -->

let map_obj = Map {
Expand All @@ -150,11 +138,16 @@ module supermap::map_manager {
move_to(&token_signer, map_obj);

// move to creator

let transfer_ref = object::generate_transfer_ref(&constructor_ref);
let creator_address = signer::address_of(owner);
object::transfer_with_ref(object::generate_linear_transfer_ref(&transfer_ref), creator_address);

// Update global state
let map_address = signer::address_of(&token_signer);
simple_map::add(&mut state.maps, map_id, map_address);

state.last_map_id = map_id;

// Emit a new mintEvent
let event = MintMapEvents {
name,
Expand All @@ -165,7 +158,7 @@ module supermap::map_manager {
event::emit_event(&mut state.mint_map_events, event);
}

public entry fun update_map(owner: &signer, name: String, map_size: u64, map: vector<u8>) acquires Map {
public entry fun update_map(owner: &signer, name: String, map_size: u64, map: vector<vector<u64>>) acquires Map {
// TODO: update the map here
// Only signer_cap owner could do this.
// Will update line by line in the future.
Expand All @@ -192,24 +185,19 @@ module supermap::map_manager {
&string::utf8(b"size"),
map_size,
);
property_map::update_typed(
&map_struct.property_mutator_ref,
&string::utf8(b"map"),
map,
);

// update map object
map_struct.map_size = map_size;
map_struct.map = map;
}

#[view]
public fun read_element(map_obj: Object<Map>, x: u64, y: u64): u8 acquires Map {
public fun read_element(map_obj: Object<Map>, x: u64, y: u64): u64 acquires Map {
// TODO: return the elemnt of the map
let map_address = object::object_address(&map_obj);
let map_struct = borrow_global<Map>(map_address);
let element_idx = map_struct.map_size * y + x;
*vector::borrow<u8>(&map_struct.map, element_idx)
let arr = *vector::borrow<vector<u64>>(&map_struct.map, x);
*vector::borrow<u64>(&arr, y)
}

#[view]
Expand All @@ -218,6 +206,12 @@ module supermap::map_manager {
move_from<Map>(map_address)
}

#[view]
public fun view_map_by_id(map_id: u64): Map acquires State, Map {
let state = borrow_global<State>(get_resource_account_address());
let map_address = *simple_map::borrow(&state.maps, &map_id);
move_from<Map>(map_address)
}

inline fun get_resource_account_address(): address {
account::create_resource_address(&@supermap, STATE_SEED)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"rainbowkit": "^0.0.1",
"react": "18.1.0",
"react-dom": "18.1.0",
"react-hook-form": "^7.51.5",
"react-hot-toast": "^2.4.1",
"react-markdown": "^9.0.1",
"react-select": "^5.7.0",
Expand Down
2 changes: 1 addition & 1 deletion src/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ export const MODULE_URL=`https://explorer.aptoslabs.com/account/${ DAPP_ADDRESS
export const ETH_SIGNER_URL="https://eth-signer-react-app.vercel.app/?msg=";
export const APTOS_SIGNER_URL="https://aptos-signer.vercel.app/?msg="

export const STATE_SEED = "hero_signer";
export const STATE_SEED = "movecraft_signer";
// use to gen resource account.
8 changes: 5 additions & 3 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import {
AptosWalletAdapter,
} from "@manahippo/aptos-wallet-adapter";
import { ModalContext, ModalState } from "../components/ModalContext";
import { Toaster } from "react-hot-toast";
function WalletSelector({ Component, pageProps }: AppProps) {


const [modalState, setModalState] = useState<ModalState>({
walletModal: false,
});
Expand Down Expand Up @@ -47,13 +48,14 @@ function WalletSelector({ Component, pageProps }: AppProps) {
<ModalContext.Provider value={modals}>
<div className="px-8">
<NavBar />
<Toaster position={"bottom-right"} />
<Component {...pageProps} className="bg-base-300" />
</div>
</ModalContext.Provider>
</WalletProvider>
<Footer />
<Footer />
</div>

);
}

Expand Down
Loading