-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathopensea.ts
83 lines (75 loc) · 2.76 KB
/
opensea.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
import { OpenSeaSDK, Chain, OrderSide } from "opensea-js";
import { SEPOLIA_CHAIN_ID, setupNearEthAdapter, sleep } from "./setup";
import * as readline from "readline";
import { ethers } from "ethers";
import { Address, Hex, encodeFunctionData } from "viem";
import seaportABI from "./abis/Seaport.json";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// This script uses the OpenSea SDK:
// https://github.com/ProjectOpenSea/opensea-js/blob/main/developerDocs/advanced-use-cases.md
const run = async (slug: string): Promise<void> => {
const evm = await setupNearEthAdapter();
// This fake provider is required to construct an openseaSDK instance (although we do not make use of it).
const dummyProvider = new ethers.JsonRpcProvider("fakeURL", 11155111);
const openseaSDK = new OpenSeaSDK(dummyProvider, {
chain: Chain.Sepolia,
// apiKey: YOUR_API_KEY,
});
// const slug = "mintbase-chain-abstraction-v2";
console.log("Retrieving Listings for...");
const listings = (await openseaSDK.api.getAllListings(slug)).listings;
if (listings.length === 0) {
console.log(`No available listings for collection: ${slug}`);
return;
}
listings.sort((a, b) =>
a.price.current.value.localeCompare(b.price.current.value)
);
const cheapestAvailable = listings[0];
console.log(
`Got ${listings.length} Listings, purchasing the cheapest available`
);
// This sleep is due to free-tier testnet rate limiting.
await sleep(1000);
const data = await openseaSDK.api.generateFulfillmentData(
evm.address,
cheapestAvailable.order_hash,
cheapestAvailable.protocol_address,
OrderSide.ASK
);
const tx = data.fulfillment_data.transaction;
const input_data = tx.input_data;
// TODO - report or fix these bugs with OpenseaSDK
// @ts-expect-error: Undocumented field on type FulfillmentData within FulfillmentDataResponse
const order = input_data.parameters;
// @ts-expect-error: Undocumented field on type FulfillmentData within FulfillmentDataResponse
const fulfillerConduitKey = input_data.fulfillerConduitKey;
let callData = "0x";
if (tx.function.includes("fulfillOrder")) {
console.log("Using fulfillOrder");
callData = encodeFunctionData({
abi: seaportABI,
functionName: "fulfillOrder",
args: [order, fulfillerConduitKey],
});
} else {
console.log("Using fulfillBasicOrder_efficient_6GL6yc");
callData = encodeFunctionData({
abi: seaportABI,
functionName: "fulfillBasicOrder_efficient_6GL6yc",
args: [order],
});
}
await evm.signAndSendTransaction({
to: tx.to as Address,
value: BigInt(tx.value),
data: callData as Hex,
chainId: SEPOLIA_CHAIN_ID,
});
};
rl.question("Provide collection slug: ", (input) => {
run(input);
});