Skip to content

Commit

Permalink
Add support for TS-SDK + Explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
666lcz committed Mar 5, 2023
1 parent fb698f5 commit 417d448
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 152 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-goats-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mysten/sui.js": minor
---

Update schema for `SuiSystemState` and `DelegatedStake`
18 changes: 4 additions & 14 deletions Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ const NUMBER_OF_VALIDATORS = 10;
export function processValidators(set: Validator[]) {
return set.map((av) => ({
name: av.metadata.name,
address: av.metadata.sui_address,
stake: av.staking_pool.sui_balance,
logo: av.metadata.image_url,
address: av.metadata.suiAddress,
stake: av.stakingPool.suiBalance,
logo: av.metadata.imageUrl,
}));
}

Expand Down Expand Up @@ -94,7 +94,7 @@ export function TopValidatorsCard({ limit, showIcon }: TopValidatorsCardProps) {
() =>
data
? validatorsTable(
data.validators.active_validators,
data.validators.activeValidators,
limit,
showIcon
)
Expand Down
12 changes: 5 additions & 7 deletions apps/explorer/src/components/validator/ValidatorMeta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { ArrowUpRight12 } from '@mysten/icons';
import { toB64, type Validator } from '@mysten/sui.js';
import { type Validator } from '@mysten/sui.js';

import { StakeButton } from './StakeButton';

Expand All @@ -17,14 +17,12 @@ type ValidatorMetaProps = {
};

export function ValidatorMeta({ validatorData }: ValidatorMetaProps) {
const validatorPublicKey = toB64(
new Uint8Array(validatorData.metadata.protocol_pubkey_bytes)
);
const validatorPublicKey = validatorData.metadata.protocolPubkeyBytes;

const validatorName = validatorData.metadata.name;
const logo = validatorData.metadata.image_url;
const logo = validatorData.metadata.imageUrl;
const description = validatorData.metadata.description;
const projectUrl = validatorData.metadata.project_url;
const projectUrl = validatorData.metadata.projectUrl;

return (
<>
Expand Down Expand Up @@ -69,7 +67,7 @@ export function ValidatorMeta({ validatorData }: ValidatorMetaProps) {
</DescriptionItem>
<DescriptionItem title="Address">
<AddressLink
address={validatorData.metadata.sui_address}
address={validatorData.metadata.suiAddress}
noTruncate
/>
</DescriptionItem>
Expand Down
6 changes: 3 additions & 3 deletions apps/explorer/src/components/validator/ValidatorStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export function ValidatorStats({
() => calculateAPY(validatorData, +epoch),
[validatorData, epoch]
);
const totalStake = +validatorData.staking_pool.sui_balance;
const commission = +validatorData.commission_rate / 100;
const rewardsPoolBalance = +validatorData.staking_pool.rewards_pool;
const totalStake = +validatorData.stakingPool.suiBalance;
const commission = +validatorData.commissionRate / 100;
const rewardsPoolBalance = +validatorData.stakingPool.rewardsPool;

return (
<div className="flex flex-col items-stretch gap-5 md:flex-row">
Expand Down
8 changes: 4 additions & 4 deletions apps/explorer/src/components/validator/calculateAPY.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { roundFloat } from '~/utils/roundFloat';
const APY_DECIMALS = 4;

export function calculateAPY(validator: Validator, epoch: number) {
const { sui_balance, starting_epoch, pool_token_balance } =
validator.staking_pool;
const { suiBalance, startingEpoch, poolTokenBalance } =
validator.stakingPool;

const num_epochs_participated = +epoch - +starting_epoch;
const num_epochs_participated = +epoch - +startingEpoch;
const apy =
Math.pow(
1 + (+sui_balance - +pool_token_balance) / +pool_token_balance,
1 + (+suiBalance - +poolTokenBalance) / +poolTokenBalance,
365 / num_epochs_participated
) - 1;

Expand Down
7 changes: 3 additions & 4 deletions apps/explorer/src/pages/validator/ValidatorDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ function ValidatorDetails() {
const validatorData = useMemo(() => {
if (!data) return null;
return (
data.validators.active_validators.find(
(av) => av.metadata.sui_address === id
data.validators.activeValidators.find(
(av) => av.metadata.suiAddress === id
) || null
);
}, [id, data]);

const numberOfValidators =
data?.validators.active_validators.length ?? null;
const numberOfValidators = data?.validators.activeValidators.length ?? null;

const { data: validatorEvents, isLoading: validatorsEventsLoading } =
useGetValidatorsEvents({
Expand Down
22 changes: 11 additions & 11 deletions apps/explorer/src/pages/validators/Validators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@ function validatorsTableData(
return {
data: validators.map((validator) => {
const validatorName = validator.metadata.name;
const totalStake = validator.staking_pool.sui_balance;
const img = validator.metadata.image_url;
const totalStake = validator.stakingPool.suiBalance;
const img = validator.metadata.imageUrl;

const event = getValidatorMoveEvent(
validatorsEvents,
validator.metadata.sui_address
validator.metadata.suiAddress
);

return {
name: {
name: validatorName,
logo: validator.metadata.image_url,
logo: validator.metadata.imageUrl,
},
stake: totalStake,
apy: calculateAPY(validator, epoch),
commission: +validator.commission_rate / 100,
commission: +validator.commissionRate / 100,
img: img,
address: validator.metadata.sui_address,
address: validator.metadata.suiAddress,
lastReward: event?.fields.stake_rewards || 0,
};
}),
Expand Down Expand Up @@ -158,7 +158,7 @@ function ValidatorPageResult() {
const { data, isLoading, isSuccess, isError } = useGetSystemObject();

const numberOfValidators = useMemo(
() => data?.validators.active_validators.length || null,
() => data?.validators.activeValidators.length || null,
[data]
);

Expand All @@ -173,17 +173,17 @@ function ValidatorPageResult() {

const totalStaked = useMemo(() => {
if (!data) return 0;
const validators = data.validators.active_validators;
const validators = data.validators.activeValidators;

return validators.reduce(
(acc, cur) => acc + +cur.staking_pool.sui_balance,
(acc, cur) => acc + +cur.stakingPool.suiBalance,
0
);
}, [data]);

const averageAPY = useMemo(() => {
if (!data) return 0;
const validators = data.validators.active_validators;
const validators = data.validators.activeValidators;

const validatorsApy = validators.map((av) =>
calculateAPY(av, +data.epoch)
Expand Down Expand Up @@ -212,7 +212,7 @@ function ValidatorPageResult() {
const validatorsTable = useMemo(() => {
if (!data || !validatorEvents) return null;

const validators = data.validators.active_validators;
const validators = data.validators.activeValidators;

return validatorsTableData(
validators,
Expand Down
6 changes: 3 additions & 3 deletions crates/sui-json-rpc/src/unit_tests/rpc_server_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ use sui_config::SUI_KEYSTORE_FILENAME;
use sui_framework_build::compiled_package::BuildConfig;
use sui_json::SuiJsonValue;

use sui_json_rpc_types::SuiObjectInfo;
use sui_json_rpc_types::{
Balance, CoinPage, DelegatedStake, DelegationStatus, SuiObjectResponse, SuiCoinMetadata,
SuiEvent, SuiExecutionStatus, SuiObjectInfo, SuiTBlsSignObjectCommitmentType,
Balance, CoinPage, DelegatedStake, DelegationStatus, SuiCoinMetadata, SuiEvent,
SuiExecutionStatus, SuiObjectDataOptions, SuiObjectResponse, SuiTBlsSignObjectCommitmentType,
SuiTransactionResponse, TransactionBytes,
};
use sui_json_rpc_types::{SuiObjectDataOptions, SuiObjectInfo};
use sui_keys::keystore::{AccountKeystore, FileBasedKeystore, Keystore};
use sui_types::balance::Supply;
use sui_types::base_types::ObjectID;
Expand Down
7 changes: 3 additions & 4 deletions crates/sui-sdk/src/apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ use std::time::{Duration, Instant};
use sui_json_rpc::api::GovernanceReadApiClient;
use sui_json_rpc_types::{
Balance, Checkpoint, CheckpointId, Coin, CoinPage, DelegatedStake, DryRunTransactionResponse,
DynamicFieldPage, EventPage, GetRawObjectDataResponse, SuiCoinMetadata, SuiEventEnvelope,
SuiEventFilter, SuiMoveNormalizedModule, SuiObjectDataOptions, SuiObjectInfo,
SuiObjectResponse, SuiPastObjectResponse, SuiSystemStateRpc, SuiTransactionResponse,
TransactionsPage,
DynamicFieldPage, EventPage, SuiCoinMetadata, SuiEventEnvelope, SuiEventFilter,
SuiMoveNormalizedModule, SuiObjectDataOptions, SuiObjectInfo, SuiObjectResponse,
SuiPastObjectResponse, SuiSystemStateRpc, SuiTransactionResponse, TransactionsPage,
};
use sui_types::balance::Supply;
use sui_types::base_types::{
Expand Down
3 changes: 2 additions & 1 deletion sdk/typescript/src/types/dynamic_fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export const DynamicFieldName = object({
export type DynamicFieldName = Infer<typeof DynamicFieldName>;

export const DynamicFieldInfo = object({
name: union([DynamicFieldName, string()]),
name: DynamicFieldName,
bcsName: string(),
type: DynamicFieldType,
objectType: string(),
objectId: ObjectId,
Expand Down
Loading

0 comments on commit 417d448

Please sign in to comment.