Skip to content

Commit

Permalink
Add unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dadogg80 committed Nov 11, 2021
1 parent a826689 commit c5cf000
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 30 deletions.
52 changes: 26 additions & 26 deletions packages/payment-processor/src/payment/erc20-escrow-payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@ import {
import { ITransactionOverrides } from './transaction-overrides';
import { encodeApproveAnyErc20 } from './erc20';

/**
* Processes the approval transaction of the payment ERC20 to be spent by the erc20EscrowToPay contract,
* during the fee proxy delegate call.
* @param request request to pay, used to know the network
* @param paymentTokenAddress picked currency to pay
* @param signerOrProvider the web3 provider. Defaults to Etherscan.
* @param overrides optionally, override default transaction values, like gas.
*/
export async function approveErc20ForEscrow(
request: ClientTypes.IRequestData,
paymentTokenAddress: string,
signerOrProvider: providers.Provider | Signer = getProvider(),
overrides?: ITransactionOverrides,
): Promise<ContractTransaction> {
const contractAddress = erc20EscrowToPayArtifact.getAddress(request.currencyInfo.network!);
const encodedTx = encodeApproveAnyErc20(paymentTokenAddress, contractAddress, signerOrProvider);
const signer = getSigner(signerOrProvider);
const tx = await signer.sendTransaction({
data: encodedTx,
to: paymentTokenAddress,
value: 0,
...overrides,
});
return tx;
}

/**
* Processes a transaction to payEscrow().
* @param request request to pay.
Expand Down Expand Up @@ -417,29 +443,3 @@ export function encodeRequestMapping(
`0x${paymentReference}`,
]);
}

/**
* Processes the approval transaction of the payment ERC20 to be spent by the erc20EscrowToPay contract,
* during the fee proxy delegate call.
* @param request request to pay, used to know the network
* @param paymentTokenAddress picked currency to pay
* @param signerOrProvider the web3 provider. Defaults to Etherscan.
* @param overrides optionally, override default transaction values, like gas.
*/
export async function approveErc20ForEscrow(
request: ClientTypes.IRequestData,
paymentTokenAddress: string,
signerOrProvider: providers.Provider | Signer = getProvider(),
overrides?: ITransactionOverrides,
): Promise<ContractTransaction> {
const contractAddress = erc20EscrowToPayArtifact.getAddress(request.currencyInfo.network!);
const encodedTx = encodeApproveAnyErc20(paymentTokenAddress, contractAddress, signerOrProvider);
const signer = getSigner(signerOrProvider);
const tx = await signer.sendTransaction({
data: encodedTx,
to: paymentTokenAddress,
value: 0,
...overrides,
});
return tx;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {
import Utils from '@requestnetwork/utils';
import {
approveErc20ForEscrow,
payRequestFromEscrow,
payEscrow,
payRequestFromEscrow,
initiateEmergencyClaim,
requestMapping,
encodeRequestMapping,
encodePayEscrow,
encodePayRequestFromEscrow,
Expand All @@ -20,7 +22,7 @@ import {
encodeFreezeRequest,
encodeRefundFrozenFunds,
} from '../../src/payment/erc20-escrow-payment';
import { getRequestPaymentValues } from '../../src/payment/utils';
import { getRequestPaymentValues, getSigner } from '../../src/payment/utils';

import { erc20EscrowToPayArtifact } from '@requestnetwork/smart-contracts';
import { getErc20Balance } from '../../src/payment/erc20';
Expand Down Expand Up @@ -196,7 +198,7 @@ describe('erc20-escrow-payment tests:', () => {
it('Should withdraw funds and pay funds from escrow to payee', async () => {
// Set a new requestID to test independent unit-tests.
const request = Utils.deepCopy(validRequest) as ClientTypes.IRequestData;
request.requestId = "aabb";
request.requestId = 'aabb';

// Execute payEscrow
await payEscrow(request, wallet, undefined, undefined);
Expand All @@ -206,7 +208,7 @@ describe('erc20-escrow-payment tests:', () => {
const escrowBeforeBalance = await getErc20Balance(request, escrowAddress);

await payRequestFromEscrow(request, wallet);

// Stores balances after withdraws to compare before balance with after balance.
const payeeAfterBalance = await getErc20Balance(request, paymentAddress);
const escrowAfterBalance = await getErc20Balance(request, escrowAddress);
Expand All @@ -220,6 +222,28 @@ describe('erc20-escrow-payment tests:', () => {
BigNumber.from(payeeAfterBalance).eq(BigNumber.from(payeeBeforeBalance).add(100)),
).toBeTruthy();
});
it('Should set funds in emergency state', async () => {
// Set a new requestID to test independent unit-tests.
const request = Utils.deepCopy(validRequest) as ClientTypes.IRequestData;
request.requestId = 'aacc';

// Assign the paymentAddress as the payee.
const payee = getSigner(provider, paymentAddress);

// Execute payEscrow.
expect(
await (await payEscrow(request, wallet, undefined, undefined)).wait(1),
).toBeTruthy();

// Initiate emergency claim.
expect(
await (await initiateEmergencyClaim(request, payee)).wait(1),
).toBeTruthy();

const tx = await requestMapping(request, wallet);
const confirmedTx = await tx.wait(1);
expect(confirmedTx).toBeTruthy();

});
});
});

0 comments on commit c5cf000

Please sign in to comment.