Skip to content

Commit

Permalink
feat: pull gas info from subgraph on other payment networks (#876)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrylaloan committed Jul 1, 2022
1 parent 82ab698 commit 9c5ae2b
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 3 deletions.
5 changes: 5 additions & 0 deletions packages/payment-detection/src/any/retrievers/thegraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ export class TheGraphConversionRetriever
to: this.toAddress,
maxRateTimespan: this.maxRateTimespan,
};

// Parses, filters and creates the events from the logs with the payment reference
const events = await this.client.GetConversionPayments(variables);

// Creates the balance events
return events.payments
.filter((payment) => {
Expand Down Expand Up @@ -82,6 +84,9 @@ export class TheGraphConversionRetriever
to: this.toAddress,
txHash: payment.txHash,
maxRateTimespan: payment.maxRateTimespan?.toString(),
from: payment.from,
gasUsed: payment.gasUsed,
gasPrice: payment.gasPrice,
},
timestamp: payment.timestamp,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ query GetConversionPayments(
amountInCrypto
feeAmountInCrypto
maxRateTimespan
gasUsed
gasPrice
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ethers } from 'ethers';
import { GraphQLClient } from 'graphql-request';
import { mocked } from 'ts-jest/utils';
import {
AdvancedLogicTypes,
ExtensionTypes,
Expand All @@ -10,6 +12,9 @@ import { CurrencyManager } from '@requestnetwork/currency';
import { ERC20__factory } from '@requestnetwork/smart-contracts/types';
import { AnyToERC20PaymentDetector } from '../../src/any/any-to-erc20-proxy';

jest.mock('graphql-request');
const graphql = mocked(GraphQLClient.prototype);

let anyToErc20Proxy: AnyToERC20PaymentDetector;
const currencyManager = CurrencyManager.getDefault();

Expand Down Expand Up @@ -328,4 +333,64 @@ describe('api/any/conversion-fee-proxy-contract', () => {

expect(spy).toHaveBeenCalledWith(ethers.constants.AddressZero, expect.anything());
});

it('should get gas info from the graph', async () => {
const mockRequest: RequestLogicTypes.IRequest = {
creator: { type: IdentityTypes.TYPE.ETHEREUM_ADDRESS, value: '0x2' },
currency: {
type: RequestLogicTypes.CURRENCY.ISO4217,
value: 'EUR',
},
events: [],
expectedAmount: '100',
extensions: {
[ExtensionTypes.ID.PAYMENT_NETWORK_ANY_TO_ERC20_PROXY]: {
events: [],
id: ExtensionTypes.ID.PAYMENT_NETWORK_ANY_TO_ERC20_PROXY,
type: ExtensionTypes.TYPE.PAYMENT_NETWORK,
values: {
feeAddress: '0x35d0e078755Cd84D3E0656cAaB417Dee1d7939c7',
feeAmount: '0',
paymentAddress: '0x98F32171D88F9511b397809534eE42ACFCe4F640',
salt: 'a9c4f042874e24b7',
network: 'rinkeby',
acceptedTokens: ['0xFab46E002BbF0b4509813474841E0716E6730136'],
},
version: '0.1.0',
},
},
extensionsData: [],
requestId: '01cbe434720b58051f5150ec8d414aead2f1e04fca4d69c9401da0cc3e733f1646',
state: RequestLogicTypes.STATE.CREATED,
timestamp: 0,
version: '0.2',
};

graphql.request.mockResolvedValue({
payments: [
{
amount: '100000000',
block: 8404818,
txHash: '0xc672ce6704182e710b92919e88b38904a27fd17ea1036269147e4ddff352ab4d',
feeAmount: '0',
feeAddress: '0x35d0e078755cd84d3e0656caab417dee1d7939c7',
from: '0x98f32171d88f9511b397809534ee42acfce4f640',
timestamp: 1618306072,
tokenAddress: '0xfab46e002bbf0b4509813474841e0716e6730136',
currency: '0x17b4158805772ced11225e77339f90beb5aae968',
amountInCrypto: '1190282493338176476',
feeAmountInCrypto: '0',
maxRateTimespan: 0,
gasUsed: '130259',
gasPrice: '73500000000',
},
],
});

const balance = await anyToErc20Proxy.getBalance(mockRequest);
expect(balance.error).toBeUndefined();
const parameters = balance.events[0].parameters as any;
expect(parameters.gasUsed).toBe('130259');
expect(parameters.gasPrice).toBe('73500000000');
});
});
14 changes: 13 additions & 1 deletion packages/payment-detection/test/any/any-to-eth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ describe('Any to ETH payment detection', () => {
timestamp: 1643647285,
};

const expectedBalanceWithGasInfo = {
...expectedBalance,
parameters: {
...expectedBalance.parameters,
gasUsed: '144262',
gasPrice: '2425000017',
from: '0x0e8d9cb9e11278ad6e2ba1ca90385c7295dc6532',
},
};

it('RPC Payment detection', async () => {
getLogs
.mockResolvedValueOnce([
Expand Down Expand Up @@ -121,6 +131,8 @@ describe('Any to ETH payment detection', () => {
timestamp: 1643647285,
tokenAddress: null,
txHash: '0x7733a0fad7d7bdd0222ff1b63902aa26f1904e0fe14e03e95de73195e22a8ae6',
gasUsed: '144262',
gasPrice: '2425000017',
},
],
});
Expand All @@ -134,6 +146,6 @@ describe('Any to ETH payment detection', () => {
const balance = await detector.getBalance(mockRequest);
expect(balance.error).not.toBeDefined();
expect(balance.balance).toBe('5000');
expect(balance.events).toMatchObject([expectedBalance]);
expect(balance.events).toMatchObject([expectedBalanceWithGasInfo]);
});
});
64 changes: 62 additions & 2 deletions packages/payment-detection/test/erc20/proxy-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import {
RequestLogicTypes,
} from '@requestnetwork/types';
import { ERC20ProxyPaymentDetector } from '../../src/erc20/proxy-contract';
import { GraphQLClient } from 'graphql-request';
import { mocked } from 'ts-jest/utils';

jest.mock('graphql-request');
const graphql = mocked(GraphQLClient.prototype);

let erc20ProxyContract: ERC20ProxyPaymentDetector;

Expand All @@ -20,7 +25,7 @@ const mockAdvancedLogic: AdvancedLogicTypes.IAdvancedLogic = {
},
extensions: {
proxyContractErc20: {
supportedNetworks: ['mainnet'],
supportedNetworks: ['mainnet', 'rinkeby'],
createAddPaymentAddressAction,
createAddRefundAddressAction,
createCreationAction,
Expand Down Expand Up @@ -155,9 +160,64 @@ describe('api/erc20/proxy-contract', () => {
error: {
code: PaymentTypes.BALANCE_ERROR_CODE.NETWORK_NOT_SUPPORTED,
message:
'Payment network WRONG not supported by pn-erc20-proxy-contract payment detection. Supported networks: mainnet',
'Payment network WRONG not supported by pn-erc20-proxy-contract payment detection. Supported networks: mainnet, rinkeby',
},
events: [],
});
});

it('should have gas info when fetching from thegraph', async () => {
const mockRequest: any = {
requestId: '01ae8d15bdff65a03271e36bb00d3f3bfb43c1ff95f8ac74338b95c069e62bb928',
currency: {
network: 'rinkeby',
type: 'ERC20',
value: '0xFab46E002BbF0b4509813474841E0716E6730136',
},
extensions: {
[ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_PROXY_CONTRACT]: {
events: [
{
name: 'create',
parameters: {
paymentAddress: '0x0e8d9cb9e11278ad6e2ba1ca90385c7295dc6532',
salt: '0944271041d2ee69',
},
timestamp: 1579705864,
},
],
id: ExtensionTypes.ID.PAYMENT_NETWORK_ERC20_PROXY_CONTRACT,
type: 'payment-network',
values: {
paymentAddress: '0x0e8d9cb9e11278ad6e2ba1ca90385c7295dc6532',
salt: '0944271041d2ee69',
},
version: '0.1.0',
},
},
};

graphql.request.mockResolvedValue({
payments: [
{
amount: '1000000000000000000',
block: 5837728,
txHash: '0xaace20feaca32b47f2174a11e319b784bbb261b05344a72b06c1e85d2ed48f81',
feeAmount: null,
feeAddress: null,
from: '0x0e8d9cb9e11278ad6e2ba1ca90385c7295dc6532',
gasUsed: '41013',
gasPrice: '1000000000',
timestamp: 1579705909,
},
],
escrowEvents: [],
});

const balance = await erc20ProxyContract.getBalance(mockRequest);

const parameters = balance.events[0].parameters as any;
expect(parameters.gasUsed).toBe('41013');
expect(parameters.gasPrice).toBe('1000000000');
});
});

0 comments on commit 9c5ae2b

Please sign in to comment.