/
gah_sellListing.ts
366 lines (326 loc) · 11.4 KB
/
gah_sellListing.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import {
ENV_AH,
AUCTION_HOUSE_ADDRESS,
TOKEN_PROGRAM_ID,
} from './helpers/constants';
import {
Transaction,
PublicKey,
LAMPORTS_PER_SOL,
SystemProgram,
TransactionInstruction,
SYSVAR_INSTRUCTIONS_PUBKEY,
SYSVAR_RENT_PUBKEY,
Connection} from '@solana/web3.js'
import { web3 } from '@project-serum/anchor';
import { RPC_CONNECTION, RPC_ENDPOINT } from '../grapeTools/constants';
import { InstructionsAndSignersSet } from "./helpers/types";
import { concat } from 'ramda';
import { AuctionHouseProgram } from '@metaplex-foundation/mpl-auction-house'
//import { Metadata } from '@metaplex-foundation/mpl-token-metadata'
import {
loadAuctionHouseProgram,
getMetadata,
} from './helpers/accounts';
import { getPriceWithMantissa } from './helpers/various';
import { decodeMetadata, Metadata } from './helpers/schema';
const {
createPublicBuyInstruction,
createPrintBidReceiptInstruction,
createExecuteSaleInstruction,
createPrintPurchaseReceiptInstruction,
} = AuctionHouseProgram.instructions
export async function gah_sellListing(offerAmount: number, mint: string, buyerPublicKey: string, mintOwner: any, weightedScore: any, daoPublicKey: string, updateAuthority: string, collectionAuctionHouse: string, tokenDecimals: number): Promise<InstructionsAndSignersSet> {
const tokenSize = 1;
const auctionHouseKey = new web3.PublicKey(collectionAuctionHouse || AUCTION_HOUSE_ADDRESS);
const mintKey = new web3.PublicKey(mint);
const anchorProgram = await loadAuctionHouseProgram(null, ENV_AH, RPC_ENDPOINT);
const auctionHouseObj = await anchorProgram.account.auctionHouse.fetch(auctionHouseKey,);
const buyerAddress = new web3.PublicKey(buyerPublicKey);
const sellerAddress = new web3.PublicKey(mintOwner);
//check if escrow amount already exists to determine if we need to deposit amount to grapevine
let lps = LAMPORTS_PER_SOL;
if (tokenDecimals){
lps = Math.pow(10, tokenDecimals);
}
const buyerPrice = Number(offerAmount) * lps
//console.log("buyerPrice: "+buyerPrice);
const auctionHouse = new PublicKey(auctionHouseKey);//new PublicKey(auctionHouseObj.auctionHouse.address)
//console.log("auctionHouse: "+auctionHouseObj.auctionHouse.address);
const authority = new PublicKey(auctionHouseObj.authority)
const auctionHouseFeeAccount = new PublicKey(
auctionHouseObj.auctionHouseFeeAccount
)
const auctionHouseTreasury = new PublicKey(
auctionHouseObj.auctionHouseTreasury
)
const treasuryMint = new PublicKey(auctionHouseObj.treasuryMint)
const tokenMint = mintKey
//console.log("mintOwner: "+JSON.stringify(mintOwner));
//const tokenAccount = new PublicKey(mintOwner)
const results = await anchorProgram.provider.connection.getTokenLargestAccounts(mintKey);
const tokenAccount: web3.PublicKey = results.value[0].address;
let sellerWalletKey = new PublicKey(mintOwner);
if (daoPublicKey){
sellerWalletKey = new web3.PublicKey(daoPublicKey);
}
const [buyerTradeState, tradeStateBump] =
await AuctionHouseProgram.findPublicBidTradeStateAddress(
buyerAddress,
auctionHouse,
treasuryMint,
tokenMint,
buyerPrice,
tokenSize
)
const [sellerTradeState, sellerTradeStateBump] =
await AuctionHouseProgram.findTradeStateAddress(
sellerWalletKey,//publicKey,
auctionHouse,
tokenAccount,
treasuryMint,
tokenMint,
buyerPrice,//offer.price.toNumber(),
tokenSize
)
const ggoconnection = RPC_CONNECTION;
//const tokenMetadata = await getMetadata(tokenMint);
//const [tokenMetadata] = await Metadata.fromAccountAddress(ggoconnection, tokenMint); //await getMetadata(tokenMint)
const metadata = await getMetadata(tokenMint);
//console.log("tokenMetadata: "+JSON.stringify(tokenMetadata));
const [programAsSigner, programAsSignerBump] =
await AuctionHouseProgram.findAuctionHouseProgramAsSignerAddress()
const [freeTradeState, freeTradeStateBump] =
await AuctionHouseProgram.findTradeStateAddress(
sellerWalletKey,
auctionHouse,
tokenAccount,
treasuryMint,
tokenMint,
0,
tokenSize
)
const buyerPubkey = new PublicKey(buyerAddress)
const [escrowPaymentAccount, escrowPaymentBump] =
await AuctionHouseProgram.findEscrowPaymentAccountAddress(
auctionHouse,
buyerPubkey
)
console.log("buyerPubkey: "+JSON.stringify(buyerPubkey));
console.log("paymentAccount: "+JSON.stringify(buyerPubkey));
console.log("transferAuthority: "+JSON.stringify(buyerPubkey));
console.log("treasuryMint: "+JSON.stringify(treasuryMint));
console.log("tokenAccount: "+JSON.stringify(tokenAccount));
console.log("metadata: "+JSON.stringify(metadata));
console.log("escrowPaymentAccount: "+JSON.stringify(escrowPaymentAccount));
console.log("authority: "+JSON.stringify(authority));
console.log("auctionHouse: "+JSON.stringify(auctionHouse));
console.log("auctionHouseFeeAccount: "+JSON.stringify(auctionHouseFeeAccount));
console.log("buyerTradeState: "+JSON.stringify(buyerTradeState));
const publicBuyInstructionAccounts = {
wallet: buyerPubkey,
paymentAccount: buyerPubkey,
transferAuthority: buyerPubkey,
treasuryMint,
tokenAccount,
metadata,
escrowPaymentAccount,
authority,
auctionHouse,
auctionHouseFeeAccount,
buyerTradeState,
}
console.log("tradeStateBump: "+JSON.stringify(tradeStateBump));
console.log("escrowPaymentBump: "+JSON.stringify(escrowPaymentBump));
console.log("buyerPrice: "+JSON.stringify(buyerPrice));
const publicBuyInstructionArgs = {
tradeStateBump,
escrowPaymentBump,
buyerPrice,
tokenSize: tokenSize,
}
const [buyerReceiptTokenAccount] =
await AuctionHouseProgram.findAssociatedTokenAccountAddress(
tokenMint,
buyerPubkey
)
const transferAuthority = web3.Keypair.generate();
const signers = true ? [] : [transferAuthority];
const executeSaleInstructionAccounts2 = {
buyer: buyerAddress,
sellerAddress,
transferAuthority,
treasuryMint,
tokenAccount,
metadata,
escrowPaymentAccount,
authority,
auctionHouse,
auctionHouseFeeAccount,
buyerTradeState,
tokenMint,
sellerPaymentReceiptAccount: sellerWalletKey,
buyerReceiptTokenAccount,
auctionHouseTreasury,
sellerTradeState,
freeTradeState,
programAsSigner,
}
const executeSaleInstructionAccounts = {
buyer: buyerAddress,
seller: sellerAddress,
tokenAccount,
auctionHouse,
tokenMint,
treasuryMint,
metadata,
authority,
sellerTradeState,
buyerTradeState,
freeTradeState,
sellerPaymentReceiptAccount: sellerAddress,
escrowPaymentAccount,
buyerReceiptTokenAccount,
auctionHouseFeeAccount,
auctionHouseTreasury,
programAsSigner,
}
const executeSaleInstructionArgs = {
escrowPaymentBump,
freeTradeStateBump,
programAsSignerBump,
buyerPrice: buyerPrice,
tokenSize: tokenSize,
partialOrderSize:null,
partialOrderPrice:null
}
const [bidReceipt, bidReceiptBump] =
await AuctionHouseProgram.findBidReceiptAddress(buyerTradeState)
const printBidReceiptAccounts = {
bookkeeper: buyerAddress,
receipt: bidReceipt,
instruction: SYSVAR_INSTRUCTIONS_PUBKEY,
}
const printBidReceiptArgs = {
receiptBump: bidReceiptBump,
}
const [purchaseReceipt, purchaseReceiptBump] =
await AuctionHouseProgram.findPurchaseReceiptAddress(
sellerTradeState,
buyerTradeState
)
const [listingReceipt, listingReceiptBump] =
await AuctionHouseProgram.findListingReceiptAddress(sellerTradeState)
const printPurchaseReceiptAccounts = {
bookkeeper: buyerAddress,
purchaseReceipt,
bidReceipt,
listingReceipt,
instruction: SYSVAR_INSTRUCTIONS_PUBKEY,
}
const printPurchaseReceiptArgs = {
purchaseReceiptBump,
}
const publicBuyInstruction = createPublicBuyInstruction(
publicBuyInstructionAccounts,
publicBuyInstructionArgs
)
const printBidReceiptInstruction = createPrintBidReceiptInstruction(
printBidReceiptAccounts,
printBidReceiptArgs
)
const executeSaleInstruction = createExecuteSaleInstruction(
executeSaleInstructionAccounts,
executeSaleInstructionArgs,
)
const printPurchaseReceiptInstruction =
createPrintPurchaseReceiptInstruction(
printPurchaseReceiptAccounts,
printPurchaseReceiptArgs
)
const txt = new Transaction()
const metadataObj = await anchorProgram.provider.connection.getAccountInfo(metadata,);
const metadataDecoded: Metadata = decodeMetadata(Buffer.from(metadataObj.data),);
const nft = metadataDecoded.data;
/*
nft.creators.map((creator: any) => (
console.log("executeSaleInstruction: "+JSON.stringify(executeSaleInstruction.keys))
));
*/
txt
.add(publicBuyInstruction)
.add(printBidReceiptInstruction)
.add(
new TransactionInstruction({
programId: AuctionHouseProgram.PUBKEY,
data: executeSaleInstruction.data,
keys: concat(
executeSaleInstruction.keys,
nft?.creators ? nft.creators.map((creator: any) => ({
pubkey: new PublicKey(creator.address),
isSigner: false,
isWritable: true,
}))
:[]
),
})
)
.add(printPurchaseReceiptInstruction)
//txt.add(publicBuyInstruction).add(printPurchaseReceiptInstruction)
//txt.recentBlockhash = (await connection.getRecentBlockhash()).blockhash
txt.feePayer = buyerAddress;
const instructions = txt.instructions;
/*
let derivedMintPDA = await web3.PublicKey.findProgramAddress([Buffer.from((mintKey).toBuffer())], auctionHouseKey);
let derivedBuyerPDA = await web3.PublicKey.findProgramAddress([Buffer.from((sellerWalletKey).toBuffer())], auctionHouseKey);
let derivedOwnerPDA = await web3.PublicKey.findProgramAddress([Buffer.from((new PublicKey(mintOwner)).toBuffer())], auctionHouseKey);
let derivedUpdateAuthorityPDA = await web3.PublicKey.findProgramAddress([Buffer.from((new PublicKey(updateAuthority)).toBuffer())], auctionHouseKey);
const GRAPE_AH_MEMO = {
state:2, // status (0: withdraw, 1: offer, 2: listing, 3: buy/execute (from listing), 4: buy/execute(accept offer), 5: cancel)
ah:auctionHouseKey.toString(), // pk
mint:mintKey.toString(), // mint
ua:updateAuthority, // updateAuthority
amount:buyerPrice, // price
score:weightedScore, // spam protection for our feed/higher score weight higher feed visibility
};
instructions.push(
SystemProgram.transfer({
fromPubkey: sellerWalletKey,
toPubkey: derivedMintPDA[0],
lamports: 0,
})
);
instructions.push(
SystemProgram.transfer({
fromPubkey: sellerWalletKey,
toPubkey: derivedBuyerPDA[0],
lamports: 0,
})
);
instructions.push(
SystemProgram.transfer({
fromPubkey: sellerWalletKey,
toPubkey: derivedOwnerPDA[0],
lamports: 0,
})
);
instructions.push(
SystemProgram.transfer({
fromPubkey: sellerWalletKey,
toPubkey: derivedUpdateAuthorityPDA[0],
lamports: 0,
})
);
instructions.push(
new TransactionInstruction({
keys: [{ pubkey: sellerWalletKey, isSigner: true, isWritable: true }],
data: Buffer.from(JSON.stringify(GRAPE_AH_MEMO), 'utf-8'),
programId: new PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),
})
);
*/
return {
signers: signers,
instructions: instructions
}
}