Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MCPayment: add erc 20 withdraw #338

Merged
merged 6 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions contracts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@iden3/contracts",
"description": "Smart Contract library for Solidity",
"version": "2.5.6",
"version": "2.5.7",
"files": [
"**/*.sol",
"/build/contracts/*.json",
Expand Down
20 changes: 20 additions & 0 deletions contracts/payment/MCPayment.sol
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,14 @@ contract MCPayment is Ownable2StepUpgradeable, EIP712Upgradeable {
return $.ownerBalance;
}

/**
* @dev Get owner ERC-20 balance
* @return balance of owner
*/
function getOwnerERC20Balance(address token) public view onlyOwner returns (uint256) {
return IERC20(token).balanceOf(address(this));
}

/**
* @dev Withdraw balance to issuer
*/
Expand All @@ -339,6 +347,18 @@ contract MCPayment is Ownable2StepUpgradeable, EIP712Upgradeable {
_withdraw(amount, owner());
}

/**
* @dev Withdraw ERC-20 balance to owner
*/
function ownerERC20Withdraw(address token) public onlyOwner {
uint amount = IERC20(token).balanceOf(address(this));
if (amount == 0) {
revert WithdrawError("There is no balance to withdraw");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would avoid adding strings as custom error params. It may cost more gas. Better change the error name

}

IERC20(token).transfer(owner(), amount);
}

function _recoverERC20PaymentSignature(
Iden3PaymentRailsERC20RequestV1 memory paymentData,
bytes memory signature
Expand Down
48 changes: 31 additions & 17 deletions test/payment/mc-payment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ describe("MC Payment Contract", () => {
};
const signature = await issuer1Signer.signTypedData(domainData, types, paymentData);

await payment.connect(userSigner).pay(paymentData, signature, {
value: 100,
});
await expect(
payment.connect(userSigner).pay(paymentData, signature, {
value: 100,
}),
).to.changeEtherBalances([userSigner, payment], [-100, 100]);

const isPaymentDone = await payment.isPaymentDone(issuer1Signer.address, 25);
expect(isPaymentDone).to.be.true;
Expand Down Expand Up @@ -236,14 +238,20 @@ describe("MC Payment Contract", () => {
.payERC20.estimateGas(paymentData, signature);
console.log("ERC-20 Payment Gas: " + erc20PaymentGas);

await payment.connect(userSigner).payERC20(paymentData, signature);

expect(await token.balanceOf(await userSigner.getAddress())).to.be.eq(90);
// 10 - 10% owner fee = 9
expect(await token.balanceOf(await issuer1Signer.getAddress())).to.be.eq(9);
expect(await token.balanceOf(await payment.getAddress())).to.be.eq(1);

await expect(
payment.connect(userSigner).payERC20(paymentData, signature),
).to.changeTokenBalances(token, [userSigner, issuer1Signer, payment], [-10, 9, 1]);
expect(await payment.isPaymentDone(issuer1Signer.address, 35)).to.be.true;
// owner ERC-20 withdraw
const tokenAddress = await token.getAddress();
const ownerBalance = await payment.getOwnerERC20Balance(tokenAddress);
expect(ownerBalance).to.be.eq(1);
await expect(payment.connect(owner).ownerERC20Withdraw(tokenAddress)).to.changeTokenBalances(
token,
[owner, payment],
[1, -1],
);
expect(await payment.getOwnerERC20Balance(tokenAddress)).to.be.eq(0);
});

it("ERC-20 payment - invalid signature", async () => {
Expand Down Expand Up @@ -364,14 +372,20 @@ describe("MC Payment Contract", () => {
.payERC20Permit.estimateGas(permitSignature, paymentData, signature);
console.log("EIP-2612 Payment Gas: " + eip2612PaymentGas);

await payment.connect(userSigner).payERC20Permit(permitSignature, paymentData, signature);

expect(await token.balanceOf(await userSigner.getAddress())).to.be.eq(90);
// 10 - 10% owner fee = 9
expect(await token.balanceOf(await issuer1Signer.getAddress())).to.be.eq(9);
expect(await token.balanceOf(await payment.getAddress())).to.be.eq(1);

await expect(
payment.connect(userSigner).payERC20Permit(permitSignature, paymentData, signature),
).to.changeTokenBalances(token, [userSigner, issuer1Signer, payment], [-10, 9, 1]);
expect(await payment.isPaymentDone(issuer1Signer.address, 35)).to.be.true;

// owner ERC-20 withdraw
const tokenAddress = await token.getAddress();
const ownerBalance = await payment.getOwnerERC20Balance(tokenAddress);
expect(ownerBalance).to.be.eq(1);
await expect(payment.connect(owner).ownerERC20Withdraw(tokenAddress)).to.changeTokenBalances(
token,
[owner, payment],
[1, -1],
);
});

it("ERC-20 Permit (EIP-2612) payment - invalid permit signature length:", async () => {
Expand Down
Loading