add permit
This commit is contained in:
@@ -53,6 +53,23 @@ contract DVM is DVMTrader, DVMFunding {
|
||||
name = string(abi.encodePacked(suffix, connect, addressToShortString(address(this))));
|
||||
symbol = "DLP";
|
||||
decimals = _BASE_TOKEN_.decimals();
|
||||
|
||||
// ============================== Permit ====================================
|
||||
uint256 chainId;
|
||||
assembly {
|
||||
chainId := chainid()
|
||||
}
|
||||
DOMAIN_SEPARATOR = keccak256(
|
||||
abi.encode(
|
||||
// keccak256('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)'),
|
||||
0x8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f,
|
||||
keccak256(bytes(name)),
|
||||
keccak256(bytes('1')),
|
||||
chainId,
|
||||
address(this)
|
||||
)
|
||||
);
|
||||
// ==========================================================================
|
||||
}
|
||||
|
||||
function addressToShortString(address _addr) public pure returns (string memory) {
|
||||
@@ -68,7 +85,6 @@ contract DVM is DVMTrader, DVMFunding {
|
||||
}
|
||||
|
||||
// ============ Version Control ============
|
||||
|
||||
function version() external pure returns (string memory) {
|
||||
return "DVM 1.0.0";
|
||||
}
|
||||
|
||||
@@ -67,14 +67,15 @@ contract DVMFunding is DVMVault {
|
||||
address to,
|
||||
uint256 baseMinAmount,
|
||||
uint256 quoteMinAmount,
|
||||
bytes calldata data
|
||||
bytes calldata data,
|
||||
uint256 deadline
|
||||
) external preventReentrant returns (uint256 baseAmount, uint256 quoteAmount) {
|
||||
require(deadline >= block.timestamp, 'DODOV2 DVMFUNDING: EXPIRED');
|
||||
require(shareAmount <= _SHARES_[msg.sender], "DLP_NOT_ENOUGH");
|
||||
uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
|
||||
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
|
||||
uint256 totalShares = totalSupply;
|
||||
|
||||
require(shareAmount <= _SHARES_[msg.sender], "DLP_NOT_ENOUGH");
|
||||
|
||||
baseAmount = baseBalance.mul(shareAmount).div(totalShares);
|
||||
quoteAmount = quoteBalance.mul(shareAmount).div(totalShares);
|
||||
|
||||
|
||||
@@ -49,6 +49,14 @@ contract DVMStorage is InitializableOwnable, ReentrancyGuard {
|
||||
mapping(address => uint256) internal _SHARES_;
|
||||
mapping(address => mapping(address => uint256)) internal _ALLOWED_;
|
||||
|
||||
|
||||
// ================= Permit ======================
|
||||
bytes32 public DOMAIN_SEPARATOR;
|
||||
// keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
|
||||
bytes32 public constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9;
|
||||
mapping(address => uint256) public nonces;
|
||||
// ===============================================
|
||||
|
||||
// ============ Variables for Pricing ============
|
||||
|
||||
IFeeRateModel public _LP_FEE_RATE_MODEL_;
|
||||
|
||||
@@ -117,11 +117,15 @@ contract DVMVault is DVMStorage {
|
||||
* @param amount The amount of tokens to be spent.
|
||||
*/
|
||||
function approve(address spender, uint256 amount) public returns (bool) {
|
||||
_ALLOWED_[msg.sender][spender] = amount;
|
||||
emit Approval(msg.sender, spender, amount);
|
||||
_approve(msg.sender, spender, amount);
|
||||
return true;
|
||||
}
|
||||
|
||||
function _approve(address owner, address spender, uint256 amount) private {
|
||||
_ALLOWED_[owner][spender] = amount;
|
||||
emit Approval(owner, spender, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Function to check the amount of tokens that an owner _ALLOWED_ to a spender.
|
||||
* @param owner address The address which owns the funds.
|
||||
@@ -146,5 +150,21 @@ contract DVMVault is DVMStorage {
|
||||
emit Transfer(user, address(0), value);
|
||||
}
|
||||
|
||||
// ============================ Permit ======================================
|
||||
function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external {
|
||||
require(deadline >= block.timestamp, 'DODO_DVM_LP: EXPIRED');
|
||||
bytes32 digest = keccak256(
|
||||
abi.encodePacked(
|
||||
'\x19\x01',
|
||||
DOMAIN_SEPARATOR,
|
||||
keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, nonces[owner]++, deadline))
|
||||
)
|
||||
);
|
||||
address recoveredAddress = ecrecover(digest, v, r, s);
|
||||
require(recoveredAddress != address(0) && recoveredAddress == owner, 'DODO_DVM_LP: INVALID_SIGNATURE');
|
||||
_approve(owner, spender, value);
|
||||
}
|
||||
// ===========================================================================
|
||||
|
||||
// function approveAndCall()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user