Skip to content

Commit

Permalink
feat: Add finishTime in transfers to Ethereum.
Browse files Browse the repository at this point in the history
  • Loading branch information
paouvrard committed Dec 21, 2021
1 parent 95461a6 commit b5dbf2a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface TransferDraft extends TransferStatus {
export interface Transfer extends TransferDraft, TransactionInfo {
id: string
startTime: string
finishTime?: string
decimals: number
destinationTokenName: string
recipient: string
Expand Down Expand Up @@ -780,7 +781,7 @@ export async function checkSync (
options.erc20LockerAbi ?? bridgeParams.erc20LockerAbi
)) {
try {
const finalizationTxHash = await findFinalizationTxOnEthereum({
const { transactions, block } = await findFinalizationTxOnEthereum({
usedProofPosition: '3',
proof,
connectorAddress: options.erc20LockerAddress ?? bridgeParams.erc20LockerAddress,
Expand All @@ -792,7 +793,8 @@ export async function checkSync (
})
transfer = {
...transfer,
unlockHashes: [...transfer.unlockHashes, ...finalizationTxHash]
finishTime: new Date(block.timestamp * 1000).toISOString(),
unlockHashes: [...transfer.unlockHashes, ...transactions]
}
} catch (error) {
// Not finding the finalization tx should not prevent processing/recovering the transfer.
Expand Down Expand Up @@ -953,10 +955,13 @@ export async function checkUnlock (
}
}

const block = await provider.getBlock(unlockReceipt.blockNumber)

return {
...transfer,
status: status.COMPLETE,
completedStep: UNLOCK,
finishTime: new Date(block.timestamp * 1000).toISOString(),
unlockReceipts: [...transfer.unlockReceipts, unlockReceipt]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface TransferDraft extends TransferStatus {
export interface Transfer extends TransferDraft, TransactionInfo {
id: string
startTime: string
finishTime?: string
decimals: number
destinationTokenName: string
recipient: string
Expand Down Expand Up @@ -740,7 +741,7 @@ export async function checkSync (
options.etherCustodianAbi ?? bridgeParams.etherCustodianAbi
)) {
try {
const finalizationTxHash = await findFinalizationTxOnEthereum({
const { transactions, block } = await findFinalizationTxOnEthereum({
usedProofPosition: '3',
proof,
connectorAddress: options.etherCustodianAddress ?? bridgeParams.etherCustodianAddress,
Expand All @@ -752,7 +753,8 @@ export async function checkSync (
})
transfer = {
...transfer,
unlockHashes: [...transfer.unlockHashes, ...finalizationTxHash]
finishTime: new Date(block.timestamp * 1000).toISOString(),
unlockHashes: [...transfer.unlockHashes, ...transactions]
}
} catch (error) {
// Not finding the finalization tx should not prevent processing/recovering the transfer.
Expand Down Expand Up @@ -913,10 +915,13 @@ export async function checkUnlock (
}
}

const block = await provider.getBlock(unlockReceipt.blockNumber)

return {
...transfer,
status: status.COMPLETE,
completedStep: UNLOCK,
finishTime: new Date(block.timestamp * 1000).toISOString(),
unlockReceipts: [...transfer.unlockReceipts, unlockReceipt]
}
}
Expand Down
9 changes: 7 additions & 2 deletions packages/near-ether/src/bridged-ether/sendToEthereum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface TransferDraft extends TransferStatus {
export interface Transfer extends TransferDraft, TransactionInfo {
id: string
startTime: string
finishTime?: string
decimals: number
destinationTokenName: string
recipient: string
Expand Down Expand Up @@ -790,7 +791,7 @@ export async function checkSync (
options.etherCustodianAbi ?? bridgeParams.etherCustodianAbi
)) {
try {
const finalizationTxHash = await findFinalizationTxOnEthereum({
const { transactions, block } = await findFinalizationTxOnEthereum({
usedProofPosition: '3',
proof,
connectorAddress: options.etherCustodianAddress ?? bridgeParams.etherCustodianAddress,
Expand All @@ -802,7 +803,8 @@ export async function checkSync (
})
transfer = {
...transfer,
unlockHashes: [...transfer.unlockHashes, ...finalizationTxHash]
finishTime: new Date(block.timestamp * 1000).toISOString(),
unlockHashes: [...transfer.unlockHashes, ...transactions]
}
} catch (error) {
// Not finding the finalization tx should not prevent processing/recovering the transfer.
Expand Down Expand Up @@ -963,10 +965,13 @@ export async function checkUnlock (
}
}

const block = await provider.getBlock(unlockReceipt.blockNumber)

return {
...transfer,
status: status.COMPLETE,
completedStep: UNLOCK,
finishTime: new Date(block.timestamp * 1000).toISOString(),
unlockReceipts: [...transfer.unlockReceipts, unlockReceipt]
}
}
Expand Down
9 changes: 7 additions & 2 deletions packages/near-ether/src/natural-near/sendToEthereum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface TransferDraft extends TransferStatus {
export interface Transfer extends TransferDraft, TransactionInfo {
id: string
startTime: string
finishTime?: string
decimals: number
destinationTokenName: string
recipient: string
Expand Down Expand Up @@ -775,7 +776,7 @@ export async function checkSync (
options.eNEARAbi ?? bridgeParams.eNEARAbi
)) {
try {
const finalizationTxHash = await findFinalizationTxOnEthereum({
const { transactions, block } = await findFinalizationTxOnEthereum({
usedProofPosition: '8',
proof,
connectorAddress: options.eNEARAddress ?? bridgeParams.eNEARAddress,
Expand All @@ -787,7 +788,8 @@ export async function checkSync (
})
transfer = {
...transfer,
mintHashes: [...transfer.mintHashes, ...finalizationTxHash]
finishTime: new Date(block.timestamp * 1000).toISOString(),
mintHashes: [...transfer.mintHashes, ...transactions]
}
} catch (error) {
// Not finding the finalization tx should not prevent processing/recovering the transfer.
Expand Down Expand Up @@ -951,10 +953,13 @@ export async function checkMint (
}
}

const block = await provider.getBlock(mintReceipt.blockNumber)

return {
...transfer,
status: status.COMPLETE,
completedStep: MINT,
finishTime: new Date(block.timestamp * 1000).toISOString(),
mintReceipts: [...transfer.mintReceipts, mintReceipt]
}
}
Expand Down
16 changes: 9 additions & 7 deletions packages/nep141-erc20/src/bridged-nep141/sendToEthereum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface TransferDraft extends TransferStatus {
export interface Transfer extends TransferDraft, TransactionInfo {
id: string
startTime: string
finishTime?: string
decimals: number
destinationTokenName: string
recipient: string
Expand Down Expand Up @@ -790,7 +791,7 @@ export async function checkSync (
options.erc20LockerAbi ?? bridgeParams.erc20LockerAbi
)) {
try {
const finalizationTxHash = await findFinalizationTxOnEthereum({
const { transactions, block } = await findFinalizationTxOnEthereum({
usedProofPosition: '3',
proof,
connectorAddress: options.erc20LockerAddress ?? bridgeParams.erc20LockerAddress,
Expand All @@ -802,7 +803,8 @@ export async function checkSync (
})
transfer = {
...transfer,
unlockHashes: [...transfer.unlockHashes, ...finalizationTxHash]
finishTime: new Date(block.timestamp * 1000).toISOString(),
unlockHashes: [...transfer.unlockHashes, ...transactions]
}
} catch (error) {
// Not finding the finalization tx should not prevent processing/recovering the transfer.
Expand Down Expand Up @@ -957,19 +959,19 @@ export async function checkUnlock (

if (unlockReceipt.transactionHash !== unlockHash) {
// Record the replacement tx unlockHash
return {
transfer = {
...transfer,
status: status.COMPLETE,
completedStep: UNLOCK,
unlockHashes: [...transfer.unlockHashes, unlockReceipt.transactionHash],
unlockReceipts: [...transfer.unlockReceipts, unlockReceipt]
unlockHashes: [...transfer.unlockHashes, unlockReceipt.transactionHash]
}
}

const block = await provider.getBlock(unlockReceipt.blockNumber)

return {
...transfer,
status: status.COMPLETE,
completedStep: UNLOCK,
finishTime: new Date(block.timestamp * 1000).toISOString(),
unlockReceipts: [...transfer.unlockReceipts, unlockReceipt]
}
}
Expand Down
9 changes: 7 additions & 2 deletions packages/utils/src/findFinalizationTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function findFinalizationTxOnEthereum (
amount: string
provider: ethers.providers.Provider
}
): Promise<string[]> {
): Promise<{ transactions: string[], block: ethers.providers.Block }> {
const usedProofsMappingPosition = '0'.repeat(63) + usedProofPosition
const usedProofsKey: string = bs58.decode(proof.outcome_proof.outcome.receipt_ids[0]!).toString('hex')
const proofStorageIndex = ethers.utils.keccak256('0x' + usedProofsKey + usedProofsMappingPosition)
Expand All @@ -49,7 +49,12 @@ export async function findFinalizationTxOnEthereum (
// NOTE: Depending on the connector, event args are in different order or not indexed, so query all and filter.
const filter = connectorContract.filters[finalizationEvent]!()
const events = await connectorContract.queryFilter(filter, minBlock, minBlock)
return events.filter(e =>
const transactions = events.filter(e =>
e.args!.recipient.toLowerCase() === recipient.toLowerCase() && e.args!.amount.toString() === amount
).map(e => e.transactionHash)
if (transactions.length === 0) {
throw new SearchError(`Finalization tx on Ethereum not found for proof: ${JSON.stringify(proof)}`)
}
const block = await provider.getBlock(minBlock)
return { transactions, block }
}

0 comments on commit b5dbf2a

Please sign in to comment.