Skip to content

Commit

Permalink
test: Add integration test for payment network any to erc20
Browse files Browse the repository at this point in the history
  • Loading branch information
vrolland committed Mar 15, 2021
1 parent 7134310 commit c9e3232
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ const conversionErc20FeeProxyContract: ExtensionTypes.PnAnyToErc20.IAnyToERC20 =
* These currencies are supported by Chainlink for conversion.
* Only ERC20 is supported as accepted token by the payment proxy.
*/
const supportedCurrencies: Record<string, Record<RequestLogicTypes.CURRENCY, string[]>> = {
const supportedCurrencies: Record<string, Record<RequestLogicTypes.CURRENCY, string[]>> = {
private: {
[RequestLogicTypes.CURRENCY.ISO4217]: ['USD', 'EUR'],
[RequestLogicTypes.CURRENCY.ERC20]: ['0x9FBDa871d559710256a2502A2517b794B482Db40'],
[RequestLogicTypes.CURRENCY.ERC20]: ['0x38cf23c52bb4b13f051aec09580a2de845a7fa35'],
[RequestLogicTypes.CURRENCY.ETH]: ['ETH'],
[RequestLogicTypes.CURRENCY.BTC]: [],
},
Expand Down
3 changes: 3 additions & 0 deletions packages/integration-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
"devDependencies": {
"@openzeppelin/test-helpers": "0.5.6",
"@requestnetwork/advanced-logic": "0.29.0",
"@requestnetwork/currency": "0.3.0",
"@requestnetwork/data-access": "0.23.4",
"@requestnetwork/epk-decryption": "0.3.28",
"@requestnetwork/epk-signature": "0.5.29",
"@requestnetwork/ethereum-storage": "0.22.4",
"@requestnetwork/multi-format": "0.15.4",
"@requestnetwork/payment-detection": "0.30.4",
"@requestnetwork/payment-processor": "0.32.0",
"@requestnetwork/request-client.js": "0.34.0",
"@requestnetwork/request-logic": "0.26.4",
"@requestnetwork/transaction-manager": "0.26.4",
Expand All @@ -54,6 +56,7 @@
"@truffle/hdwallet-provider": "1.0.44",
"@types/jest": "26.0.13",
"@types/node": "14.14.16",
"ethers": "4.0.48",
"jest": "26.4.2",
"lint-staged": "10.3.0",
"npm-run-all": "4.1.5",
Expand Down
82 changes: 81 additions & 1 deletion packages/integration-test/test/node-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ import MultiFormat from '@requestnetwork/multi-format';
import { Request, RequestNetwork, Types } from '@requestnetwork/request-client.js';
import { IdentityTypes, PaymentTypes } from '@requestnetwork/types';
import Utils from '@requestnetwork/utils';
import {
payRequest,
approveErc20ForProxyConversionIfNeeded,
} from '@requestnetwork/payment-processor';

import { Wallet, utils } from 'ethers';
import { JsonRpcProvider } from 'ethers/providers';

const mnemonic = 'candy maple cake sugar pudding cream honey rich smooth crumble sweet treat';
const provider = new JsonRpcProvider('http://localhost:8545');
const wallet = Wallet.fromMnemonic(mnemonic).connect(provider);

// tslint:disable-next-line: no-magic-numbers
jest.setTimeout(10000);
Expand Down Expand Up @@ -491,7 +502,7 @@ describe('ERC20 localhost request creation and detection test', () => {
},
};

const contractAddress = '0x9FBDa871d559710256a2502A2517b794B482Db40';
const contractAddress = '0x38cf23c52bb4b13f051aec09580a2de845a7fa35';

const erc20requestCreationHash: Types.IRequestInfo = {
currency: {
Expand Down Expand Up @@ -529,4 +540,73 @@ describe('ERC20 localhost request creation and detection test', () => {
expect(requestData.state).toBe(Types.RequestLogic.STATE.CREATED);
expect(requestData.pending).toBeNull();
});

it('can create ERC20 requests with any to erc20 proxy', async () => {
const requestNetwork = new RequestNetwork({
signatureProvider,
useMockStorage: true,
});

const tokenContractAddress = '0x38cf23c52bb4b13f051aec09580a2de845a7fa35';

const paymentNetworkAnyToERC20: PaymentTypes.IPaymentNetworkCreateParameters = {
id: PaymentTypes.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY,
parameters: {
paymentAddress: '0xc12F17Da12cd01a9CDBB216949BA0b41A6Ffc4EB',
refundAddress: '0x821aEa9a577a9b44299B9c15c88cf3087F3b5544',
feeAddress: '0x0d1d4e623D10F9FBA5Db95830F7d3839406C6AF2',
feeAmount: '200',
network: 'private',
acceptedTokens: [tokenContractAddress],
maxRateTimespan: 1000000,
},
};

const request = await requestNetwork.createRequest({
paymentNetwork: paymentNetworkAnyToERC20,
requestInfo: requestCreationHashUSD,
signer: payeeIdentity,
});

let data = await request.refresh();
expect(data.balance).toBeNull();
const approval = await approveErc20ForProxyConversionIfNeeded(
data,
payeeIdentity.value,
tokenContractAddress,
wallet,
'100000000000000000000000000000000000',
);
if (approval) {
await approval.wait();
}

// USD => token
const maxToSpend = new utils.BigNumber(2).pow(255);
const paymentTx = await payRequest(data, wallet, undefined, undefined, {
currency: {
type: Types.RequestLogic.CURRENCY.ERC20,
value: tokenContractAddress,
network: 'private',
},
maxToSpend,
});
await paymentTx.wait();

data = await request.refresh();

expect(data.balance?.balance).toBe('1000');
expect(data.balance?.events.length).toBe(1);
const event = data.balance?.events[0];
expect(event?.amount).toBe('1000');
expect(event?.name).toBe('payment');

expect(event?.parameters?.feeAmount).toBe('200');
expect(event?.parameters?.feeAddress).toBe('0x0d1d4e623D10F9FBA5Db95830F7d3839406C6AF2');
expect(event?.parameters?.feeAmountInCrypto).toBe('9900990099009900990');
expect(event?.parameters?.to).toBe('0xc12F17Da12cd01a9CDBB216949BA0b41A6Ffc4EB');
expect(event?.parameters?.tokenAddress).toBe('0x38cF23C52Bb4B13F051Aec09580a2dE845a7FA35');
expect(event?.parameters?.amountInCrypto).toBe('1980198019801980198');
expect(event?.parameters?.maxRateTimespan).toBe('1000000');
});
});
2 changes: 2 additions & 0 deletions packages/integration-test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
"references": [
{ "path": "../advanced-logic" },
{ "path": "../request-client.js" },
{ "path": "../currency" },
{ "path": "../data-access" },
{ "path": "../ethereum-storage" },
{ "path": "../epk-decryption" },
{ "path": "../epk-signature" },
{ "path": "../multi-format" },
{ "path": "../payment-processor" },
{ "path": "../request-logic" },
{ "path": "../transaction-manager" },
{ "path": "../types" }
Expand Down
1 change: 1 addition & 0 deletions packages/payment-processor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './payment/eth-input-data';
export * from './payment/eth-proxy';
export * from './payment/swap-erc20';
export * from './payment/swap-erc20-fee-proxy';
export * from './payment/conversion-erc20';
import * as utils from './payment/utils';

export { utils };

0 comments on commit c9e3232

Please sign in to comment.