From e01b08beccb64112ae4ec333e9a5a2ea872b816f Mon Sep 17 00:00:00 2001 From: Atticus Date: Wed, 27 Mar 2024 13:17:03 -0600 Subject: [PATCH] feat(gar methods): add gar write methods to the ario client --- src/common.ts | 35 +++++++++++++------- src/common/ar-io.ts | 47 +++++++++++++++++++++------ src/common/contracts/warp-contract.ts | 2 +- src/common/error.ts | 3 ++ 4 files changed, 65 insertions(+), 22 deletions(-) diff --git a/src/common.ts b/src/common.ts index ef37f41b..822a0959 100644 --- a/src/common.ts +++ b/src/common.ts @@ -44,7 +44,7 @@ export type ContractConfiguration = { | { contract?: | (BaseContract & ReadContract) - | (BaseContract & ReadContract & WriteContract); + | (BaseContract & ReadWriteContract); } | { contractTxId: string; @@ -53,7 +53,11 @@ export type ContractConfiguration = { export function isContractConfiguration( config: ContractConfiguration, -): config is { contract: BaseContract & ReadContract } { +): config is { + contract: + | (BaseContract & ReadContract) + | (BaseContract & ReadWriteContract); +} { return 'contract' in config; } @@ -82,9 +86,7 @@ export type WriteParameters = { export interface BaseContract { getState(params: EvaluationParameters): Promise; - connect( - signer: ContractSigner, - ): this & BaseContract & ReadContract & WriteContract; + connect(signer: ContractSigner): this & BaseContract & ReadWriteContract; } export interface ReadContract { @@ -108,6 +110,8 @@ export interface WriteContract { >; } +export interface ReadWriteContract extends ReadContract, WriteContract {} + export interface SmartWeaveContract { getContractState(params: EvaluationParameters): Promise; readInteraction({ @@ -181,22 +185,31 @@ export interface ArIOContract extends BaseContract { type?: RegistrationType; }>): Promise; // write interactions - joinNetwork(params: JoinNetworkParams): Promise; + joinNetwork(params: JoinNetworkParams): Promise; updateGatewaySettings( params: UpdateGatewaySettingsParams, - ): Promise; - increaseOperatorStake(params: { qty: number }): Promise; - decreaseOperatorStake(params: { qty: number }): Promise; + ): Promise; + increaseOperatorStake(params: { + qty: number; + }): Promise; + decreaseOperatorStake(params: { + qty: number; + }): Promise; increaseDelegateState(params: { target: WalletAddress; qty: number; - }): Promise; + }): Promise; decreaseDelegateState(params: { target: WalletAddress; qty: number; - }): Promise; + }): Promise; } +export type WriteInteractionResult = + | Transaction + | DataItem + | InteractionResult; + export type JoinNetworkParams = { qty: number; allowDelegatedStaking?: boolean; diff --git a/src/common/ar-io.ts b/src/common/ar-io.ts index 3a2c8c64..59b373e7 100644 --- a/src/common/ar-io.ts +++ b/src/common/ar-io.ts @@ -14,9 +14,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import { DataItem } from 'arbundles/node'; -import { Transaction } from 'warp-contracts/web'; - import { ARNS_TESTNET_REGISTRY_TX } from '../constants.js'; import { ArIOContract, @@ -37,11 +34,16 @@ import { UpdateGatewaySettingsParams, WeightedObserver, WriteContract, + WriteInteractionResult, isContractConfiguration, isContractTxIdConfiguration, } from '../types.js'; import { RemoteContract } from './contracts/remote-contract.js'; -import { WarpContract } from './index.js'; +import { + NO_SIGNER_ERROR, + WarpContract, + WriteInteractionError, +} from './index.js'; export class ArIO implements ArIOContract, BaseContract { private contract: @@ -261,8 +263,14 @@ export class ArIO implements ArIOContract, BaseContract { return auctions; } + // write methods - async joinNetwork(params: JoinNetworkParams): Promise { + async joinNetwork( + params: JoinNetworkParams, + ): Promise { + if (!this.signer) { + throw new WriteInteractionError(NO_SIGNER_ERROR); + } return this.contract.connect(this.signer).writeInteraction({ functionName: 'joinNetwork', inputs: params, @@ -270,7 +278,10 @@ export class ArIO implements ArIOContract, BaseContract { } async updateGatewaySettings( params: UpdateGatewaySettingsParams, - ): Promise { + ): Promise { + if (!this.signer) { + throw new WriteInteractionError(NO_SIGNER_ERROR); + } return this.contract.connect(this.signer).writeInteraction({ functionName: 'updateGatewaySettings', inputs: params, @@ -280,7 +291,10 @@ export class ArIO implements ArIOContract, BaseContract { async increaseDelegateState(params: { target: string; qty: number; - }): Promise { + }): Promise { + if (!this.signer) { + throw new WriteInteractionError(NO_SIGNER_ERROR); + } return this.contract.connect(this.signer).writeInteraction({ functionName: 'delegateState', inputs: params, @@ -290,21 +304,34 @@ export class ArIO implements ArIOContract, BaseContract { async decreaseDelegateState(params: { target: string; qty: number; - }): Promise { + }): Promise { + if (!this.signer) { + throw new WriteInteractionError(NO_SIGNER_ERROR); + } return this.contract.connect(this.signer).writeInteraction({ functionName: 'decreaseDelegateState', inputs: params, }); } - async increaseOperatorStake(params: { qty: number }): Promise { + async increaseOperatorStake(params: { + qty: number; + }): Promise { + if (!this.signer) { + throw new WriteInteractionError(NO_SIGNER_ERROR); + } return this.contract.connect(this.signer).writeInteraction({ functionName: 'increaseOperatorStake', inputs: params, }); } - async decreaseOperatorStake(params: { qty: number }): Promise { + async decreaseOperatorStake(params: { + qty: number; + }): Promise { + if (!this.signer) { + throw new WriteInteractionError(NO_SIGNER_ERROR); + } return this.contract.connect(this.signer).writeInteraction({ functionName: 'decreaseOperatorStake', inputs: params, diff --git a/src/common/contracts/warp-contract.ts b/src/common/contracts/warp-contract.ts index 9341975c..e61f5af3 100644 --- a/src/common/contracts/warp-contract.ts +++ b/src/common/contracts/warp-contract.ts @@ -89,7 +89,7 @@ export class WarpContract }; } - connect(signer: ContractSigner) { + connect(signer: ContractSigner): this { const warpSigner = new Signature(this.warp, { signer: async (tx: Transaction) => { const dataToSign = await tx.getSignatureData(); diff --git a/src/common/error.ts b/src/common/error.ts index c221fbb2..06460913 100644 --- a/src/common/error.ts +++ b/src/common/error.ts @@ -34,3 +34,6 @@ export class FailedRequestError extends BaseError { 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.';