@@ -185,39 +185,36 @@ export class PgStore extends BasePgStore {
185
185
...l ,
186
186
timestamp : sql `TO_TIMESTAMP(${ l . timestamp } )` ,
187
187
} ) ) ;
188
+ // Insert locations, figure out moved inscriptions, insert inscription transfers.
188
189
for await ( const batch of batchIterate ( entries , INSERT_BATCH_SIZE ) )
189
190
await sql `
190
- INSERT INTO locations ${ sql ( batch ) }
191
- ON CONFLICT (ordinal_number, block_height, tx_index) DO NOTHING
192
- ` ;
193
- // Insert block transfers.
194
- let block_transfer_index = 0 ;
195
- const transferEntries = [ ] ;
196
- for ( const transfer of cache . locations ) {
197
- const transferred = await sql < { genesis_id : string ; number : string } [ ] > `
198
- SELECT genesis_id, number FROM inscriptions
199
- WHERE ordinal_number = ${ transfer . ordinal_number } AND (
200
- block_height < ${ transfer . block_height }
201
- OR (block_height = ${ transfer . block_height } AND tx_index < ${ transfer . tx_index } )
191
+ WITH location_inserts AS (
192
+ INSERT INTO locations ${ sql ( batch ) }
193
+ ON CONFLICT (ordinal_number, block_height, tx_index) DO NOTHING
194
+ RETURNING ordinal_number, block_height, block_hash, tx_index
195
+ ),
196
+ prev_transfer_index AS (
197
+ SELECT MAX(block_transfer_index) AS max
198
+ FROM inscription_transfers
199
+ WHERE block_height = (SELECT block_height FROM location_inserts LIMIT 1)
200
+ ),
201
+ moved_inscriptions AS (
202
+ SELECT
203
+ i.genesis_id, i.number, i.ordinal_number, li.block_height, li.block_hash, li.tx_index,
204
+ (
205
+ ROW_NUMBER() OVER (ORDER BY li.tx_index ASC) + (SELECT COALESCE(max, -1) FROM prev_transfer_index)
206
+ ) AS block_transfer_index
207
+ FROM inscriptions AS i
208
+ INNER JOIN location_inserts AS li ON li.ordinal_number = i.ordinal_number
209
+ WHERE
210
+ i.block_height < li.block_height
211
+ OR (i.block_height = li.block_height AND i.tx_index < li.tx_index)
202
212
)
213
+ INSERT INTO inscription_transfers
214
+ (genesis_id, number, ordinal_number, block_height, block_hash, tx_index, block_transfer_index)
215
+ (SELECT * FROM moved_inscriptions)
216
+ ON CONFLICT (block_height, block_transfer_index) DO NOTHING
203
217
` ;
204
- for ( const inscription of transferred )
205
- transferEntries . push ( {
206
- genesis_id : inscription . genesis_id ,
207
- number : inscription . number ,
208
- ordinal_number : transfer . ordinal_number ,
209
- block_height : transfer . block_height ,
210
- block_hash : transfer . block_hash ,
211
- tx_index : transfer . tx_index ,
212
- block_transfer_index : block_transfer_index ++ ,
213
- } ) ;
214
- }
215
- if ( transferEntries . length )
216
- for await ( const batch of batchIterate ( transferEntries , INSERT_BATCH_SIZE ) )
217
- await sql `
218
- INSERT INTO inscription_transfers ${ sql ( batch ) }
219
- ON CONFLICT (block_height, block_transfer_index) DO NOTHING
220
- ` ;
221
218
}
222
219
if ( cache . recursiveRefs . size )
223
220
for ( const [ genesis_id , refs ] of cache . recursiveRefs ) {
0 commit comments