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

fix: fix tests and ignore failed transfers if already executed #238

Merged
merged 2 commits into from
Dec 20, 2024
Merged
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
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ services:
restart: always
environment:
DATABASE_URL: mongodb://mongo1:30001/sygmaprotocol-explorer-indexer?replicaSet=my-replica-set&authSource=admin&retryWrites=true&w=majority
SHARED_CONFIG_URL: https://ipfs.io/ipfs/QmW9RhxFFotHtvsU2PMVYU2NNZigeCpbg1ywDCZ3vX675b
SHARED_CONFIG_URL: https://ipfs.io/ipfs/bafkreiasnla2ya55of6nwm3swjstip4q2ixfa3t6tvixyibclfovxnerte
RPC_URL_CONFIG: '[{"id": 1, "endpoint": "http://evm1:8545"}, {"id": 2, "endpoint": "http://evm2:8545"}, {"id": 3, "endpoint": "ws://substrate-pallet:9944"}]'
RETRY_COUNT: '1'
BACKOFF: '100'
Expand Down
58 changes: 7 additions & 51 deletions src/indexer/repository/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export type TransferMetadata = {
class TransferRepository {
public transfer = new PrismaClient().transfer

public async insertDepositTransfer(decodedLog: DecodedDepositLog & { usdValue: number | null }): Promise<Transfer> {
public async upsertDepositTransfer(decodedLog: DecodedDepositLog & { usdValue: number | null }): Promise<Transfer> {
const transferData = {
depositNonce: decodedLog.depositNonce,
amount: decodedLog.amount,
Expand All @@ -68,9 +68,13 @@ class TransferRepository {
id: Number(decodedLog.toDomainId),
},
},
account: {
connect: {
id: decodedLog.sender,
},
},
usdValue: decodedLog.usdValue,
}

return await this.transfer.upsert({
where: {
transferId: {
Expand All @@ -81,63 +85,15 @@ class TransferRepository {
},
update: {
...transferData,
account: {
connect: {
id: decodedLog.sender,
},
},
status: undefined,
},
create: {
id: new ObjectId().toString(),
...transferData,
account: {
connect: {
id: decodedLog.sender,
},
},
},
})
}

public async insertSubstrateDepositTransfer(
substrateDepositData: Pick<
DecodedDepositLog,
"depositNonce" | "sender" | "amount" | "destination" | "resourceID" | "toDomainId" | "fromDomainId"
> & { usdValue: number },
): Promise<Transfer> {
const transferData = {
id: new ObjectId().toString(),
depositNonce: substrateDepositData.depositNonce,
amount: substrateDepositData.amount,
destination: substrateDepositData.destination,
status: TransferStatus.pending,
message: "",
resource: {
connect: {
id: substrateDepositData.resourceID,
},
},
fromDomain: {
connect: {
id: Number(substrateDepositData.fromDomainId),
},
},
toDomain: {
connect: {
id: Number(substrateDepositData.toDomainId),
},
},
account: {
connect: {
id: substrateDepositData.sender,
},
},
usdValue: substrateDepositData.usdValue,
}

return await this.transfer.create({ data: transferData })
}

public async insertExecutionTransfer(
{ depositNonce, fromDomainId }: Pick<DecodedProposalExecutionLog, "depositNonce" | "fromDomainId">,
toDomainId: number,
Expand Down
16 changes: 4 additions & 12 deletions src/indexer/utils/evm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,6 @@ export async function saveDepositLogs(
coinMarketCapService: CoinMarketCapService,
sharedConfig: SharedConfig,
): Promise<void> {
let transfer = await transferRepository.findTransfer(decodedLog.depositNonce, Number(decodedLog.fromDomainId), Number(decodedLog.toDomainId))

const { sender, amount, fromDomainId } = decodedLog

const currentDomain = sharedConfig.domains.find(domain => domain.id == parseInt(fromDomainId))
Expand Down Expand Up @@ -319,16 +317,7 @@ export async function saveDepositLogs(
addressStatus: senderStatus,
})

if (!transfer) {
transfer = await transferRepository.insertDepositTransfer({ ...decodedLog, usdValue: amountInUSD })
} else {
const dataToSave = {
...decodedLog,
usdValue: amountInUSD,
}
await transferRepository.updateTransfer(dataToSave, transfer.id)
}

const transfer = await transferRepository.upsertDepositTransfer({ ...decodedLog, usdValue: amountInUSD })
const deposit = {
id: new ObjectId().toString(),
type: decodedLog.transferType,
Expand Down Expand Up @@ -393,6 +382,9 @@ export async function saveFailedHandlerExecutionLogs(
executionRepository: ExecutionRepository,
): Promise<void> {
let transfer = await transferRepository.findTransfer(error.depositNonce, Number(error.domainId), toDomainId)
if (transfer?.status == TransferStatus.executed) {
return
}
if (!transfer) {
transfer = await transferRepository.insertFailedTransfer(error, toDomainId)
} else {
Expand Down
80 changes: 28 additions & 52 deletions src/indexer/utils/substrate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
SubstrateTypeTransfer,
SygmaPalleteEvents,
} from "../../services/substrateIndexer/substrateTypes"
import { DecodedDepositLog } from "../../../indexer/services/evmIndexer/evmTypes"
import { Domain, SharedConfig, SubstrateResource } from "../../../indexer/config"
import { getSubstrateEvents } from "../../../indexer/services/substrateIndexer/substrateEventParser"
import AccountRepository from "../../repository/account"
Expand Down Expand Up @@ -74,6 +73,9 @@ export async function saveFailedHandlerExecution(
const numDepositNonce = Number(depositNonce.replace(/,/g, ""))

let transfer = await transferRepository.findTransfer(numDepositNonce, Number(originDomainId), toDomainId)
if (transfer?.status == TransferStatus.executed) {
return
}
// there is no transfer yet, but a proposal execution exists
if (!transfer) {
transfer = await transferRepository.insertFailedTransfer(
Expand Down Expand Up @@ -121,12 +123,10 @@ export async function saveDeposit(
} = substrateDepositData

const currentDomain = sharedConfig.domains.find(domain => domain.id === originDomainId)
const tokenSymbol = currentDomain?.resources.find(resource => resource.resourceId === resourceId)?.symbol
const resource = currentDomain?.resources.find(resource => resource.resourceId === resourceId)
const tokenSymbol = resource?.symbol
const decodedAmount = getDecodedAmount(depositData)
const numDepositNonce = Number(depositNonce.replace(/,/g, ""))

let transfer = await transferRepository.findTransfer(numDepositNonce, originDomainId, Number(destinationDomainId))

let amountInUSD

try {
Expand All @@ -136,56 +136,32 @@ export async function saveDeposit(
amountInUSD = 0
}

if (transfer) {
let dataTransferToUpdate = {
depositNonce: numDepositNonce,
amount: decodedAmount,
resourceID: resourceId,
fromDomainId: originDomainId.toString(),
toDomainId: destinationDomainId,
timestamp: timestamp,
destination: `0x${depositData.substring(2).slice(128, depositData.length - 1)}`,
} as Pick<
DecodedDepositLog,
"depositNonce" | "amount" | "destination" | "resourceID" | "toDomainId" | "fromDomainId" | "timestamp" | "sender"
> & { usdValue: number }

if (transfer.accountId !== null) {
dataTransferToUpdate = {
...dataTransferToUpdate,
sender: transfer.accountId,
}
} else {
await accountRepository.insertAccount({ id: sender, addressStatus: "" })

dataTransferToUpdate = {
...dataTransferToUpdate,
sender,
usdValue: amountInUSD,
}
}
await transferRepository.updateTransfer(dataTransferToUpdate, transfer.id)
} else {
const transferData = {
id: new ObjectId().toString(),
depositNonce: numDepositNonce,
sender,
const transferData = {
blockNumber: Number(blockNumber),
txHash: txIdentifier,
depositData,
handlerResponse,
transferType: "fungible",
fee: {
tokenSymbol: tokenSymbol!,
amount: decodedAmount,
resourceID: resourceId,
fromDomainId: `${originDomainId}`,
toDomainId: `${destinationDomainId}`,
timestamp: timestamp,
destination: `0x${depositData.substring(2).slice(128, depositData.length - 1)}`,
usdValue: amountInUSD,
} as Pick<
DecodedDepositLog,
"depositNonce" | "sender" | "amount" | "destination" | "resourceID" | "toDomainId" | "fromDomainId" | "timestamp"
> & { usdValue: number }
decimals: resource?.decimals!,
tokenAddress: resource?.address!,
},
depositNonce: numDepositNonce,
sender,
amount: decodedAmount,
resourceID: resourceId,
fromDomainId: `${originDomainId}`,
toDomainId: `${destinationDomainId}`,
timestamp: timestamp,
destination: `0x${depositData.substring(2).slice(128, depositData.length - 1)}`,
usdValue: amountInUSD,
}

await accountRepository.insertAccount({ id: sender, addressStatus: "" })
await accountRepository.insertAccount({ id: sender, addressStatus: "" })

transfer = await transferRepository.insertSubstrateDepositTransfer(transferData)
}
const transfer = await transferRepository.upsertDepositTransfer(transferData)

const deposit = {
id: new ObjectId().toString(),
Expand Down
42 changes: 21 additions & 21 deletions tests/e2e/domainAndResourceRoute.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("Get all transfers for a specific resource between source and destinati
before(async () => {
let transfers = 0
let isProcessing = false
while (transfers !== 35 || isProcessing) {
while (transfers !== 31 || isProcessing) {
const res: { data: Array<TransferResponse> } = await axios.get("http://localhost:8000/api/transfers?page=1&limit=100")

transfers = res.data.length
Expand Down Expand Up @@ -75,7 +75,7 @@ describe("Get all transfers for a specific resource between source and destinati
fromDomainId: 1,
toDomainId: 2,
destination: "0x8e0a907331554af72563bd8d43051c2e64be5d35",
amount: "2296080355773541392",
amount: "2935717020161974584",
status: "executed",
accountId: "0x5C1F5961696BaD2e73f73417f07EF55C62a2dC5b",
message: "",
Expand All @@ -90,22 +90,22 @@ describe("Get all transfers for a specific resource between source and destinati
id: transfers[0].fee.id,
transferId: transfers[0].id,
decimals: 18,
amount: "1000000000000000",
amount: "100000000000000",
tokenAddress: "0x0000000000000000000000000000000000000000",
tokenSymbol: "eth",
},
deposit: {
txHash: "0x7b7c2be6b60c25a1be9f506fdd75e1aab76d3016f0bc708715405f2e6718c6df",
blockNumber: "591",
txHash: "0x12327002087fe09d30a4bd45e97e55549d92dbf05d254788591dc2b6bca4ef0f",
blockNumber: "130",
depositData:
"0x0000000000000000000000000000000000000000000000001fdd50eb1da26c1000000000000000000000000000000000000000000000000000000000000000148e0a907331554af72563bd8d43051c2e64be5d35000000000000000000000000000000000000000000000000000000000000000c6d657461646174612e75726c",
"0x00000000000000000000000000000000000000000000000028bdc31363d2a13800000000000000000000000000000000000000000000000000000000000000148e0a907331554af72563bd8d43051c2e64be5d35000000000000000000000000000000000000000000000000000000000000000c6d657461646174612e75726c",
handlerResponse: "0x6d657461646174612e746573746d657461646174612e75726c",
timestamp: "2023-07-17T08:31:22.000Z",
timestamp: "2024-11-14T08:17:11.000Z",
},
execution: {
txHash: "0x3de2201e548a8332aaa50147a2fb02e2b6669184f042b4dbcf23b4f5d40edcfb",
blockNumber: "598",
timestamp: "2023-07-17T08:31:35.000Z",
txHash: "0x43c02a7cee493621c550e059489db14500b5a388185d61deeb7d9a7f52959e8d",
blockNumber: "138",
timestamp: "2024-11-14T08:17:31.000Z",
},
account: { addressStatus: "" },
},
Expand All @@ -124,11 +124,11 @@ describe("Get all transfers for a specific resource between source and destinati
expect(transfers).to.be.deep.equal([
{
id: transfers[0].id,
depositNonce: 29,
depositNonce: 28,
resourceID: "0x0000000000000000000000000000000000000000000000000000000000000500",
fromDomainId: 1,
toDomainId: 2,
destination: "0xb1387b365ae7294ea13bad9db83436e671dd16ba",
destination: "0xa2451c8553371e754f5e93a440adcca1c0dcf395",
amount: "",
status: "executed",
accountId: "0x5C1F5961696BaD2e73f73417f07EF55C62a2dC5b",
Expand All @@ -141,25 +141,25 @@ describe("Get all transfers for a specific resource between source and destinati
toDomain: { name: "evm2", lastIndexedBlock: transfers[0].toDomain.lastIndexedBlock, id: 2 },
fromDomain: { name: "Ethereum 1", lastIndexedBlock: transfers[0].fromDomain.lastIndexedBlock, id: 1 },
fee: {
amount: "1000000000000000",
amount: "100000000000000",
id: transfers[0].fee.id,
decimals: 18,
tokenAddress: "0x0000000000000000000000000000000000000000",
tokenSymbol: "eth",
transferId: transfers[0].id,
},
deposit: {
txHash: "0x18fa527a4773789a5ba487dae5bc3d00cc04dc50509b6f67e438efdb60e75c67",
blockNumber: "623",
txHash: "0x2b355542a454d8faedccc75c8741ef0d2f531ea4cd8ed53544734ff681377699",
blockNumber: "155",
depositData:
"0x0000000000000000000000000000000000000000000000000000000000030d400004ea287d1514b1387b365ae7294ea13bad9db83436e671dd16ba145c1f5961696bad2e73f73417f07ef55c62a2dc5b47ed248f568cc8f9fe4371a1d1fab88a62af595f8efb9aeff6f0e043b7ea33b10000000000000000000000005c1f5961696bad2e73f73417f07ef55c62a2dc5b",
"0x00000000000000000000000000000000000000000000000000000000000927c00004ea287d1514a2451c8553371e754f5e93a440adcca1c0dcf395145c1f5961696bad2e73f73417f07ef55c62a2dc5b35353436383833363939383137363233353732000000000000000000000000000000000000000000000000005c1f5961696bad2e73f73417f07ef55c62a2dc5b",
handlerResponse: "0x",
timestamp: "2023-07-17T08:32:27.000Z",
timestamp: "2024-11-14T08:18:03.000Z",
},
execution: {
txHash: "0xcc7c318cfd71745c27111772f21dec553f53277c9dc218fe07b54f897560c0cb",
blockNumber: "631",
timestamp: "2023-07-17T08:32:42.000Z",
txHash: "0x508195d23128b60c20a577eca7ace567e6ec68f636bad42ddb554b7d96644dd3",
blockNumber: "162",
timestamp: "2024-11-14T08:18:20.000Z",
},
account: { addressStatus: "" },
},
Expand All @@ -173,7 +173,7 @@ describe("Get all transfers for a specific resource between source and destinati
const transfers = res.data as Array<TransferResponse>

expect(res.status).to.be.deep.equal(200)
expect(transfers.length).to.be.deep.equal(3)
expect(transfers.length).to.be.deep.equal(1)

for (const transfer of transfers) {
expect(transfer.resourceID).to.be.deep.equal(ERC20LRTEST_RESOURCE_ID)
Expand Down
Loading
Loading