-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from faranak-cs/main
Add contracts
- Loading branch information
Showing
8 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
interface IERC20Permit { | ||
function totalSupply() external view returns (uint); | ||
|
||
function balanceOf(address account) external view returns (uint); | ||
|
||
function transfer(address recipient, uint amount) external returns (bool); | ||
|
||
function allowance( | ||
address owner, | ||
address spender | ||
) external view returns (uint); | ||
|
||
function approve(address spender, uint amount) external returns (bool); | ||
|
||
function transferFrom( | ||
address sender, | ||
address recipient, | ||
uint amount | ||
) external returns (bool); | ||
|
||
function permit( | ||
address owner, | ||
address spender, | ||
uint value, | ||
uint deadline, | ||
uint8 v, | ||
bytes32 r, | ||
bytes32 s | ||
) external; | ||
|
||
event Transfer(address indexed from, address indexed to, uint value); | ||
event Approval(address indexed owner, address indexed spender, uint value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol"; | ||
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
|
||
contract OurToken is ERC20, ERC20Permit { | ||
constructor() ERC20("OurToken", "OT") ERC20Permit("OurToken") {} | ||
|
||
function mint(address _to, uint _amount) external { | ||
_mint(_to, _amount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import "./IERC20Permit.sol"; | ||
|
||
contract Vault { | ||
IERC20Permit public immutable token; | ||
|
||
constructor(address _token) { | ||
token = IERC20Permit(_token); | ||
} | ||
|
||
// transfer funds by executing transaction | ||
function deposit(uint amount) external { | ||
token.transferFrom(msg.sender, address(this), amount); | ||
} | ||
|
||
// transfer funds by using digital signature | ||
function depositWithPermit( | ||
uint amount, | ||
uint deadline, | ||
uint8 v, | ||
bytes32 r, | ||
bytes32 s | ||
) external { | ||
token.permit(msg.sender, address(this), amount, deadline, v, r, s); | ||
token.transferFrom(msg.sender, address(this), amount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
const { expect } = require("chai"); | ||
const { ethers } = require("hardhat"); | ||
|
||
async function getPermitSignature(signer, token, spender, value, deadline) { | ||
const [nonce, name, version, chainId] = await Promise.all([ | ||
token.nonces(signer.address), | ||
token.name(), | ||
"1", | ||
31337, | ||
]); | ||
|
||
return ethers.Signature.from( | ||
await signer.signTypedData( | ||
{ | ||
name, | ||
version, | ||
chainId, | ||
verifyingContract: token.target, | ||
}, | ||
{ | ||
Permit: [ | ||
{ | ||
name: "owner", | ||
type: "address", | ||
}, | ||
{ | ||
name: "spender", | ||
type: "address", | ||
}, | ||
{ | ||
name: "value", | ||
type: "uint256", | ||
}, | ||
{ | ||
name: "nonce", | ||
type: "uint256", | ||
}, | ||
{ | ||
name: "deadline", | ||
type: "uint256", | ||
}, | ||
], | ||
}, | ||
{ | ||
owner: signer.address, | ||
spender, | ||
value, | ||
nonce, | ||
deadline, | ||
} | ||
) | ||
); | ||
} | ||
|
||
describe("ERC20Permit", function () { | ||
it("ERC20 permit", async function () { | ||
const accounts = await ethers.getSigners(1); | ||
const signer = accounts[0]; | ||
|
||
const OurToken = await ethers.getContractFactory("OurToken"); | ||
const ourToken = await OurToken.deploy(); | ||
await ourToken.waitForDeployment(); | ||
|
||
// assign the token contract address to vault contract | ||
|
||
const Vault = await ethers.getContractFactory("Vault"); | ||
const vault = await Vault.deploy(ourToken.target); | ||
await vault.waitForDeployment(); | ||
|
||
// amount of tokens to be minted | ||
const amount = 1000; | ||
// call mint function | ||
await ourToken.mint(signer.address, amount); | ||
|
||
const deadline = ethers.MaxUint256; | ||
|
||
const { v, r, s } = await getPermitSignature( | ||
signer, | ||
ourToken, | ||
vault.target, | ||
amount, | ||
deadline | ||
); | ||
|
||
await vault.depositWithPermit(amount, deadline, v, r, s); | ||
|
||
// test cases | ||
expect(await ourToken.balanceOf(vault.target)).to.equal(amount); | ||
}); | ||
}); |