const BigNumber = require('bignumber.js');
const { Server, Networks, TransactionBuilder, Operation, Asset, Account, Keypair } = require('stellar-sdk');
const server = new Server('https://horizon-testnet.stellar.org');
// NFT Params
const userAccount = "GCBGKEX5E5AGNYKPGT6ZQHTLDKGDIBUWPOEKB3MCVVZOJSE5PV6RAQE3"
const NFTName = "PILOT";
const quantity = 5;
const ipfsHash = "ipfs://"
const STELLAR_NETWORK = "TESTNET"
//
const nftQuantity = parseInt(quantity) / 10000000
const newAccountKeypair = Keypair.random()
const issuerAccount = newAccountKeypair.publicKey()
const NFT = new Asset(`${NFTName}`, issuerAccount);
const account = await server.loadAccount(userAccount);
const operations = [
Operation.createAccount({
destination: issuerAccount,
startingBalance: "1.5",
source: userAccount
}),
Operation.setOptions({
setFlags: 15,
inflationDest: userAccount,
source: issuerAccount,
}),
Operation.manageData({
name: 'ipfshash',
value: ipfsHash,
source: issuerAccount,
}),
Operation.changeTrust({
asset: NFT,
limit: `${quantity}`,
source: userAccount,
}),
Operation.setTrustLineFlags({
trustor: userAccount,
asset: NFT,
flags: {
authorized: true,
},
source: issuerAccount,
}),
Operation.payment({
destination: userAccount,
asset: NFT,
amount: `${nftQuantity}`,
source: issuerAccount,
}),
Operation.setTrustLineFlags({
trustor: userAccount,
asset: NFT,
flags: {
authorized: false,
},
source: issuerAccount,
})
];
const fee = new BigNumber(1).div('0.0000001').div(operations.length).toFixed(0, 3)
const networkPassphrase = Networks[STELLAR_NETWORK];
let transaction = new TransactionBuilder(account, { fee, networkPassphrase }).setTimeout(0);
operations.forEach((operation) => transaction.addOperation(operation));
transaction = transaction.build();
transaction.sign(newAccountKeypair);
const tx = transaction.toXDR();
console.log('TX XDR', tx);