Skip to content

Commit

Permalink
fix(contracttxid): make contractTxID require in remote state cache in…
Browse files Browse the repository at this point in the history
…stance
  • Loading branch information
atticusofsparta committed Feb 26, 2024
1 parent e92da07 commit dc82d21
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 39 deletions.
21 changes: 16 additions & 5 deletions src/common/ar-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,29 @@ import {
ARNS_DEVNET_REGISTRY_TX,
ARNS_TESTNET_REGISTRY_TX,
} from '../constants.js';
import { ArIOContract, ContractCache } from '../types/index.js';
import { ArIOContract } from '../types/index.js';
import { ArNSRemoteCache } from './index.js';

export class ArIO {
protected cache: ContractCache = new ArNSRemoteCache({});
protected cache: ArIOContract = new ArNSRemoteCache({
contractTxId: ARNS_TESTNET_REGISTRY_TX,
});

public testnet: ArIOContract;
public devnet: ArIOContract;

constructor({ remoteCacheUrl }: { remoteCacheUrl?: string }) {
this.cache = new ArNSRemoteCache({ url: remoteCacheUrl });
this.testnet = this.cache.setContractTxId(ARNS_TESTNET_REGISTRY_TX);
this.devnet = this.cache.setContractTxId(ARNS_DEVNET_REGISTRY_TX);
this.cache = new ArNSRemoteCache({
url: remoteCacheUrl,
contractTxId: ARNS_TESTNET_REGISTRY_TX,
});
this.testnet = new ArNSRemoteCache({
contractTxId: ARNS_TESTNET_REGISTRY_TX,
url: remoteCacheUrl,
});
this.devnet = new ArNSRemoteCache({
contractTxId: ARNS_DEVNET_REGISTRY_TX,
url: remoteCacheUrl,
});
}
}
32 changes: 4 additions & 28 deletions src/common/caches/arns-remote-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ import { ARNS_TESTNET_REGISTRY_TX } from '../../constants.js';
import {
ArIOContract,
ArNSStateResponse,
ContractCache,
Gateway,
HTTPClient,
} from '../../types/index.js';
import { NotFound } from '../error.js';
import { AxiosHTTPService } from '../http.js';
import { DefaultLogger } from '../logger.js';

export class ArNSRemoteCache implements ContractCache, ArIOContract {
export class ArNSRemoteCache implements ArIOContract {
private contractTxId: string;
private logger: DefaultLogger;
private http: HTTPClient;
Expand All @@ -37,29 +36,21 @@ export class ArNSRemoteCache implements ContractCache, ArIOContract {
level: 'debug',
logFormat: 'simple',
}),
contractTxId = ARNS_TESTNET_REGISTRY_TX,
}: {
url?: string;
logger?: DefaultLogger;
contractTxId?: string;
}) {
this.contractTxId = ARNS_TESTNET_REGISTRY_TX;
this.contractTxId = contractTxId;
this.logger = logger;
this.http = new AxiosHTTPService({
url: `${url}/${this.apiVersion}`,
logger,
});
}

setContractTxId(contractTxId: string): ArIOContract {
this.contractTxId = contractTxId;
return this;
}

async getGateway({ address }: { address: string }) {
if (!this.contractTxId) {
throw new Error(
'Contract TxId not set, set one before calling this function.',
);
}
this.logger.debug(`Fetching gateway ${address}`);
const gateway = await this.getGateways().then((gateways) => {
if (gateways[address] === undefined) {
Expand All @@ -71,11 +62,6 @@ export class ArNSRemoteCache implements ContractCache, ArIOContract {
}

async getGateways() {
if (!this.contractTxId) {
throw new Error(
'Contract TxId not set, set one before calling this function.',
);
}
this.logger.debug(`Fetching gateways`);
const { result } = await this.http.get<
ArNSStateResponse<'result', Record<string, Gateway>>
Expand All @@ -86,11 +72,6 @@ export class ArNSRemoteCache implements ContractCache, ArIOContract {
}

async getBalance({ address }: { address: string }) {
if (!this.contractTxId) {
throw new Error(
'Contract TxId not set, set one before calling this function.',
);
}
this.logger.debug(`Fetching balance for ${address}`);
const { result } = await this.http
.get<ArNSStateResponse<'result', number>>({
Expand All @@ -106,11 +87,6 @@ export class ArNSRemoteCache implements ContractCache, ArIOContract {
}

async getBalances() {
if (!this.contractTxId) {
throw new Error(
'Contract TxId not set, set one before calling this function.',
);
}
this.logger.debug(`Fetching balances`);
const { result } = await this.http.get<
ArNSStateResponse<'result', Record<string, number>>
Expand Down
6 changes: 0 additions & 6 deletions src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
*/
import { Gateway } from './contract-state.js';

export interface ContractCache {
/**
* The ContractCache interface is used to define a contract state provider.
*/
setContractTxId(contractTxId: string): ArIOContract;
}
// TODO: extend with additional methods
export interface ArIOContract {
getGateway({ address }: { address: WalletAddress }): Promise<Gateway>;
Expand Down

0 comments on commit dc82d21

Please sign in to comment.