Skip to content

Commit

Permalink
Merge branch 'master' into adv-logic-src-third-part-declarative-delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
vrolland committed Jun 30, 2021
2 parents 4da8006 + 6c7cf35 commit 3ee42f0
Show file tree
Hide file tree
Showing 24 changed files with 688 additions and 462 deletions.
2 changes: 1 addition & 1 deletion packages/currency/package.json
Expand Up @@ -49,7 +49,7 @@
"devDependencies": {
"@types/jest": "26.0.13",
"@types/node-dijkstra": "2.5.1",
"ethers": "5.1.4",
"ethers": "5.2.0",
"jest": "26.4.2",
"prettier": "2.1.1",
"shx": "0.3.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/data-format/package.json
Expand Up @@ -40,7 +40,7 @@
},
"dependencies": {
"ajv": "6.12.4",
"ethers": "5.1.4"
"ethers": "5.2.0"
},
"devDependencies": {
"@types/node": "14.14.16",
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/package.json
Expand Up @@ -44,7 +44,7 @@
"bn.js": "5.1.3",
"classnames": "2.2.6",
"core-js": "3.6.5",
"ethers": "5.1.4",
"ethers": "5.2.0",
"mobx": "5.15.6",
"react": "16.13.1",
"react-dom": "16.13.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/ethereum-storage/package.json
Expand Up @@ -43,7 +43,7 @@
"@requestnetwork/types": "0.35.0",
"@requestnetwork/utils": "0.35.0",
"bluebird": "3.7.2",
"ethers": "5.1.4",
"ethers": "5.2.0",
"form-data": "3.0.0",
"ipfs-unixfs": "2.0.3",
"keyv": "4.0.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/integration-test/package.json
Expand Up @@ -55,7 +55,7 @@
"@truffle/hdwallet-provider": "1.2.3",
"@types/jest": "26.0.13",
"@types/node": "14.14.16",
"ethers": "5.1.4",
"ethers": "5.2.0",
"jest": "26.4.2",
"npm-run-all": "4.1.5",
"prettier": "2.2.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/payment-detection/package.json
Expand Up @@ -45,7 +45,7 @@
"@requestnetwork/types": "0.35.0",
"@requestnetwork/utils": "0.35.0",
"axios": "0.21.1",
"ethers": "5.1.4",
"ethers": "5.2.0",
"graphql": "15.5.0",
"graphql-request": "3.4.0",
"graphql-tag": "2.12.4",
Expand Down
Expand Up @@ -2,7 +2,7 @@ import { Currency } from '@requestnetwork/currency';
import { PaymentTypes, RequestLogicTypes } from '@requestnetwork/types';
import { BigNumber, ethers } from 'ethers';
import { getDefaultProvider } from '../provider';
import { parseLogArgs } from '../utils';
import { parseLogArgs, unpadAmountFromChainlink } from '../utils';

// The conversion proxy smart contract ABI fragment containing TransferWithConversionAndReference event
const erc20ConversionProxyContractAbiFragment = [
Expand Down Expand Up @@ -152,21 +152,21 @@ export default class ProxyERC20InfoRetriever
)
// Creates the balance events
.map(async ({ conversionLog, proxyLog, blockNumber, transactionHash }) => {
const chainlinkDecimal = 8;
const decimalPadding = chainlinkDecimal - new Currency(this.requestCurrency).getDecimals();
const requestCurrency = new Currency(this.requestCurrency);

const amountWithRightDecimal = conversionLog.amount.div(10 ** decimalPadding).toString();
const feeAmountWithRightDecimal = conversionLog.feeAmount
.div(10 ** decimalPadding)
.toString();
const amount = unpadAmountFromChainlink(conversionLog.amount, requestCurrency).toString();
const feeAmount = unpadAmountFromChainlink(
conversionLog.feeAmount,
requestCurrency,
).toString();

return {
amount: amountWithRightDecimal,
amount,
name: this.eventName,
parameters: {
block: blockNumber,
feeAddress: proxyLog.feeAddress || undefined,
feeAmount: feeAmountWithRightDecimal,
feeAmount,
feeAmountInCrypto: proxyLog.feeAmount.toString() || undefined,
amountInCrypto: proxyLog.amount.toString(),
tokenAddress: proxyLog.tokenAddress,
Expand Down
4 changes: 4 additions & 0 deletions packages/payment-detection/src/index.ts
Expand Up @@ -7,6 +7,7 @@ import * as Erc20PaymentNetwork from './erc20';
import * as EthPaymentNetwork from './eth';
import { initPaymentDetectionApiKeys, setProviderFactory, getDefaultProvider } from './provider';
import { getTheGraphClient, networkSupportsTheGraph } from './thegraph';
import { parseLogArgs, padAmountForChainlink, unpadAmountFromChainlink } from './utils';
export type { TheGraphClient } from './thegraph';

export {
Expand All @@ -21,4 +22,7 @@ export {
getDefaultProvider,
getTheGraphClient,
networkSupportsTheGraph,
parseLogArgs,
padAmountForChainlink,
unpadAmountFromChainlink,
};
33 changes: 33 additions & 0 deletions packages/payment-detection/src/utils.ts
@@ -1,3 +1,6 @@
import { Currency } from '@requestnetwork/currency';
import { RequestLogicTypes } from '@requestnetwork/types';
import { BigNumber, BigNumberish } from 'ethers';
import { LogDescription } from 'ethers/lib/utils';

/**
Expand All @@ -9,3 +12,33 @@ export const parseLogArgs = <T>({ args, eventFragment }: LogDescription): T => {
return prev;
}, {});
};

/**
* Pads an amount to match Chainlink's own currency decimals (eg. for fiat amounts).
*/
export const padAmountForChainlink = (amount: BigNumberish, currency: Currency): BigNumber => {
// eslint-disable-next-line no-magic-numbers
return BigNumber.from(amount).mul(10 ** getChainlinkPaddingSize(currency));
};

export const unpadAmountFromChainlink = (amount: BigNumberish, currency: Currency): BigNumber => {
// eslint-disable-next-line no-magic-numbers
return BigNumber.from(amount).div(10 ** getChainlinkPaddingSize(currency));
};

const getChainlinkPaddingSize = (currency: Currency): number => {
switch (currency.type) {
case RequestLogicTypes.CURRENCY.ISO4217: {
const chainlinkFiatDecimal = 8;
return Math.max(chainlinkFiatDecimal - currency.getDecimals(), 0);
}
case RequestLogicTypes.CURRENCY.ETH:
case RequestLogicTypes.CURRENCY.ERC20: {
return 0;
}
default:
throw new Error(
'Unsupported request currency for conversion with Chainlink. The request currency has to be fiat, ETH or ERC20.',
);
}
};
46 changes: 46 additions & 0 deletions packages/payment-detection/test/utils.test.ts
@@ -0,0 +1,46 @@
import { Currency } from '@requestnetwork/currency';
import { padAmountForChainlink, unpadAmountFromChainlink } from '../src';

describe('conversion: padding amounts for Chainlink', () => {
it('should throw on currencies not implemented in the library', () => {
const requestCurrency = Currency.fromSymbol('BTC');
const twentyBtc = '2000000000';
expect(() => padAmountForChainlink(twentyBtc, requestCurrency)).toThrowError(
'Unsupported request currency for conversion with Chainlink. The request currency has to be fiat, ETH or ERC20.',
);
});
it('should pad fiat amounts', () => {
const requestCurrency = Currency.fromSymbol('EUR');
const twentyEur = '2000';
expect(padAmountForChainlink(twentyEur, requestCurrency).toString()).toBe('2000000000');
});
it('should unpad fiat amounts', () => {
const requestCurrency = Currency.fromSymbol('EUR');
expect(unpadAmountFromChainlink('2000000000', requestCurrency).toString()).toBe('2000');
});
it('should not pad crypto amounts (ETH)', () => {
const requestCurrency = Currency.fromSymbol('ETH');
const twentyEth = '20000000000000000000';
expect(padAmountForChainlink(twentyEth, requestCurrency).toString()).toBe(twentyEth);
});
it('should not unpad fiat amounts (ETH)', () => {
const requestCurrency = Currency.fromSymbol('ETH');
const twentyEth = '20000000000000000000';
expect(unpadAmountFromChainlink(twentyEth, requestCurrency).toString()).toBe(twentyEth);
});
it('should not pad crypto amounts (DAI)', () => {
const requestCurrency = Currency.fromSymbol('DAI');
const twentyDai = '20000000000000000000';
expect(padAmountForChainlink(twentyDai, requestCurrency).toString()).toBe(twentyDai);
});
it('should not unpad crypto amounts (DAI)', () => {
const requestCurrency = Currency.fromSymbol('DAI');
const twentyDai = '20000000000000000000';
expect(unpadAmountFromChainlink(twentyDai, requestCurrency).toString()).toBe(twentyDai);
});
it('should not pad crypto amounts (USDC)', () => {
const requestCurrency = Currency.fromSymbol('USDC');
const twentyUsdc = '20000000';
expect(padAmountForChainlink(twentyUsdc, requestCurrency).toString()).toBe(twentyUsdc);
});
});
2 changes: 1 addition & 1 deletion packages/payment-processor/package.json
Expand Up @@ -43,7 +43,7 @@
"@requestnetwork/smart-contracts": "0.28.0",
"@requestnetwork/types": "0.35.0",
"@requestnetwork/utils": "0.35.0",
"ethers": "5.1.4"
"ethers": "5.2.0"
},
"devDependencies": {
"@types/jest": "26.0.13",
Expand Down
Expand Up @@ -11,9 +11,9 @@ import {
getProvider,
getRequestPaymentValues,
getSigner,
padAmountForChainlink,
validateConversionFeeProxyRequest,
} from './utils';
import { padAmountForChainlink } from '@requestnetwork/payment-detection';

/**
* Details required to pay a request with on-chain conversion:
Expand Down
25 changes: 0 additions & 25 deletions packages/payment-processor/src/payment/utils.ts
Expand Up @@ -264,28 +264,3 @@ export function getAmountToPay(
}
return amountToPay;
}

/**
* Pads an amount to match Chainlink's own currency decimals (eg. for fiat amounts).
*/
export const padAmountForChainlink = (amount: BigNumberish, currency: Currency) => {
let decimalPadding: number;
switch (currency.type) {
case RequestLogicTypes.CURRENCY.ISO4217: {
const chainlinkFiatDecimal = 8;
decimalPadding = Math.max(chainlinkFiatDecimal - currency.getDecimals(), 0);
break;
}
case RequestLogicTypes.CURRENCY.ETH:
case RequestLogicTypes.CURRENCY.ERC20: {
decimalPadding = 0;
break;
}
default:
throw new Error(
'Unsupported request currency for conversion with Chainlink. The request currency has to be fiat, ETH or ERC20.',
);
}
// eslint-disable-next-line no-magic-numbers
return BigNumber.from(amount).mul(10 ** decimalPadding);
};
2 changes: 1 addition & 1 deletion packages/payment-processor/test/payment/eth-proxy.test.ts
Expand Up @@ -131,7 +131,7 @@ describe('payEthProxyRequest', () => {
expect(balanceEthBefore.toString()).toBe(
balanceEthAfter
.add(validRequest.expectedAmount)
.add(confirmedTx.gasUsed!.mul(tx.gasPrice))
.add(confirmedTx.gasUsed?.mul(tx?.gasPrice ?? 1))
.toString(),
);
});
Expand Down
32 changes: 0 additions & 32 deletions packages/payment-processor/test/payment/utils.test.ts
@@ -1,12 +1,10 @@
import { Wallet, providers, BigNumber } from 'ethers';

import { Currency } from '@requestnetwork/currency';
import {
getAmountToPay,
getNetworkProvider,
getProvider,
getSigner,
padAmountForChainlink,
} from '../../src/payment/utils';

describe('getAmountToPay', () => {
Expand Down Expand Up @@ -132,33 +130,3 @@ describe('getSigner', () => {
expect(() => getSigner({} as any)).toThrowError('cannot get signer');
});
});

describe('conversion: padding amounts for Chainlink', () => {
it('should throw on currencies not implemented in the library', () => {
const requestCurrency = Currency.fromSymbol('BTC');
const twentyBtc = '2000000000';
expect(() => padAmountForChainlink(twentyBtc, requestCurrency)).toThrowError(
'Unsupported request currency for conversion with Chainlink. The request currency has to be fiat, ETH or ERC20.',
);
});
it('should pad fiat amounts', () => {
const requestCurrency = Currency.fromSymbol('EUR');
const twentyEur = '2000';
expect(padAmountForChainlink(twentyEur, requestCurrency).toString()).toBe('2000000000');
});
it('should not pad crypto amounts (ETH)', () => {
const requestCurrency = Currency.fromSymbol('ETH');
const twentyEth = '20000000000000000000';
expect(padAmountForChainlink(twentyEth, requestCurrency).toString()).toBe(twentyEth);
});
it('should not pad crypto amounts (DAI)', () => {
const requestCurrency = Currency.fromSymbol('DAI');
const twentyDai = '20000000000000000000';
expect(padAmountForChainlink(twentyDai, requestCurrency).toString()).toBe(twentyDai);
});
it('should not pad crypto amounts (USDC)', () => {
const requestCurrency = Currency.fromSymbol('USDC');
const twentyUsdc = '20000000';
expect(padAmountForChainlink(twentyUsdc, requestCurrency).toString()).toBe(twentyUsdc);
});
});
2 changes: 1 addition & 1 deletion packages/request-client.js/package.json
Expand Up @@ -55,7 +55,7 @@
"@requestnetwork/types": "0.35.0",
"@requestnetwork/utils": "0.35.0",
"axios": "0.21.1",
"ethers": "5.1.4"
"ethers": "5.2.0"
},
"devDependencies": {
"@compodoc/compodoc": "1.1.11",
Expand Down
8 changes: 4 additions & 4 deletions packages/smart-contracts/package.json
Expand Up @@ -46,21 +46,21 @@
"deploy": "truffle --contracts_directory=./src deploy",
"test": "truffle test --contracts_directory=./src test/contracts/*.js",
"test:lib": "jest test test/lib",
"generate-types": "typechain --target=ethers-v5 --outDir=src/types 'build/contracts/*.json'"
"generate-types": "typechain --target=ethers-v5 --out-dir=src/types 'build/contracts/*.json'"
},
"devDependencies": {
"@openzeppelin/contracts": "2.5.1",
"@openzeppelin/test-helpers": "0.5.6",
"@requestnetwork/currency": "0.8.0",
"@requestnetwork/utils": "0.35.0",
"@typechain/ethers-v5": "6.0.5",
"@typechain/ethers-v5": "7.0.1",
"@types/node": "14.14.16",
"chai-as-promised": "7.1.1",
"chai-bn": "0.2.1",
"ethers": "5.1.4",
"ethers": "5.2.0",
"ganache-cli": "6.12.0",
"shx": "0.3.2",
"truffle": "5.1.44",
"typechain": "4.0.3"
"typechain": "5.1.1"
}
}
2 changes: 1 addition & 1 deletion packages/toolbox/package.json
Expand Up @@ -46,7 +46,7 @@
"@requestnetwork/smart-contracts": "0.28.0",
"@requestnetwork/types": "0.35.0",
"axios": "0.21.1",
"ethers": "5.1.4",
"ethers": "5.2.0",
"yargs": "16.2.0"
},
"devDependencies": {
Expand Down
13 changes: 1 addition & 12 deletions packages/toolbox/src/chainlinkConversionPathTools.ts
@@ -1,11 +1,10 @@
import { ethers } from 'ethers';
import { chainlinkConversionPath } from '@requestnetwork/smart-contracts';
import { getDefaultProvider } from '@requestnetwork/payment-detection';
import { getDefaultProvider, parseLogArgs } from '@requestnetwork/payment-detection';
import { ChainlinkConversionPath__factory } from '@requestnetwork/smart-contracts/types';
import { Currency } from '@requestnetwork/currency';
import { RequestLogicTypes } from '@requestnetwork/types';
import iso4217 from '@requestnetwork/currency/dist/iso4217';
import { LogDescription } from 'ethers/lib/utils';

export interface IOptions {
network?: string;
Expand All @@ -19,16 +18,6 @@ type AggregatorUpdatedArgs = {
_aggregator: string;
};

/**
* Converts the Log's args from array to an object with keys being the name of the arguments
*/
export const parseLogArgs = <T>({ args, eventFragment }: LogDescription): T => {
return args.reduce((prev, current, i) => {
prev[eventFragment.inputs[i].name] = current;
return prev;
}, {});
};

/**
* Retrieves a list of payment events from a payment reference, a destination address, a token address and a proxy contract
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/types/package.json
Expand Up @@ -37,7 +37,7 @@
"prepare": "yarn run build"
},
"dependencies": {
"ethers": "5.1.4",
"ethers": "5.2.0",
"events": "3.2.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/usage-examples/package.json
Expand Up @@ -37,7 +37,7 @@
"@requestnetwork/transaction-manager": "0.26.9",
"@requestnetwork/types": "0.35.0",
"@requestnetwork/utils": "0.35.0",
"ethers": "5.1.4"
"ethers": "5.2.0"
},
"devDependencies": {
"prettier": "2.2.1",
Expand Down

0 comments on commit 3ee42f0

Please sign in to comment.