-
Notifications
You must be signed in to change notification settings - Fork 6
/
acceptOffer.js
89 lines (77 loc) · 2.6 KB
/
acceptOffer.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const xrpl = require("xrpl");
require("dotenv").config();
/**
* Run this file locally with 2 arguments just like the example below to generate signed tx blob
* - The first argument is seed for the user wallet
* - The second argument should be NFT offer ID returned from `api/claim` endpoint
* `node .\acceptOffer.js sEdVMJSLjuTAjaSeeZ6TEkpUWuTS83j 80517CC6087108B777710DFD0C48B6CB66A43947A96CA4C4B145574E27E9749A`
*/
let nftOffer;
let myWallet;
if (process && typeof process.exit == "function") {
nftOffer = process.argv.length > 3 ? process.argv[3] : "";
myWallet =
process.argv.length > 2 ? xrpl.Wallet.fromSeed(process.argv[2]) : "";
}
async function getBatchNFTokens(address, taxon) {
try {
if (!address) throw new Error(`Address can't be emty`);
const client = new xrpl.Client(process.env.SELECTED_NETWORK);
await client.connect();
let nfts = await client.request({
method: "account_nfts",
account: address,
});
let accountNfts = nfts.result.account_nfts;
//console.log("Found ", accountNfts.length, " NFTs in account ", address);
for (;;) {
if (nfts["result"]["marker"] === undefined) {
break;
} else {
nfts = await client.request({
method: "account_nfts",
account: address,
marker: nfts["result"]["marker"],
});
accountNfts = accountNfts.concat(nfts.result.account_nfts);
}
}
client.disconnect();
if (taxon) return accountNfts.filter((a) => a.NFTokenTaxon == taxon);
return accountNfts;
} catch (error) {
console.error(error);
return error;
}
}
async function acceptNFTOffer() {
try {
const client = new xrpl.Client(process.env.SELECTED_NETWORK);
await client.connect();
console.log(myWallet.classicAddress);
console.log(
`Amount of NFTs that were found in account before claim: `,
(await getBatchNFTokens(myWallet.classicAddress)).length
);
const transactionBlob = {
TransactionType: "NFTokenAcceptOffer",
Account: myWallet.classicAddress,
NFTokenSellOffer: nftOffer,
};
const tx = await client.submitAndWait(transactionBlob, {
wallet: myWallet,
});
await client.disconnect();
if (tx.result.meta.TransactionResult == "tecOBJECT_NOT_FOUND")
throw new Error("NFT wasn't minted successfuly");
console.log(
`Amount of NFTs that were found in account after claim: `,
(await getBatchNFTokens(myWallet.classicAddress)).length
);
console.log(tx, `\n`, `NFT was minted successfully`);
} catch (error) {
console.error(error);
return error;
}
}
acceptNFTOffer();