forked from blockparty-sh/waifu_faucet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slpfaucet.ts
171 lines (145 loc) · 8.94 KB
/
slpfaucet.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import BigNumber from "bignumber.js";
import { BITBOX } from "bitbox-sdk";
import { GrpcClient } from "grpc-bchrpc-node";
import * as slpjs from "slpjs";
import { BchdNetwork, BchdValidator, Utils } from "slpjs";
import { FaucetUtils } from "./faucet_utils";
const bitbox = new BITBOX();
const client = new GrpcClient({url: "bchd.fountainhead.cash" });
const validator = new BchdValidator(client, console);
const faucetUtils = new FaucetUtils(bitbox, validator)
export class SlpFaucetHandler {
public addresses: string[];
public wifs: { [key: string]: string };
public network: BchdNetwork;
public currentFaucetAddressIndex = 4;
private unconfirmedChainLength = new Map<string, number>();
private blockHeight = 0;
constructor(mnemonic: string) {
const masterNode = bitbox.HDNode.fromSeed(bitbox.Mnemonic.toSeed(mnemonic!)).derivePath("m/44'/245'/0'");
this.addresses = [];
this.wifs = {};
for (let i = 0; i < 18; i++) {
const childNode = masterNode.derivePath("0/" + i);
const address = Utils.toSlpAddress(bitbox.ECPair.toCashAddress(bitbox.ECPair.fromWIF(bitbox.HDNode.toWIF(childNode))));
this.wifs[address] = bitbox.HDNode.toWIF(childNode);
this.addresses.push(address);
this.unconfirmedChainLength.set(address, 0);
}
this.network = new BchdNetwork({ BITBOX: bitbox, validator, logger: console, client: validator.client });
}
get currentAddress() {
return this.addresses[this.currentFaucetAddressIndex];
}
public async evenlyDistributeTokens(tokenId: string): Promise<string> {
// TODO: use a threshold to determine if split should be made automatically
if (this.addresses.length > 19) {
throw Error("Cannot split token to more than 19 addresses");
}
const utxos: slpjs.SlpAddressUtxoResult[] = [];
const balances = ((await this.network.getAllSlpBalancesAndUtxos(this.addresses)) as R[]);
// add input token UTXOs
const tokenBalances = balances.filter((i) => {
try {
return i.result.slpTokenBalances[tokenId].isGreaterThan(0);
} catch (_) {
return false;
}
});
tokenBalances.map<void>((i) =>
i.result.slpTokenUtxos[tokenId].forEach((j) => j.wif = this.wifs[ i.address as any]));
tokenBalances.forEach((a) => {
try {
a.result.slpTokenUtxos[tokenId].forEach((txo) => utxos.push(txo));
} catch (_) { }
});
if (tokenBalances.length === 0) {
throw Error("Token balance in faucet wallet is 0.");
}
// add input BCH (non-token) UTXOs
const bchBalances = balances.filter((i) => i.result.nonSlpUtxos.length > 0);
bchBalances.map((i) => i.result.nonSlpUtxos.forEach((j) => j.wif = this.wifs[ i.address as any]));
bchBalances.forEach((a) => a.result.nonSlpUtxos.forEach((txo) => utxos.push(txo)));
if (bchBalances.length === 0) {
throw Error("BCH balance in faucet wallet is 0.");
}
const totalToken: BigNumber = tokenBalances.reduce((t, v) => t = t.plus(v.result.slpTokenBalances[tokenId]), new BigNumber(0));
console.log("total token amount to distribute:", totalToken.toFixed());
console.log("spread amount", totalToken.dividedToIntegerBy(this.addresses.length).toFixed());
await this.increaseChainLength();
return await this.network.simpleTokenSend(tokenId, Array(this.addresses.length).fill(totalToken.dividedToIntegerBy(this.addresses.length)), utxos, this.addresses, this.addresses[0]);
}
public async evenlyDistributeBch(): Promise<string> {
// TODO: use a threshold to determine if split should be made automatically
// spread the bch across all of the addresses
const utxos: slpjs.SlpAddressUtxoResult[] = [];
const balances = ((await this.network.getAllSlpBalancesAndUtxos(this.addresses)) as R[]);
// add input BCH (non-token) UTXOs
const bchBalances = balances.filter((i) => i.result.nonSlpUtxos.length > 0);
bchBalances.map((i) => i.result.nonSlpUtxos.forEach((j) => j.wif = this.wifs[ i.address as any]));
bchBalances.forEach((a) => a.result.nonSlpUtxos.forEach((txo) => utxos.push(txo)));
if (bchBalances.length === 0) {
throw Error("BCH balance in faucet wallet is 0.");
}
const totalBch = bchBalances.reduce((t, v) => t = t.plus(v.result.satoshis_available_bch), new BigNumber(0));
const sendCost = this.network.slp.calculateSendCost(0, utxos.length, this.addresses.length, this.addresses[0], 1, false); // rough overestimate
console.log("estimated send cost:", sendCost);
console.log("total BCH to distribute:", totalBch.toFixed());
console.log("spread amount:", totalBch.minus(sendCost).dividedToIntegerBy(this.addresses.length).toFixed());
await this.increaseChainLength();
return await this.network.simpleBchSend(Array(this.addresses.length).fill(totalBch.minus(sendCost).dividedToIntegerBy(this.addresses.length)), utxos, this.addresses, this.addresses[0]);
}
public async selectFaucetAddressForTokens(tokenId: string): Promise<{ address: string, balance: slpjs.SlpBalancesResult }> {
this.currentFaucetAddressIndex = Math.floor(Math.random() * 18);
const addresses = this.addresses.filter((_, i) => i >= this.currentFaucetAddressIndex).map((a) => Utils.toCashAddress(a));
for (let i = 0; i < addresses.length; i++) {
if (this.unconfirmedChainLength.get(this.addresses[i])! > 49) {
continue;
}
const bals = (await this.network.getAllSlpBalancesAndUtxos(addresses[i]) as slpjs.SlpBalancesResult);
if (bals.nonSlpUtxos.length === 0 || !bals.slpTokenUtxos[tokenId]) {
continue;
}
console.log("-----------------------------------");
console.log("Address Index: ", this.currentFaucetAddressIndex);
console.log(`Unconfirmed chain length: ${this.unconfirmedChainLength.get(this.currentAddress)}`);
// console.log("slp address:", Utils.toSlpAddress(a[i].cashAddress));
console.log("cash address:", Utils.toCashAddress(addresses[i]));
// console.log("unconfirmed balanceSat (includes tokens):", a[i].unconfirmedBalanceSat);
// console.log("confirmed balanceSat (includes tokens):", a[i].balanceSat);
console.log("Processing this address' UTXOs with SLP validator...");
const sendCost = this.network.slp.calculateSendCost(60, bals.nonSlpUtxos.length + bals.slpTokenUtxos[tokenId].length, 3, addresses[0]) - 546;
console.log("Token input quantity: ", bals.slpTokenBalances[tokenId].toFixed());
console.log("BCH (satoshis_available_bch):", bals.satoshis_available_bch);
console.log("Estimated send cost (satoshis):", sendCost);
if (bals.slpTokenBalances[tokenId].isGreaterThan(0) === true && bals.satoshis_available_bch > sendCost) {
console.log("Using address index:", this.currentFaucetAddressIndex);
console.log("-----------------------------------");
// this.currentFaucetAddressIndex++;
return { address: Utils.toSlpAddress(addresses[i]), balance: bals };
}
console.log("Address index", this.currentFaucetAddressIndex, "has insufficient BCH to fuel token transaction, trying the next index.");
console.log("-----------------------------------");
this.currentFaucetAddressIndex = (this.currentFaucetAddressIndex + 1) % 17
}
throw Error("There are no addresses with sufficient balance");
}
public async tokenSend(tokenId: string, sendAmount: BigNumber, inputUtxos: slpjs.SlpAddressUtxoResult[], tokenReceiverAddresses: string | string[], changeReceiverAddress: string): Promise<string> {
await this.increaseChainLength();
if (process.env.NFT!.toLowerCase() === 'yes') {
const receiverAddress = (typeof tokenReceiverAddresses === 'string') ? tokenReceiverAddresses : tokenReceiverAddresses[0];
return await faucetUtils.nftTokenSend(tokenId, inputUtxos, receiverAddress, changeReceiverAddress, this.wifs[changeReceiverAddress]);
}
return await this.network.simpleTokenSend(tokenId, sendAmount, inputUtxos, tokenReceiverAddresses, changeReceiverAddress);
}
private async increaseChainLength() {
const height = (await client.getBlockchainInfo()).getBestHeight();
if (height !== this.blockHeight) {
this.blockHeight = height;
this.unconfirmedChainLength.forEach((_, addr) => this.unconfirmedChainLength.set(addr, 0));
}
const len = this.unconfirmedChainLength.get(this.currentAddress)!;
this.unconfirmedChainLength.set(this.currentAddress, len + 1);
}
}
interface R { address: string; result: slpjs.SlpBalancesResult; }