-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreservedSupply.ts
More file actions
59 lines (52 loc) · 2.37 KB
/
reservedSupply.ts
File metadata and controls
59 lines (52 loc) · 2.37 KB
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
import { Wallet, TokenSendRequest, TestNetWallet, toBch, type Utxo } from "mainnet-js";
import { queryAuthHead } from "./queryChainGraph.js";
// Fill in this variables
const tokenId = "";
const network = "mainnet"; // mainnet or chipnet
const seedphase = "";
const derivationPathAddress = "m/44'/145'/0'/0/0"; // last number is the address index from electron cash
// start of the program code
const authHeadTxId = await queryAuthHead(tokenId);
// mainnet-js generates m/44'/0'/0'/0/0 by default so have to switch it
const walletClass = network == "mainnet" ? Wallet : TestNetWallet;
const wallet = await walletClass.fromSeed(seedphase, derivationPathAddress);
const walletAddress = wallet.getDepositAddress();
const balance = await wallet.getBalance();
const tokenBalance = await wallet.getTokenBalance(tokenId);
console.log(`wallet address: ${walletAddress}`);
console.log(`Bch amount in walletAddress is ${toBch(balance)}bch or ${balance}sats`);
if(balance < 1000n) throw new Error("Not enough BCH to make the transaction!");
console.log(`Balance of configured token is ${tokenBalance}`);
let authUtxo: Utxo | undefined;
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}`);
if(authUtxo) {
console.log(authUtxo)
console.log(`The tokenBalance on the authHead is ${authUtxo?.token?.amount}`)
addAllToReserves(authUtxo, tokenBalance);
} else {
throw new Error("wallet does not hold the authority to update the authChain")
}
// Function to add all tokens with configure tokenId on the single address wallet to the authHead
async function addAllToReserves(
authUtxo: Utxo, newReservedSupply: bigint
) {
try {
// Construct new reservedSupply output
const reservedSupplyOutput = new TokenSendRequest({
cashaddr: walletAddress,
value: 1000n,
category: tokenId,
amount: newReservedSupply
});
const outputs = [ reservedSupplyOutput ];
const { txId } = await wallet.send(outputs, { ensureUtxos: [authUtxo] });
const displayId = `${authHeadTxId.slice(0, 20)}...${authHeadTxId.slice(-10)}`;
console.log(`Published Auth update in tx ${displayId}, returned Auth to ${walletAddress} \n$https://explorer.bitcoinunlimited.info/tx/${txId}`);
} catch (error) {
console.log(error);
}
}