-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnft_marketplace.js
54 lines (44 loc) · 1.56 KB
/
nft_marketplace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function Listing(creator, contract, id, price) {
let listingId = STD.crypto.sha256(creator + contract + id + price)
return {
id: listingId,
creator: creator,
contract: contract,
token: id,
price: price,
}
}
function SaveListing(listing) {
STD.write(listing.id, JSON.stringify(listing))
}
function RemoveListing(id) {
STD.delete(id)
}
function LoadListing(id) {
let sListing = STD.read(id)
return JSON.parse(sListing)
}
CONTRACT.functions.list = function(contract, token, price) {
let listing = Listing(CTX.sender, contract, parseInt(token), price)
SaveListing(listing)
let res = STD.fetch(contract, "add_approval", STD.FORWARD, token, CONTRACT.address)
if (res !== "true") {
STD.panic("failed to add approval for token " + token + " on " + contract + "(" + CONTRACT.address + ")\nFull Log: " + JSON.stringify(res))
}
}
CONTRACT.functions.buy = function(id) {
let listing = LoadListing(id)
let res = STD.fetch(listing.contract, "transfer", STD.POST, listing.token, CTX.sender)
if (res !== "true") {
STD.panic("failed to transfer token " + listing.token + " on " + listing.contract + "(" + CONTRACT.address + ")\nFull Log: " + JSON.stringify(res))
}
let ok = STD.bank.sendTokens(CONTRACT.address, listing.price)
if (!ok) {
STD.panic("not enough balance of " + listing.price)
}
ok = STD.bank.withdrawTokens(listing.creator, listing.price)
if (!ok) {
STD.panic("not enough balance of " + listing.price)
}
RemoveListing(listing.id)
}