Skip to content

Commit

Permalink
fix(gar write): fix types and flow on gar write
Browse files Browse the repository at this point in the history
  • Loading branch information
Atticus committed Mar 27, 2024
1 parent e01b08b commit f5e7774
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 41 deletions.
32 changes: 27 additions & 5 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export type WriteParameters<Input> = {

export interface BaseContract<T> {
getState(params: EvaluationParameters): Promise<T>;
connect(signer: ContractSigner): this & BaseContract<T> & ReadWriteContract;
connect(signer: ContractSigner): this;
connected(): boolean;
}

export interface ReadContract {
Expand Down Expand Up @@ -185,10 +186,31 @@ export interface ArIOContract extends BaseContract<ArIOState> {
type?: RegistrationType;
}>): Promise<ArNSAuctionData>;
// write interactions
joinNetwork(params: JoinNetworkParams): Promise<WriteInteractionResult>;
updateGatewaySettings(
params: UpdateGatewaySettingsParams,
): Promise<WriteInteractionResult>;
joinNetwork({
qty,
allowDelegatedStaking,
delegateRewardShareRatio,
fqdn,
label,
minDelegatedStake,
note,
port,
properties,
protocol,
autoStake,
}: JoinNetworkParams): Promise<WriteInteractionResult>;
updateGatewaySettings({
allowDelegatedStaking,
delegateRewardShareRatio,
fqdn,
label,
minDelegatedStake,
note,
port,
properties,
protocol,
autoStake,
}: UpdateGatewaySettingsParams): Promise<WriteInteractionResult>;
increaseOperatorStake(params: {
qty: number;
}): Promise<WriteInteractionResult>;
Expand Down
5 changes: 5 additions & 0 deletions src/common/ant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export class ANT implements ANTContract, BaseContract<ANTState> {

return this;
}

connected(): boolean {
return this.signer !== undefined;
}

/**
* Returns the current state of the contract.
*/
Expand Down
62 changes: 32 additions & 30 deletions src/common/ar-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ import {
isContractTxIdConfiguration,
} from '../types.js';
import { RemoteContract } from './contracts/remote-contract.js';
import {
NO_SIGNER_ERROR,
WarpContract,
WriteInteractionError,
} from './index.js';
import { InvalidSignerError, WarpContract } from './index.js';

export class ArIO implements ArIOContract, BaseContract<ArIOState> {
private contract:
Expand Down Expand Up @@ -71,9 +67,7 @@ export class ArIO implements ArIOContract, BaseContract<ArIOState> {
}
}

connect(
signer: ContractSigner,
): this & BaseContract<ArIOState> & ReadContract & WriteContract {
connect(signer: ContractSigner): this {
this.signer = signer;
if (this.contract instanceof RemoteContract) {
const config = this.contract.configuration();
Expand All @@ -84,11 +78,13 @@ export class ArIO implements ArIOContract, BaseContract<ArIOState> {
}
this.contract.connect(this.signer);

return this as this &
BaseContract<ArIOState> &
ReadContract &
WriteContract;
return this;
}

connected(): boolean {
return this.signer !== undefined;
}

/**
* Returns the current state of the contract.
*/
Expand Down Expand Up @@ -265,24 +261,30 @@ export class ArIO implements ArIOContract, BaseContract<ArIOState> {
}
// write methods

isContractWritable(
contract: BaseContract<ArIOState>,
): contract is BaseContract<ArIOState> & WriteContract {
return 'writeInteraction' in contract && contract.connected();
}

async joinNetwork(
params: JoinNetworkParams,
): Promise<WriteInteractionResult> {
if (!this.signer) {
throw new WriteInteractionError(NO_SIGNER_ERROR);
if (!this.isContractWritable(this.contract)) {
throw new InvalidSignerError();
}
return this.contract.connect(this.signer).writeInteraction({
return this.contract.writeInteraction({
functionName: 'joinNetwork',
inputs: params,
});
}
async updateGatewaySettings(
params: UpdateGatewaySettingsParams,
): Promise<WriteInteractionResult> {
if (!this.signer) {
throw new WriteInteractionError(NO_SIGNER_ERROR);
if (!this.isContractWritable(this.contract)) {
throw new InvalidSignerError();
}
return this.contract.connect(this.signer).writeInteraction({
return this.contract.writeInteraction({
functionName: 'updateGatewaySettings',
inputs: params,
});
Expand All @@ -292,10 +294,10 @@ export class ArIO implements ArIOContract, BaseContract<ArIOState> {
target: string;
qty: number;
}): Promise<WriteInteractionResult> {
if (!this.signer) {
throw new WriteInteractionError(NO_SIGNER_ERROR);
if (!this.isContractWritable(this.contract)) {
throw new InvalidSignerError();
}
return this.contract.connect(this.signer).writeInteraction({
return this.contract.writeInteraction({
functionName: 'delegateState',
inputs: params,
});
Expand All @@ -305,10 +307,10 @@ export class ArIO implements ArIOContract, BaseContract<ArIOState> {
target: string;
qty: number;
}): Promise<WriteInteractionResult> {
if (!this.signer) {
throw new WriteInteractionError(NO_SIGNER_ERROR);
if (!this.isContractWritable(this.contract)) {
throw new InvalidSignerError();
}
return this.contract.connect(this.signer).writeInteraction({
return this.contract.writeInteraction({
functionName: 'decreaseDelegateState',
inputs: params,
});
Expand All @@ -317,10 +319,10 @@ export class ArIO implements ArIOContract, BaseContract<ArIOState> {
async increaseOperatorStake(params: {
qty: number;
}): Promise<WriteInteractionResult> {
if (!this.signer) {
throw new WriteInteractionError(NO_SIGNER_ERROR);
if (!this.isContractWritable(this.contract)) {
throw new InvalidSignerError();
}
return this.contract.connect(this.signer).writeInteraction({
return this.contract.writeInteraction({
functionName: 'increaseOperatorStake',
inputs: params,
});
Expand All @@ -329,10 +331,10 @@ export class ArIO implements ArIOContract, BaseContract<ArIOState> {
async decreaseOperatorStake(params: {
qty: number;
}): Promise<WriteInteractionResult> {
if (!this.signer) {
throw new WriteInteractionError(NO_SIGNER_ERROR);
if (!this.isContractWritable(this.contract)) {
throw new InvalidSignerError();
}
return this.contract.connect(this.signer).writeInteraction({
return this.contract.writeInteraction({
functionName: 'decreaseOperatorStake',
inputs: params,
});
Expand Down
8 changes: 4 additions & 4 deletions src/common/contracts/remote-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
HTTPClient,
Logger,
ReadContract,
WriteContract,
} from '../../types.js';
import { AxiosHTTPService } from '../http.js';
import { DefaultLogger } from '../logger.js';
Expand Down Expand Up @@ -59,12 +58,13 @@ export class RemoteContract<T> implements BaseContract<T>, ReadContract {

/* eslint-disable */
// @ts-ignore
connect(
signer: ContractSigner,
): this & BaseContract<T> & ReadContract & WriteContract {
connect(signer: ContractSigner): this {
/* eslint-enable */
throw new Error('Cannot connect to a remote contract');
}
connected(): boolean {
return false;
}

async getState({ evaluationOptions }: EvaluationParameters = {}): Promise<T> {
this.logger.debug(`Fetching contract state`, {
Expand Down
4 changes: 4 additions & 0 deletions src/common/contracts/warp-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ export class WarpContract<T>
return this;
}

connected(): boolean {
return this.signer !== undefined;
}

async getState({ evaluationOptions = {} }: EvaluationParameters): Promise<T> {
await this.ensureContractInit();
const evalTo = evaluationOptions?.evalTo;
Expand Down
10 changes: 8 additions & 2 deletions src/common/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,11 @@ export class UnknownError extends BaseError {}

export class WriteInteractionError extends BaseError {}

export const NO_SIGNER_ERROR =
'No signer provided in contract configuration. Connect a signer to write transaction.';
export const INVALID_SIGNER_ERROR =
'Invalid signer. Please provide a valid signer to interact with the contract.';

export class InvalidSignerError extends BaseError {
constructor() {
super(INVALID_SIGNER_ERROR);
}
}

0 comments on commit f5e7774

Please sign in to comment.