Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stacks url check api call & stacking utils #15

Merged
merged 5 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 63 additions & 12 deletions api/stacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ import {
import { API_TIMEOUT_MILLI } from '../constant';
import { StacksMainnet, StacksTestnet } from '@stacks/network';
import {
BufferCV,
bufferCVFromString,
callReadOnlyFunction,
ClarityType,
cvToString,
PrincipalCV,
ResponseCV,
TupleCV,
} from '@stacks/transactions';
deDuplicatePendingTx,
mapTransferTransactionData,
parseMempoolStxTransactionsData,
parseStxTransactionData,
} from './helper';
import { BufferCV, bufferCVFromString, callReadOnlyFunction, ClarityType, cvToHex, cvToString, estimateTransfer, hexToCV, makeUnsignedSTXTokenTransfer, PrincipalCV, ResponseCV, SomeCV, standardPrincipalCV, TupleCV, tupleCV, UIntCV, } from '@stacks/transactions';
import { AddressToBnsResponse, CoreInfo, DelegationInfo } from '../types/api/stacks/assets';

import{
SettingsNetwork,
Expand All @@ -36,9 +34,6 @@ import{
Transaction,
TransferTransactionsData,
} from 'types';
import { AddressToBnsResponse } from '../types/api/stacks/assets';
import { deDuplicatePendingTx, mapTransferTransactionData, parseMempoolStxTransactionsData, parseStxTransactionData } from './helper';


export async function fetchStxAddressData(
stxAddress: string,
Expand Down Expand Up @@ -369,6 +364,62 @@ export async function getTransferTransactions(
});
}

export async function getStacksInfo(network:string){
const url = `${network}/v2/info`;
return axios
.get<CoreInfo>(url, {
timeout: 30000,
})
.then((response) => {
return response?.data;
})
.catch((error) => {
return undefined;
});

}

export async function fetchDelegationState(
stxAddress: string,
network: SettingsNetwork
): Promise<DelegationInfo> {
const poxContractAddress = 'SP000000000000000000002Q6VF78';
const poxContractName = 'pox';
const mapName = 'delegation-state';
const mapEntryPath = `/${poxContractAddress}/${poxContractName}/${mapName}`;
const apiUrl = `${network.address}/v2/map_entry${mapEntryPath}?proof=0`;
const key = cvToHex(tupleCV({ stacker: standardPrincipalCV(stxAddress) }));
const headers = {
'Content-Type': 'application/json',
};
return axios.post(apiUrl, JSON.stringify(key), { headers: headers }).then((response) => {
const responseCV = hexToCV(response.data.data);
if (responseCV.type === ClarityType.OptionalNone) {
return {
delegated: false,
};
} else {
const someCV = responseCV as SomeCV;
const tupleCV = someCV.value as TupleCV;
const amount: UIntCV = tupleCV.data['amount-ustx'] as UIntCV;
const delegatedTo: PrincipalCV = tupleCV.data['delegated-to'] as PrincipalCV;
const untilBurnHeightSomeCV: SomeCV = tupleCV.data['until-burn-ht'] as SomeCV;
var untilBurnHeight = 9999999;
if (untilBurnHeightSomeCV.type === ClarityType.OptionalSome) {
const untilBurnHeightUIntCV: UIntCV = untilBurnHeightSomeCV.value as UIntCV;
untilBurnHeight = Number(untilBurnHeightUIntCV.value);
}
const delegatedAmount = new BigNumber(amount.value.toString());

const delegationInfo = {
delegated: true,
amount: delegatedAmount.toString(),
delegatedTo: cvToString(delegatedTo),
untilBurnHeight: untilBurnHeight,
};

return delegationInfo;
}
});
}

24 changes: 24 additions & 0 deletions api/xverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
SupportedCurrency,
CoinsResponse,
FeesMultipliers,
StackingPoolInfo,
StackerInfo,
} from 'types';

export async function fetchBtcFeeRate(): Promise<BtcFeeResponse> {
Expand Down Expand Up @@ -82,3 +84,25 @@ export async function fetchAppInfo(): Promise<FeesMultipliers | null> {
return null;
});
}

export async function fetchStackingPoolInfo(): Promise<StackingPoolInfo> {
return fetch(`${XVERSE_API_BASE_URL}/v1/pool/info`, {
method: 'GET',
})
.then((response) => response.json())
.then((response) => {
return response;
});
}

