Skip to content

Commit

Permalink
fix(types): use generic types and modify the requirements for init fu…
Browse files Browse the repository at this point in the history
…nctions
  • Loading branch information
dtfiedler committed Apr 25, 2024
1 parent f0e012f commit 9350f78
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 75 deletions.
37 changes: 16 additions & 21 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ export type TransactionId = string;

// TODO: append this with other configuration options (e.g. local vs. remote evaluation)
export type ContractSigner = ArweaveSigner | ArconnectSigner;
export type WithSigner = { signer: ContractSigner }; // TODO: optionally allow JWK in place of signer
export type WithSigner<T = NonNullable<unknown>> = {
signer: ContractSigner;
} & T; // TODO: optionally allow JWK in place of signer
export type OptionalSigner<T = NonNullable<unknown>> = {
signer?: ContractSigner;
} & T;
export type ContractConfiguration =
| {
contract?: WarpContract<unknown> | RemoteContract<unknown>;
contract: WarpContract<unknown> | RemoteContract<unknown>;
}
| {
contractTxId: string;
Expand All @@ -63,13 +68,15 @@ export type EvaluationParameters<T = NonNullable<unknown>> = {
evaluationOptions?: EvaluationOptions | Record<string, never> | undefined;
} & T;

export type WriteParameters<Input> = {
export type ReadParameters<Input> = {
functionName: string;
inputs: Input;
dryWrite?: boolean;
// TODO: add syncState and abortSignal options
inputs?: Input;
};

export type WriteParameters<Input> = WithSigner<
Required<ReadParameters<Input>>
>;

export interface BaseContract<T> {
getState(params: EvaluationParameters): Promise<T>;
}
Expand All @@ -79,10 +86,7 @@ export interface ReadContract {
functionName,
inputs,
evaluationOptions,
}: EvaluationParameters<{
functionName: string;
inputs?: Input;
}>): Promise<State>;
}: EvaluationParameters<ReadParameters<Input>>): Promise<State>;
}

export interface WriteContract {
Expand All @@ -95,15 +99,6 @@ export interface WriteContract {
>): Promise<WriteInteractionResult>;
}

export interface SmartWeaveContract<T> {
getContractState(params: EvaluationParameters): Promise<T>;
readInteraction<I, K>({
functionName,
inputs,
evaluationOptions,
}: EvaluationParameters<{ functionName: string; inputs?: I }>): Promise<K>;
}

