Skip to content

Commit

Permalink
fix(records): remove contractTxId filter remove lodash shrink readme
Browse files Browse the repository at this point in the history
  • Loading branch information
atticusofsparta committed Feb 26, 2024
1 parent 1b7f54f commit ea2cc83
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 65 deletions.
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,7 @@ const contractTxIds = [
'I-cxQhfh0Zb9UqQNizC9PiLC41KpUeA9hjiVV02rQRw',
'DGWp8_6c1YywKgCgBFhncMglciQyCdfX1swil4qjNSc',
];
// testnet
const testnetBalance = await arIO.testnet.getBalance({ address });
const testnetGateway = await arIO.testnet.getGateway({ address });
const testnetRecord = await arIO.testnet.getRecord({ domain });
const testnetRecords = await arIO.testnet.getRecords({ contractTxIds });
const allTestnetRecords = await arIO.testnet.getRecords({});

// mainnet

const balance = await arIO.mainnet.getBalance({ address });
const gateway = await arIO.mainnet.getGateway({ address });
const record = await arIO.mainnet.getRecord({ domain });
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
"dependencies": {
"arweave": "^1.14.4",
"axios": "1.4.0",
"lodash": "^4.17.21",
"warp-contracts": "^1.4.34",
"winston": "^3.11.0"
}
Expand Down
39 changes: 7 additions & 32 deletions src/common/caches/arns-remote-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +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 { chunk } from 'lodash';

import { ARNS_TESTNET_REGISTRY_TX } from '../../constants.js';
import {
ArIOContract,
Expand Down Expand Up @@ -131,38 +129,15 @@ export class ArNSRemoteCache implements ContractCache, ArIOContract {
return record;
}

async getRecords({
contractTxIds,
}: {
contractTxIds?: string[];
}): Promise<Record<string, ArNSNameData>> {
async getRecords(): Promise<Record<string, ArNSNameData>> {
validateContractTxId(this.contractTxId);

const contractTxIdsSet = new Set(contractTxIds);
const batches = chunk([...contractTxIdsSet]);

this.logger.debug(`Fetching records for ${contractTxIdsSet.size} ANT's`);
const records = await Promise.all(
batches.map((batch: string[]) =>
this.http.get<
ArNSStateResponse<'records', Record<string, ArNSNameData>>
>({
endpoint: `/contract/${this.contractTxId.toString()}/records${new URLSearchParams(batch.map((id) => ['contractTxId', id.toString()])).toString()}`,
}),
),
).then(
(
responses: ArNSStateResponse<'records', Record<string, ArNSNameData>>[],
) => {
const recordsObj = responses.reduce(
(acc: Record<string, ArNSNameData>, response) => {
return { ...acc, ...response.records };
},
{},
);
return recordsObj;
},
);
this.logger.debug(`Fetching all records`);
const { records } = await this.http.get<
ArNSStateResponse<'records', Record<string, ArNSNameData>>
>({
endpoint: `/contract/${this.contractTxId.toString()}/records`,
});
return records;
}
}
6 changes: 1 addition & 5 deletions src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ export interface ArIOContract {
getBalance({ address }: { address: WalletAddress }): Promise<number>;
getBalances(): Promise<Record<WalletAddress, number>>;
getRecord({ domain }: { domain: string }): Promise<ArNSNameData>;
getRecords({
contractTxIds,
}: {
contractTxIds?: string[];
}): Promise<Record<string, ArNSNameData>>;
getRecords(): Promise<Record<string, ArNSNameData>>;
}

/* eslint-disable @typescript-eslint/no-explicit-any */
Expand Down
21 changes: 2 additions & 19 deletions tests/arns-remote-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,9 @@ describe('ArNSRemoteCache', () => {
expect(error).toBeInstanceOf(NotFound);
});

it('should fetch records for list of contractIDs', async () => {
const allRecords = await remoteCacheProvider
.getRecords({})
.then((res) => Object.entries(res).slice(200))
.catch((e) => e); // deliberately attempting to get more than URL params can handle to test batching but limiting to 200 to not strain service

const contractTxIds = allRecords.map(([, record]) => record.contractTxId); // deliberately attempting to get more than URL params can handle to test batching but limiting to 200 to not strain service

const expectedRecords = allRecords
.map(([domain]) => domain)
.sort((a: string, b: string) => a.localeCompare(b));
const records = await remoteCacheProvider.getRecords({
contractTxIds: [...contractTxIds, ...contractTxIds], // mapping twice to test duplicates
});

const actualRecords = Object.keys(records).sort((a: string, b: string) =>
a.localeCompare(b),
);
it('should fetch all records', async () => {
const records = await remoteCacheProvider.getRecords();

expect(records).toBeDefined();
expect(actualRecords).toEqual(expectedRecords);
});
});

0 comments on commit ea2cc83

Please sign in to comment.