Skip to content

Commit

Permalink
feat: erc721 token media special uri (#900)
Browse files Browse the repository at this point in the history
* feat: erc721 token media special uri

* refactor: handle error

* fix: test

* fix: test

* refactor: save error job fail
  • Loading branch information
phamphong9981 authored Sep 10, 2024
1 parent 364170c commit d5744b0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/services/evm/erc721.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,15 @@ export default class Erc721Service extends BullableService {
tokenMedia.onchain.token_uri
);
} catch (error) {
this.logger.error(error);
await Erc721Token.query()
.where('id', tokenMedia.erc721_token_id)
.patch({
media_info: {
onchain: {
token_uri: tokenMedia.onchain.token_uri,
},
},
});
throw error;
}
}
Expand Down Expand Up @@ -484,9 +492,6 @@ export default class Erc721Service extends BullableService {
{},
{
removeOnComplete: true,
removeOnFail: {
count: 3,
},
repeat: {
every: config.erc721.millisecondRepeatJob,
},
Expand Down
19 changes: 15 additions & 4 deletions src/services/evm/erc721_media_handler.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/* eslint-disable no-param-reassign */
import { fromBase64, fromUtf8 } from '@cosmjs/encoding';
import { AWSError } from 'aws-sdk';
import axios, { AxiosError } from 'axios';
import * as FileType from 'file-type';
import * as isIPFS from 'is-ipfs';
import parse from 'parse-uri';
import Moleculer from 'moleculer';
import parse from 'parse-uri';
import { Config } from '../../common';
import { S3Service } from '../../common/utils/s3';

const SUPPORT_DECODED_TOKEN_URI = {
BASE64: 'data:application/json;base64',
};
export interface ITokenMediaInfo {
erc721_token_id: number;
address: string;
Expand Down Expand Up @@ -168,7 +172,10 @@ export async function getMetadata(token_uri: string): Promise<{
image?: string;
animation_url?: string;
}> {
console.log(parseIPFSUri(token_uri));
if (token_uri.split(',')[0] === SUPPORT_DECODED_TOKEN_URI.BASE64) {
const base64Metadata = token_uri.split(',')[1];
return JSON.parse(fromUtf8(fromBase64(base64Metadata)));
}
const metadata = await downloadAttachment(parseIPFSUri(token_uri));
return JSON.parse(metadata.toString());
}
Expand All @@ -181,8 +188,9 @@ export async function downloadAttachment(url: string) {
maxContentLength: parseInt(MAX_CONTENT_LENGTH_BYTE, 10),
maxBodyLength: parseInt(MAX_BODY_LENGTH_BYTE, 10),
});

return axiosClient.get(url).then((response: any) => {
const fromGithub = url.includes('//github.com');
const formatedUrl = fromGithub ? `${url}?raw=true` : url;
return axiosClient.get(formatedUrl).then((response: any) => {
const buffer = Buffer.from(response.data, 'base64');
return buffer;
});
Expand All @@ -206,6 +214,9 @@ export function parseFilename(media_uri: string) {
}
return parsed.path.substring(1); // http://ipfs.io/ipfs/QmWov9DpE1vYZtTH7JLKXb7b8bJycN91rEPJEmXRXdmh2G/nerd_access_pass.gif
}
if (media_uri.includes('//github.com')) {
return parsed.path.substring(1); // https://github.com/storyprotocol/protocol-core/blob/main/assets/license-image.gif
}
}
if (media_uri.startsWith('/ipfs/')) {
return media_uri.substring(1); // /ipfs/QmPAGifcMvxDBgYr1XmEz9gZiC3DEkfYeinFdVSe364uQp/689.png
Expand Down
1 change: 1 addition & 0 deletions test/unit/services/erc721/erc721_handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default class Erc721HandlerTest {
tx_msg_id: 222,
tx_id: 223,
index: 1,
from: hexToBytes('0x51aeade652867f342ddc012e15c27d0cd6220398'),
});

erc721Contract1 = Erc721Contract.fromJson({
Expand Down
1 change: 1 addition & 0 deletions test/unit/services/evm/erc20_handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const evmTransaction = EVMTransaction.fromJson({
tx_msg_id: 4752908,
contract_address: null,
index: 0,
from: hexToBytes('0x51aeade652867f342ddc012e15c27d0cd6220398'),
});

@Describe('Test erc20 reindex')
Expand Down
1 change: 1 addition & 0 deletions test/unit/services/evm/erc20_reindex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const evmTransaction = EVMTransaction.fromJson({
tx_msg_id: 4752908,
contract_address: null,
index: 0,
from: hexToBytes('0x51aeade652867f342ddc012e15c27d0cd6220398'),
});

@Describe('Test erc20 reindex')
Expand Down

0 comments on commit d5744b0

Please sign in to comment.