// TODO: extend with additional methods
export interface ArIOReadContract extends BaseContract<ArIOState> {
getGateway({
Expand Down Expand Up @@ -139,9 +134,9 @@ export interface ArIOReadContract extends BaseContract<ArIOState> {
getEpoch({
blockHeight,
evaluationOptions,
}: {
}: EvaluationParameters<{
blockHeight: number;
} & EvaluationParameters): Promise<EpochDistributionData>;
}>): Promise<EpochDistributionData>;
getCurrentEpoch({
evaluationOptions,
}: EvaluationParameters): Promise<EpochDistributionData>;
Expand Down
20 changes: 7 additions & 13 deletions src/common/ant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
ContractSigner,
EvaluationOptions,
EvaluationParameters,
OptionalSigner,
WithSigner,
WriteInteractionResult,
} from '../types.js';
Expand Down Expand Up @@ -83,20 +84,13 @@ export class ANT {
* ```
*/
static init(
config: ContractConfiguration &
WithSigner &
({ contract: WarpContract<ANTState> } | { contractTxId: string }),
config: WithSigner<
{ contract: WarpContract<ANTState> } | { contractTxId: string }
>,
): ANTWritable;
static init(
config?: ContractConfiguration &
({ contract?: RemoteContract<ANTState> } | { contractTxId: string }),
): ANTReadable;
static init(
config: ContractConfiguration & {
signer?: ContractSigner;
} = {},
) {
if (config?.signer) {
static init(config?: ContractConfiguration): ANTReadable;
static init(config: OptionalSigner<ContractConfiguration>) {
if (config.signer) {
const signer = config.signer;
const contract = this.createContract(config);
return new ANTWritable({ signer, contract });
Expand Down
18 changes: 6 additions & 12 deletions src/common/ar-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
Gateway,
JoinNetworkParams,
Observations,
OptionalSigner,
RegistrationType,
TransactionId,
UpdateGatewaySettingsParams,
Expand Down Expand Up @@ -98,19 +99,12 @@ export class ArIO {
* ```
*/
static init(
config: ContractConfiguration &
WithSigner &
({ contract: WarpContract<ArIOState> } | { contractTxId: string }),
config: WithSigner<
{ contract: WarpContract<ArIOState> } | { contractTxId: string }
>,
): ArIOWritable;
static init(
config?: ContractConfiguration &
({ contract?: RemoteContract<ArIOState> } | { contractTxId: string }),
): ArIOReadable;
static init(
config: ContractConfiguration & {
signer?: ContractSigner;
} = {},
) {
static init(config?: ContractConfiguration): ArIOReadable;
static init(config?: OptionalSigner<ContractConfiguration>) {
if (config?.signer) {
const signer = config.signer;
const contract = this.createContract(config);
Expand Down
23 changes: 23 additions & 0 deletions src/common/arweave.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Arweave from 'arweave';

export const defaultArweave = Arweave.init({
host: 'arweave.net',
port: 443,
protocol: 'https',
});
15 changes: 4 additions & 11 deletions src/common/contracts/warp-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import {
Warp,
} from 'warp-contracts';

import { defaultWarp } from '../../constants.js';
import {
BaseContract,
ContractSigner,
EvaluationParameters,
Logger,
OptionalSigner,
ReadContract,
WriteContract,
WriteInteractionResult,
Expand All @@ -39,6 +39,7 @@ import { sha256B64Url, toB64Url } from '../../utils/base64.js';
import { getContractManifest } from '../../utils/smartweave.js';
import { FailedRequestError, WriteInteractionError } from '../error.js';
import { DefaultLogger } from '../logger.js';
import { defaultWarp } from '../warp.js';

LoggerFactory.INST.setOptions({
logLevel: 'fatal',
Expand Down Expand Up @@ -99,8 +100,6 @@ export class WarpContract<T>
},
type: 'arweave',
});
//this.contract = this.contract.connect(warpSigner);
//this.signer = warpSigner;
return warpSigner;
}

Expand All @@ -122,11 +121,7 @@ export class WarpContract<T>
return evaluationResult.cachedValue.state as T;
}

async ensureContractInit({
signer,
}: {
signer?: ContractSigner;
} = {}): Promise<void> {
async ensureContractInit({ signer }: OptionalSigner = {}): Promise<void> {
this.logger.debug(`Checking contract initialized`, {
contractTxId: this.contractTxId,
});
Expand Down Expand Up @@ -190,9 +185,7 @@ export class WarpContract<T>
inputs,
signer,
// TODO: support dryWrite
}: EvaluationParameters<WriteParameters<Input>> & {
signer: ContractSigner;
}): Promise<WriteInteractionResult> {
}: WriteParameters<Input>): Promise<WriteInteractionResult> {
try {
this.logger.debug(`Write interaction: ${functionName}`, {
contractTxId: this.contractTxId,
Expand Down
28 changes: 28 additions & 0 deletions src/common/warp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { WarpFactory, defaultCacheOptions } from 'warp-contracts';

import { defaultArweave } from './arweave.js';

export const defaultWarp = WarpFactory.forMainnet(
{
...defaultCacheOptions,
inMemory: true,
},
true,
defaultArweave,
);
18 changes: 0 additions & 18 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Arweave from 'arweave';
import { WarpFactory, defaultCacheOptions } from 'warp-contracts';

export const ARWEAVE_TX_REGEX = new RegExp('^[a-zA-Z0-9_-]{43}$');
// sortkey: padded blockheight to 12, JS timestamp, hash of transactionID + block hash. Timestamp only applicable to L2 and normally is all zeros.
export const SORT_KEY_REGEX = new RegExp(
Expand All @@ -27,18 +24,3 @@ export const ARNS_TESTNET_REGISTRY_TX =

export const ARNS_DEVNET_REGISTRY_TX =
'_NctcA2sRy1-J4OmIQZbYFPM17piNcbdBPH2ncX2RL8';

export const defaultArweave = Arweave.init({
host: 'ar-io.dev',
port: 443,
protocol: 'https',
});

export const defaultWarp = WarpFactory.forMainnet(
{
...defaultCacheOptions,
inMemory: true,
},
true,
defaultArweave,
);

0 comments on commit 9350f78

Please sign in to comment.