Skip to content

Commit

Permalink
Ms/ts sdk governance api endpoints (MystenLabs#7673)
Browse files Browse the repository at this point in the history
- Add governance api methods in ts sdk
- Add governance types
- e2e test

---------

Co-authored-by: Chris Li <[email protected]>
  • Loading branch information
siomari and 666lcz authored Feb 3, 2023
1 parent bf56fb8 commit e6a7188
Show file tree
Hide file tree
Showing 34 changed files with 678 additions and 257 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-zebras-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mysten/sui.js": minor
---

Rename getDelegatedStake to getDelegatedStakes
5 changes: 5 additions & 0 deletions .changeset/unlucky-lobsters-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mysten/sui.js": patch
---

Add convenience methods in RpcTxnDataSerializer for building staking transactions
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { is, SuiObject, type ValidatorsFields } from '@mysten/sui.js';
import {
is,
SuiObject,
type MoveSuiSystemObjectFields,
type MoveActiveValidator,
} from '@mysten/sui.js';
import { useMemo } from 'react';

import { ReactComponent as ArrowRight } from '../../assets/SVGIcons/12px/ArrowRight.svg';
import { StakeColumn } from './StakeColumn';

import { useGetObject } from '~/hooks/useGetObject';
import {
VALIDATORS_OBJECT_ID,
type ActiveValidator,
} from '~/pages/validator/ValidatorDataTypes';
import { VALIDATORS_OBJECT_ID } from '~/pages/validator/ValidatorDataTypes';
import { Banner } from '~/ui/Banner';
import { ImageIcon } from '~/ui/ImageIcon';
import { ValidatorLink } from '~/ui/InternalLink';
Expand All @@ -23,7 +25,7 @@ import { getName } from '~/utils/getName';

const NUMBER_OF_VALIDATORS = 10;

export function processValidators(set: ActiveValidator[]) {
export function processValidators(set: MoveActiveValidator[]) {
return set.map((av) => {
const rawName = av.fields.metadata.fields.name;
const delegatedStake =
Expand All @@ -43,7 +45,7 @@ export function processValidators(set: ActiveValidator[]) {
}

const validatorsTable = (
validatorsData: ValidatorsFields,
validatorsData: MoveSuiSystemObjectFields,
limit?: number,
showIcon?: boolean
) => {
Expand Down Expand Up @@ -119,7 +121,7 @@ export function TopValidatorsCard({ limit, showIcon }: TopValidatorsCardProps) {
data &&
is(data.details, SuiObject) &&
data.details.data.dataType === 'moveObject'
? (data.details.data.fields as ValidatorsFields)
? (data.details.data.fields as MoveSuiSystemObjectFields)
: null;

const tableData = useMemo(
Expand Down
6 changes: 2 additions & 4 deletions apps/explorer/src/components/validator/ValidatorMeta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

import { ArrowUpRight12 } from '@mysten/icons';
import { Base64DataBuffer } from '@mysten/sui.js';
import { Base64DataBuffer, type MoveActiveValidator } from '@mysten/sui.js';
import { useMemo } from 'react';

import { StakeButton } from './StakeButton';

import type { ActiveValidator } from '~/pages/validator/ValidatorDataTypes';

import { DescriptionList, DescriptionItem } from '~/ui/DescriptionList';
import { Heading } from '~/ui/Heading';
import { ImageIcon } from '~/ui/ImageIcon';
Expand All @@ -17,7 +15,7 @@ import { Text } from '~/ui/Text';
import { getName } from '~/utils/getName';

type ValidatorMetaProps = {
validatorData: ActiveValidator;
validatorData: MoveActiveValidator;
};

export function ValidatorMeta({ validatorData }: ValidatorMetaProps) {
Expand Down
4 changes: 2 additions & 2 deletions apps/explorer/src/components/validator/ValidatorStats.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { type ActiveValidator } from '@mysten/sui.js';
import { type MoveActiveValidator } from '@mysten/sui.js';
import { useMemo } from 'react';

import { DelegationAmount } from './DelegationAmount';
Expand All @@ -13,7 +13,7 @@ import { Stats } from '~/ui/Stats';
import { getStakedPercent } from '~/utils/getStakedPercent';

type StatsCardProps = {
validatorData: ActiveValidator;
validatorData: MoveActiveValidator;
totalValidatorStake: string;
epoch: number | string;
};
Expand Down
4 changes: 2 additions & 2 deletions apps/explorer/src/components/validator/calculateAPY.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import type { ActiveValidator } from '@mysten/sui.js';
import type { MoveActiveValidator } from '@mysten/sui.js';

import { roundFloat } from '~/utils/roundFloat';

const APY_DECIMALS = 4;

export function calculateAPY(validators: ActiveValidator, epoch: number) {
export function calculateAPY(validators: MoveActiveValidator, epoch: number) {
const { sui_balance, starting_epoch, delegation_token_supply } =
validators.fields.delegation_staking_pool.fields;

Expand Down
4 changes: 2 additions & 2 deletions apps/explorer/src/hooks/useGetObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
SuiObject,
type GetObjectDataResponse,
normalizeSuiAddress,
type ValidatorsFields,
type MoveSuiSystemObjectFields,
} from '@mysten/sui.js';
import { useQuery, type UseQueryResult } from '@tanstack/react-query';

Expand All @@ -20,7 +20,7 @@ export function useGetSystemObject() {
data &&
is(data.details, SuiObject) &&
data.details.data.dataType === 'moveObject'
? (data.details.data.fields as ValidatorsFields)
? (data.details.data.fields as MoveSuiSystemObjectFields)
: null;

return {
Expand Down
48 changes: 32 additions & 16 deletions apps/explorer/src/pages/validator/ValidatorDataTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export interface StakeSubsidy {
}

export interface StakeSubsidyFields {
balance: string;
current_epoch_amount: string;
epoch_counter: string;
balance: Value;
current_epoch_amount: number;
epoch_counter: number;
}

export interface Supply {
Expand All @@ -57,7 +57,7 @@ export interface Supply {
}

export interface SuiSupplyFields {
value: string;
value: number;
}

export interface ValidatorReportRecords {
Expand Down Expand Up @@ -98,6 +98,7 @@ export interface ActiveValidatorFields {
pending_stake: string;
pending_withdraw: string;
stake_amount: string;
voting_power: string | null;
}

export interface DelegationStakingPool {
Expand All @@ -106,22 +107,31 @@ export interface DelegationStakingPool {
}

export interface DelegationStakingPoolFields {
delegation_token_supply: Supply;
pending_delegations: Pending;
pending_withdraws: Pending;
rewards_pool: string;
starting_epoch: string;
sui_balance: string;
delegation_token_supply: SuiSupplyFields;
pending_delegations: ContentsFields;
pending_withdraws: PendingDelegationsFields;
rewards_pool: Value;
starting_epoch: number;
sui_balance: number;
validator_address: string;
}

export interface Value {
value: number;
}

export interface Pending {
type: string;
fields: PendingDelegationsFields;
}

export interface PendingDelegationsFields {
contents: Contents;
contents: ContentsFieldsWithdraw;
}

export interface ContentsFieldsWithdraw {
id: string;
size: number;
}

export interface Contents {
Expand All @@ -130,8 +140,14 @@ export interface Contents {
}

export interface ContentsFields {
id: ID;
size: string;
id: string;
size: number;
head: Vector;
tail: Vector;
}

export interface Vector {
vec: any[];
}

export interface NextEpochValidator {
Expand All @@ -142,9 +158,9 @@ export interface NextEpochValidator {
export interface NextEpochValidatorFields {
consensus_address: number[];
name: number[] | string;
image_url?: number[] | string;
description?: number[] | string;
project_url?: number[] | string;
image_url?: number[] | string | null;
description?: number[] | string | null;
project_url?: number[] | string | null;
net_address: number[];
network_pubkey_bytes: number[];
next_epoch_commission_rate: string;
Expand Down
4 changes: 2 additions & 2 deletions apps/explorer/src/pages/validator/ValidatorDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { is, SuiObject, type ValidatorsFields } from '@mysten/sui.js';
import { is, SuiObject, type MoveSuiSystemObjectFields } from '@mysten/sui.js';
import { useMemo } from 'react';
import { useParams } from 'react-router-dom';

Expand All @@ -20,7 +20,7 @@ function ValidatorDetails() {
data &&
is(data.details, SuiObject) &&
data.details.data.dataType === 'moveObject'
? (data.details.data.fields as ValidatorsFields)
? (data.details.data.fields as MoveSuiSystemObjectFields)
: null;

const validatorData = useMemo(() => {
Expand Down
16 changes: 9 additions & 7 deletions apps/explorer/src/pages/validators/Validators.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { is, SuiObject, type ValidatorsFields } from '@mysten/sui.js';
import {
is,
SuiObject,
type MoveSuiSystemObjectFields,
type MoveActiveValidator,
} from '@mysten/sui.js';
import { lazy, Suspense, useMemo } from 'react';

import { ErrorBoundary } from '~/components/error-boundary/ErrorBoundary';
import { StakeColumn } from '~/components/top-validators-card/StakeColumn';
import { DelegationAmount } from '~/components/validator/DelegationAmount';
import { calculateAPY } from '~/components/validator/calculateAPY';
import { useGetObject } from '~/hooks/useGetObject';
import {
VALIDATORS_OBJECT_ID,
type ActiveValidator,
} from '~/pages/validator/ValidatorDataTypes';
import { VALIDATORS_OBJECT_ID } from '~/pages/validator/ValidatorDataTypes';
import { Banner } from '~/ui/Banner';
import { Card } from '~/ui/Card';
import { Heading } from '~/ui/Heading';
Expand All @@ -30,7 +32,7 @@ const APY_DECIMALS = 4;

const NodeMap = lazy(() => import('../../components/node-map'));

function validatorsTableData(validators: ActiveValidator[], epoch: number) {
function validatorsTableData(validators: MoveActiveValidator[], epoch: number) {
return {
data: validators.map((validator, index) => {
const validatorName = getName(
Expand Down Expand Up @@ -158,7 +160,7 @@ function ValidatorPageResult() {
data &&
is(data.details, SuiObject) &&
data.details.data.dataType === 'moveObject'
? (data.details.data.fields as ValidatorsFields)
? (data.details.data.fields as MoveSuiSystemObjectFields)
: null;

const totalStaked = useMemo(() => {
Expand Down
4 changes: 2 additions & 2 deletions apps/wallet/src/ui/app/staking/calculateAPY.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { type ActiveValidator } from '@mysten/sui.js';
import { type MoveActiveValidator } from '@mysten/sui.js';

import { roundFloat } from '_helpers';

const APY_DECIMALS = 4;

export function calculateAPY(validators: ActiveValidator, epoch: number) {
export function calculateAPY(validators: MoveActiveValidator, epoch: number) {
const { sui_balance, starting_epoch, delegation_token_supply } =
validators.fields.delegation_staking_pool.fields;

Expand Down
4 changes: 2 additions & 2 deletions apps/wallet/src/ui/app/staking/getStakingRewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import BigNumber from 'bignumber.js';

import type { ActiveValidator, DelegatedStake } from '@mysten/sui.js';
import type { MoveActiveValidator, DelegatedStake } from '@mysten/sui.js';

export function getStakingRewards(
activeValidators: ActiveValidator[],
activeValidators: MoveActiveValidator[],
delegation: DelegatedStake
) {
if (
Expand Down
4 changes: 2 additions & 2 deletions apps/wallet/src/ui/app/staking/home/DelegationCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useFormatCoin } from '_app/hooks';
import { Text } from '_src/ui/app/shared/text';
import { IconTooltip } from '_src/ui/app/shared/tooltip';

import type { ActiveValidator, DelegatedStake } from '@mysten/sui.js';
import type { MoveActiveValidator, DelegatedStake } from '@mysten/sui.js';

export enum DelegationState {
WARM_UP = 'WARM_UP',
Expand All @@ -21,7 +21,7 @@ export enum DelegationState {

interface DelegationCardProps {
delegationObject: DelegatedStake;
activeValidators: ActiveValidator[];
activeValidators: MoveActiveValidator[];
currentEpoch: number;
}

Expand Down
2 changes: 1 addition & 1 deletion apps/wallet/src/ui/app/staking/useGetDelegatedStake.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function useGetDelegatedStake(
): UseQueryResult<DelegatedStake[], Error> {
const rpc = useRpc();
return useQuery(['validator', address], () =>
rpc.getDelegatedStake(address)
rpc.getDelegatedStakes(address)
);
}

Expand Down
6 changes: 3 additions & 3 deletions apps/wallet/src/ui/app/staking/validatorsFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import {
is,
SuiObject,
type ValidatorsFields,
type MoveSuiSystemObjectFields,
type GetObjectDataResponse,
} from '@mysten/sui.js';

export function validatorsFields(
data?: GetObjectDataResponse
): ValidatorsFields | null {
): MoveSuiSystemObjectFields | null {
return data &&
is(data.details, SuiObject) &&
data.details.data.dataType === 'moveObject'
? (data.details.data.fields as ValidatorsFields)
? (data.details.data.fields as MoveSuiSystemObjectFields)
: null;
}
4 changes: 2 additions & 2 deletions dapps/frenemies/src/components/Validators/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useWalletKit } from "@mysten/wallet-kit";
import { useMyType } from "../../network/queries/use-raw";
import { GridItem } from "./GridItem";
import { ValidatorItem } from "./Validator";
import { ActiveValidator, normalizeSuiAddress } from "@mysten/sui.js";
import { MoveActiveValidator, normalizeSuiAddress } from "@mysten/sui.js";

function Header({ children }: { children: ReactNode }) {
return (
Expand All @@ -20,7 +20,7 @@ function Header({ children }: { children: ReactNode }) {

interface Props {
/** Set of 40 currently active validators */
validators: ActiveValidator[];
validators: MoveActiveValidator[];
}

export function Table({ validators }: Props) {
Expand Down
Loading

0 comments on commit e6a7188

Please sign in to comment.