Skip to content

Commit

Permalink
fix(types): update tests, readme, and types
Browse files Browse the repository at this point in the history
  • Loading branch information
Atticus committed Apr 10, 2024
1 parent e3c97e9 commit fb11d97
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 72 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ const updateGatewaySettingsParams = {
};

const signer = new ArweaveSigner(jwk);
// connection required for write interactions
// signer required for write interactions APIs
const authenticatedArIO = ArIO.init({ signer });
const updateGatewaySettingsTx = await authenticatedArIO.updateGatewaySettings(
updateGatewaySettingsParams,
Expand All @@ -635,7 +635,7 @@ const params = {
};

const signer = new ArweaveSigner(jwk);
// connection required for write interactions
// signer required for write interactions APIs
const authenticatedArIO = ArIO.init({ signer });
const increaseDelegateStakeTx =
await authenticatedArIO.increaseDelegateStake(params);
Expand All @@ -657,7 +657,7 @@ const params = {
};

const signer = new ArweaveSigner(jwk);
// connection required for write interactions
// signer required for write interactions APIs
const authenticatedArIO = ArIO.init({ signer });
const decreaseDelegateStakeTx =
await authenticatedArIO.decreaseDelegateStake(params);
Expand All @@ -678,7 +678,7 @@ const params = {
};

const signer = new ArweaveSigner(jwk);
// connection required for write interactions
// signer required for write interactions APIs
const authenticatedArIO = ArIO.init({ signer });
const increaseOperatorStakeTx =
await authenticatedArIO.increaseOperatorStake(params);
Expand All @@ -699,7 +699,7 @@ const params = {
};

const signer = new ArweaveSigner(jwk);
// connection required for write interactions
// signer required for write interactions APIs
const authenticatedArIO = ArIO.init({ signer });
const decreaseOperatorStakeTx =
await authenticatedArIO.decreaseOperatorStake(params);
Expand Down
5 changes: 1 addition & 4 deletions src/common/ant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export class ANT implements ANTContract, BaseContract<ANTState> {
private contract: BaseContract<ANTState>;
private signer: ContractSigner | undefined;

constructor({ signer, ...config }: ContractConfiguration) {
this.signer = signer;
constructor(config: ContractConfiguration) {
if (isContractConfiguration<ANTState>(config)) {
this.contract = config.contract;
} else if (isContractTxIdConfiguration(config)) {
Expand All @@ -50,10 +49,8 @@ export class ANT implements ANTContract, BaseContract<ANTState> {
const config = this.contract.configuration();
this.contract = new WarpContract<ANTState>({
...config,
signer,
});
}
this.contract.connect(this.signer);

return this;
}
Expand Down
22 changes: 12 additions & 10 deletions src/common/ar-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,11 @@ export class ArIO {
if (isContractConfiguration<ArIOState>(config)) {
if (config.contract instanceof WarpContract) {
return config.contract;
} else {
// TODO: throw error if contract is not of WarpContract type
return new WarpContract<ArIOState>(config.contract.configuration());
}
} else if (isContractTxIdConfiguration(config)) {
return new WarpContract<ArIOState>({ contractTxId: config.contractTxId });
} else {
throw new Error('Invalid configuration.');
}
throw new Error('Invalid configuration.');
}

/**
Expand All @@ -82,14 +78,20 @@ export class ArIO {
* const readable = ArIO.init({ contract: myContract });
*/
static init(
config?: ContractConfiguration &
WithSigner & { contract?: WarpContract<ArIOState> },
config: ContractConfiguration &
WithSigner &
({ contract: WarpContract<ArIOState> } | { contractTxId: string }),
): ArIOWritable;
static init(config?: ContractConfiguration): ArIOReadable;
static init(
config: ContractConfiguration & { signer?: ContractSigner } = {},
config?: ContractConfiguration &
({ contract?: RemoteContract<ArIOState> } | { contractTxId: string }),
): ArIOReadable;
static init(
config: ContractConfiguration & {
signer?: ContractSigner;
} = {},
) {
if (config.signer) {
if (config?.signer) {
const signer = config.signer;
const contract = this.createContract(config);
return new ArIOWritable({ signer, contract });
Expand Down
75 changes: 24 additions & 51 deletions tests/integration/ant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import {

const contractTxId = 'UC2zwawQoTnh0TNd9mYLQS4wObBBeaOU5LPQTNETqA4';
const localCacheUrl = `https://api.arns.app`;

const testCases = [
[{ sortKey: evaluateToSortKey.toString() }],
[{ blockHeight: evaluateToBlockHeight }],
[undefined],
] as const;
describe('ANT contract apis', () => {
const ant = new ANT({
contract: new RemoteContract<ANTState>({
Expand All @@ -28,59 +34,42 @@ describe('ANT contract apis', () => {
expect(ant).toBeInstanceOf(ANT);
});

it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[undefined],
[{ blockHeight: evaluateToBlockHeight }],
])(
it.each(testCases)(
`should get contract state with evaluation options: ${JSON.stringify('%s')}`,
async (evalTo) => {
const state = await ant.getState({ evaluationOptions: { evalTo } });
expect(state).toBeDefined();
},
);

it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[undefined],
[{ blockHeight: evaluateToBlockHeight }],
])(`should get record: ${JSON.stringify('%s')}`, async (evalTo) => {
const record = await ant.getRecord({
domain: '@',
evaluationOptions: { evalTo },
});
expect(record).toBeDefined();
});
it.each(testCases)(
`should get record: ${JSON.stringify('%s')}`,
async (evalTo) => {
const record = await ant.getRecord({
domain: '@',
evaluationOptions: { evalTo },
});
expect(record).toBeDefined();
},
);

it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[undefined],
[{ blockHeight: evaluateToBlockHeight }],
])(
it.each(testCases)(
`should get records with evaluation options: ${JSON.stringify('%s')}`,
async (evalTo) => {
const records = await ant.getRecords({ evaluationOptions: { evalTo } });
expect(records).toBeDefined();
},
);

it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[undefined],
[{ blockHeight: evaluateToBlockHeight }],
])(
it.each(testCases)(
`should get owner with evaluation options: ${JSON.stringify('%s')}`,
async (evalTo) => {
const owner = await ant.getOwner({ evaluationOptions: { evalTo } });
expect(owner).toBeDefined();
},
);

it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[undefined],
[{ blockHeight: evaluateToBlockHeight }],
])(
it.each(testCases)(
`should get controllers with evaluation options: ${JSON.stringify('%s')}`,
async (evalTo) => {
const controllers = await ant.getControllers({
Expand All @@ -90,47 +79,31 @@ describe('ANT contract apis', () => {
},
);

it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[undefined],
[{ blockHeight: evaluateToBlockHeight }],
])(
it.each(testCases)(
`should get name with evaluation options: ${JSON.stringify('%s')}`,
async (evalTo) => {
const state = await ant.getName({ evaluationOptions: { evalTo } });
expect(state).toBeDefined();
},
);

it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[undefined],
[{ blockHeight: evaluateToBlockHeight }],
])(
it.each(testCases)(
`should get ticker with evaluation options: ${JSON.stringify('%s')}`,
async (evalTo) => {
const state = await ant.getTicker({ evaluationOptions: { evalTo } });
expect(state).toBeDefined();
},
);

it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[undefined],
[{ blockHeight: evaluateToBlockHeight }],
])(
it.each(testCases)(
`should get balances with evaluation options: ${JSON.stringify('%s')}`,
async (evalTo) => {
const state = await ant.getBalances({ evaluationOptions: { evalTo } });
expect(state).toBeDefined();
},
);

it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[undefined],
[{ blockHeight: evaluateToBlockHeight }],
])(
it.each(testCases)(
`should get balance with evaluation options: ${JSON.stringify('%s')}`,
async (evalTo) => {
const state = await ant.getBalance({
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/ar-io.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ArweaveSigner } from 'arbundles';

import { ArIO } from '../../src/common/ar-io.js';
import { RemoteContract } from '../../src/common/contracts/remote-contract.js';
import { WarpContract } from '../../src/common/index.js';
import { DefaultLogger } from '../../src/common/logger.js';
import { ARNS_DEVNET_REGISTRY_TX } from '../../src/constants.js';
import { ArIOState } from '../../src/contract-state.js';
Expand All @@ -21,10 +22,9 @@ const testCases = [
] as const;
describe('ArIO Client', () => {
const signer = new ArweaveSigner(JSON.parse(process.env.PRIMARY_WALLET_JWK!));
const ar = ArIO.init();
const arIO = ArIO.init({
signer,
contract: new RemoteContract<ArIOState>({
contract: new WarpContract<ArIOState>({
cacheUrl: localCacheUrl,
contractTxId,
logger: new DefaultLogger({ level: 'none' }),
Expand Down

0 comments on commit fb11d97

Please sign in to comment.