From 1f0bb29dba5b14b0515fe96c5c10073a53ff4e3a Mon Sep 17 00:00:00 2001 From: Darko Kolev Date: Thu, 17 Feb 2022 13:32:12 +0100 Subject: [PATCH] testing getEscrow method --- .../src/erc20/custom-detector.ts | 16 +++- .../test/erc20/escrow-proxy-contract.test.ts | 84 +++++++++++++++---- 2 files changed, 84 insertions(+), 16 deletions(-) diff --git a/packages/payment-detection/src/erc20/custom-detector.ts b/packages/payment-detection/src/erc20/custom-detector.ts index 3d5fbd30d4..33a1896f95 100644 --- a/packages/payment-detection/src/erc20/custom-detector.ts +++ b/packages/payment-detection/src/erc20/custom-detector.ts @@ -104,15 +104,27 @@ export class CustomProxyDetector extends ERC20FeeProxyPaymentDetector { paymentExtension.version, ); const paymentReference = this.getPaymentReference(request); - const infoRetriever = new EscrowERC20GraphInfoRetriever( + const escrow = await this.getEscrowFromGraph( paymentReference, deploymentInformation.address, paymentChain, ); - const escrow = await infoRetriever.getEscrow(); return escrow.length ? escrow[0] : null; } + protected async getEscrowFromGraph( + paymentReference: string, + contractAddress: string, + paymentChain: string, + ): Promise { + const infoRetriever = new EscrowERC20GraphInfoRetriever( + paymentReference, + contractAddress, + paymentChain, + ); + return await infoRetriever.getEscrow(); + } + protected async extractCustomEvents( eventName: PaymentTypes.ESCROW_EVENTS_NAMES | undefined, to: string, diff --git a/packages/payment-detection/test/erc20/escrow-proxy-contract.test.ts b/packages/payment-detection/test/erc20/escrow-proxy-contract.test.ts index b90973f5ca..20218d8e97 100644 --- a/packages/payment-detection/test/erc20/escrow-proxy-contract.test.ts +++ b/packages/payment-detection/test/erc20/escrow-proxy-contract.test.ts @@ -7,8 +7,9 @@ import { } from '@requestnetwork/types'; import { CurrencyManager } from '@requestnetwork/currency'; import { CustomProxyDetector } from '../../src/erc20/custom-detector'; +import { PaymentReferenceCalculator } from '../../src'; -let escrowProxyContract: CustomProxyDetector; +let escrowProxyDetector: CustomProxyDetector; const createAddPaymentAddressAction = jest.fn(); const createAddRefundAddressAction = jest.fn(); @@ -40,7 +41,7 @@ const currencyManager = CurrencyManager.getDefault(); /* eslint-disable @typescript-eslint/no-unused-expressions */ describe('api/erc20/escrow-proxy-contract', () => { beforeEach(() => { - escrowProxyContract = new CustomProxyDetector({ + escrowProxyDetector = new CustomProxyDetector({ advancedLogic: mockAdvancedLogic, currencyManager, }); @@ -51,7 +52,7 @@ describe('api/erc20/escrow-proxy-contract', () => { }); it('can createExtensionsDataForCreation', async () => { - await escrowProxyContract.createExtensionsDataForCreation({ + await escrowProxyDetector.createExtensionsDataForCreation({ paymentAddress: 'ethereum address', salt: 'ea3bc7caf64110ca', }); @@ -66,7 +67,7 @@ describe('api/erc20/escrow-proxy-contract', () => { }); it('can createExtensionsDataForCreation with fee amount and address', async () => { - await escrowProxyContract.createExtensionsDataForCreation({ + await escrowProxyDetector.createExtensionsDataForCreation({ feeAddress: 'fee address', feeAmount: '2000', paymentAddress: 'ethereum address', @@ -83,7 +84,7 @@ describe('api/erc20/escrow-proxy-contract', () => { }); it('can createExtensionsDataForCreation without salt', async () => { - await escrowProxyContract.createExtensionsDataForCreation({ + await escrowProxyDetector.createExtensionsDataForCreation({ paymentAddress: 'ethereum address', }); @@ -92,7 +93,7 @@ describe('api/erc20/escrow-proxy-contract', () => { }); it('can createExtensionsDataForAddPaymentInformation', async () => { - escrowProxyContract.createExtensionsDataForAddPaymentInformation({ + escrowProxyDetector.createExtensionsDataForAddPaymentInformation({ paymentInfo: 'ethereum address', }); @@ -102,7 +103,7 @@ describe('api/erc20/escrow-proxy-contract', () => { }); it('can createExtensionsDataForAddPaymentAddress', async () => { - escrowProxyContract.createExtensionsDataForAddPaymentAddress({ + escrowProxyDetector.createExtensionsDataForAddPaymentAddress({ paymentAddress: 'ethereum address', }); @@ -112,7 +113,7 @@ describe('api/erc20/escrow-proxy-contract', () => { }); it('can createExtensionsDataForAddRefundAddress', async () => { - escrowProxyContract.createExtensionsDataForAddRefundAddress({ + escrowProxyDetector.createExtensionsDataForAddRefundAddress({ refundAddress: 'ethereum address', }); @@ -122,7 +123,7 @@ describe('api/erc20/escrow-proxy-contract', () => { }); it('can createExtensionsDataForAddRefundInformation', async () => { - escrowProxyContract.createExtensionsDataForAddRefundInformation({ + escrowProxyDetector.createExtensionsDataForAddRefundInformation({ refundInfo: 'ethereum address', }); @@ -132,7 +133,7 @@ describe('api/erc20/escrow-proxy-contract', () => { }); it('can createExtensionsDataForAddFeeInformation', async () => { - escrowProxyContract.createExtensionsDataForAddFeeInformation({ + escrowProxyDetector.createExtensionsDataForAddFeeInformation({ feeAddress: 'ethereum address', feeAmount: '2000', }); @@ -145,7 +146,7 @@ describe('api/erc20/escrow-proxy-contract', () => { it('should not throw when getBalance fail', async () => { expect( - await escrowProxyContract.getBalance({ extensions: {} } as RequestLogicTypes.IRequest), + await escrowProxyDetector.getBalance({ extensions: {} } as RequestLogicTypes.IRequest), ).toEqual({ balance: null, error: { @@ -257,16 +258,16 @@ describe('api/erc20/escrow-proxy-contract', () => { }, ]); }; - escrowProxyContract = new CustomProxyDetector({ + escrowProxyDetector = new CustomProxyDetector({ advancedLogic: mockAdvancedLogic, currencyManager, }); jest - .spyOn(escrowProxyContract as any, 'extractEvents') + .spyOn(escrowProxyDetector as any, 'extractEvents') .mockImplementation(mockExtractTransferEvents); - const balance = await escrowProxyContract.getBalance(mockRequest); + const balance = await escrowProxyDetector.getBalance(mockRequest); expect(balance.error).toBeUndefined(); // 500 + 500 (2 payments) - 10 (1 refund) = 990 @@ -276,4 +277,59 @@ describe('api/erc20/escrow-proxy-contract', () => { .feeBalance.balance, ).toBe('5'); }); + + it.only('can get escrow data from escrow detector', async () => { + const mockRequest: RequestLogicTypes.IRequest = { + creator: { type: IdentityTypes.TYPE.ETHEREUM_ADDRESS, value: '0x2' }, + currency: { + network: 'private', + type: RequestLogicTypes.CURRENCY.ERC20, + value: '0x9FBDa871d559710256a2502A2517b794B482Db40', // local ERC20 token + }, + events: [], + expectedAmount: '1000', + extensions: { + [ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_FEE_PROXY_CONTRACT]: { + events: [], + id: ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_FEE_PROXY_CONTRACT, + type: ExtensionTypes.TYPE.PAYMENT_NETWORK, + values: { + feeAddress: '0xC5fdf4076b8F3A5357c5E395ab970B5B54098Fef', + feeAmount: '5', + paymentAddress: '0xf17f52151EbEF6C7334FAD080c5704D77216b732', + refundAddress: '0xrefundAddress', + salt: 'abcd', + }, + version: '0.1.0', + }, + }, + extensionsData: [], + requestId: '0x1', + state: RequestLogicTypes.STATE.CREATED, + timestamp: 0, + version: '0.2', + }; + + const mockEscrowFromTheGraphAction = jest.fn((...args: unknown[]) => { + console.log(...args); + return Promise.resolve([]); + }); + + jest + .spyOn(escrowProxyDetector as any, 'getEscrowFromGraph') + .mockImplementation(mockEscrowFromTheGraphAction); + + await escrowProxyDetector.getEscrow(mockRequest); + const reference = PaymentReferenceCalculator.calculate( + mockRequest.requestId, + 'abcd', + '0xf17f52151EbEF6C7334FAD080c5704D77216b732', + ); + const privateContractAddress = '0xF08dF3eFDD854FEDE77Ed3b2E515090EEe765154'; + expect(mockEscrowFromTheGraphAction).toHaveBeenCalledWith( + reference, + privateContractAddress, + 'private', + ); + }); });