-
Notifications
You must be signed in to change notification settings - Fork 3
/
coin_drop_client.ts
155 lines (126 loc) · 6.23 KB
/
coin_drop_client.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
// Copyright (c) The Aptos Foundation
// SPDX-License-Identifier: Apache-2.0
import { Account, RestClient, TESTNET_URL, FAUCET_URL, FaucetClient } from "./first_transaction";
const contractAddress = '0x8fff3fcbdd7eddcdd23acaf260dac4566b5c824726074df27cbbedcba1231f91';
export class CoinDropClient {
restClient: RestClient;
constructor(restClient: RestClient) {
this.restClient = restClient;
}
async submitTransactionHelper(account: Account, payload: Record<string, any>) {
const txn_request = await this.restClient.generateTransaction(account.address(), payload)
const signed_txn = await this.restClient.signTransaction(account, txn_request)
const res = await this.restClient.submitTransaction(signed_txn)
await this.restClient.waitForTransaction(res["hash"])
return res["hash"];
}
async initCoinDrop(account: Account, coin_amount: number) {
const payload: { function: string; arguments: string[]; type: string; type_arguments: any[] } = {
type: "script_function_payload",
function: `${contractAddress}::NFTMarketplace::initialize_coin_drop`,
type_arguments: [],
arguments: [
coin_amount.toString()
]
};
return await this.submitTransactionHelper(account, payload);
}
async deposit(account: Account, seller: string, deposit_amount: number) {
const payload: { function: string; arguments: string[]; type: string; type_arguments: any[] } = {
type: "script_function_payload",
function: `${contractAddress}::NFTMarketplace::deposit`,
type_arguments: [],
arguments: [
seller,
deposit_amount.toString()
]
};
return await this.submitTransactionHelper(account, payload);
}
async withdraw(account: Account, seller: string, withdraw_amount: number) {
const payload: { function: string; arguments: string[]; type: string; type_arguments: any[] } = {
type: "script_function_payload",
function: `${contractAddress}::NFTMarketplace::withdraw`,
type_arguments: [],
arguments: [
seller,
withdraw_amount.toString()
]
};
return await this.submitTransactionHelper(account, payload);
}
async claimAsOwner(account: Account) {
const payload: { function: string; arguments: string[]; type: string; type_arguments: any[] } = {
type: "script_function_payload",
function: `${contractAddress}::NFTMarketplace::claimAsOwner`,
type_arguments: [],
arguments: [
]
};
return await this.submitTransactionHelper(account, payload);
}
async claim(account: Account, seller: string) {
const payload: { function: string; arguments: string[]; type: string; type_arguments: any[] } = {
type: "script_function_payload",
function: `${contractAddress}::NFTMarketplace::claim`,
type_arguments: [],
arguments: [
seller
]
};
return await this.submitTransactionHelper(account, payload);
}
}
async function main() {
const restClient = new RestClient(TESTNET_URL);
const client = new CoinDropClient(restClient);
const faucet_client = new FaucetClient(FAUCET_URL, restClient);
const seller = new Account();
const bidder1 = new Account();
const bidder2 = new Account();
const bidder3 = new Account();
console.log("\n=== Addresses ===");
console.log(`Seller: ${seller.address()}`);
console.log(`Bidder1: ${bidder1.address()}`);
console.log(`Bidder2: ${bidder2.address()}`);
console.log(`Bidder3: ${bidder3.address()}`);
await faucet_client.fundAccount(seller.address(), 10_000_000);
await faucet_client.fundAccount(bidder1.address(), 10_000_000);
await faucet_client.fundAccount(bidder2.address(), 10_000_000);
await faucet_client.fundAccount(bidder3.address(), 10_000_000);
console.log("\nAptosCollection and AptosToken created");
const sellerAddress = `0x${seller.address().toString()}`;
const creatorAddress = `0x${seller.address().toString()}`;
console.log("\n=== Initializing Auction ===");
console.log("transaction hashes");
console.log(await client.initCoinDrop(seller, 1000)); //10 secs in microseconds
console.log("\n=== Bidding on the token ===");
console.log("transaction hashes");
console.log(await client.deposit(bidder1, sellerAddress, creatorAddress, collection_name, token_name, 10));
console.log(await client.bid(bidder2, sellerAddress, creatorAddress, collection_name, token_name, 10));
console.log(await client.bid(bidder3, sellerAddress, creatorAddress, collection_name, token_name, 20));
function delay(ms: number) {
return new Promise( resolve => setTimeout(resolve, ms) );
}
await delay(10000);
console.log("\n=== Claiming Token and Coins ===");
console.log("transaction hashes");
console.log(await client.claimToken(bidder1, sellerAddress, creatorAddress, collection_name, token_name));
console.log(await client.claimToken(bidder2, sellerAddress, creatorAddress, collection_name, token_name));
console.log(await client.claimToken(bidder3, sellerAddress, creatorAddress, collection_name, token_name));
console.log(await client.claimCoins(seller, creatorAddress, collection_name, token_name));
var token_balance = await tokenClient.getTokenBalance(sellerAddress, creatorAddress, collection_name, token_name);
console.log(`\nSeller token balance: ${token_balance}`)
token_balance = await tokenClient.getTokenBalance(bidder1.address(), creatorAddress, collection_name, token_name);
console.log(`Bidder 1 token balance: ${token_balance}`)
token_balance = await tokenClient.getTokenBalance(bidder2.address(), creatorAddress, collection_name, token_name);
console.log(`Bidder 2 token balance: ${token_balance}`)
token_balance = await tokenClient.getTokenBalance(bidder3.address(), creatorAddress, collection_name, token_name);
console.log(`Bidder 3 token balance: ${token_balance}`)
// const token_balance = await tokenClient.getTokenBalance('96ac91f63da8514d35c385e76aa5ab5701e5aa13978b35a836feaf31f026aef9', 'f407a02ea4af34410ca0c298eb3e7a43e56bfbc06773d32d15ac6bee5965ee23', collection_name, token_name);
// console.log(`Bidder 3 token balance: ${token_balance}`)
return "Test Completed"
}
if (require.main === module) {
main().then((resp) => console.log(resp));
}