Skip to content

Commit

Permalink
fix(types): require atleast one param to update gateway settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Atticus committed Apr 15, 2024
1 parent f025171 commit 857ebdc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
30 changes: 16 additions & 14 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import {
ArNSNameData,
EpochDistributionData,
Gateway,
GatewayConnectionSettings,
GatewayMetadata,
GatewayStakingSettings,
Observations,
RegistrationType,
WeightedObserver,
Expand Down Expand Up @@ -214,21 +217,12 @@ export type WriteInteractionResult =
| DataItem
| InteractionResult<unknown, unknown>;

export type JoinNetworkParams = {
qty: number;
allowDelegatedStaking?: boolean;
delegateRewardShareRatio?: number;
fqdn: string;
label: string;
minDelegatedStake?: number;
note: string;
port: number;
properties: string;
protocol: AllowedProtocols;
autoStake?: boolean;
};
export type JoinNetworkParams = GatewayConnectionSettings &
GatewayStakingSettings &
GatewayMetadata & { qty: number };

export type UpdateGatewaySettingsParams = {
// Original type definition refined with proper field-specific types
export type UpdateGatewaySettingsParamsBase = {
allowDelegatedStaking?: boolean;
delegateRewardShareRatio?: number;
fqdn?: string;
Expand All @@ -241,6 +235,14 @@ export type UpdateGatewaySettingsParams = {
autoStake?: boolean;
};

// Utility type to require at least one of the fields
export type AtLeastOne<T, U = { [K in keyof T]-?: T[K] }> = Partial<U> &
{ [K in keyof U]: Required<Pick<U, K>> }[keyof U];

// Define the type used for function parameters
export type UpdateGatewaySettingsParams =
AtLeastOne<UpdateGatewaySettingsParamsBase>;

export interface ANTContract extends BaseContract<ANTState> {
getRecord({
domain,
Expand Down
24 changes: 12 additions & 12 deletions src/common/ar-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/
import { ARNS_TESTNET_REGISTRY_TX } from '../constants.js';
import {
AR_IO_CONTRACT_FUNCTIONS,
ArIOReadContract,
ArIOState,
ArIOWriteContract,
ArNSAuctionData,
ArNSNameData,
CONTRACT_FUNCTIONS,
ContractConfiguration,
ContractSigner,
EpochDistributionData,
Expand Down Expand Up @@ -184,7 +184,7 @@ export class ArIOReadable implements ArIOReadContract {
}: EvaluationParameters<{ address: string }>): Promise<Gateway | undefined> {
return this.contract
.readInteraction<{ target: string }, Gateway>({
functionName: CONTRACT_FUNCTIONS.GATEWAY,
functionName: AR_IO_CONTRACT_FUNCTIONS.GATEWAY,
inputs: {
target: address,
},
Expand All @@ -202,7 +202,7 @@ export class ArIOReadable implements ArIOReadContract {
Record<string, Gateway> | Record<string, never>
> {
return this.contract.readInteraction({
functionName: CONTRACT_FUNCTIONS.GATEWAYS,
functionName: AR_IO_CONTRACT_FUNCTIONS.GATEWAYS,
evaluationOptions,
});
}
Expand All @@ -214,7 +214,7 @@ export class ArIOReadable implements ArIOReadContract {
evaluationOptions,
}: EvaluationParameters = {}): Promise<EpochDistributionData> {
return this.contract.readInteraction({
functionName: CONTRACT_FUNCTIONS.EPOCH,
functionName: AR_IO_CONTRACT_FUNCTIONS.EPOCH,
evaluationOptions,
});
}
Expand All @@ -232,7 +232,7 @@ export class ArIOReadable implements ArIOReadContract {
{ height: number },
EpochDistributionData
>({
functionName: CONTRACT_FUNCTIONS.EPOCH,
functionName: AR_IO_CONTRACT_FUNCTIONS.EPOCH,
inputs: {
height: blockHeight,
},
Expand All @@ -247,7 +247,7 @@ export class ArIOReadable implements ArIOReadContract {
evaluationOptions,
}: EvaluationParameters = {}): Promise<WeightedObserver[]> {
return this.contract.readInteraction<never, WeightedObserver[]>({
functionName: CONTRACT_FUNCTIONS.PRESCRIBED_OBSERVERS,
functionName: AR_IO_CONTRACT_FUNCTIONS.PRESCRIBED_OBSERVERS,
evaluationOptions,
});
}
Expand Down Expand Up @@ -317,7 +317,7 @@ export class ArIOWritable extends ArIOReadable implements ArIOWriteContract {
params: JoinNetworkParams,
): Promise<WriteInteractionResult> {
return this.contract.writeInteraction({
functionName: CONTRACT_FUNCTIONS.JOIN_NETWORK,
functionName: AR_IO_CONTRACT_FUNCTIONS.JOIN_NETWORK,
inputs: params,
signer: this.signer,
});
Expand All @@ -326,7 +326,7 @@ export class ArIOWritable extends ArIOReadable implements ArIOWriteContract {
params: UpdateGatewaySettingsParams,
): Promise<WriteInteractionResult> {
return this.contract.writeInteraction({
functionName: CONTRACT_FUNCTIONS.UPDATE_GATEWAY_SETTINGS,
functionName: AR_IO_CONTRACT_FUNCTIONS.UPDATE_GATEWAY_SETTINGS,
inputs: params,
signer: this.signer,
});
Expand All @@ -337,7 +337,7 @@ export class ArIOWritable extends ArIOReadable implements ArIOWriteContract {
qty: number;
}): Promise<WriteInteractionResult> {
return this.contract.writeInteraction({
functionName: CONTRACT_FUNCTIONS.DELEGATE_STAKE,
functionName: AR_IO_CONTRACT_FUNCTIONS.DELEGATE_STAKE,
inputs: params,
signer: this.signer,
});
Expand All @@ -348,7 +348,7 @@ export class ArIOWritable extends ArIOReadable implements ArIOWriteContract {
qty: number;
}): Promise<WriteInteractionResult> {
return this.contract.writeInteraction({
functionName: CONTRACT_FUNCTIONS.DECREASE_DELEGATE_STAKE,
functionName: AR_IO_CONTRACT_FUNCTIONS.DECREASE_DELEGATE_STAKE,
inputs: params,
signer: this.signer,
});
Expand All @@ -358,7 +358,7 @@ export class ArIOWritable extends ArIOReadable implements ArIOWriteContract {
qty: number;
}): Promise<WriteInteractionResult> {
return this.contract.writeInteraction({
functionName: CONTRACT_FUNCTIONS.INCREASE_OPERATOR_STAKE,
functionName: AR_IO_CONTRACT_FUNCTIONS.INCREASE_OPERATOR_STAKE,
inputs: params,
signer: this.signer,
});
Expand All @@ -368,7 +368,7 @@ export class ArIOWritable extends ArIOReadable implements ArIOWriteContract {
qty: number;
}): Promise<WriteInteractionResult> {
const res = this.contract.writeInteraction({
functionName: CONTRACT_FUNCTIONS.DECREASE_OPERATOR_STAKE,
functionName: AR_IO_CONTRACT_FUNCTIONS.DECREASE_OPERATOR_STAKE,
inputs: params,
signer: this.signer,
});
Expand Down
22 changes: 16 additions & 6 deletions src/contract-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { WalletAddress } from './common.js';

// Gateways

export const CONTRACT_FUNCTIONS = {
export const AR_IO_CONTRACT_FUNCTIONS = {
GATEWAY: 'gateway',
GATEWAYS: 'gateways',
PRESCRIBED_OBSERVERS: 'prescribedObservers',
Expand Down Expand Up @@ -61,19 +61,29 @@ export type Gateway = {
weights: ObserverWeights;
};

export type GatewaySettings = {
export type GatewayStakingSettings = {
allowDelegatedStaking: boolean;
delegateRewardShareRatio: number;
fqdn: string;
label: string;
minDelegatedStake: number;
autoStake: boolean;
};

export type GatewayMetadata = {
label: string;
note?: string;
port: number;
properties?: string;
};

export type GatewayConnectionSettings = {
fqdn: string;
port: number;
protocol: AllowedProtocols;
autoStake: boolean;
};

export type GatewaySettings = GatewayConnectionSettings &
GatewayStakingSettings &
GatewayMetadata;

export type AllowedProtocols = 'http' | 'https';

export type GatewayStats = {
Expand Down

0 comments on commit 857ebdc

Please sign in to comment.