export async function fetchPoolStackerInfo(
stxAddress: string,
): Promise<StackerInfo> {
return fetch(`${XVERSE_API_BASE_URL}/v1/pool/${stxAddress}/status`, {
method: 'GET',
})
.then((response) => response.json())
.then((response) => {
return response;
});
}
16 changes: 6 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions stacking/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type StackingState =
| 'Loading'
| 'NotStacking'
| 'Pending'
| 'Delegated'
| 'Stacking'
| 'Completed'
| 'Revoked'
| 'Error';

4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"types/*": ["./types"],
"currency/*": ["./currency"],
"transactions/*": ["./transactions"],
"wallet/*": ["./wallet"]
"wallet/*": ["./wallet"],
"stacking/*": ["./stacking"],

},
"declaration": true,
"sourceMap": true,
Expand Down
24 changes: 24 additions & 0 deletions types/api/stacks/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@ export interface NftEventsResponse {
export interface AddressToBnsResponse {
names: string[];
}
export interface PoxData {
Imamah-Zafar marked this conversation as resolved.
Show resolved Hide resolved
contract_id: string;
first_burnchain_block_height: number;
min_amount_ustx: string;
prepare_cycle_length: number;
rejection_fraction: number;
reward_cycle_id: number;
rejection_votes_left_required: string;
total_liquid_supply_ustx: string;
next_reward_cycle_in: number;
}
export interface DelegationInfo {
delegated: boolean;
amount?: string;
delegatedTo?: string;
untilBurnHeight?: number;
}

export interface CoreInfo {
burn_block_height: number;
stable_burn_block_height: number;
stacks_tip_height: number;
stacks_tip: string;
}

export function getBnsNftName(nft: NonFungibleToken) {
const hex = nft.value.hex;
Expand Down
78 changes: 78 additions & 0 deletions types/api/xverse/stacking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import BigNumber from "bignumber.js";
import { CoreInfo } from "../stacks/assets";

export interface Pool {
address: string;
contract: string;
contract_version: number;
reward_address: string;
starting_cycle: number;
cycle_duration: number;
available_cycle_durations: number[];
minimum: string;
fee_percent: number;
estimated_yield: number;
}

export interface StackingPoolInfo {
open: boolean;
enrollment_closing_blocks: number;
enrollment_open_blocks: number;
pools: Array<Pool>;
pool_total: string;
pox: {
contract_id: string;
first_burnchain_block_height: number;
min_amount_ustx: string;
prepare_cycle_length: number;
rejection_fraction: number;
reward_cycle_id: number;
reward_cycle_length: number;
rejection_votes_left_required: string;
total_liquid_supply_ustx: string;
next_reward_cycle_in: number;
};
}

export interface StackerInfo {
stacked: boolean;
pool_total?: string;
amount: string;
reward_share_pct?: string;
first_reward_cycle: number;
lock_period: number;
pool_pox_address: string;
user_pox_address: string;
poolContractName?: string;
}

export interface DelegationInfo {
delegated: boolean;
amount?: string;
delegatedTo?: string;
untilBurnHeight?: number;
}

export interface CurrentPoolReward {
currentCyclePoolRewardInBtc: string;
currentCyclePoolRewardInCurrency: string;
}

export interface StackingData {
poolInfo: StackingPoolInfo;
delegationInfo: DelegationInfo;
coreInfo: CoreInfo;
stackerInfo?: StackerInfo;
currentPoolReward?: CurrentPoolReward;
}

export interface StackingStateData {
delegated: boolean;
txid: string;
amount: BigNumber;
startingCycle: number;
duration: number;
poolContractAddress: string;
poolContractName: string;
revoked: boolean;
}
8 changes: 8 additions & 0 deletions types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ import {
NftsListData,
NonFungibleToken,
NftEventsResponse,
CoreInfo,
AddressToBnsResponse,
getBnsNftName
} from './api/stacks/assets';
import { NftDetailResponse } from './api/gamma/currency';
import { SupportedCurrency } from './currency';
import { StackerInfo, StackingData, StackingPoolInfo, StackingStateData, Pool } from './api/xverse/stacking';

export { getBnsNftName, cvToHex, uintCV };
export {
Expand Down Expand Up @@ -105,6 +107,12 @@ export {
NftsListData,
NonFungibleToken,
NftEventsResponse,
StackingPoolInfo,
StackerInfo,
StackingData,
CoreInfo,
StackingStateData,
Pool,
AddressToBnsResponse,
PostConditionsOptions,
UnsignedStacksTransation
Expand Down