-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
116 lines (107 loc) · 3.55 KB
/
lib.js
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
const {
makeContractCall,
makeSTXTokenTransfer,
broadcastTransaction,
privateKeyToString,
} = require("@stacks/transactions");
const { StacksTestnet } = require("@stacks/network");
// Load private key and network configuration
const privateKey = "your-private-key-here";
const network = new StacksTestnet();
const contractAddress = "your-contract-address-here";
const contractName = "your-contract-name-here";
const sbtcTokenAddress = "your-sbtc-token-contract-address-here";
/**
* Creates an HTLC contract call.
*
* @param {number} id - The HTLC ID.
* @param {string} recipient - The recipient of the HTLC.
* @param {string} hash - The hash of the HTLC.
* @param {number} amount - The amount of tokens locked in the HTLC.
* @param {number} timeout - The timeout in blocks.
* @param {string|null} [tokenContractAddress=null] - The contract address of the token, if any.
*
* @returns {Promise<string>} - A Promise that resolves to the transaction ID of the contract call.
*/
async function createHTLC(id, recipient, hash, amount, timeout, tokenContractAddress) {
const txOptions = {
contractAddress: contractAddress,
contractName: contractName,
functionName: "create-htlc",
functionArgs: [
{ type: "uint", value: id },
{ type: "principal", value: recipient },
{ type: "buff", value: hash },
{ type: "uint", value: amount },
{ type: "uint", value: timeout },
tokenContractAddress
? { type: "some", value: { type: "principal", value: tokenContractAddress } }
: { type: "none" },
],
senderKey: privateKeyToString(privateKey),
network: network,
};
const transaction = await makeContractCall(txOptions);
const result = await broadcastTransaction(transaction, network);
return result;
}
/**
* Funds the HTLC channel with STX.
*
* @param {number} amount - The amount of STX to fund the channel with.
*
* @returns {Promise<string>} - A Promise that resolves to the transaction ID of the STX transfer.
*/
async function fundChannel(amount) {
const txOptions = makeSTXTokenTransfer({
recipient: contractAddress,
amount: amount,
senderKey: privateKeyToString(privateKey),
network: network,
memo: "Fund HTLC Channel",
});
const transaction = await makeContractCall(txOptions);
const result = await broadcastTransaction(transaction, network);
return result;
}
/**
* Redeems an HTLC.
*
* @param {number} id - The HTLC ID.
* @param {string} preimage - The preimage to use for redemption.
*
* @returns {Promise<string>} - A Promise that resolves to the transaction ID of the contract call.
*/
async function redeemHTLC(id, preimage) {
const txOptions = {
contractAddress: contractAddress,
contractName: contractName,
functionName: "redeem-htlc",
functionArgs: [id, preimage],
senderKey: privateKeyToString(privateKey),
network: network,
};
const transaction = await makeContractCall(txOptions);
const result = await broadcastTransaction(transaction, network);
return result;
}
/**
* Refunds an expired HTLC.
*
* @param {number} id - The HTLC ID.
*
* @returns {Promise<string>} - A Promise that resolves to the transaction ID of the contract call.
*/
async function refundHTLC(id) {
const txOptions = {
contractAddress: contractAddress,
contractName: contractName,
functionName: "refund-htlc",
functionArgs: [id],
senderKey: privateKeyToString(privateKey),
network: network,
};
const transaction = await makeContractCall(txOptions);
const result = await broadcastTransaction(transaction, network);
return result;
}