Skip to content

Commit

Permalink
consider new function to implement permit2
Browse files Browse the repository at this point in the history
  • Loading branch information
cwsnt committed Jan 19, 2024
1 parent 36f9645 commit 3795e66
Show file tree
Hide file tree
Showing 4 changed files with 377 additions and 375 deletions.
83 changes: 46 additions & 37 deletions contracts/integration/MoC/MocIntegration.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,55 @@ contract MocIntegration is OwnableUpgradeable, ERC1967UpgradeUpgradeable {
) external {
// transfer _dllrAmount to this contract by permit (EIP-2612)
address thisAddress = address(this);
dllr.transferWithPermit(
msg.sender,
thisAddress,
_dllrAmount,
_permitParams.deadline,
_permitParams.v,
_permitParams.r,
_permitParams.s
);

// redeem DoC from DLLR
require(
massetManager.redeemTo(address(doc), _dllrAmount, thisAddress) == _dllrAmount,
"MocIntegration:: redeemed incorrect DoC amount"
);

// redeem RBTC from DoC using Money On Chain and send to the user
uint256 rbtcBalanceBefore = thisAddress.balance;
moc.redeemFreeDocVendors(_dllrAmount, payable(mocVendorAccount));
uint256 rbtcAmount = thisAddress.balance - rbtcBalanceBefore;
(bool success, ) = msg.sender.call{ value: rbtcAmount }("");
require(success, "MocIntegration:: error transferring redeemed RBTC");

emit GetDocFromDllrAndRedeemRBTC(msg.sender, _dllrAmount, rbtcAmount);
}

/**
* @notice how getDocFromDllrAndRedeemRBTC function works:
* -------------------------------------------------------------------------------------------
* | Mynt | Money On Chain |
* -------------------------------------------------------------------------------------------
* | get DLLR (EIP-2612) -> convert DLLR to DoC | -> get RBTC from DoC -> send RBTC to user |
* -------------------------------------------------------------------------------------------
*
* @param permit permit data, in form of PermitTransferFrom struct.
* @param signature signatue of the permit data.
*/
function getDocFromDllrAndRedeemRBTCWithPermit2(
ISignatureTransfer.PermitTransferFrom memory permit,
bytes memory signature
) external {
address thisAddress = address(this);
uint256 _dllrAmount = permit.permitted.amount;

ISignatureTransfer.PermitTransferFrom memory permit = _generateERC20PermitTransfer(_dllrAmount, _permitParams.deadline, _useNonce(msg.sender));
ISignatureTransfer.SignatureTransferDetails memory transferDetails = _generateTransferDetails(thisAddress, _dllrAmount);

permit2.permitTransferFrom(permit, transferDetails, msg.sender, _generatePermit2Signature(_permitParams.v, _permitParams.r, _permitParams.s));
permit2.permitTransferFrom(permit, transferDetails, msg.sender, signature);

_useNonce(msg.sender);

// redeem DoC from DLLR
require(
Expand Down Expand Up @@ -123,28 +167,6 @@ contract MocIntegration is OwnableUpgradeable, ERC1967UpgradeUpgradeable {
return ERC1967UpgradeUpgradeable._getImplementation();
}

/**
* @dev view function to construct PermiTransferFrom struct to be used by Permit2
*
* @param _amount amount of transfer
* @param _deadline signature deadline
* @param _nonce nonce
*
* @return PermitTransferFrom struct object
*/
function _generateERC20PermitTransfer(uint256 _amount, uint256 _deadline, uint256 _nonce) private view returns (ISignatureTransfer.PermitTransferFrom memory) {
ISignatureTransfer.PermitTransferFrom memory permit = ISignatureTransfer.PermitTransferFrom({
permitted: ISignatureTransfer.TokenPermissions({
token: address(dllr),
amount: _amount
}),
nonce: _nonce,
deadline: _deadline
});

return permit;
}

/**
* @dev view function to construct SignatureTransferDetails struct to be used by Permit2
*
Expand All @@ -162,19 +184,6 @@ contract MocIntegration is OwnableUpgradeable, ERC1967UpgradeUpgradeable {
return transferDetails;
}

/**
* @dev view function to generate permit2 signature
*
* @param _v v component of ECDSA signature
* @param _r r component of ECDSA signature
* @param _s s component of ECDSA signature
*
* @return constructed signature
*/
function _generatePermit2Signature(uint8 _v, bytes32 _r, bytes32 _s) private pure returns (bytes memory) {
return bytes.concat(_r, _s, bytes1(_v));
}

/**
* @dev "Consume a nonce": return the current value and increment.
*
Expand Down
2 changes: 1 addition & 1 deletion docs/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"init": "yarn install && set TS_NODE_TRANSPILE_ONLY=1 && yarn hardhat compile --force",
"prepare": "yarn patch-package && rm -rf ./dist/ && set TS_NODE_TRANSPILE_ONLY=1 && yarn hardhat compile --force && tsc",
"contract-sizes": "yarn hardhat size-contracts",
"prettify": "prettier --write test/**/*.ts types/generated/*.ts types/**/*.ts contracts/**/*.sol",
"prettify": "prettier --write tests/**/*.ts types/generated/*.ts types/**/*.ts contracts/**/*.sol",
"prettier-check": "prettier --check .",
"flatten": "sol-merger \"./contracts/[^fish]**/*.sol\" ./_flat",
"prepublishOnly": "yarn run compile",
Expand Down
Loading

0 comments on commit 3795e66

Please sign in to comment.