-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
210 lines (187 loc) · 7.15 KB
/
index.ts
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
import {
announceAuction,
authorizeBidders,
awaitTxConfirmed,
claimAuctionLotBidder,
claimAuctionLotSeller,
claimDepositLoser,
cleanupAuction,
discoverBidders,
discoverSellerSignature,
enterAuction,
getWalletVk,
mintTokenUsingAlwaysMints,
moveBidL2,
placeBidL2,
queryAuctions,
queryStandingBidState,
startBidding
} from "hydra-auction-offchain";
import type {
AnnounceAuctionContractParams,
ContractConfig,
ContractOutput,
POSIXTime,
TokenName,
TransactionHash
} from "hydra-auction-offchain";
function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function logConfirmContract<T extends { txHash: TransactionHash }>(
label: string,
config: ContractConfig,
output: ContractOutput<T | TransactionHash>
): Promise<void> {
console.log(label + ":", output);
if (output.tag !== "result") {
throw new Error(label + " contract failed.");
}
await awaitTxConfirmed(config, output.value.txHash ?? output.value);
}
(async () => {
await delay(1000); // need some time for cardano object to be injected
const config: ContractConfig = {
tag: "network",
network: "Preview",
blockfrostApiKey: process.env.BLOCKFROST_API_KEY,
walletApp: "Nami"
};
const preBiddingPeriod = config.tag === "plutip" ? 15000 : 90000;
const biddingPeriod = 600000; // 10 min
const tokenName: TokenName = "4d6f6e614c697361"; // MonaLisa
const mintTxHash = await mintTokenUsingAlwaysMints(config, tokenName, "1");
await awaitTxConfirmed(config, mintTxHash);
// seller: announceAuction
const announceAuctionResult = await runAnnounceAuction(
config,
tokenName,
preBiddingPeriod,
biddingPeriod
);
await logConfirmContract("AnnounceAuction", config, announceAuctionResult);
const auctionInfo = announceAuctionResult.value.auctionInfo;
const auctionCs = auctionInfo.auctionId;
const auctionTerms = auctionInfo.auctionTerms;
const delegateInfo = auctionInfo.delegateInfo;
// seller: queryAuctions
const sellerAuctions = await queryAuctions(config, { myRole: "ActorRoleSeller" });
console.log("Seller auctions:", sellerAuctions);
// bidder: queryAuctions
const auctions = await queryAuctions(config);
console.log("All auctions:", auctions);
// bidder: enterAuction
const depositAmount = "2800000";
const enterAuctionResult = await enterAuction(config, { auctionInfo, depositAmount });
await logConfirmContract("EnterAuction", config, enterAuctionResult);
// bidder: queryAuctions
const bidderAuctions = await queryAuctions(config, { myRole: "ActorRoleBidder" });
console.log("Bidder auctions:", bidderAuctions);
// seller: discoverBidders
const bidders = await discoverBidders(config, auctionInfo);
console.log("Candidate bidders:", bidders);
// seller: authorizeBidders
const biddersToAuthorize = bidders.map((bidder) => bidder.bidderInfo.bidderVk);
const authBiddersParams = { auctionCs, biddersToAuthorize };
const authBiddersResult = await authorizeBidders(config, authBiddersParams);
await logConfirmContract("AuthorizeBidders", config, authBiddersResult);
// bidder: discoverSellerSignature
const sellerAddress = auctionInfo.auctionTerms.sellerAddress;
const bidderVk = await getWalletVk(config);
const sellerSignature = await discoverSellerSignature(config, {
auctionCs,
sellerAddress,
bidderVk: bidderVk.value
});
console.log("Seller signature:", sellerSignature);
// seller: startBidding
document.getElementById("startBidding")?.addEventListener("click", async (event) => {
const startBiddingResult = await startBidding(config, { auctionInfo });
await logConfirmContract("StartBidding", config, startBiddingResult);
});
document.getElementById("moveBidL2")?.addEventListener("click", async (event) => {
// anyone: moveBidL2
const moveBidParams = { auctionCs, auctionTerms, delegateInfo };
const moveBidResult = await moveBidL2(config, moveBidParams);
console.log("MoveBid:", moveBidResult);
});
document.getElementById("placeBidL2")?.addEventListener("click", async (event) => {
// bidder: placeBidL2
const placeBidParams = {
auctionCs,
auctionTerms,
delegateInfo,
sellerSignature: sellerSignature.value,
bidAmount: document.getElementById("bidLovelace")?.value
};
const placeBidResult = await placeBidL2(config, placeBidParams);
console.log("PlaceBid:", placeBidResult);
});
document.getElementById("claimAuctionLot")?.addEventListener("click", async (event) => {
// bidder: queryStandingBidState
const bidState = await queryStandingBidState(config, auctionInfo);
console.log("Standing bid:", bidState);
// bidder: claimAuctionLotBidder
const claimAuctionLotBidderResult = await claimAuctionLotBidder(config, auctionInfo);
await logConfirmContract("ClaimAuctionLotBidder", config, claimAuctionLotBidderResult);
});
/*
// bidder: queryStandingBidState
const bidState = await queryStandingBidState(config, auctionInfo);
console.log("Standing bid:", bidState);
// bidder: claimAuctionLotBidder
await delay(biddingPeriod + 2000);
const claimAuctionLotBidderResult = await claimAuctionLotBidder(config, auctionInfo);
await logConfirmContract("ClaimAuctionLotBidder", config, claimAuctionLotBidderResult);
// bidder: claimDepositLoser (stub)
const claimDepositLoserResult = await claimDepositLoser(config, auctionInfo);
console.log("ClaimDepositLoser:", claimDepositLoserResult);
// seller: claimAuctionLotSeller (stub)
const claimAuctionLotSellerResult = await claimAuctionLotSeller(config, auctionInfo);
console.log("ClaimAuctionLotSeller:", claimAuctionLotSellerResult);
// anyone: cleanupAuction (stub)
const cleanupAuctionResult = await cleanupAuction(config, auctionInfo);
console.log("CleanupAuction:", cleanupAuctionResult);
*/
})();
async function runAnnounceAuction(
config: ContractConfig,
tokenName: TokenName,
preBiddingPeriod: number,
biddingPeriod: number
): Promise<ContractOutput<TransactionHash>> {
const nowTime = Date.now();
const biddingStart = nowTime + preBiddingPeriod;
const biddingEnd = biddingStart + biddingPeriod + 3600000;
const purchaseDeadline = biddingEnd + 3600000;
const cleanup = purchaseDeadline + 60000;
const params: AnnounceAuctionContractParams = {
auctionTerms: {
auctionLot: [
{
cs: "c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d", // AlwaysMints
tn: tokenName,
quantity: "1"
}
],
delegates: [
"ac55de689702d745e77050ce83b77ff9619383bb802e40fb90aa3be4",
"e17e5b8c19c89d663c66b3e4943972d7b6dd70a711fade4cf1a6a95a"
],
biddingStart: biddingStart.toString(),
biddingEnd: biddingEnd.toString(),
purchaseDeadline: purchaseDeadline.toString(),
cleanup: cleanup.toString(),
auctionFeePerDelegate: "3000000",
startingBid: "8000000",
minBidIncrement: "1000000",
minDepositAmount: "3000000"
},
delegateInfo: {
httpServers: ["http://127.0.0.1:7010", "http://127.0.0.1:7011"],
wsServers: ["ws://127.0.0.1:7020", "ws://127.0.0.1:7021"]
},
additionalAuctionLotOrefs: []
};
return await announceAuction(config, params);
}