-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathissueSupply.ts
46 lines (39 loc) · 1.64 KB
/
issueSupply.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
import { Wallet, TokenSendRequest } from "mainnet-js";
import { queryAuthHead } from "./queryChainGraph.js";
const seedphase = "";
const derivationPathAddress = "m/44'/145'/0'/0/0"; // last number is the address index from electron cash
const tokenId = ""
const destinationAddress = ""
const issueAmount = 100_000_000
// start of the program code
const authHeadTxId = await queryAuthHead(tokenId);
const wallet = await Wallet.fromSeed(seedphase, derivationPathAddress);
const walletAddress = wallet.getDepositAddress();
const balance = await wallet.getBalance();
if(typeof balance == "number" || !balance?.sat) throw new Error("Error in getBalance")
console.log(`wallet address: ${walletAddress}`);
console.log(`Bch amount in walletAddress is ${balance.bch}bch or ${balance.sat}sats`);
if(balance.sat < 1000) throw new Error("Not enough BCH to make the transaction!");
let authUtxo;
const utxosWallet = await wallet.getUtxos();
utxosWallet.forEach(utxo => {
if(utxo.txid == authHeadTxId && utxo.vout == 0) authUtxo = utxo;
})
console.log(`The authHead is the first output of the transaction with id ${authHeadTxId}`);
const newReservedSupply = authUtxo?.token?.amount - issueAmount;
const reservedSupplyOutput = new TokenSendRequest({
cashaddr: walletAddress,
value: 1000,
tokenId: tokenId,
amount: newReservedSupply
});
const issuedSupplyOutput = new TokenSendRequest({
cashaddr: destinationAddress,
value: 1000,
tokenId: tokenId,
amount: issueAmount
});
const outputs = [ reservedSupplyOutput, issuedSupplyOutput ];
console.log(outputs)
const { txId } = await wallet.send(outputs, { ensureUtxos: [authUtxo] });
console.log(txId)