Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
yomarion committed Apr 27, 2021
2 parents fa444e8 + bbdd0b6 commit ac500a4
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 32 deletions.
Expand Up @@ -245,6 +245,9 @@ function applyCreation(
throw Error('version is missing');
}
if (!extensionAction.parameters.paymentAddress) {
throw Error('paymentAddress is missing');
}
if (!extensionAction.parameters.salt) {
throw Error('salt is missing');
}
if (
Expand Down
7 changes: 5 additions & 2 deletions packages/currency/src/token.ts
Expand Up @@ -22,11 +22,14 @@ export class Token extends Currency {
*/
static from(symbolOrAddress: string): Token {
try {
const currencyFromSymbol = this.fromSymbol(symbolOrAddress);
const currencyFromSymbol = this.fromSymbol(
symbolOrAddress.split('-')[0],
symbolOrAddress.split('-')[1],
);
return new Token(
currencyFromSymbol.value,
currencyFromSymbol.type,
symbolOrAddress,
symbolOrAddress.split('-')[0],
currencyFromSymbol.network,
);
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion packages/currency/test/currency.test.ts
Expand Up @@ -530,7 +530,7 @@ describe('api/currency', () => {
});

it('FAU from FAU-rinkeby', () => {
expect(Currency.from('FAU')).toMatchObject({
expect(Currency.from('FAU-rinkeby')).toMatchObject({
type: RequestLogicTypes.CURRENCY.ERC20,
value: '0xFab46E002BbF0b4509813474841E0716E6730136',
network: 'rinkeby',
Expand Down
2 changes: 1 addition & 1 deletion packages/currency/test/token.test.ts
Expand Up @@ -100,7 +100,7 @@ describe('api/currency Token', () => {
});

it('FAU from FAU-rinkeby', () => {
expect(Token.from('FAU')).toMatchObject({
expect(Token.from('FAU-rinkeby')).toMatchObject({
symbol: 'FAU',
type: RequestLogicTypes.CURRENCY.ERC20,
value: '0xFab46E002BbF0b4509813474841E0716E6730136',
Expand Down
4 changes: 2 additions & 2 deletions packages/payment-processor/src/payment/swap-any-to-erc20.ts
@@ -1,7 +1,7 @@
import { constants, ContractTransaction, Signer, BigNumber, providers } from 'ethers';

import { erc20SwapConversionArtifact } from '@requestnetwork/smart-contracts';
import { ERC20SwapToPayWithConversion__factory } from '@requestnetwork/smart-contracts/types';
import { ERC20SwapToConversion__factory } from '@requestnetwork/smart-contracts/types';
import { ClientTypes, PaymentTypes } from '@requestnetwork/types';

import {
Expand Down Expand Up @@ -117,7 +117,7 @@ export function encodeSwapToPayAnyToErc20Request(
}

const contractAddress = erc20SwapConversionArtifact.getAddress(network);
const swapToPayContract = ERC20SwapToPayWithConversion__factory.connect(contractAddress, signer);
const swapToPayContract = ERC20SwapToConversion__factory.connect(contractAddress, signer);

return swapToPayContract.interface.encodeFunctionData('swapTransferWithReference', [
paymentAddress, // _to: string,
Expand Down
Expand Up @@ -5,7 +5,7 @@ const AggEUR_USD = artifacts.require("AggEurUsd");
const AggUSDT_ETH = artifacts.require("AggUsdtEth");
const USDT_fake = artifacts.require("UsdtFake");
const FakeSwapRouter = artifacts.require('FakeSwapRouter');
const ERC20SwapToPayWithConversion = artifacts.require('ERC20SwapToPayWithConversion');
const ERC20SwapToConversion = artifacts.require('ERC20SwapToConversion');

const ERC20FeeProxy = artifacts.require('./ERC20FeeProxy.sol');
const ChainlinkConversionPath = artifacts.require("ChainlinkConversionPath");
Expand Down Expand Up @@ -38,7 +38,7 @@ module.exports = async function (deployer) {
await deployer.deploy(Erc20ConversionProxy, ERC20FeeProxy.address, ChainlinkConversionPath.address);

// erc20SwapConversion
const erc20SwapConversion = await deployer.deploy(ERC20SwapToPayWithConversion, FakeSwapRouter.address, Erc20ConversionProxy.address);
const erc20SwapConversion = await deployer.deploy(ERC20SwapToConversion, FakeSwapRouter.address, Erc20ConversionProxy.address);

await erc20SwapConversion.approvePaymentProxyToSpend('0x38cF23C52Bb4B13F051Aec09580a2dE845a7FA35');
await erc20SwapConversion.approveRouterToSpend('0x9FBDa871d559710256a2502A2517b794B482Db40');
Expand Down
Expand Up @@ -20,10 +20,10 @@ interface IUniswapV2Router02 {


/**
* @title ERC20SwapToPayWithConversion
* @title ERC20SwapToConversion
* @notice This contract swaps ERC20 tokens before paying a request thanks to a payment proxy
*/
contract ERC20SwapToPayWithConversion is Ownable {
contract ERC20SwapToConversion is Ownable {
using SafeERC20 for IERC20;
using SafeMath for uint256;

Expand Down
@@ -1,13 +1,12 @@
const artifactsERC20SwapToPayWithConversion = require('../../artifacts/Erc20SwapConversion/artifacts.json');
const ARTIFACTS_VERSION: string = artifactsERC20SwapToPayWithConversion.lastVersion;
const artifactsERC20SwapToConversion = require('../../artifacts/Erc20SwapConversion/artifacts.json');
const ARTIFACTS_VERSION: string = artifactsERC20SwapToConversion.lastVersion;

/**
* Retrieve the abi from the artifact of the used version
* @returns the abi of the artifact as a json object
*/
export function getContractAbi(): any {
const artifactFilename: string =
artifactsERC20SwapToPayWithConversion[ARTIFACTS_VERSION].artifact;
const artifactFilename: string = artifactsERC20SwapToConversion[ARTIFACTS_VERSION].artifact;

const artifact = require(`../../artifacts/Erc20SwapConversion/${artifactFilename.replace(
/\.[^/.]+$/,
Expand Down Expand Up @@ -53,7 +52,7 @@ export function getDeploymentInformation(
artifactsVersion: string = ARTIFACTS_VERSION,
): { address: string; creationBlockNumber: number } {
const deploymentInformation =
artifactsERC20SwapToPayWithConversion[artifactsVersion].deployment[networkName];
artifactsERC20SwapToConversion[artifactsVersion].deployment[networkName];

// Check the artifact has been deployed into the specified network
if (!deploymentInformation) {
Expand Down
Expand Up @@ -15,9 +15,9 @@ const Erc20ConversionProxy = artifacts.require('./Erc20ConversionProxy.sol');

const FakeSwapRouter = artifacts.require('./FakeSwapRouter.sol');

const ERC20SwapToPayWithConversion = artifacts.require('./ERC20SwapToPayWithConversion.sol');
const ERC20SwapToConversion = artifacts.require('./ERC20SwapToConversion.sol');

contract('ERC20SwapToPayWithConversion', function (accounts) {
contract('ERC20SwapToConversion', function (accounts) {
const admin = accounts[0];
const from = accounts[1];
const to = accounts[2];
Expand All @@ -32,7 +32,7 @@ contract('ERC20SwapToPayWithConversion', function (accounts) {
let erc20FeeProxy;
let erc20ConversionProxy;
let fakeSwapRouter;
let testERC20SwapToPayWithConversion;
let testERC20SwapToConversion;
let initialFromBalance;
let chainlinkConversion;
let aggTest;
Expand Down Expand Up @@ -83,10 +83,10 @@ contract('ERC20SwapToPayWithConversion', function (accounts) {
from: admin,
});

testERC20SwapToPayWithConversion = await ERC20SwapToPayWithConversion.new(fakeSwapRouter.address, erc20ConversionProxy.address);
testERC20SwapToConversion = await ERC20SwapToConversion.new(fakeSwapRouter.address, erc20ConversionProxy.address);

initialFromBalance = await spentErc20.balanceOf(from);
await spentErc20.approve(testERC20SwapToPayWithConversion.address, initialFromBalance, { from });
await spentErc20.approve(testERC20SwapToConversion.address, initialFromBalance, { from });
});

expectPayerBalanceUnchanged = async () => {
Expand All @@ -96,19 +96,19 @@ contract('ERC20SwapToPayWithConversion', function (accounts) {

afterEach(async () => {
// The contract should never keep any fund
const contractPaymentCcyBalance = await paymentNetworkErc20.balanceOf(testERC20SwapToPayWithConversion.address);
const contractRequestCcyBalance = await spentErc20.balanceOf(testERC20SwapToPayWithConversion.address);
const contractPaymentCcyBalance = await paymentNetworkErc20.balanceOf(testERC20SwapToConversion.address);
const contractRequestCcyBalance = await spentErc20.balanceOf(testERC20SwapToConversion.address);
expect(contractPaymentCcyBalance.toNumber()).to.equals(0);
expect(contractRequestCcyBalance.toNumber()).to.equals(0);
});

it('converts, swaps and pays the request', async function () {
const beforePayerBalance = await spentErc20.balanceOf(from);
await testERC20SwapToPayWithConversion.approvePaymentProxyToSpend(paymentNetworkErc20.address);
await testERC20SwapToPayWithConversion.approveRouterToSpend(spentErc20.address);
await testERC20SwapToConversion.approvePaymentProxyToSpend(paymentNetworkErc20.address);
await testERC20SwapToConversion.approveRouterToSpend(spentErc20.address);

// Simulate request payment for 10 (fiat) + 1 (fiat) fee, in paymentNetworkErc20
let { tx } = await testERC20SwapToPayWithConversion.swapTransferWithReference(
let { tx } = await testERC20SwapToConversion.swapTransferWithReference(
to,
fiatDecimal.mul(10),
erc20Decimal.mul(70),
Expand Down Expand Up @@ -152,7 +152,7 @@ contract('ERC20SwapToPayWithConversion', function (accounts) {
let {
tx,
receipt: { gasUsed },
} = await testERC20SwapToPayWithConversion.swapTransferWithReference(
} = await testERC20SwapToConversion.swapTransferWithReference(
to,
0,
0,
Expand Down Expand Up @@ -191,7 +191,7 @@ contract('ERC20SwapToPayWithConversion', function (accounts) {

it('cannot swap with a too low maximum spent', async function () {
await expectRevert.unspecified(
testERC20SwapToPayWithConversion.swapTransferWithReference(
testERC20SwapToConversion.swapTransferWithReference(
to,
fiatDecimal.mul(10),
erc20Decimal.mul(50),
Expand All @@ -210,7 +210,7 @@ contract('ERC20SwapToPayWithConversion', function (accounts) {

it('cannot swap with a past deadline', async function () {
await expectRevert.unspecified(
testERC20SwapToPayWithConversion.swapTransferWithReference(
testERC20SwapToConversion.swapTransferWithReference(
to,
fiatDecimal.mul(10),
erc20Decimal.mul(66),
Expand All @@ -229,10 +229,10 @@ contract('ERC20SwapToPayWithConversion', function (accounts) {


it('cannot swap more tokens than liquidity', async function () {
await spentErc20.approve(testERC20SwapToPayWithConversion.address, erc20Decimal.mul(6600), { from });
await spentErc20.approve(testERC20SwapToConversion.address, erc20Decimal.mul(6600), { from });

await expectRevert.unspecified(
testERC20SwapToPayWithConversion.swapTransferWithReference(
testERC20SwapToConversion.swapTransferWithReference(
to,
fiatDecimal.mul(1000),
erc20Decimal.mul(6600),
Expand All @@ -250,10 +250,10 @@ contract('ERC20SwapToPayWithConversion', function (accounts) {
});

it('cannot swap more tokens than allowance', async function () {
await spentErc20.approve(testERC20SwapToPayWithConversion.address, erc20Decimal.mul(60), { from });
await spentErc20.approve(testERC20SwapToConversion.address, erc20Decimal.mul(60), { from });

await expectRevert.unspecified(
testERC20SwapToPayWithConversion.swapTransferWithReference(
testERC20SwapToConversion.swapTransferWithReference(
to,
fiatDecimal.mul(10),
erc20Decimal.mul(66),
Expand Down

0 comments on commit ac500a4

Please sign in to comment.