-
Notifications
You must be signed in to change notification settings - Fork 6
feat: implement ERC20CommerceEscrowWrapper with event handling and schema updates #112
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
758c5bf
feat: implement ERC20CommerceEscrowWrapper with event handling and sc…
rodrigopavezi 3545f34
Update src/erc20CommerceEscrowWrapper.ts
rodrigopavezi b4d2a88
fix: remove duplicate variable declaration in handlePaymentCharged fu…
rodrigopavezi 10d21dc
chore: update @requestnetwork/smart-contracts to version 0.49.0 and a…
rodrigopavezi 1ad5550
fix: update generateCommerceEscrowEventId function to use Bytes.fromU…
rodrigopavezi ffffcbe
Update src/shared.ts
rodrigopavezi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import { Bytes, log } from "@graphprotocol/graph-ts"; | ||
| import { | ||
| PaymentAuthorized, | ||
| PaymentCaptured, | ||
| PaymentCharged, | ||
| PaymentVoided, | ||
| PaymentReclaimed, | ||
| PaymentRefunded, | ||
| } from "../generated/ERC20CommerceEscrowWrapper/ERC20CommerceEscrowWrapper"; | ||
| import { CommerceEscrow, CommerceEscrowEvent } from "../generated/schema"; | ||
| import { | ||
| generateCommerceEscrowId, | ||
| generateCommerceEscrowEventId, | ||
| createCommerceEscrowEvent, | ||
| } from "./shared"; | ||
|
|
||
| /** | ||
| * Handle the PaymentAuthorized event - creates a new commerce escrow | ||
| */ | ||
| export function handlePaymentAuthorized(event: PaymentAuthorized): void { | ||
| log.info("PaymentAuthorized at tx {} for reference {}", [ | ||
| event.transaction.hash.toHexString(), | ||
| event.params.paymentReference.toHexString(), | ||
| ]); | ||
|
|
||
| let paymentReference = Bytes.fromHexString( | ||
| event.params.paymentReference.toHexString(), | ||
| ); | ||
| let escrowId = generateCommerceEscrowId(paymentReference); | ||
| let escrow = CommerceEscrow.load(escrowId); | ||
|
|
||
| if (escrow == null) { | ||
| escrow = new CommerceEscrow(escrowId); | ||
| escrow.contractAddress = event.address; | ||
| escrow.reference = paymentReference; | ||
| escrow.commercePaymentHash = event.params.commercePaymentHash; | ||
| escrow.creationBlock = event.block.number.toI32(); | ||
| escrow.creationTimestamp = event.block.timestamp.toI32(); | ||
| escrow.tokenAddress = event.params.token; | ||
| escrow.amount = event.params.amount.toBigDecimal(); | ||
| escrow.payer = event.params.payer; | ||
| escrow.merchant = event.params.merchant; | ||
| escrow.escrowState = "authorized"; | ||
| escrow.save(); | ||
| } | ||
|
|
||
| createCommerceEscrowEvent(event, paymentReference, "authorized", null); | ||
| } | ||
|
|
||
| /** | ||
| * Handle the PaymentCaptured event - updates escrow state to captured | ||
| */ | ||
| export function handlePaymentCaptured(event: PaymentCaptured): void { | ||
| log.info("PaymentCaptured at tx {} for reference {}", [ | ||
| event.transaction.hash.toHexString(), | ||
| event.params.paymentReference.toHexString(), | ||
| ]); | ||
|
|
||
| let paymentReference = Bytes.fromHexString( | ||
| event.params.paymentReference.toHexString(), | ||
| ); | ||
| updateCommerceEscrowState(paymentReference, "captured"); | ||
| createCommerceEscrowEvent( | ||
| event, | ||
| paymentReference, | ||
| "captured", | ||
| event.params.capturedAmount.toBigDecimal(), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Handle the PaymentVoided event - updates escrow state to voided | ||
| */ | ||
| export function handlePaymentVoided(event: PaymentVoided): void { | ||
| log.info("PaymentVoided at tx {} for reference {}", [ | ||
| event.transaction.hash.toHexString(), | ||
| event.params.paymentReference.toHexString(), | ||
| ]); | ||
|
|
||
| let paymentReference = Bytes.fromHexString( | ||
| event.params.paymentReference.toHexString(), | ||
| ); | ||
| updateCommerceEscrowState(paymentReference, "voided"); | ||
| createCommerceEscrowEvent( | ||
| event, | ||
| paymentReference, | ||
| "voided", | ||
| event.params.voidedAmount.toBigDecimal(), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Handle the PaymentCharged event - creates a new commerce escrow in charged state | ||
| */ | ||
| export function handlePaymentCharged(event: PaymentCharged): void { | ||
| log.info("PaymentCharged at tx {} for reference {}", [ | ||
| event.transaction.hash.toHexString(), | ||
| event.params.paymentReference.toHexString(), | ||
| ]); | ||
|
|
||
| let paymentReference = Bytes.fromHexString( | ||
| event.params.paymentReference.toHexString(), | ||
| ); | ||
| let escrowId = generateCommerceEscrowId(paymentReference); | ||
| let escrow = CommerceEscrow.load(escrowId); | ||
|
|
||
| if (escrow == null) { | ||
| escrow = new CommerceEscrow(escrowId); | ||
| escrow.contractAddress = event.address; | ||
| escrow.reference = paymentReference; | ||
| escrow.commercePaymentHash = event.params.commercePaymentHash; | ||
| escrow.creationBlock = event.block.number.toI32(); | ||
| escrow.creationTimestamp = event.block.timestamp.toI32(); | ||
| escrow.tokenAddress = event.params.token; | ||
| escrow.amount = event.params.amount.toBigDecimal(); | ||
| escrow.payer = event.params.payer; | ||
| escrow.merchant = event.params.merchant; | ||
| } | ||
| escrow.escrowState = "charged"; | ||
| escrow.save(); | ||
|
|
||
| createCommerceEscrowEvent( | ||
| event, | ||
| paymentReference, | ||
| "charged", | ||
| event.params.amount.toBigDecimal(), | ||
| ); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Handle the PaymentReclaimed event - updates escrow state to reclaimed | ||
| */ | ||
| export function handlePaymentReclaimed(event: PaymentReclaimed): void { | ||
| log.info("PaymentReclaimed at tx {} for reference {}", [ | ||
| event.transaction.hash.toHexString(), | ||
| event.params.paymentReference.toHexString(), | ||
| ]); | ||
|
|
||
| let paymentReference = Bytes.fromHexString( | ||
| event.params.paymentReference.toHexString(), | ||
| ); | ||
| updateCommerceEscrowState(paymentReference, "reclaimed"); | ||
| createCommerceEscrowEvent( | ||
| event, | ||
| paymentReference, | ||
| "reclaimed", | ||
| event.params.reclaimedAmount.toBigDecimal(), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Handle the PaymentRefunded event - updates escrow state to refunded | ||
| */ | ||
| export function handlePaymentRefunded(event: PaymentRefunded): void { | ||
| log.info("PaymentRefunded at tx {} for reference {}", [ | ||
| event.transaction.hash.toHexString(), | ||
| event.params.paymentReference.toHexString(), | ||
| ]); | ||
|
|
||
| let paymentReference = Bytes.fromHexString( | ||
| event.params.paymentReference.toHexString(), | ||
| ); | ||
| updateCommerceEscrowState(paymentReference, "refunded"); | ||
| createCommerceEscrowEvent( | ||
| event, | ||
| paymentReference, | ||
| "refunded", | ||
| event.params.refundedAmount.toBigDecimal(), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Helper function to update commerce escrow state | ||
| */ | ||
| function updateCommerceEscrowState( | ||
| paymentReference: Bytes, | ||
| eventType: string, | ||
| ): void { | ||
| let escrow = CommerceEscrow.load(generateCommerceEscrowId(paymentReference)); | ||
| if (!escrow) return; | ||
| escrow.escrowState = eventType; | ||
| escrow.save(); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.