Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: erc721 contract total supply #897

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions migrations/evm/20240905034826_erc721_contract_add_total_supply.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Knex } from 'knex';
import { Erc721Token } from '../../src/models';

export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('erc721_contract', (table) => {
table.integer('total_supply').defaultTo(0).index();
});
await knex.raw(`set statement_timeout to 600000`);
const totalSupplies = await Erc721Token.query(knex)
.select('erc721_token.erc721_contract_address')
.count()
.groupBy('erc721_token.erc721_contract_address');
if (totalSupplies.length > 0) {
const stringListUpdates = totalSupplies
.map(
(totalSuply) =>
`('${totalSuply.erc721_contract_address}', ${totalSuply.count})`
)
.join(',');
await knex.raw(
`UPDATE erc721_contract SET total_supply = temp.total_supply from (VALUES ${stringListUpdates}) as temp(address, total_supply) where temp.address = erc721_contract.address`
);
}
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('erc721_contract', (table) => {
table.dropColumn('total_supply');
});
}
2 changes: 2 additions & 0 deletions src/models/erc721_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export class Erc721Contract extends BaseModel {

last_updated_height!: number;

total_supply!: number;

static get tableName() {
return 'erc721_contract';
}
Expand Down
16 changes: 15 additions & 1 deletion src/services/evm/erc721.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ export default class Erc721Service extends BullableService {
);
await this.handleMissingErc721Contract(erc721Activities, trx);
if (erc721Activities.length > 0) {
const erc721Contracts = _.keyBy(
await Erc721Contract.query()
.whereIn(
'address',
erc721Activities.map((e) => e.erc721_contract_address)
)
.transacting(trx),
'address'
);
const erc721Tokens = _.keyBy(
await Erc721Token.query()
.whereIn(
Expand All @@ -108,9 +117,14 @@ export default class Erc721Service extends BullableService {
.transacting(trx),
(o) => `${o.erc721_contract_address}_${o.token_id}`
);
const erc721Handler = new Erc721Handler(erc721Tokens, erc721Activities);
const erc721Handler = new Erc721Handler(
erc721Contracts,
erc721Tokens,
erc721Activities
);
erc721Handler.process();
await Erc721Handler.updateErc721(
Object.values(erc721Handler.erc721Contracts),
erc721Activities,
Object.values(erc721Handler.erc721Tokens),
trx
Expand Down
24 changes: 24 additions & 0 deletions src/services/evm/erc721_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,20 @@ export const ERC721_ACTION = {
APPROVAL_FOR_ALL: 'approval_for_all',
};
export class Erc721Handler {
erc721Contracts: Dictionary<Erc721Contract>;

// key: {contract_address}_{token_id}
// value: erc721 token
erc721Tokens: Dictionary<Erc721Token>;

erc721Activities: Erc721Activity[];

constructor(
erc721Contracts: Dictionary<Erc721Contract>,
erc721Tokens: Dictionary<Erc721Token>,
erc721Activities: Erc721Activity[]
) {
this.erc721Contracts = erc721Contracts;
this.erc721Tokens = erc721Tokens;
this.erc721Activities = erc721Activities;
}
Expand All @@ -91,10 +95,15 @@ export class Erc721Handler {
this.erc721Tokens[
`${erc721Activity.erc721_contract_address}_${erc721Activity.token_id}`
];
const erc721Contract =
this.erc721Contracts[`${erc721Activity.erc721_contract_address}`];
if (token) {
// update new owner and last updated height
token.owner = erc721Activity.to;
token.last_updated_height = erc721Activity.height;
if (erc721Activity.to === ZERO_ADDRESS) {
erc721Contract.total_supply -= 1;
}
} else if (erc721Activity.from === ZERO_ADDRESS) {
// handle mint
this.erc721Tokens[
Expand All @@ -106,6 +115,7 @@ export class Erc721Handler {
last_updated_height: erc721Activity.height,
burned: false,
});
erc721Contract.total_supply += 1;
} else {
throw new Error('Handle erc721 tranfer error');
}
Expand Down Expand Up @@ -266,10 +276,24 @@ export class Erc721Handler {
}

static async updateErc721(
erc721Contracts: Erc721Contract[],
erc721Activities: Erc721Activity[],
erc721Tokens: Erc721Token[],
trx: Knex.Transaction
) {
if (erc721Contracts.length > 0) {
const stringListUpdates = erc721Contracts
.map(
(erc721Contract) =>
`('${erc721Contract.address}', ${erc721Contract.total_supply})`
)
.join(',');
await knex
.raw(
`UPDATE erc721_contract SET total_supply = temp.total_supply from (VALUES ${stringListUpdates}) as temp(address, total_supply) where temp.address = erc721_contract.address`
)
.transacting(trx);
}
let updatedTokens: Dictionary<Erc721Token> = {};
if (erc721Tokens.length > 0) {
updatedTokens = _.keyBy(
Expand Down
11 changes: 9 additions & 2 deletions src/services/evm/erc721_reindex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ export class Erc721Reindexer {
contract.read.name().catch(() => Promise.resolve(undefined)),
contract.read.symbol().catch(() => Promise.resolve(undefined)),
]);
await Erc721Contract.query()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const newErc721Contract: Erc721Contract[] = await Erc721Contract.query()
.insert(
Erc721Contract.fromJson({
evm_smart_contract_id: erc721Contract.evm_smart_contract_id,
Expand All @@ -120,7 +122,12 @@ export class Erc721Reindexer {
[address],
trx
);
await Erc721Handler.updateErc721(activities, tokens, trx);
await Erc721Handler.updateErc721(
newErc721Contract,
activities,
tokens,
trx
);
});
const erc721Stats = await Erc721Handler.calErc721Stats([address]);
if (erc721Stats.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ export default class TestErc721MediaService {
expect(parsedIpfsPath).toEqual(`${IPFS_GATEWAY}${path}`);
}

/**
* @description test parseFilename function
* @input ipfs uri
* @result media file url which store at Aura
*/
@Test('Test parseFilename function')
public async testParseFilename() {
const host = 'ipfs';
Expand Down
Loading
Loading