diff --git a/migrations/20230915032739_cw721_activity_update_owner_for_each.ts b/migrations/20230915032739_cw721_activity_update_owner_for_each.ts index 8743bc36d..d453a61af 100644 --- a/migrations/20230915032739_cw721_activity_update_owner_for_each.ts +++ b/migrations/20230915032739_cw721_activity_update_owner_for_each.ts @@ -8,37 +8,46 @@ export async function up(knex: Knex): Promise { sender: knex.ref('from'), }); await knex.transaction(async (trx) => { - const activities = await CW721Activity.query() - .joinRelated('event') - .orderBy('event.id', 'asc') - .transacting(trx); + let currentId = 0; const latestOwners: Dictionary = {}; - activities.forEach((activity) => { - const latestOwner = - latestOwners[ - activity.cw721_contract_id + '_' + activity.cw721_token_id - ]; - if (latestOwner) { - activity.from = latestOwner; + while (true) { + const activities = await CW721Activity.query() + .transacting(trx) + .joinRelated('event') + .where('event.id', '>', currentId) + .orderBy('event.id', 'asc') + .limit(1000) + .select('cw721_activity.*', 'event.id as event_id'); + activities.forEach((activity) => { + const latestOwner = + latestOwners[ + activity.cw721_contract_id + '_' + activity.cw721_token_id + ]; + if (latestOwner) { + activity.from = latestOwner; + } else { + activity.from = null; + } + if ( + activity.action === CW721_ACTION.MINT || + activity.action === CW721_ACTION.TRANSFER || + activity.action === CW721_ACTION.SEND_NFT + ) { + latestOwners[ + activity.cw721_contract_id + '_' + activity.cw721_token_id + ] = activity.to; + } + }); + if (activities.length > 0) { + await CW721Activity.query() + .insert(activities.map((activity) => _.omit(activity, 'event_id'))) + .onConflict(['id']) + .merge() + .transacting(trx); + currentId = activities[activities.length - 1].event_id; } else { - activity.from = null; + break; } - if ( - activity.action === CW721_ACTION.MINT || - activity.action === CW721_ACTION.TRANSFER || - activity.action === CW721_ACTION.SEND_NFT - ) { - latestOwners[ - activity.cw721_contract_id + '_' + activity.cw721_token_id - ] = activity.to; - } - }); - if (activities.length > 0) { - await CW721Activity.query() - .insert(activities) - .onConflict(['id']) - .merge() - .transacting(trx); } }); } diff --git a/src/services/cw721/cw721.service.ts b/src/services/cw721/cw721.service.ts index 0dd28ee89..681f71bee 100644 --- a/src/services/cw721/cw721.service.ts +++ b/src/services/cw721/cw721.service.ts @@ -188,7 +188,7 @@ export default class Cw721HandlerService extends BullableService { getAttributeFrom( msg.attributes, EventAttribute.ATTRIBUTE_KEY.TOKEN_ID - ), + ) || null, ]) ), (o) => `${o.contract.smart_contract.address}_${o.token_id}`