-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #261 from blockfrost/feat/get_adahandle
Feat/get adahandle
- Loading branch information
Showing
11 changed files
with
199 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
"lovelaces", | ||
"memoizee", | ||
"nixify", | ||
"preprod", | ||
"tailwindcss", | ||
"timelock", | ||
"txid", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { prepareMessage } from '../utils/message.js'; | ||
import { blockfrostAPI } from '../utils/blockfrost-api.js'; | ||
import { MessageId } from '../types/message.js'; | ||
import { BlockfrostServerError } from '@blockfrost/blockfrost-js'; | ||
|
||
const policyID = 'f0ff48bbb7bbe9d59a40f1ce90e9e9d0ff5002ec48f232b49ca0fb9a'; | ||
|
||
export default async (id: MessageId, clientId: string, name: string): Promise<string> => { | ||
let data: { address: string } | null; | ||
|
||
try { | ||
const result = await blockfrostAPI.assetsAddresses( | ||
policyID + Buffer.from(name, 'utf8').toString('hex'), | ||
); | ||
|
||
if (result.length > 1) { | ||
throw new Error('Double minted Ada Handle detected'); | ||
} | ||
|
||
if (result.length === 0) { | ||
data = null; | ||
} else { | ||
const { address } = result[0]; | ||
|
||
data = { address }; | ||
} | ||
} catch (error) { | ||
if (error instanceof BlockfrostServerError && error.status_code === 404) { | ||
data = null; | ||
} else { | ||
throw error; | ||
} | ||
} | ||
|
||
return prepareMessage({ id, clientId, data }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { expect } from 'vitest'; | ||
|
||
export default [ | ||
{ | ||
handle: 'test', | ||
testName: 'GET_ADA_HANDLE success - preprod', | ||
result: { address: expect.any(String) }, | ||
}, | ||
{ | ||
handle: 'does_not_exist', | ||
testName: 'GET_ADA_HANDLE not existing - preprod', | ||
result: null, | ||
}, | ||
] as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { getFixtures } from '../utils/fixtures-loader.js'; | ||
import { getWebSocketClient } from '../utils/setup-websocket-client.js'; | ||
|
||
const fixtures = await getFixtures('get-ada-handle'); | ||
|
||
describe('get-ada-handle', () => { | ||
for (const { handle, result, testName } of fixtures) { | ||
test(testName, async () => { | ||
const ws = getWebSocketClient(); | ||
const { data } = await ws.sendAndWait('GET_ADA_HANDLE', { name: handle }); | ||
expect(data).toMatchObject(result); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: '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'), | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
}); |