Skip to content

Commit

Permalink
Merge pull request #216 from fireblocks/staking-sdk
Browse files Browse the repository at this point in the history
Staking sdk
  • Loading branch information
aviba authored Nov 1, 2023
2 parents b776ab3 + 59dfa20 commit 6d494d9
Show file tree
Hide file tree
Showing 5 changed files with 484 additions and 2 deletions.
61 changes: 59 additions & 2 deletions src/fireblocks-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ import { AxiosProxyConfig, AxiosResponse } from "axios";
import { PIIEncryption } from "./pii-client";
import { NcwApiClient } from "./ncw-api-client";
import { NcwSdk } from "./ncw-sdk";
import { StakingApiClient } from "./staking/staking-api-client";
import {
ChainInfo, CheckTermsOfServiceResponseDto,
DelegationSummaryDto,
DelegationSummaryDtoByVault,
ExecuteActionResponse, StakeRequestDto, StakingAction,
StakingChain, StakingPosition, StakingValidator, UnstakeRequestDto, WithdrawRequestDto
} from "./staking";

export * from "./types";

Expand Down Expand Up @@ -152,6 +160,7 @@ export class FireblocksSDK {
private readonly apiBaseUrl: string;
private readonly apiClient: ApiClient;
private readonly apiNcw: NcwApiClient;
private readonly stakingApiClient: StakingApiClient;

private piiClient: PIIEncryption;

Expand All @@ -177,6 +186,8 @@ export class FireblocksSDK {
}

this.apiNcw = new NcwApiClient(this.apiClient);

this.stakingApiClient = new StakingApiClient(this.apiClient);
}

/**
Expand All @@ -188,14 +199,60 @@ export class FireblocksSDK {
public get NCW(): NcwSdk {
return this.apiNcw;
}

/**
* Get the instance of ApiClient used by the FireblocksSDK
*/
public getApiClient(): ApiClient {
return this.apiClient;
}

/**
* Get all staking chains
*/
public async getStakingChains(): Promise<string[]> {
return await this.stakingApiClient.getChains();
}
/**
* Get chain info
*/
public async getStakingChainInfo(chainDescriptor: StakingChain): Promise<ChainInfo> {
return await this.stakingApiClient.getChainInfo(chainDescriptor);
}
/**
* Get staking positions summary
*/
public async getStakingPositionsSummary(byVault?: boolean): Promise<DelegationSummaryDto | DelegationSummaryDtoByVault> {
return await this.stakingApiClient.getPositionsSummary(byVault);
}
/**
* Execute staking action on a chain
*/
public async executeStakingAction(actionId: StakingAction, chainDescriptor: StakingChain, body: StakeRequestDto | UnstakeRequestDto | WithdrawRequestDto): Promise<ExecuteActionResponse> {
return await this.stakingApiClient.executeAction(actionId, chainDescriptor, body);
}
/**
* Get all staking positions, optionally filtered by chain
*/
public async getStakingPositions(chainDescriptor?: StakingChain): Promise<StakingPosition[]> {
return await this.stakingApiClient.getPositions(chainDescriptor);
}
/**
* Get a staking position by id
*/
public async getStakingPosition(positionId: string): Promise<StakingPosition[]> {
return await this.stakingApiClient.getPosition(positionId);
}
/**
* Get all staking validators, filtered by chain
*/
public async getStakingValidatorsByChain(chainDescriptor: StakingChain): Promise<StakingValidator[]> {
return await this.stakingApiClient.getValidatorsByChain(chainDescriptor);
}
/**
* Approve staking provider terms of service
*/
public async approveStakingProviderTermsOfService(validatorProviderId: string): Promise<CheckTermsOfServiceResponseDto> {
return await this.stakingApiClient.approveProviderTermsOfService(validatorProviderId);
}
/**
* Gets all assets that are currently supported by Fireblocks
*/
Expand Down
3 changes: 3 additions & 0 deletions src/staking/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./staking-api-client";
export * from "./staking-sdk";
export * from "./types";
54 changes: 54 additions & 0 deletions src/staking/staking-api-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
ChainInfo,
CheckTermsOfServiceResponseDto,
DelegationSummaryDto,
DelegationSummaryDtoByVault,
ExecuteActionResponse, StakeRequestDto,
StakingAction,
StakingChain,
StakingPosition,
StakingValidator, UnstakeRequestDto, WithdrawRequestDto,
} from "./types";
import { StakingSDK } from "./staking-sdk";
import { ApiClient } from "../api-client";

