Skip to content

Commit

Permalink
fixup! feat: add GET_ADA_HANDLE command
Browse files Browse the repository at this point in the history
  • Loading branch information
iccicci committed Nov 18, 2024
1 parent 2521184 commit 699264d
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 14 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,13 +487,12 @@ Input message:

Response output message `data` type:

``` TypeScript
```ts
{
/** Address containing the specific asset */
address: string;
/** Asset quantity on the specific address */
quantity: string;
}[]
id: number | string;
type: 'message';
data: string | null;
}
```

### GET_BLOCK
Expand Down
17 changes: 13 additions & 4 deletions src/methods/get-ada-handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@ import { BlockfrostServerError } from '@blockfrost/blockfrost-js';
const policyID = 'f0ff48bbb7bbe9d59a40f1ce90e9e9d0ff5002ec48f232b49ca0fb9a';

export default async (id: MessageId, clientId: string, name: string): Promise<string> => {
let data: { address: string; quantity: string }[];
let data: string | null;

try {
data = await blockfrostAPI.assetsAddresses(
const result = await blockfrostAPI.assetsAddresses(
policyID + Buffer.from(name, 'utf8').toString('hex'),
);

if (result.length > 1) {
throw new Error('Double minted Ada Handle detected');
}

data = result.length === 0 ? null : result[0].address;
} catch (error) {
if (error instanceof BlockfrostServerError && error.error === 'Not Found') data = [];
else throw error;
if (error instanceof BlockfrostServerError && error.status_code === 404) {
data = null;
} else {
throw error;
}
}

return prepareMessage({ id, clientId, data });
Expand Down
2 changes: 1 addition & 1 deletion src/types/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export type Responses = {
| ServerInfo
| AccountInfo
| string
| Schemas['asset_addresses']
| null
| Schemas['block_content']
| BalanceHistoryData[]
| TxIdsToTransactionsResponse[]
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/fixtures/preprod/get-ada-handle.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ export default [
{
handle: 'test',
testName: 'GET_ADA_HANDLE success - preprod',
result: [{ address: expect.any(String), quantity: "1" }],
result: expect.any(String),
},
{
handle: 'does_not_exist',
testName: 'GET_ADA_HANDLE not existing - preprod',
result: [],
result: null,
},
] as const;
2 changes: 1 addition & 1 deletion test/e2e/tests/get-ada-handle.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getWebSocketClient } from '../utils/setup-websocket-client.js';

const fixtures = await getFixtures('get-ada-handle');

describe('get-block', () => {
describe('get-ada-handle', () => {
for (const { handle, result, testName } of fixtures) {
test(testName, async () => {
const ws = getWebSocketClient();
Expand Down
57 changes: 57 additions & 0 deletions test/unit/fixtures/getAdaHandle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { BlockfrostServerError } from "@blockfrost/blockfrost-js";

export default [
{
testName: 'getAdaHandle success',
id: 1,
assets: [{
address: 'address',
quantity: '1',
}],
result: {
id: 1,
type: 'message',
data: 'address',
},
},
{
testName: 'getAdaHandle not found',
id: 1,
error: new BlockfrostServerError({
error: 'error',
message: 'Not found',
status_code: 404,
url: 'url',
}),
result: {
id: 1,
type: 'message',
data: null,
},
},
{
testName: 'getAdaHandle empty result',
id: 1,
assets: [],
result: {
id: 1,
type: 'message',
data: null,
},
},
{
testName: 'getAdaHandle double minted',
id: 1,
assets: [
{
address: 'address1',
quantity: '1',
},
{
address: 'address2',
quantity: '1',
}
],
thrown: new Error('Double minted Ada Handle detected'),
},
];
26 changes: 26 additions & 0 deletions test/unit/tests/methods/get-ada-handle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sinon from 'sinon';
import { describe, test, expect } from 'vitest';
import fixtures from '../../fixtures/getAdaHandle.js';
import { blockfrostAPI } from '../../../../src/utils/blockfrost-api.js';
import getAdaHandle from '../../../../src/methods/get-ada-handle.js';

describe('getAdaHandle', () => {
for (const fixture of fixtures) {
test(fixture.testName, async () => {
const mock1 = fixture.assets ?
sinon.stub(blockfrostAPI, 'assetsAddresses').resolves(fixture.assets) :
sinon.stub(blockfrostAPI, 'assetsAddresses').rejects(fixture.error);

if(fixture.result) {
const result = await getAdaHandle(1, 'test', 'test');

expect(result).toBe(JSON.stringify(fixture.result));
}
else {
await expect(getAdaHandle(1, 'test', 'test')).rejects.toEqual(fixture.thrown);
}

mock1.restore();
});
}
});

0 comments on commit 699264d

Please sign in to comment.