@stacks/stacking
Version:
Library for Stacking.
764 lines • 35.4 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StackingClient = void 0;
const common_1 = require("@stacks/common");
const network_1 = require("@stacks/network");
const transactions_1 = require("@stacks/transactions");
const constants_1 = require("./constants");
const utils_1 = require("./utils");
__exportStar(require("./utils"), exports);
class StackingClient {
constructor(opts) {
this.address = opts.address;
this.network = (0, network_1.networkFrom)(opts.network ?? 'mainnet');
this.client = Object.assign({}, (0, network_1.clientFromNetwork)(this.network), opts.client);
}
get baseUrl() {
return this.client.baseUrl;
}
get fetch() {
return this.client.fetch;
}
getCoreInfo() {
return this.client.fetch(`${this.client.baseUrl}/v2/info`).then(res => res.json());
}
getPoxInfo() {
return this.client.fetch(`${this.client.baseUrl}/v2/pox`).then(res => res.json());
}
async getTargetBlockTime() {
const res = await this.client
.fetch(`${this.client.baseUrl}/extended/v1/info/network_block_times`)
.then((res) => res.json());
if (this.network.chainId === network_1.ChainId.Mainnet)
return res.mainnet.target_block_time;
return res.testnet.target_block_time;
}
async getAccountStatus() {
return this.client
.fetch(`${this.client.baseUrl}/v2/accounts/${this.address}?proof=0`)
.then(res => res.json())
.then(json => {
json.balance = BigInt(json.balance);
json.locked = BigInt(json.locked);
return json;
});
}
async getAccountBalance() {
return this.getAccountStatus().then(a => a.balance);
}
async getAccountExtendedBalances() {
return this.client
.fetch(`${this.client.baseUrl}/extended/v1/address/${this.address}/balances`)
.then(res => res.json())
.then(json => {
json.stx.balance = BigInt(json.stx.balance);
json.stx.total_sent = BigInt(json.stx.total_sent);
json.stx.total_received = BigInt(json.stx.total_received);
json.stx.locked = BigInt(json.stx.locked);
return json;
});
}
async getAccountBalanceLocked() {
return this.getAccountStatus().then(a => a.locked);
}
async getCycleDuration() {
const poxInfoPromise = this.getPoxInfo();
const targetBlockTimePromise = await this.getTargetBlockTime();
return Promise.all([poxInfoPromise, targetBlockTimePromise]).then(([poxInfo, targetBlockTime]) => {
return poxInfo.reward_cycle_length * targetBlockTime;
});
}
async getRewardsTotalForBtcAddress() {
return this.client
.fetch(`${this.client.baseUrl}/extended/v1/burnchain/rewards/${this.address}/total`)
.then(res => res.json())
.then(json => {
json.reward_amount = BigInt(json.reward_amount);
return json;
});
}
async getRewardsForBtcAddress(options) {
let url = `${this.client.baseUrl}/extended/v1/burnchain/rewards/${this.address}`;
if (options)
url += `?limit=${options.limit}&offset=${options.offset}`;
return this.client.fetch(url).then(res => res.json());
}
async getRewardHoldersForBtcAddress(options) {
let url = `${this.client.baseUrl}/extended/v1/burnchain/reward_slot_holders/${this.address}`;
if (options)
url += `?limit=${options.limit}&offset=${options.offset}`;
return this.client.fetch(url).then(res => res.json());
}
async getRewardSet(options) {
const [contractAddress, contractName] = this.parseContractId(options?.contractId);
const result = await (0, transactions_1.fetchCallReadOnlyFunction)({
client: this.client,
senderAddress: this.address,
contractAddress,
contractName,
functionArgs: [(0, transactions_1.uintCV)(options.rewardCyleId), (0, transactions_1.uintCV)(options.rewardSetIndex)],
functionName: 'get-reward-set-pox-address',
});
return (0, utils_1.unwrapMap)(result, tuple => ({
pox_address: {
version: (0, common_1.hexToBytes)(tuple.value['pox-addr'].value['version'].value),
hashbytes: (0, common_1.hexToBytes)(tuple.value['pox-addr'].value['hashbytes'].value),
},
total_ustx: BigInt(tuple.value['total-ustx'].value),
}));
}
async getSecondsUntilNextCycle() {
const poxInfoPromise = this.getPoxInfo();
const targetBlockTimePromise = this.getTargetBlockTime();
const coreInfoPromise = this.getCoreInfo();
return Promise.all([poxInfoPromise, targetBlockTimePromise, coreInfoPromise]).then(([poxInfo, targetBlockTime, coreInfo]) => {
const blocksToNextCycle = poxInfo.reward_cycle_length -
((coreInfo.burn_block_height - poxInfo.first_burnchain_block_height) %
poxInfo.reward_cycle_length);
return blocksToNextCycle * targetBlockTime;
});
}
async getSecondsUntilStackingDeadline() {
const poxInfoPromise = this.getPoxInfo();
const targetBlockTimePromise = this.getTargetBlockTime();
return Promise.all([poxInfoPromise, targetBlockTimePromise]).then(([poxInfo, targetBlockTime]) => poxInfo.next_cycle.blocks_until_prepare_phase * targetBlockTime);
}
async getPoxOperationInfo(poxInfo) {
poxInfo = poxInfo ?? (await this.getPoxInfo());
const poxContractVersions = [...poxInfo.contract_versions].sort((a, b) => a.activation_burnchain_block_height - b.activation_burnchain_block_height);
const [pox1, pox2, pox3, pox4] = poxContractVersions;
const activatedPoxs = poxContractVersions.filter((c) => poxInfo?.current_burnchain_block_height >= c.activation_burnchain_block_height);
const current = activatedPoxs[activatedPoxs.length - 1];
return { period: constants_1.PoxOperationPeriod.Period3, pox1, pox2, pox3, pox4, current };
}
async hasMinimumStx() {
const balance = await this.getAccountBalance();
const min = BigInt((await this.getPoxInfo()).min_amount_ustx);
return balance >= min;
}
async canStack({ poxAddress, cycles }) {
const balancePromise = this.getAccountBalance();
const poxInfoPromise = this.getPoxInfo();
return Promise.all([balancePromise, poxInfoPromise])
.then(([balance, poxInfo]) => {
const address = (0, utils_1.poxAddressToTuple)(poxAddress);
const [contractAddress, contractName] = this.parseContractId(poxInfo.contract_id);
return (0, transactions_1.fetchCallReadOnlyFunction)({
client: this.client,
contractName,
contractAddress,
functionName: 'can-stack-stx',
senderAddress: this.address,
functionArgs: [
address,
(0, transactions_1.uintCV)(balance.toString()),
(0, transactions_1.uintCV)(poxInfo.reward_cycle_id),
(0, transactions_1.uintCV)(cycles.toString()),
],
});
})
.then((responseCV) => {
if (responseCV.type === transactions_1.ClarityType.ResponseOk) {
return {
eligible: true,
};
}
else {
const errorCV = responseCV;
return {
eligible: false,
reason: constants_1.StackingErrors[+(0, transactions_1.cvToString)(errorCV.value)],
};
}
});
}
async stack({ amountMicroStx, poxAddress, cycles, burnBlockHeight, signerKey, signerSignature, maxAmount, authId, ...txOptions }) {
const poxInfo = await this.getPoxInfo();
const poxOperationInfo = await this.getPoxOperationInfo(poxInfo);
const contract = await this.getStackingContract(poxOperationInfo);
(0, utils_1.ensureLegacyBtcAddressForPox1)({ contract, poxAddress });
(0, utils_1.ensureSignerArgsReadiness)({ contract, signerKey, signerSignature, maxAmount, authId });
const callOptions = this.getStackOptions({
contract,
amountMicroStx,
cycles,
poxAddress,
burnBlockHeight,
signerKey,
signerSignature,
maxAmount,
authId,
});
const tx = await (0, transactions_1.makeContractCall)({
...callOptions,
...renamePrivateKey(txOptions),
});
return (0, transactions_1.broadcastTransaction)({ transaction: tx, client: this.client });
}
async stackExtend({ extendCycles, poxAddress, signerKey, signerSignature, maxAmount, authId, ...txOptions }) {
const poxInfo = await this.getPoxInfo();
const poxOperationInfo = await this.getPoxOperationInfo(poxInfo);
(0, utils_1.ensurePox2Activated)(poxOperationInfo);
(0, utils_1.ensureSignerArgsReadiness)({
contract: poxInfo.contract_id,
signerKey,
signerSignature,
maxAmount,
authId,
});
const callOptions = this.getStackExtendOptions({
contract: poxInfo.contract_id,
extendCycles,
poxAddress,
signerKey,
signerSignature,
maxAmount,
authId,
});
const tx = await (0, transactions_1.makeContractCall)({
...callOptions,
...renamePrivateKey(txOptions),
});
return (0, transactions_1.broadcastTransaction)({ transaction: tx, client: this.client });
}
async stackIncrease({ increaseBy, signerKey, signerSignature, maxAmount, authId, ...txOptions }) {
const poxInfo = await this.getPoxInfo();
const poxOperationInfo = await this.getPoxOperationInfo(poxInfo);
(0, utils_1.ensurePox2Activated)(poxOperationInfo);
(0, utils_1.ensureSignerArgsReadiness)({
contract: poxInfo.contract_id,
signerKey,
signerSignature,
maxAmount,
authId,
});
const callOptions = this.getStackIncreaseOptions({
contract: poxInfo.contract_id,
increaseBy,
signerKey,
signerSignature,
maxAmount,
authId,
});
const tx = await (0, transactions_1.makeContractCall)({
...callOptions,
...renamePrivateKey(txOptions),
});
return (0, transactions_1.broadcastTransaction)({ transaction: tx, client: this.client });
}
async delegateStx({ amountMicroStx, delegateTo, untilBurnBlockHeight, poxAddress, ...txOptions }) {
const poxInfo = await this.getPoxInfo();
const poxOperationInfo = await this.getPoxOperationInfo(poxInfo);
const contract = await this.getStackingContract(poxOperationInfo);
(0, utils_1.ensureLegacyBtcAddressForPox1)({ contract, poxAddress });
const callOptions = this.getDelegateOptions({
contract,
amountMicroStx,
delegateTo,
untilBurnBlockHeight,
poxAddress,
});
const tx = await (0, transactions_1.makeContractCall)({
...callOptions,
...renamePrivateKey(txOptions),
});
return (0, transactions_1.broadcastTransaction)({ transaction: tx, client: this.client });
}
async delegateStackStx({ stacker, amountMicroStx, poxAddress, burnBlockHeight, cycles, ...txOptions }) {
const poxInfo = await this.getPoxInfo();
const poxOperationInfo = await this.getPoxOperationInfo(poxInfo);
const contract = await this.getStackingContract(poxOperationInfo);
(0, utils_1.ensureLegacyBtcAddressForPox1)({ contract, poxAddress });
const callOptions = this.getDelegateStackOptions({
contract,
stacker,
amountMicroStx,
poxAddress,
burnBlockHeight,
cycles,
});
const tx = await (0, transactions_1.makeContractCall)({
...callOptions,
...renamePrivateKey(txOptions),
});
return (0, transactions_1.broadcastTransaction)({ transaction: tx, client: this.client });
}
async delegateStackExtend({ stacker, poxAddress, extendCount, ...txOptions }) {
const poxInfo = await this.getPoxInfo();
const contract = poxInfo.contract_id;
const callOptions = this.getDelegateStackExtendOptions({
contract,
stacker,
poxAddress,
extendCount,
});
const tx = await (0, transactions_1.makeContractCall)({
...callOptions,
...renamePrivateKey(txOptions),
});
return (0, transactions_1.broadcastTransaction)({ transaction: tx, client: this.client });
}
async delegateStackIncrease({ stacker, poxAddress, increaseBy, ...txOptions }) {
const poxInfo = await this.getPoxInfo();
const poxOperationInfo = await this.getPoxOperationInfo(poxInfo);
(0, utils_1.ensurePox2Activated)(poxOperationInfo);
const callOptions = this.getDelegateStackIncreaseOptions({
contract: poxInfo.contract_id,
stacker,
poxAddress,
increaseBy,
});
const tx = await (0, transactions_1.makeContractCall)({
...callOptions,
...renamePrivateKey(txOptions),
});
return (0, transactions_1.broadcastTransaction)({ transaction: tx, client: this.client });
}
async stackAggregationCommit({ poxAddress, rewardCycle, signerKey, signerSignature, maxAmount, authId, ...txOptions }) {
const contract = await this.getStackingContract();
(0, utils_1.ensureLegacyBtcAddressForPox1)({ contract, poxAddress });
(0, utils_1.ensureSignerArgsReadiness)({ contract, signerKey, signerSignature, maxAmount, authId });
const callOptions = this.getStackAggregationCommitOptions({
contract,
poxAddress,
rewardCycle,
signerKey,
signerSignature,
maxAmount,
authId,
});
const tx = await (0, transactions_1.makeContractCall)({
...callOptions,
...renamePrivateKey(txOptions),
});
return (0, transactions_1.broadcastTransaction)({ transaction: tx, client: this.client });
}
async stackAggregationCommitIndexed({ poxAddress, rewardCycle, signerKey, signerSignature, maxAmount, authId, ...txOptions }) {
const contract = await this.getStackingContract();
(0, utils_1.ensureLegacyBtcAddressForPox1)({ contract, poxAddress });
(0, utils_1.ensureSignerArgsReadiness)({ contract, signerKey, signerSignature, maxAmount, authId });
const callOptions = this.getStackAggregationCommitOptionsIndexed({
contract,
poxAddress,
rewardCycle,
signerKey,
signerSignature,
maxAmount,
authId,
});
const tx = await (0, transactions_1.makeContractCall)({
...callOptions,
...renamePrivateKey(txOptions),
});
return (0, transactions_1.broadcastTransaction)({ transaction: tx, client: this.client });
}
async stackAggregationIncrease({ poxAddress, rewardCycle, rewardIndex, signerKey, signerSignature, maxAmount, authId, ...txOptions }) {
const contract = await this.getStackingContract();
(0, utils_1.ensureLegacyBtcAddressForPox1)({ contract, poxAddress });
(0, utils_1.ensureSignerArgsReadiness)({ contract, signerKey, signerSignature, maxAmount, authId });
const callOptions = this.getStackAggregationIncreaseOptions({
contract,
poxAddress,
rewardCycle,
rewardCycleIndex: rewardIndex,
signerKey,
signerSignature,
maxAmount,
authId,
});
const tx = await (0, transactions_1.makeContractCall)({
...callOptions,
...renamePrivateKey(txOptions),
});
return (0, transactions_1.broadcastTransaction)({ transaction: tx, client: this.client });
}
async revokeDelegateStx(arg) {
if (typeof arg === 'string')
arg = { privateKey: arg };
const poxInfo = await this.getPoxInfo();
const contract = poxInfo.contract_id;
const callOptions = this.getRevokeDelegateStxOptions(contract);
const tx = await (0, transactions_1.makeContractCall)({
...callOptions,
...renamePrivateKey(arg),
});
return (0, transactions_1.broadcastTransaction)({ transaction: tx, client: this.client });
}
getStackOptions({ amountMicroStx, poxAddress, cycles, contract, burnBlockHeight, signerKey, signerSignature, maxAmount, authId, }) {
const address = (0, utils_1.poxAddressToTuple)(poxAddress);
const [contractAddress, contractName] = this.parseContractId(contract);
const functionArgs = [
(0, transactions_1.uintCV)(amountMicroStx),
address,
(0, transactions_1.uintCV)(burnBlockHeight),
(0, transactions_1.uintCV)(cycles),
];
if (signerKey && maxAmount && typeof authId !== 'undefined') {
functionArgs.push(signerSignature ? (0, transactions_1.someCV)((0, transactions_1.bufferCV)((0, common_1.hexToBytes)(signerSignature))) : (0, transactions_1.noneCV)());
functionArgs.push((0, transactions_1.bufferCV)((0, common_1.hexToBytes)(signerKey)));
functionArgs.push((0, transactions_1.uintCV)(maxAmount));
functionArgs.push((0, transactions_1.uintCV)(authId));
}
const callOptions = {
client: this.client,
contractAddress,
contractName,
functionName: 'stack-stx',
functionArgs,
validateWithAbi: true,
network: this.network,
};
return callOptions;
}
getStackExtendOptions({ extendCycles, poxAddress, contract, signerKey, signerSignature, maxAmount, authId, }) {
const address = (0, utils_1.poxAddressToTuple)(poxAddress);
const [contractAddress, contractName] = this.parseContractId(contract);
const functionArgs = [(0, transactions_1.uintCV)(extendCycles), address];
if (signerKey && maxAmount && typeof authId !== 'undefined') {
functionArgs.push(signerSignature ? (0, transactions_1.someCV)((0, transactions_1.bufferCV)((0, common_1.hexToBytes)(signerSignature))) : (0, transactions_1.noneCV)());
functionArgs.push((0, transactions_1.bufferCV)((0, common_1.hexToBytes)(signerKey)));
functionArgs.push((0, transactions_1.uintCV)(maxAmount));
functionArgs.push((0, transactions_1.uintCV)(authId));
}
const callOptions = {
client: this.client,
contractAddress,
contractName,
functionName: 'stack-extend',
functionArgs,
validateWithAbi: true,
network: this.network,
};
return callOptions;
}
getStackIncreaseOptions({ increaseBy, contract, signerKey, signerSignature, maxAmount, authId, }) {
const [contractAddress, contractName] = this.parseContractId(contract);
const functionArgs = [(0, transactions_1.uintCV)(increaseBy)];
if (signerKey && maxAmount && typeof authId !== 'undefined') {
functionArgs.push(signerSignature ? (0, transactions_1.someCV)((0, transactions_1.bufferCV)((0, common_1.hexToBytes)(signerSignature))) : (0, transactions_1.noneCV)());
functionArgs.push((0, transactions_1.bufferCV)((0, common_1.hexToBytes)(signerKey)));
functionArgs.push((0, transactions_1.uintCV)(maxAmount));
functionArgs.push((0, transactions_1.uintCV)(authId));
}
const callOptions = {
client: this.client,
contractAddress,
contractName,
functionName: 'stack-increase',
functionArgs,
validateWithAbi: true,
network: this.network,
};
return callOptions;
}
getDelegateOptions({ contract, amountMicroStx, delegateTo, untilBurnBlockHeight, poxAddress, }) {
const address = poxAddress ? (0, transactions_1.someCV)((0, utils_1.poxAddressToTuple)(poxAddress)) : (0, transactions_1.noneCV)();
const [contractAddress, contractName] = this.parseContractId(contract);
const callOptions = {
client: this.client,
contractAddress,
contractName,
functionName: 'delegate-stx',
functionArgs: [
(0, transactions_1.uintCV)(amountMicroStx),
(0, transactions_1.principalCV)(delegateTo),
untilBurnBlockHeight ? (0, transactions_1.someCV)((0, transactions_1.uintCV)(untilBurnBlockHeight)) : (0, transactions_1.noneCV)(),
address,
],
validateWithAbi: true,
network: this.network,
};
return callOptions;
}
getDelegateStackOptions({ contract, stacker, amountMicroStx, poxAddress, burnBlockHeight, cycles, }) {
const address = (0, utils_1.poxAddressToTuple)(poxAddress);
const [contractAddress, contractName] = this.parseContractId(contract);
const callOptions = {
client: this.client,
contractAddress,
contractName,
functionName: 'delegate-stack-stx',
functionArgs: [
(0, transactions_1.principalCV)(stacker),
(0, transactions_1.uintCV)(amountMicroStx),
address,
(0, transactions_1.uintCV)(burnBlockHeight),
(0, transactions_1.uintCV)(cycles),
],
validateWithAbi: true,
network: this.network,
};
return callOptions;
}
getDelegateStackExtendOptions({ contract, stacker, poxAddress, extendCount, }) {
const address = (0, utils_1.poxAddressToTuple)(poxAddress);
const [contractAddress, contractName] = this.parseContractId(contract);
const callOptions = {
client: this.client,
contractAddress,
contractName,
functionName: 'delegate-stack-extend',
functionArgs: [(0, transactions_1.principalCV)(stacker), address, (0, transactions_1.uintCV)(extendCount)],
validateWithAbi: true,
network: this.network,
};
return callOptions;
}
getDelegateStackIncreaseOptions({ contract, stacker, poxAddress, increaseBy, }) {
const address = (0, utils_1.poxAddressToTuple)(poxAddress);
const [contractAddress, contractName] = this.parseContractId(contract);
const callOptions = {
client: this.client,
contractAddress,
contractName,
functionName: 'delegate-stack-increase',
functionArgs: [(0, transactions_1.principalCV)(stacker), address, (0, transactions_1.uintCV)(increaseBy)],
validateWithAbi: true,
network: this.network,
};
return callOptions;
}
getStackAggregationCommitOptions({ contract, poxAddress, rewardCycle, signerKey, signerSignature, maxAmount, authId, }) {
const address = (0, utils_1.poxAddressToTuple)(poxAddress);
const [contractAddress, contractName] = this.parseContractId(contract);
const functionArgs = [address, (0, transactions_1.uintCV)(rewardCycle)];
if (signerKey && maxAmount && typeof authId !== 'undefined') {
functionArgs.push(signerSignature ? (0, transactions_1.someCV)((0, transactions_1.bufferCV)((0, common_1.hexToBytes)(signerSignature))) : (0, transactions_1.noneCV)());
functionArgs.push((0, transactions_1.bufferCV)((0, common_1.hexToBytes)(signerKey)));
functionArgs.push((0, transactions_1.uintCV)(maxAmount));
functionArgs.push((0, transactions_1.uintCV)(authId));
}
const callOptions = {
client: this.client,
contractAddress,
contractName,
functionName: 'stack-aggregation-commit',
functionArgs,
validateWithAbi: true,
network: this.network,
};
return callOptions;
}
getStackAggregationIncreaseOptions({ contract, poxAddress, rewardCycle, rewardCycleIndex, signerKey, signerSignature, maxAmount, authId, }) {
const address = (0, utils_1.poxAddressToTuple)(poxAddress);
const [contractAddress, contractName] = this.parseContractId(contract);
const functionArgs = [address, (0, transactions_1.uintCV)(rewardCycle), (0, transactions_1.uintCV)(rewardCycleIndex)];
if (signerKey && maxAmount && typeof authId !== 'undefined') {
functionArgs.push(signerSignature ? (0, transactions_1.someCV)((0, transactions_1.bufferCV)((0, common_1.hexToBytes)(signerSignature))) : (0, transactions_1.noneCV)());
functionArgs.push((0, transactions_1.bufferCV)((0, common_1.hexToBytes)(signerKey)));
functionArgs.push((0, transactions_1.uintCV)(maxAmount));
functionArgs.push((0, transactions_1.uintCV)(authId));
}
const callOptions = {
client: this.client,
contractAddress,
contractName,
functionName: 'stack-aggregation-increase',
functionArgs,
validateWithAbi: true,
network: this.network,
};
return callOptions;
}
getStackAggregationCommitOptionsIndexed({ contract, poxAddress, rewardCycle, signerKey, signerSignature, maxAmount, authId, }) {
const address = (0, utils_1.poxAddressToTuple)(poxAddress);
const [contractAddress, contractName] = this.parseContractId(contract);
const functionArgs = [address, (0, transactions_1.uintCV)(rewardCycle)];
if (signerKey && maxAmount && typeof authId !== 'undefined') {
functionArgs.push(signerSignature ? (0, transactions_1.someCV)((0, transactions_1.bufferCV)((0, common_1.hexToBytes)(signerSignature))) : (0, transactions_1.noneCV)());
functionArgs.push((0, transactions_1.bufferCV)((0, common_1.hexToBytes)(signerKey)));
functionArgs.push((0, transactions_1.uintCV)(maxAmount));
functionArgs.push((0, transactions_1.uintCV)(authId));
}
const callOptions = {
client: this.client,
contractAddress,
contractName,
functionName: 'stack-aggregation-commit-indexed',
functionArgs,
validateWithAbi: true,
network: this.network,
};
return callOptions;
}
getRevokeDelegateStxOptions(contract) {
const [contractAddress, contractName] = this.parseContractId(contract);
const callOptions = {
client: this.client,
contractAddress,
contractName,
functionName: 'revoke-delegate-stx',
functionArgs: [],
validateWithAbi: true,
network: this.network,
};
return callOptions;
}
async getStatus() {
const poxInfo = await this.getPoxInfo();
const [contractAddress, contractName] = this.parseContractId(poxInfo.contract_id);
const account = await this.getAccountStatus();
const functionName = 'get-stacker-info';
return (0, transactions_1.fetchCallReadOnlyFunction)({
contractAddress,
contractName,
functionName,
senderAddress: this.address,
functionArgs: [(0, transactions_1.principalCV)(this.address)],
client: this.client,
}).then((responseCV) => {
if (responseCV.type === transactions_1.ClarityType.OptionalSome) {
const someCV = responseCV;
const tupleCV = someCV.value;
const poxAddress = tupleCV.value['pox-addr'];
const firstRewardCycle = tupleCV.value['first-reward-cycle'];
const lockPeriod = tupleCV.value['lock-period'];
const version = poxAddress.value['version'];
const hashbytes = poxAddress.value['hashbytes'];
return {
stacked: true,
details: {
first_reward_cycle: Number(firstRewardCycle.value),
lock_period: Number(lockPeriod.value),
unlock_height: account.unlock_height,
pox_address: {
version: (0, common_1.hexToBytes)(version.value),
hashbytes: (0, common_1.hexToBytes)(hashbytes.value),
},
},
};
}
else if (responseCV.type === transactions_1.ClarityType.OptionalNone) {
return {
stacked: false,
};
}
else {
throw new Error(`Error fetching stacker info`);
}
});
}
async getDelegationStatus() {
const poxInfo = await this.getPoxInfo();
const [contractAddress, contractName] = this.parseContractId(poxInfo.contract_id);
const functionName = 'get-delegation-info';
return (0, transactions_1.fetchCallReadOnlyFunction)({
contractAddress,
contractName,
functionName,
functionArgs: [(0, transactions_1.principalCV)(this.address)],
senderAddress: this.address,
client: this.client,
}).then((responseCV) => {
if (responseCV.type === transactions_1.ClarityType.OptionalSome) {
const tupleCV = responseCV.value;
const amountMicroStx = tupleCV.value['amount-ustx'];
const delegatedTo = tupleCV.value['delegated-to'];
const poxAddress = (0, utils_1.unwrapMap)(tupleCV.value['pox-addr'], tuple => ({
version: (0, common_1.hexToBytes)(tuple.value['version'].value)[0],
hashbytes: (0, common_1.hexToBytes)(tuple.value['hashbytes'].value),
}));
const untilBurnBlockHeight = (0, utils_1.unwrap)(tupleCV.value['until-burn-ht']);
return {
delegated: true,
details: {
amount_micro_stx: BigInt(amountMicroStx.value),
delegated_to: delegatedTo.value,
pox_address: poxAddress,
until_burn_ht: untilBurnBlockHeight ? Number(untilBurnBlockHeight.value) : undefined,
},
};
}
else if (responseCV.type === transactions_1.ClarityType.OptionalNone) {
return {
delegated: false,
};
}
else {
throw new Error(`Error fetching delegation info`);
}
});
}
async verifySignerKeySignature({ topic, poxAddress, rewardCycle, period, signerSignature, signerKey, amount, maxAmount, authId, }) {
const poxInfo = await this.getPoxInfo();
const [contractAddress, contractName] = this.parseContractId(poxInfo.contract_id);
const functionName = 'verify-signer-key-sig';
const functionArgs = [
(0, utils_1.poxAddressToTuple)(poxAddress),
(0, transactions_1.uintCV)(rewardCycle),
(0, transactions_1.stringAsciiCV)(topic),
(0, transactions_1.uintCV)(period),
signerSignature ? (0, transactions_1.someCV)((0, transactions_1.bufferCV)((0, common_1.hexToBytes)(signerSignature))) : (0, transactions_1.noneCV)(),
(0, transactions_1.bufferCV)((0, common_1.hexToBytes)(signerKey)),
(0, transactions_1.uintCV)(amount),
(0, transactions_1.uintCV)(maxAmount),
(0, transactions_1.uintCV)(authId),
];
return (0, transactions_1.fetchCallReadOnlyFunction)({
contractAddress,
contractName,
functionName,
functionArgs,
senderAddress: this.address,
client: this.client,
}).then(responseCV => responseCV.type === transactions_1.ClarityType.ResponseOk);
}
async getStackingContract(poxOperationInfo) {
poxOperationInfo = poxOperationInfo ?? (await this.getPoxOperationInfo());
switch (poxOperationInfo.period) {
case constants_1.PoxOperationPeriod.Period1:
return poxOperationInfo.pox1.contract_id;
case constants_1.PoxOperationPeriod.Period2a:
case constants_1.PoxOperationPeriod.Period2b:
return poxOperationInfo.pox2.contract_id;
case constants_1.PoxOperationPeriod.Period3:
default:
return poxOperationInfo.current.contract_id;
}
}
modifyLockTxFee({ tx, amountMicroStx, }) {
const fee = (0, transactions_1.getFee)(tx.auth);
tx.payload.functionArgs[0] = (0, transactions_1.uintCV)((0, common_1.intToBigInt)(amountMicroStx) - fee);
return tx;
}
parseContractId(contract) {
const parts = contract.split('.');
if (parts.length === 2 && (0, transactions_1.validateStacksAddress)(parts[0]) && parts[1].startsWith('pox')) {
return parts;
}
throw new Error('Stacking contract ID is malformed');
}
signPoxSignature({ topic, poxAddress, rewardCycle, period, signerPrivateKey, authId, maxAmount, }) {
return (0, utils_1.signPox4SignatureHash)({
topic,
poxAddress,
rewardCycle,
period,
network: this.network,
privateKey: signerPrivateKey,
maxAmount,
authId,
});
}
}
exports.StackingClient = StackingClient;
function renamePrivateKey(txOptions) {
txOptions.senderKey = txOptions.privateKey;
delete txOptions.privateKey;
return txOptions;
}
//# sourceMappingURL=index.js.map