-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.mjs
234 lines (198 loc) · 6.81 KB
/
index.mjs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { loadStdlib, ask } from '@reach-sh/stdlib';
import * as backend from './build/index.main.mjs';
(async () => {
const stdlib = await loadStdlib();
const startingBalance = stdlib.parseCurrency(100);
const fmt = (x) => stdlib.formatCurrency(x, 4);
const getBalance = async (who) => fmt(await stdlib.balanceOf(who));
const isCreator = await ask.ask(
`Are you a Creator?`,
ask.yesno
);
const who = isCreator ? 'Creator' : 'Bidder';
console.log(`Starting Nft Market! as ${who}`);
let acc = null;
const createAcc = await ask.ask(
`Would you like to create an account? (only possible on devnet)`,
ask.yesno
);
if (createAcc) {
acc = await stdlib.newTestAccount(startingBalance);
} else {
const secret = await ask.ask(
`What is your account secret?`,
(x => x)
);
acc = await stdlib.newAccountFromSecret(secret);
}
let ctc = null;
if (isCreator) {
ctc = acc.contract(backend);
ctc.getInfo().then((info) => {
console.log(`The contract is deployed as = ${JSON.stringify(info)}`); });
} else {
const info = await ask.ask(
`Please paste the contract information:`,
JSON.parse
);
ctc = acc.contract(backend, info);
}
const before = await getBalance(acc);
console.log(`Your balance is ${before}`);
const Interact = {
seeOutcome: (price, address) => {
const winner = stdlib.formatAddress(address)
console.log(`The Winner of address: ${address} has paid ${price}`);
},
showBid: (bid) => {
console.log(`A bid of ${fmt(bid)} has been placed` );
},
informTimeout: () => {
console.log("Payment was not performed.")
process.exit(0);
},
isAuctionOn: async () => {
const startAuction = await ask.ask(
`Do you want to start the auction?`,
ask.yesno
);
if(!startAuction){
console.log(`Auction has been ended.`)
return false;
};
console.log(`Auction has been started.`)
return true;
}
};
if(isCreator){
const id = await ask.ask(
`Enter NFT Id: `,
(x => x)
);
const deadline = await ask.ask(
`Enter Auction Dealine: `,
(x => x)
);
const basePirce = await ask.ask(
`Enter Base Price: `,
(x => x)
);
const royalty = await ask.ask(
`Enter Asking Royalty: `,
(x => x)
);
const url = await ask.ask(
`Enter NFT url: `,
(x => x)
);
Interact.createNFT = () => {
return {
basePrice: stdlib.parseCurrency(basePirce),
royalty: royalty,
uri: url
}
},
Interact.getId = id
Interact.deadline = deadline
}else{
Interact.getBid = async (price) => {
console.log("Current price is: ", fmt(price))
const counterBid = await ask.ask(
`Enter Counter Bid: `,
(x => x)
);
if (price < stdlib.parseCurrency(counterBid)) {
console.log("You have bid ", counterBid, "ALGO");
return stdlib.parseCurrency(counterBid);
};
console.log("You have bid ", fmt(price), "ALGO")
return price;
}
}
const part = isCreator ? ctc.p.Creator : ctc.p.Bidder;
await part(Interact);
const after = await getBalance(acc);
console.log(`Your balance is now ${after}`);
ask.done();
// const accCreator = await stdlib.newTestAccount(startingBalance);
// const accBidderA = await stdlib.newTestAccount(startingBalance);
// const accBidderB = await stdlib.newTestAccount(startingBalance);
// const addessBidderA = accBidderA.getAddress();
// const ctcCreator = accCreator.contract(backend);
// const ctcInfo = ctcCreator.getInfo();
// const bids = {
// "BiddderA": {
// maxBid: stdlib.parseCurrency(9),
// },
// "BidderB": {
// maxBid: stdlib.parseCurrency(6),
// },
// };
// const id = stdlib.randomUInt();
// const deadline = 200;
// const meter = {
// meter: 0,
// bidder: "",
// }
// const Common = (who) => ({
// seeOutcome: (price, address) => {
// const winner = (stdlib.addressEq(addessBidderA, address)) ? "BiddderA" : "BidderB";
// console.log(`${who} Saw The Winner is: `, winner);
// },
// showBid: (bid) => {
// console.log(`${who} saw the bid of ${fmt(bid)} from ${meter.bidder}` );
// },
// informTimeout: () => {
// console.log("Payment was not performed.")
// process.exit(0);
// },
// isAuctionOn: () => {
// meter.meter += 1;
// if(meter.meter == 3){
// console.log(`${who} ended auction.`)
// return false;
// };
// console.log(`${who} started the auction.`)
// return true;
// }
// });
// const makeOwner = (acc, who) => {
// const ctc = acc.contract(backend, ctcInfo);
// return backend.Bidder(ctc, {
// ...Common(who),
// getBid: (price) => {
// console.log(who," see's the price as: ", fmt(price))
// if (stdlib.add(price, stdlib.parseCurrency(1)) < bids[who].maxBid) {
// const bid = stdlib.add(price, stdlib.parseCurrency(1));
// console.log(who," therefore bids ", fmt(bid), "ALGO")
// meter.bidder = who
// return bid;
// };
// return price;
// }
// })
// };
// await Promise.all([
// backend.Creator(
// ctcCreator,{
// ...Common("Creator"),
// createNFT: () => {
// return {
// basePrice: stdlib.parseCurrency(3),
// royalty: 49,
// uri: ""
// }
// },
// getId: id,
// deadline: deadline,
// }),
// makeOwner(accBidderA, "BiddderA"),
// makeOwner(accBidderB, "BidderB"),
// ]);
// const bobAfter = await getBalance(accBidderB);
// const aliceAfter = await getBalance(accBidderA);
// const creatorAfter = await getBalance(accCreator);
// console.log("BidderB now left with: " + bobAfter);
// console.log("BiddderA now left with: " + aliceAfter);
// console.log("Creator now left with: " + creatorAfter);
})();