-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathvalidateOrders.ts
149 lines (133 loc) · 4.3 KB
/
validateOrders.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import {
JsonRpcApiProvider,
Contract,
Interface,
TransactionReceipt,
Signer
} from 'ethers'
import { fetchEventFromTransaction } from '../../../utils/util.js'
import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20TemplateEnterprise.sol/ERC20TemplateEnterprise.json' assert { type: 'json' }
import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json' assert { type: 'json' }
import { CORE_LOGGER } from '../../../utils/logging/common.js'
import { EVENTS } from '../../../utils/index.js'
interface ValidateTransactionResponse {
isValid: boolean
message: string
}
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
export async function fetchTransactionReceipt(
txId: string,
provider: JsonRpcApiProvider,
retries: number = 2
): Promise<TransactionReceipt> {
while (retries > 0) {
try {
const txReceipt = await provider.getTransactionReceipt(txId)
if (txReceipt) {
return txReceipt
}
if (retries > 1) {
// If it's not the last retry, sleep before the next retry
await sleep(1000)
}
retries--
} catch (error) {
const errorMsg = `Error fetching transaction receipt: ${error}`
CORE_LOGGER.logMessage(errorMsg)
return null
}
}
}
export async function validateOrderTransaction(
txId: string,
userAddress: string,
provider: JsonRpcApiProvider,
dataNftAddress: string,
datatokenAddress: string,
serviceIndex: number,
serviceTimeout: number,
signer: Signer
): Promise<ValidateTransactionResponse> {
const contractInterface = new Interface(ERC20Template.abi)
let txReceiptMined = await fetchTransactionReceipt(txId, provider)
if (!txReceiptMined) {
const errorMsg = `Tx receipt cannot be processed, because tx id ${txId} was not mined.`
CORE_LOGGER.logMessage(errorMsg)
return {
isValid: false,
message: errorMsg
}
}
const erc20Address = txReceiptMined.to
const datatokenContract = new Contract(erc20Address, ERC20Template.abi, signer)
const erc721Address = await datatokenContract.getERC721Address()
const orderReusedEvent = fetchEventFromTransaction(
txReceiptMined,
EVENTS.ORDER_REUSED,
contractInterface
)
if (orderReusedEvent && orderReusedEvent?.length > 0) {
const reusedTxId = orderReusedEvent[0].args[0]
txReceiptMined = await fetchTransactionReceipt(reusedTxId, provider)
if (!txReceiptMined) {
const errorMsg = `Tx receipt cannot be processed, because tx id ${txId} was not mined.`
CORE_LOGGER.logMessage(errorMsg)
return {
isValid: false,
message: errorMsg
}
}
}
const OrderStartedEvent = fetchEventFromTransaction(
txReceiptMined,
EVENTS.ORDER_STARTED,
contractInterface
)
let orderEvent
for (const event of OrderStartedEvent) {
if (
(userAddress?.toLowerCase() === event.args[0]?.toLowerCase() ||
userAddress?.toLowerCase() === event.args[1]?.toLowerCase()) &&
erc20Address?.toLowerCase() === datatokenAddress?.toLowerCase() &&
erc721Address?.toLowerCase() === dataNftAddress?.toLowerCase()
) {
orderEvent = event
break
}
}
if (!orderEvent) {
return {
isValid: false,
message:
'Tx id used not valid, one of the NFT addresses, Datatoken address or the User address contract address does not match.'
}
}
const eventServiceIndex = orderEvent.args[3]
if (BigInt(serviceIndex) !== eventServiceIndex) {
return {
isValid: false,
message: 'Invalid service index.'
}
}
const ERC721Contract = new Contract(dataNftAddress, ERC721Template.abi, provider)
const isDatatokenDeployed = await ERC721Contract.isDeployed(datatokenAddress)
if (!isDatatokenDeployed) {
return {
isValid: false,
message: 'Datatoken was not deployed by this DataNFT.'
}
}
const eventTimestamp = (await provider.getBlock(txReceiptMined.blockHash)).timestamp
const currentTimestamp = Math.floor(Date.now() / 1000)
const timeElapsed = currentTimestamp - eventTimestamp
if (serviceTimeout !== 0 && timeElapsed > serviceTimeout) {
return {
isValid: false,
message: 'The order has expired.'
}
}
return {
isValid: true,
message: 'Transaction is valid.'
}
}