-
Notifications
You must be signed in to change notification settings - Fork 1
/
buy.js
executable file
·65 lines (60 loc) · 2.18 KB
/
buy.js
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
#!/usr/bin/env node
const { ACCOUNT } = require('./constants')
const { getQuantityFromPriceListing, getHumanFriendlyAmount } = require('./utils')
const { getListings } = require('./nft/get-listings');
const { buyNft } = require('./nft/marketplace-buy')
const pressAnyKey = require('./node_modules/press-any-key')
var argv = require('yargs/yargs')(process.argv)
.command('Buy NFTs by sale_id')
.example('$0 --sale_id 1234')
.describe('sale_id', 'sale_id')
.alias('s', 'sale_id')
.demand('sale_id')
.boolean(['debug'])
.argv
const buy = async (sale_id) => {
// Get listing in order to know the price
const listings = await getListings({
sale_ids: sale_id,
limit: 2
})
if (listings.length > 1) {
console.log(listings)
console.log(`Error: multiple listings fetched (${listings.length}) with the sale_id ${sale_id}`)
return
}
const listing = listings[0]
if (argv.debug) console.log(listing) // debug
const aId = listing.assets[0].asset_id
const aName = listing.assets[0].name
const tId = listing.assets[0].template.template_id
const mint = listing.assets[0].template_mint
const seller = listing.seller
const tokenContract = listing.price.token_contract
const symbol = listing.price.token_symbol
const quantity = getQuantityFromPriceListing(listing.price)
const humanPrice = getHumanFriendlyAmount(listing.price.amount, symbol)
console.log(`Buy ${aName} nr ${mint} (t ${tId}) for ${humanPrice.toLocaleString()} ${symbol} (sale ${sale_id} by ${seller}, asset ${aId})`)
pressAnyKey('Press ENTER to continue, or Ctrl + C to stop.', {
ctrlC: 'reject'
})
.then(async () => {
try {
await buyNft({
token_contract: tokenContract,
quantity: quantity,
sale_id: sale_id
})
} catch (e) {
if (e.message.includes("overdrawn balance")) {
console.log("Error: you don't have enough balance to buy this NFT.")
} else {
console.log(e)
}
}
})
.catch(() => {
console.log('You pressed Ctrl + C')
})
}
buy(argv.sale_id)