const STAKING_BASE_PATH = "/v1/staking";

export class StakingApiClient implements StakingSDK {
constructor(private readonly apiClient: ApiClient) {}
public async getChains(): Promise<string[]> {
return await this.apiClient.issueGetRequest(`${STAKING_BASE_PATH}/chains`);
}
public async getChainInfo(chainDescriptor: StakingChain): Promise<ChainInfo> {
return await this.apiClient.issueGetRequest(`${STAKING_BASE_PATH}/chains/${chainDescriptor}/chainInfo`);
}
public async getPositionsSummary(byVault?: boolean): Promise<DelegationSummaryDto | DelegationSummaryDtoByVault> {
return await this.apiClient.issueGetRequest(`${STAKING_BASE_PATH}/positions/summary${byVault ? "?byVault=true" : "?byVault=false"}`);
}
public async executeAction(
actionId: StakingAction,
chainDescriptor: StakingChain,
body: StakeRequestDto | UnstakeRequestDto | WithdrawRequestDto,
): Promise<ExecuteActionResponse> {
return await this.apiClient.issuePostRequest(
`${STAKING_BASE_PATH}/chains/${chainDescriptor}/${actionId}`,
body,
);
}
public async getPositions(chainDescriptor?: StakingChain): Promise<StakingPosition[]> {
const url = `${STAKING_BASE_PATH}/positions${chainDescriptor ? `?chainDescriptor=${chainDescriptor}` : ""}`;
return await this.apiClient.issueGetRequest(url);
}
public async getPosition(positionId: string): Promise<StakingPosition[]> {
return await this.apiClient.issueGetRequest(`${STAKING_BASE_PATH}/positions/${positionId}`);
}
public async getValidatorsByChain(chainDescriptor: StakingChain): Promise<StakingValidator[]> {
return await this.apiClient.issueGetRequest(`${STAKING_BASE_PATH}/validators/${chainDescriptor}`);
}
public async approveProviderTermsOfService(validatorProviderId: string): Promise<CheckTermsOfServiceResponseDto> {
return await this.apiClient.issuePostRequest(`${STAKING_BASE_PATH}/providers/approveTermsOfService`, {
validatorProviderId,
});
}
}

49 changes: 49 additions & 0 deletions src/staking/staking-sdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
ChainInfo, CheckTermsOfServiceResponseDto,
DelegationSummaryDto,
DelegationSummaryDtoByVault,
ExecuteActionResponse,
StakingAction,
StakingChain, StakingPosition, StakingValidator
} from "./types";

export interface StakingSDK {
/**
* Get all staking chains
*/
getChains(): Promise<string[]>;

/**
* Get chain info
*/
getChainInfo(chainDescriptor: StakingChain): Promise<ChainInfo>;

/**
* Get staking positions summary
*/
getPositionsSummary(): Promise<DelegationSummaryDto | DelegationSummaryDtoByVault>;

/**
* Execute staking action on a chain
*/
executeAction(actionId: StakingAction, chainDescriptor: StakingChain, body: any): Promise<ExecuteActionResponse>;

/**
* Get all staking positions, optionally filtered by chain
*/
getPositions(chainDescriptor?: StakingChain): Promise<StakingPosition[]>;
/**
* Get a staking position by id
*/
getPosition(positionId?: string): Promise<StakingPosition[]>;

/**
* Get all staking validators, filtered by chain
*/
getValidatorsByChain(chainDescriptor: StakingChain): Promise<StakingValidator[]>;

/**
* Approve staking provider terms of service
*/
approveProviderTermsOfService(validatorProviderId: string): Promise<CheckTermsOfServiceResponseDto>;
}
Loading

0 comments on commit 6d494d9

Please sign in to comment.