Skip to content

Commit

Permalink
ts-sdk: add delegation utility class
Browse files Browse the repository at this point in the history
  • Loading branch information
pchrysochoidis committed Aug 16, 2022
1 parent b0ec0ae commit 97157ec
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 3 deletions.
70 changes: 69 additions & 1 deletion sdk/typescript/src/index.guard.ts

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

78 changes: 76 additions & 2 deletions sdk/typescript/src/types/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import {
GetObjectDataResponse,
SuiMoveObject,
SuiObjectInfo,
SuiObject,
SuiData,
getMoveObjectType,
} from './objects';

import { getMoveObjectType } from './objects';
import { SuiAddress } from './common';

import BN from 'bn.js';
import { getOption, Option } from './option';

const COIN_TYPE = '0x2::coin::Coin';
const COIN_TYPE_ARG_REGEX = /^0x2::coin::Coin<(.+)>$/;
Expand Down Expand Up @@ -61,3 +64,74 @@ export class Coin {
return data.type;
}
}


export type DelegationData = SuiMoveObject &
Pick<SuiData, 'dataType'> & {
type: '0x2::delegation::Delegation';
fields: {
active_delegation: Option<number>;
delegate_amount: number;
next_reward_unclaimed_epoch: number;
validator_address: SuiAddress;
info: {
id: string;
version: number;
};
coin_locked_until_epoch: Option<SuiMoveObject>;
ending_epoch: Option<number>;
};
};

export type DelegationSuiObject = Omit<SuiObject, 'data'> & {
data: DelegationData;
};

// Class for delegation.move
// see https://github.com/MystenLabs/fastnft/blob/161aa27fe7eb8ecf2866ec9eb192e768f25da768/crates/sui-framework/sources/governance/delegation.move
export class Delegation {
public static readonly SUI_OBJECT_TYPE = '0x2::delegation::Delegation';
private suiObject: DelegationSuiObject;

public static isDelegationSuiObject(
obj: SuiObject
): obj is DelegationSuiObject {
return (
'type' in obj.data &&
obj.data.type === Delegation.SUI_OBJECT_TYPE
);
}

constructor(obj: DelegationSuiObject) {
this.suiObject = obj;
}

public nextRewardUnclaimedEpoch() {
return this.suiObject.data.fields.next_reward_unclaimed_epoch;
}

public activeDelegation() {
return BigInt(getOption(this.suiObject.data.fields.active_delegation) || 0);
}

public delegateAmount() {
return this.suiObject.data.fields.delegate_amount;
}

public endingEpoch() {
return getOption(this.suiObject.data.fields.ending_epoch);
}

public validatorAddress() {
return this.suiObject.data.fields.validator_address;
}

public isActive() {
return this.activeDelegation() > 0 && !this.endingEpoch();
}

public hasUnclaimedRewards(epoch: number) {
return this.nextRewardUnclaimedEpoch() <= epoch && (this.isActive() || (this.endingEpoch() || 0) > epoch);
}
}

16 changes: 16 additions & 0 deletions sdk/typescript/src/types/option.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

export type Option<T> = T | {
fields: {
vec: '';
};
type: string;
}

export function getOption<T>(option: Option<T>): T | undefined {
if (typeof option === 'object' && 'type' in option && option.type.startsWith('0x1::option::Option<')) {
return undefined;
}
return option as T;
}

0 comments on commit 97157ec

Please sign in to comment.