Skip to content

Commit

Permalink
Merge pull request #8 from terraswap/fix/sorted-transfer
Browse files Browse the repository at this point in the history
fix: sorted transfer
  • Loading branch information
jbamlee authored Jun 8, 2023
2 parents e92338d + 866b49e commit e3944af
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ TERRASWAP_FACTORY=

DB_HOST=
DB_PORT=
DB_USER=
DB_USERNAME=
DB_PASSWORD=
DB_NAME=

Expand Down
22 changes: 19 additions & 3 deletions src/collector/indexer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@ import { TxHistoryIndexer } from './txHistoryIndexer'
import { NativeTransferIndexer, NonnativeTransferIndexer } from './transferIndexer'
import { factoryAddress } from 'lib/terraswap/'
import logRules from '../log-finder/log-rules'
import { EventKV } from '@terra-money/terra.js'


const createPairLF = createCreatePairLogFinders(factoryAddress)
const nativeTransferLF = createNativeTransferLogFinders()
const nonnativeTransferLF = createNonnativeTransferLogFinder()

function sortNativeTransferAttributes(attrs: EventKV[]): EventKV[] {
let startIdx = 0
const splitEvery = 3
const sorted: EventKV[] = []

while (startIdx < attrs.length) {
sorted.push(...(attrs.slice(startIdx, startIdx + splitEvery).sort((a, b) => a.key < b.key ? -1 : 1)))
startIdx += splitEvery
}
return sorted
}

export async function runIndexers(
manager: EntityManager,
txs: Tx[],
Expand Down Expand Up @@ -41,10 +54,13 @@ export async function runIndexers(

spwfLogFounds.length > 0 && await TxHistoryIndexer(manager, exchangeRate, timestamp, txHash, spwfLogFounds)

// native transfer
const nativeTransferLogFounds = nativeTransferLF(event)
if (event.type === "transfer") {
event.attributes = sortNativeTransferAttributes(event.attributes)
// native transfer
const nativeTransferLogFounds = nativeTransferLF(event)

await NativeTransferIndexer(pairList, manager, exchangeRate, timestamp, nativeTransferLogFounds)
await NativeTransferIndexer(pairList, manager, exchangeRate, timestamp, nativeTransferLogFounds)
}

// nonnative transfer
const nonnativeTransferLogFounds = nonnativeTransferLF(event)
Expand Down
18 changes: 9 additions & 9 deletions src/collector/log-finder/log-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ function nonnativeTransferRuleFromV2(): LogFinderRule {
}
}

function nativeTransferRuleV2(): LogFinderRule {
function sortedNativeTransferRuleFinderRule(): LogFinderRule {
return {
type: 'transfer',
attributes: [['recipient'], ['sender'], ['amount']],
attributes: [['amount'], ['recipient'], ['sender']],
}
}

Expand All @@ -73,7 +73,7 @@ const phoenix = {
spwRule: spwRuleV2,
nonnativeTransferRule: nonnativeTransferRuleV2,
nonnativeTransferRuleFrom: nonnativeTransferRuleFromV2,
nativeTransferRule: nativeTransferRuleV2,
nativeTransferRule: sortedNativeTransferRuleFinderRule,
}


Expand Down Expand Up @@ -137,10 +137,10 @@ function nonnativeTransferRuleFrom(): LogFinderRule {
}
}

function nativeTransferRule(): LogFinderRule {
function sortedNativeTransferRule(): LogFinderRule {
return {
type: 'transfer',
attributes: [['recipient'], ['sender'], ['amount']],
attributes: [['amount'], ['recipient'], ['sender']],
}
}

Expand All @@ -149,7 +149,7 @@ const classic = {
spwRule: spwRule,
nonnativeTransferRule: nonnativeTransferRule,
nonnativeTransferRuleFrom: nonnativeTransferRuleFrom,
nativeTransferRule: nativeTransferRule,
nativeTransferRule: sortedNativeTransferRule,
}


Expand Down Expand Up @@ -213,10 +213,10 @@ export function col4NonnativeTransferRuleFrom(): LogFinderRule {
}
}

export function col4NativeTransferRule(): LogFinderRule {
export function col4SortedNativeTransferRule(): LogFinderRule {
return {
type: 'transfer',
attributes: [['recipient'], ['sender'], ['amount']],
attributes: [['amount'], ['recipient'], ['sender']],
}
}

Expand All @@ -226,7 +226,7 @@ const columbus4 = {
spwRule: col4SpwRule,
nonnativeTransferRule: col4NonnativeTransferRule,
nonnativeTransferRuleFrom: col4NonnativeTransferRuleFrom,
nativeTransferRule: col4NativeTransferRule,
nativeTransferRule: col4SortedNativeTransferRule,
}

const target = process.env.TERRA_CHAIN_ID?.includes("phoenix") ? phoenix : process.env.TERRA_CHAIN_ID?.includes("columbus-4") ? columbus4 : classic
Expand Down
25 changes: 21 additions & 4 deletions src/collector/log-finder/nativeTransferLF.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,31 @@ export function createNativeTransferLogFinders(): ReturningLogFinderMapper<
NativeTransferTransformed[]
> {
return createReturningLogFinder(logRules.nativeTransferRule(), (_, match) => {
if (!match[2].value) {
let amountString = ''
let sender = ''
let recipient = ''

match.forEach(m => {
if (m.key === 'amount') {
amountString = m.value
}
if (m.key === 'sender') {
sender = m.value
}
if (m.key === 'recipient') {
recipient = m.value
}
})

if (!amountString) {
return
}
const assetsInfo = trimAssets(match[2].value, true)

const assetsInfo = trimAssets(amountString, true)
return assetsInfo.map((asset) => {
return {
recipient: match[0].value,
sender: match[1].value,
recipient,
sender,
assets: asset,
}
})
Expand Down

0 comments on commit e3944af

Please sign in to comment.