Skip to content

Commit 3329b36

Browse files
ShallWeKissForeveralex4506
authored andcommitted
NFT Market
1 parent 9af2e0d commit 3329b36

28 files changed

+19338
-0
lines changed

100243303/NFTMarket/Move.toml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "nftmarket"
3+
version = "1.0.0"
4+
authors = []
5+
6+
[addresses]
7+
nftmarket = "0x8b256cbe708582fd2deeb0638e286311242b9f8fc386bce8799a24aa9082a271"
8+
9+
[dev-addresses]
10+
11+
[dependencies.AptosTokenObjects]
12+
git = "https://github.com/aptos-labs/aptos-core.git"
13+
rev = "mainnet"
14+
subdir = "aptos-move/framework/aptos-token-objects"
15+
16+
[dependencies.AptosFramework]
17+
git = "https://github.com/aptos-labs/aptos-core.git"
18+
rev = "mainnet"
19+
subdir = "aptos-move/framework/aptos-framework"
20+
21+
[dev-dependencies]
+324
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
1+
module nftmarket::nftmarket{
2+
use std::option;
3+
use std::signer;
4+
use std::signer::address_of;
5+
use std::string;
6+
use aptos_std::string_utils;
7+
use aptos_std::table;
8+
use aptos_std::table::Table;
9+
use aptos_framework::account;
10+
use aptos_framework::account::SignerCapability;
11+
use aptos_framework::aptos_coin::AptosCoin;
12+
use aptos_framework::coin;
13+
use aptos_framework::event;
14+
use aptos_framework::object;
15+
use aptos_framework::object::{Object};
16+
use aptos_token_objects::collection;
17+
use aptos_token_objects::token;
18+
use aptos_token_objects::token::Token;
19+
20+
const RESOURCECAPSEED : vector<u8> = b"Gauss";
21+
22+
const CollectionDescription: vector<u8> = b"gauss nft test";
23+
24+
const CollectionName: vector<u8> = b"gauss";
25+
26+
const CollectionURI: vector<u8> = b"ipfs://QmWmgfYhDWjzVheQyV2TnpVXYnKR25oLWCB2i9JeBxsJbz";
27+
28+
const TokenURI: vector<u8> = b"ipfs://bafybeiearr64ic2e7z5ypgdpu2waasqdrslhzjjm65hrsui2scqanau3ya/";
29+
30+
const TokenPrefix: vector<u8> = b"Gauss #";
31+
32+
struct ResourceCap has key {
33+
cap: SignerCapability
34+
}
35+
36+
struct TokenRefsStore has key {
37+
burn_ref: token::BurnRef
38+
}
39+
40+
struct Content has key {
41+
content: string::String
42+
}
43+
44+
struct Orders has key, store {
45+
orders: Table<u64, Order>,
46+
order_counter: u64
47+
}
48+
49+
#[event]
50+
struct Order has store, drop, copy {
51+
orderId: u64,
52+
seller: address,
53+
price: u64,
54+
token: Object<Token>,
55+
completed: bool
56+
}
57+
58+
#[event]
59+
struct MintEvent has drop, store {
60+
owner: address,
61+
token: address,
62+
content: string::String
63+
}
64+
65+
#[event]
66+
struct ModifyEvent has drop,store {
67+
owner: address,
68+
tokenId: address,
69+
old_content: string::String,
70+
new_content: string::String
71+
}
72+
73+
#[event]
74+
struct BurnEvent has drop, store {
75+
owner: address,
76+
tokenId: address,
77+
content: string::String
78+
}
79+
80+
#[event]
81+
struct TransferEvent has drop, store {
82+
buyer: address,
83+
seller: address,
84+
price: u64,
85+
tokenId: address
86+
}
87+
88+
#[event]
89+
struct ChangePrice has drop, store {
90+
orderId: u64,
91+
old_price: u64,
92+
new_price: u64
93+
}
94+
95+
#[event]
96+
struct CancelOrder has drop, store {
97+
orderId: u64,
98+
seller: address,
99+
token: address
100+
}
101+
102+
fun init_module(sender: &signer) {
103+
104+
let (resource_signer, resource_cap) = account::create_resource_account(
105+
sender, RESOURCECAPSEED
106+
);
107+
108+
move_to(&resource_signer, ResourceCap{ cap:resource_cap });
109+
110+
collection::create_unlimited_collection(
111+
&resource_signer,
112+
string::utf8(CollectionDescription),
113+
string::utf8(CollectionName),
114+
option::none(),
115+
string::utf8(CollectionURI)
116+
);
117+
118+
let orders = Orders{
119+
orders: table::new(),
120+
order_counter: 0
121+
};
122+
123+
move_to(sender, orders);
124+
125+
}
126+
127+
entry public fun mint(sender: &signer, content: string::String) acquires ResourceCap {
128+
129+
let resource_cap = &borrow_global<ResourceCap>(account::create_resource_address(
130+
&@nftmarket, RESOURCECAPSEED
131+
)).cap;
132+
let resource_signer = &account::create_signer_with_capability(resource_cap);
133+
134+
let token_ref = token::create_numbered_token(
135+
resource_signer,
136+
string::utf8(CollectionName),
137+
string::utf8(CollectionDescription),
138+
string::utf8(TokenPrefix),
139+
string::utf8(b""),
140+
option::none(),
141+
string::utf8(TokenURI),
142+
);
143+
144+
//auto set token's picture
145+
let url = string::utf8(TokenURI);
146+
let id = token::index<Token>(object::object_from_constructor_ref(&token_ref));
147+
string::append(&mut url, string_utils::to_string(&id));
148+
string::append(&mut url, string::utf8(b".png"));
149+
let token_mutator_ref = token::generate_mutator_ref(&token_ref);
150+
token::set_uri(&token_mutator_ref, url);
151+
152+
let token_signer = object::generate_signer(&token_ref);
153+
154+
move_to(&token_signer, TokenRefsStore{
155+
burn_ref: token::generate_burn_ref(&token_ref)
156+
});
157+
move_to(&token_signer, Content{ content });
158+
159+
event::emit(
160+
MintEvent{
161+
owner: signer::address_of(sender),
162+
token: object::address_from_constructor_ref(&token_ref),
163+
content
164+
}
165+
);
166+
167+
object::transfer(
168+
resource_signer,
169+
object::object_from_constructor_ref<Token>(&token_ref),
170+
signer::address_of(sender),
171+
);
172+
173+
}
174+
175+
entry fun modify(sender: &signer, token: Object<Content>, content: string::String) acquires Content {
176+
177+
assert!(object::is_owner(token, signer::address_of(sender)), 1);
178+
179+
let old_content = borrow_global<Content>(object::object_address(&token)).content;
180+
181+
event::emit(
182+
ModifyEvent{
183+
owner: object::owner(token),
184+
tokenId: object::object_address(&token),
185+
old_content,
186+
new_content: content
187+
}
188+
);
189+
190+
borrow_global_mut<Content>(object::object_address(&token)).content = content;
191+
192+
}
193+
194+
entry fun burn(sender: &signer, token: Object<Content>) acquires TokenRefsStore, Content {
195+
196+
assert!(object::is_owner(token, signer::address_of(sender)), 1);
197+
198+
let TokenRefsStore{ burn_ref } = move_from<TokenRefsStore>(object::object_address(&token));
199+
let Content { content } = move_from<Content>(object::object_address(&token));
200+
201+
event::emit(
202+
BurnEvent{
203+
owner: signer::address_of(sender),
204+
tokenId: object::object_address(&token),
205+
content
206+
}
207+
);
208+
209+
token::burn(burn_ref);
210+
211+
}
212+
213+
entry fun createOrder(seller: &signer, token:Object<Token>, price: u64) acquires Orders {
214+
215+
assert!( object::is_owner( token, address_of(seller) ), 2);
216+
217+
let orders = borrow_global_mut<Orders>(@nftmarket);
218+
//order_counter is key
219+
orders.order_counter = orders.order_counter + 1;
220+
//create a new order
221+
let new_order = Order{
222+
orderId: orders.order_counter,
223+
seller: address_of(seller),
224+
price,
225+
token,
226+
completed: false
227+
};
228+
//upsert the new order to orders
229+
table::upsert(&mut orders.orders, orders.order_counter, new_order);
230+
//deposite the seller's NFT to ResourceAccount
231+
object::transfer(seller, token, account::create_resource_address(&@nftmarket, RESOURCECAPSEED));
232+
233+
event::emit(
234+
new_order
235+
);
236+
237+
}
238+
239+
entry fun transfer(buyer: &signer, orderId: u64) acquires ResourceCap, Orders {
240+
241+
let orders = borrow_global_mut<Orders>( @nftmarket );
242+
let order = table::borrow(&orders.orders, orderId);
243+
244+
let tokenOfOrder = order.token;
245+
let tokenOfSeller = order.seller;
246+
let tokenOfPrice = order.price;
247+
let _ = order;
248+
249+
//check the order state and the balance of buyer
250+
assert!( order.completed==false, 2 );
251+
assert!(coin::balance<AptosCoin>(signer::address_of(buyer)) >= order.price, 2);
252+
253+
//transfer APT from buyer to seller
254+
coin::transfer<AptosCoin>(buyer, order.seller, order.price);
255+
256+
//obtain the ResourceAccountSigner to transfer NFT
257+
let resource_cap = &borrow_global<ResourceCap>( account::create_resource_address( &@nftmarket, RESOURCECAPSEED) ).cap;
258+
let resource_signer = account::create_signer_with_capability( resource_cap );
259+
260+
//transfer token from seller to buyer
261+
object::transfer( &resource_signer, order.token, address_of(buyer) );
262+
263+
table::remove(&mut orders.orders, orderId);
264+
265+
event::emit(
266+
TransferEvent{
267+
buyer: address_of(buyer),
268+
seller: tokenOfSeller,
269+
price: tokenOfPrice,
270+
tokenId: object::object_address(&tokenOfOrder),
271+
}
272+
);
273+
274+
}
275+
276+
entry fun changePrice(orderId: u64, new_price: u64) acquires Orders {
277+
278+
let orders = borrow_global_mut<Orders>( @nftmarket );
279+
let order = table::borrow_mut(&mut orders.orders, orderId);
280+
281+
let old_price = order.price;
282+
order.price = new_price;
283+
284+
event::emit(
285+
ChangePrice{
286+
orderId,
287+
old_price,
288+
new_price
289+
}
290+
);
291+
292+
}
293+
294+
entry fun cancelOrder( seller: &signer, orderId: u64) acquires Orders, ResourceCap {
295+
296+
let orders = borrow_global_mut<Orders>( @nftmarket );
297+
let order = table::borrow(&orders.orders, orderId);
298+
299+
let tokenOfOrder = order.token;
300+
let _ = order;
301+
302+
//only the creater of order can cancel the order
303+
assert!( order.seller==address_of(seller), 2);
304+
305+
let resource_cap = &borrow_global<ResourceCap>( account::create_resource_address( &@nftmarket, RESOURCECAPSEED) ).cap;
306+
let resource_signer = account::create_signer_with_capability( resource_cap );
307+
308+
//ResourceAccount repay NFT to seller
309+
object::transfer(&resource_signer, order.token, address_of(seller));
310+
311+
//remove order from orders
312+
table::remove( &mut orders.orders, orderId );
313+
314+
event::emit(
315+
CancelOrder{
316+
orderId,
317+
seller: address_of(seller),
318+
token: object::object_address(&tokenOfOrder)
319+
}
320+
);
321+
322+
}
323+
324+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Getting Started with Create React App
2+
3+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4+
5+
## Available Scripts
6+
7+
In the project directory, you can run:
8+
9+
### `npm start`
10+
11+
Runs the app in the development mode.\
12+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13+
14+
The page will reload if you make edits.\
15+
You will also see any lint errors in the console.
16+
17+
### `npm test`
18+
19+
Launches the test runner in the interactive watch mode.\
20+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21+
22+
### `npm run build`
23+
24+
Builds the app for production to the `build` folder.\
25+
It correctly bundles React in production mode and optimizes the build for the best performance.
26+
27+
The build is minified and the filenames include the hashes.\
28+
Your app is ready to be deployed!
29+
30+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31+
32+
### `npm run eject`
33+
34+
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
35+
36+
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37+
38+
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
39+
40+
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
41+
42+
## Learn More
43+
44+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45+
46+
To learn React, check out the [React documentation](https://reactjs.org/).

0 commit comments

Comments
 (0)