Skip to content

Commit

Permalink
Merge pull request #504 from casper-ecosystem/CSDK-214
Browse files Browse the repository at this point in the history
 Add new get auction info v2 method support
  • Loading branch information
alexmyshchyshyn authored Feb 5, 2025
2 parents 160d080 + acfb7f0 commit 9a8bae6
Show file tree
Hide file tree
Showing 19 changed files with 1,391 additions and 95 deletions.
73 changes: 73 additions & 0 deletions src/rpc/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
RpcResponse,
StateGetAccountInfo,
StateGetAuctionInfoResult,
StateGetAuctionInfoV1Result,
StateGetBalanceResult,
StateGetDictionaryResult,
StateGetEntityResult,
Expand All @@ -38,14 +39,86 @@ import {
import { Deploy, PublicKey, Transaction } from '../types';

export interface ClientPOS {
/**
* Retrieves the latest auction information, including Validator Bids and Era Validators.
* @returns A Promise resolving to the latest auction info.
* @rpc state_get_auction_info_v2 (fallback: state_get_auction_info)
*/
getLatestAuctionInfo(): Promise<StateGetAuctionInfoResult>;

/**
* Retrieves auction information by block hash, including Validator Bids and Era Validators.
* @param blockHash - The hash of the block to retrieve auction info for.
* @returns A Promise resolving to the auction info.
* @rpc state_get_auction_info_v2 (fallback: state_get_auction_info)
*/
getAuctionInfoByHash(blockHash: string): Promise<StateGetAuctionInfoResult>;

/**
* Retrieves auction information by block height, including Validator Bids and Era Validators.
* @param height - The height of the block to retrieve auction info for.
* @returns A Promise resolving to the auction info.
* @rpc state_get_auction_info_v2 (fallback: state_get_auction_info)
*/
getAuctionInfoByHeight(height: number): Promise<StateGetAuctionInfoResult>;

/**
* Retrieves the latest auction information (version 1), including Validator Bids and Era Validators.
* @returns A Promise resolving to the latest auction info (V1).
* @rpc state_get_auction_info
*/
getLatestAuctionInfoV1(): Promise<StateGetAuctionInfoV1Result>;

/**
* Retrieves auction information by block hash (version 1), including Validator Bids and Era Validators.
* @param blockHash - The hash of the block to retrieve auction info for.
* @returns A Promise resolving to the auction info (V1).
* @rpc state_get_auction_info
*/
getAuctionInfoV1ByHash(
blockHash: string
): Promise<StateGetAuctionInfoV1Result>;

/**
* Retrieves auction information by block height (version 1), including Validator Bids and Era Validators.
* @param height - The height of the block to retrieve auction info for.
* @returns A Promise resolving to the auction info (V1).
* @rpc state_get_auction_info
*/
getAuctionInfoV1ByHeight(
height: number
): Promise<StateGetAuctionInfoV1Result>;

/**
* Retrieves the latest EraInfo from the network.
* Only the last block in an era (switch block) contains an era summary.
* This method returns information about the latest block, which may not be the last block in the era.
* @returns A Promise resolving to the latest EraInfo.
*/
getEraInfoLatest(): Promise<ChainGetEraInfoResult>;

/**
* Retrieves EraInfo by block height.
* Only the last block in an era (switch block) contains an era summary.
* @param height - The height of the block to retrieve EraInfo for.
* @returns A Promise resolving to the EraInfo.
*/
getEraInfoByBlockHeight(height: number): Promise<ChainGetEraInfoResult>;

/**
* Retrieves EraInfo by block hash.
* Only the last block in an era (switch block) contains an era summary.
* @param hash - The hash of the block to retrieve EraInfo for.
* @returns A Promise resolving to the EraInfo.
*/
getEraInfoByBlockHash(hash: string): Promise<ChainGetEraInfoResult>;

/**
* Retrieves status changes of active validators.
* The changes occurred during the EraId contained within the response itself.
* A validator may show more than one change in a single era.
* @returns A Promise resolving to validator status changes.
*/
getValidatorChangesInfo(): Promise<InfoGetValidatorChangesResult>;
}

Expand Down
1 change: 1 addition & 0 deletions src/rpc/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export enum Method {
GetBlockTransfers = 'chain_get_block_transfers',
GetEraSummary = 'chain_get_era_summary',
GetAuctionInfo = 'state_get_auction_info',
GetAuctionInfoV2 = 'state_get_auction_info_v2',
GetValidatorChanges = 'info_get_validator_changes',
GetStateRootHash = 'chain_get_state_root_hash',
GetStatus = 'info_get_status',
Expand Down
24 changes: 24 additions & 0 deletions src/rpc/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import {
Hash,
PublicKey,
CLValueUInt512,
AuctionStateV1,
AuctionStateV2,
AddressableEntity
} from '../types';

Expand Down Expand Up @@ -63,6 +65,28 @@ export class StateGetAuctionInfoResult {
rawJSON?: any;
}

@jsonObject
export class StateGetAuctionInfoV1Result {
@jsonMember({ name: 'api_version', constructor: String })
version: string;

@jsonMember({ name: 'auction_state', constructor: AuctionStateV1 })
auctionState: AuctionStateV1;

rawJSON?: any;
}

@jsonObject
export class StateGetAuctionInfoV2Result {
@jsonMember({ name: 'api_version', constructor: String })
version: string;

@jsonMember({ name: 'auction_state', constructor: AuctionStateV2 })
auctionState: AuctionStateV2;

rawJSON?: any;
}

@jsonObject
export class StateGetBalanceResult {
@jsonMember({ name: 'api_version', constructor: String })
Expand Down
131 changes: 127 additions & 4 deletions src/rpc/rpc_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
RpcResponse,
StateGetAccountInfo,
StateGetAuctionInfoResult,
StateGetAuctionInfoV1Result,
StateGetAuctionInfoV2Result,
StateGetBalanceResult,
StateGetDictionaryResult,
StateGetEntityResult,
Expand Down Expand Up @@ -61,7 +63,8 @@ import {
Deploy,
PublicKey,
Hash,
Transaction
Transaction,
AuctionState
} from '../types';
import { HttpError } from './error';

Expand Down Expand Up @@ -773,9 +776,45 @@ export class RpcClient implements IClient {
}

async getLatestAuctionInfo(): Promise<StateGetAuctionInfoResult> {
try {
const auctionInfoV2 = await this.getLatestAuctionInfoV2();
const auctionInfoResult = new StateGetAuctionInfoResult();
auctionInfoResult.auctionState = AuctionState.fromV2(
auctionInfoV2.auctionState
);
auctionInfoResult.version = auctionInfoV2.version;
auctionInfoResult.rawJSON = auctionInfoV2.rawJSON;
return auctionInfoResult;
} catch (err) {
const errorMessage = err?.message || '';
if (!errorMessage.includes('Method not found')) {
throw err;
}

const auctionInfoV1 = await this.getLatestAuctionInfoV1();
const auctionInfoResult = new StateGetAuctionInfoResult();
auctionInfoResult.auctionState = AuctionState.fromV1(
auctionInfoV1.auctionState
);
auctionInfoResult.version = auctionInfoV1.version;
auctionInfoResult.rawJSON = auctionInfoV1.rawJSON;
return auctionInfoResult;
}
}

async getLatestAuctionInfoV1(): Promise<StateGetAuctionInfoV1Result> {
const resp = await this.processRequest(Method.GetAuctionInfo, null);

const result = this.parseResponse(StateGetAuctionInfoResult, resp.result);
const result = this.parseResponse(StateGetAuctionInfoV1Result, resp.result);
result.rawJSON = resp.result;

return result;
}

async getLatestAuctionInfoV2(): Promise<StateGetAuctionInfoV2Result> {
const resp = await this.processRequest(Method.GetAuctionInfoV2, null);

const result = this.parseResponse(StateGetAuctionInfoV2Result, resp.result);
result.rawJSON = resp.result;

return result;
Expand All @@ -784,6 +823,31 @@ export class RpcClient implements IClient {
async getAuctionInfoByHash(
blockHash: string
): Promise<StateGetAuctionInfoResult> {
try {
const resV2 = await this.getAuctionInfoV2ByHash(blockHash);
const result = new StateGetAuctionInfoResult();
result.auctionState = AuctionState.fromV2(resV2.auctionState);
result.version = resV2.version;
result.rawJSON = resV2.rawJSON;
return result;
} catch (err) {
const errorMessage = err?.message || '';
if (!errorMessage.includes('Method not found')) {
throw err;
}

const resV1 = await this.getAuctionInfoV1ByHash(blockHash);
const result = new StateGetAuctionInfoResult();
result.auctionState = AuctionState.fromV1(resV1.auctionState);
result.version = resV1.version;
result.rawJSON = resV1.rawJSON;
return result;
}
}

async getAuctionInfoV1ByHash(
blockHash: string
): Promise<StateGetAuctionInfoV1Result> {
const serializer = new TypedJSON(ParamBlockIdentifier);
const blockIdentifierParam = ParamBlockIdentifier.byHash(blockHash);

Expand All @@ -792,7 +856,24 @@ export class RpcClient implements IClient {
serializer.toPlainJson(blockIdentifierParam) as ParamBlockIdentifier
);

const result = this.parseResponse(StateGetAuctionInfoResult, resp.result);
const result = this.parseResponse(StateGetAuctionInfoV1Result, resp.result);
result.rawJSON = resp.result;

return result;
}

async getAuctionInfoV2ByHash(
blockHash: string
): Promise<StateGetAuctionInfoV2Result> {
const serializer = new TypedJSON(ParamBlockIdentifier);
const blockIdentifierParam = ParamBlockIdentifier.byHash(blockHash);

const resp = await this.processRequest(
Method.GetAuctionInfoV2,
serializer.toPlainJson(blockIdentifierParam) as ParamBlockIdentifier
);

const result = this.parseResponse(StateGetAuctionInfoV2Result, resp.result);
result.rawJSON = resp.result;

return result;
Expand All @@ -801,6 +882,31 @@ export class RpcClient implements IClient {
async getAuctionInfoByHeight(
height: number
): Promise<StateGetAuctionInfoResult> {
try {
const resV2 = await this.getAuctionInfoV2ByHeight(height);
const result = new StateGetAuctionInfoResult();
result.auctionState = AuctionState.fromV2(resV2.auctionState);
result.version = resV2.version;
result.rawJSON = resV2.rawJSON;
return result;
} catch (err) {
const errorMessage = err?.message || '';
if (!errorMessage.includes('Method not found')) {
throw err;
}

const resV1 = await this.getAuctionInfoV1ByHeight(height);
const result = new StateGetAuctionInfoResult();
result.auctionState = AuctionState.fromV1(resV1.auctionState);
result.version = resV1.version;
result.rawJSON = resV1.rawJSON;
return result;
}
}

async getAuctionInfoV1ByHeight(
height: number
): Promise<StateGetAuctionInfoV1Result> {
const serializer = new TypedJSON(ParamBlockIdentifier);
const blockIdentifierParam = ParamBlockIdentifier.byHeight(height);

Expand All @@ -809,7 +915,24 @@ export class RpcClient implements IClient {
serializer.toPlainJson(blockIdentifierParam) as ParamBlockIdentifier
);

const result = this.parseResponse(StateGetAuctionInfoResult, resp.result);
const result = this.parseResponse(StateGetAuctionInfoV1Result, resp.result);
result.rawJSON = resp.result;

return result;
}

async getAuctionInfoV2ByHeight(
height: number
): Promise<StateGetAuctionInfoV2Result> {
const serializer = new TypedJSON(ParamBlockIdentifier);
const blockIdentifierParam = ParamBlockIdentifier.byHeight(height);

const resp = await this.processRequest(
Method.GetAuctionInfoV2,
serializer.toPlainJson(blockIdentifierParam) as ParamBlockIdentifier
);

const result = this.parseResponse(StateGetAuctionInfoV2Result, resp.result);
result.rawJSON = resp.result;

return result;
Expand Down
14 changes: 14 additions & 0 deletions src/tests/data/bid/auction_bid_example_v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"bonding_purse": "uref-b3c03358245a0d9514064b6d0c3dd90023d29a0fe137507d430a26990f5ce8e3-007",
"staked_amount": "900000000000",
"delegation_rate": 10,
"delegators": [
{
"public_key": "01d829cbfb66b2b11ef8d8feb6d3f2155789fc22f407bb57f89b05f6ba4b9ae070",
"staked_amount": "555000000000",
"bonding_purse": "uref-41059a3aa5c5759c7f5e3c826cb47e0b21257ac57ee63a4c00dc305133e55d64-007",
"delegatee": "01197f6b23e16c8532c6abc838facd5ea789be0c76b2920334039bfa8b3d368d61"
}
],
"inactive": false
}
22 changes: 22 additions & 0 deletions src/tests/data/bid/auction_bid_example_v2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"validator_public_key": "01197f6b23e16c8532c6abc838facd5ea789be0c76b2920334039bfa8b3d368d61",
"bonding_purse": "uref-fafafafafafafafafafafafafafafafafafafafafafafafafafafafafafafafa-007",
"staked_amount": "900000000000",
"delegation_rate": 0,
"vesting_schedule": null,
"delegators": [
{
"delegator_public_key": "014508a07aa941707f3eb2db94c8897a80b2c1197476b6de213ac273df7d86c4ff",
"delegator": {
"delegator_kind": {
"PublicKey": "01d829cbfb66b2b11ef8d8feb6d3f2155789fc22f407bb57f89b05f6ba4b9ae070"
},
"staked_amount": "10",
"bonding_purse": "uref-fbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfb-007",
"validator_public_key": "01197f6b23e16c8532c6abc838facd5ea789be0c76b2920334039bfa8b3d368d61",
"vesting_schedule": null
}
}
],
"inactive": false
}
5 changes: 5 additions & 0 deletions src/tests/data/bid/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import auctionBidV1Json from './auction_bid_example_v1.json';
import auctionBidV2Json from './auction_bid_example_v2.json';
import storedBidJson from './stored_bid_example.json';

export { auctionBidV1Json, auctionBidV2Json, storedBidJson };
24 changes: 24 additions & 0 deletions src/tests/data/bid/stored_bid_example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"validator_public_key": "01018525deae6091abccab6704a0fa44e12c495eec9e8fe6929862e1b75580e715",
"bonding_purse": "uref-7f9a0dae980f0bb2cb37449f74095843e14ed1b79b3ce9f0a7b1611c30eb10d9-007",
"staked_amount": "1196098880899",
"delegation_rate": 10,
"vesting_schedule": null,
"delegators": {
"014832ab8c1822bca82f75581d0e476ef9833a8536977db8f11324a39544c11dc5": {
"delegator_public_key": "014832ab8c1822bca82f75581d0e476ef9833a8536977db8f11324a39544c11dc5",
"staked_amount": "594256548131",
"bonding_purse": "uref-b3749a7f7a8c187bb983f64601bf573580a4d0f013dacddc9044453daed0bcb4-007",
"validator_public_key": "01018525deae6091abccab6704a0fa44e12c495eec9e8fe6929862e1b75580e715",
"vesting_schedule": null
},
"01e6eb2475f3d978afae6efe6da3434141cf24d2d3e2523ca0d790ff8b08baee05": {
"delegator_public_key": "01e6eb2475f3d978afae6efe6da3434141cf24d2d3e2523ca0d790ff8b08baee05",
"staked_amount": "2421183792212",
"bonding_purse": "uref-befe1d9e57a748f115ae926af4267d70beefde6d217e1fbcb96a244e18f332fd-007",
"validator_public_key": "01018525deae6091abccab6704a0fa44e12c495eec9e8fe6929862e1b75580e715",
"vesting_schedule": null
}
},
"inactive": false
}
1 change: 1 addition & 0 deletions src/tests/data/era/era_summary_example.json

Large diffs are not rendered by default.

Loading

0 comments on commit 9a8bae6

Please sign in to comment.