Skip to content

Commit 33313b1

Browse files
authored
fix: treat incorrect get-token-uri none values as undefined (#1183)
1 parent 4e05e51 commit 33313b1

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

src/tests-tokens/strict-mode-tests.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as nock from 'nock';
2-
import { ChainID, ClarityAbi, cvToHex, uintCV } from '@stacks/transactions';
2+
import { ChainID, ClarityAbi, cvToHex, noneCV, uintCV } from '@stacks/transactions';
33
import { PoolClient } from 'pg';
44
import { TestBlockBuilder } from '../test-utils/test-builders';
55
import { ApiServer, startApiServer } from '../api/init';
@@ -262,6 +262,34 @@ describe('token metadata strict mode', () => {
262262
expect(entry.result?.processed).toBe(false);
263263
});
264264

265+
test('incorrect none uri strings are parsed as undefined', async () => {
266+
const mockResponse = {
267+
okay: true,
268+
result: cvToHex(noneCV()),
269+
};
270+
nock('http://127.0.0.1:20443')
271+
.post(
272+
'/v2/contracts/call-read/SP176ZMV706NZGDDX8VSQRGMB7QN33BBDVZ6BMNHD/project-indigo-act1/get-token-uri'
273+
)
274+
.reply(200, mockResponse);
275+
const handler = new TokensContractHandler({
276+
contractId: contractId,
277+
smartContractAbi: NFT_CONTRACT_ABI,
278+
datastore: db,
279+
chainId: ChainID.Testnet,
280+
txId: contractTxId,
281+
dbQueueId: 1,
282+
});
283+
await handler.start();
284+
const entry = await db.getTokenMetadataQueueEntry(1);
285+
expect(entry.result?.retry_count).toEqual(0);
286+
expect(entry.result?.processed).toBe(true);
287+
const metadata = await db.getNftMetadata(
288+
'SP176ZMV706NZGDDX8VSQRGMB7QN33BBDVZ6BMNHD.project-indigo-act1'
289+
);
290+
expect(metadata.result?.token_uri).toEqual('');
291+
});
292+
265293
test('metadata timeout errors get retried immediately', async () => {
266294
process.env['STACKS_API_TOKEN_METADATA_FETCH_TIMEOUT_MS'] = '500';
267295
const mockTokenUri = {

src/token-metadata/tokens-contract-handler.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,13 +505,15 @@ export class TokensContractHandler {
505505
);
506506
}
507507

508-
private checkAndParseString(responseCV: ClarityValue): string {
508+
private checkAndParseString(responseCV: ClarityValue): string | undefined {
509509
const unwrappedClarityValue = this.unwrapClarityType(responseCV);
510510
if (
511511
unwrappedClarityValue.type === ClarityType.StringASCII ||
512512
unwrappedClarityValue.type === ClarityType.StringUTF8
513513
) {
514514
return unwrappedClarityValue.data;
515+
} else if (unwrappedClarityValue.type === ClarityType.OptionalNone) {
516+
return undefined;
515517
}
516518
throw new RetryableTokenMetadataError(
517519
`Unexpected Clarity type '${unwrappedClarityValue.type}' while unwrapping string`

src/token-metadata/tokens-processor-queue.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,6 @@ export class TokensProcessorQueue {
144144
contractId: queueEntry.contractId,
145145
txId: queueEntry.txId,
146146
});
147-
logger.info(
148-
`[token-metadata] finished token contract processing for: ${queueEntry.contractId} from tx ${queueEntry.txId}`
149-
);
150147
if (this.queuedEntries.size < this.queue.concurrency) {
151148
void this.checkDbQueue();
152149
}

0 commit comments

Comments
 (0)