-
Notifications
You must be signed in to change notification settings - Fork 7
/
invoke-payout.ts
49 lines (41 loc) · 2.37 KB
/
invoke-payout.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
import { SignatureTemplate } from "cashscript";
import { hexToBin, vmNumberToBigInt } from "@bitauth/libauth";
import { Wallet, TestNetWallet } from "mainnet-js";
import { tokenId, collectionSize, payoutAddress, network } from "./mintingParams.ts";
import { generateContract } from "./generateContract.js";
import 'dotenv/config';
// Get seedphrase + addressDerivationPath for invoke-payout from .env file
const seedphrasePayout = process.env.SEEDPHRASE_PAYOUT as string;
const addressDerivationPath = process.env.DERIVATIONPATH_PAYOUT;
// Instantiate wallet
const walletClass = network == "mainnet" ? Wallet : TestNetWallet;
const wallet = await walletClass.fromSeed(seedphrasePayout, addressDerivationPath);
if(!wallet.privateKeyWif) throw new Error("Error in wallet.privateKey")
const signatureTemplate = new SignatureTemplate(wallet.privateKeyWif);
// Check if the right wallet is configured to invoke payouts
if (wallet.address != payoutAddress) throw new Error("Provided wallet does not match Payout wallet (addresses don't match)")
const contract = generateContract();
console.log('Total balance contracts:', await contract.getBalance());
const contractUtxos = await contract.getUtxos();
for (const contractUtxo of contractUtxos) {
// Filter UTXOs on smart contract address
const isMintingUtxo = contractUtxo?.token?.category == tokenId && contractUtxo?.token?.nft?.capability == "minting";
if (!isMintingUtxo) continue
const payoutAmount = contractUtxo.satoshis - 2000n;
if (payoutAmount < 1000) continue
const tokenDetails = contractUtxo.token;
try {
const transaction = contract.functions
.payout(signatureTemplate, signatureTemplate.getPublicKey())
.from(contractUtxo)
.withoutChange()
// Check commitment to see minting contract is ongoing
const contractCommitment = vmNumberToBigInt(hexToBin(contractUtxo?.token?.nft?.commitment as string))
if(typeof contractCommitment == "string") throw new Error("Error in vmNumberToBigInt")
// If mint is ongoing, need to recreate minting contract at payout
if (contractCommitment <= BigInt(collectionSize)) transaction.to(contract.tokenAddress, 1000n, tokenDetails);
transaction.to(payoutAddress, payoutAmount);
const { txid } = await transaction.send();
console.log(`Payout transaction of ${payoutAmount} satoshis succesfully sent! \ntxid: ${txid}`);
} catch (error) { console.log(error) }
}