dsa-connectors-old/contracts/connectors/fee.sol

118 lines
3.2 KiB
Solidity
Raw Permalink Normal View History

2020-12-16 14:34:24 +00:00
pragma solidity ^0.6.0;
pragma experimental ABIEncoderV2;
interface MemoryInterface {
function getUint(uint _id) external returns (uint _num);
function setUint(uint _id, uint _val) external;
}
contract DSMath {
uint256 constant WAD = 10 ** 18;
function add(uint x, uint y) internal pure returns (uint z) {
require((z = x + y) >= x, "math-not-safe");
}
2021-01-04 20:07:01 +00:00
function sub(uint x, uint y) internal pure returns (uint z) {
require((z = x - y) <= x, "sub-overflow");
}
2020-12-16 14:34:24 +00:00
function mul(uint x, uint y) internal pure returns (uint z) {
require(y == 0 || (z = x * y) / y == x, "math-not-safe");
}
function wmul(uint x, uint y) internal pure returns (uint z) {
z = add(mul(x, y), WAD / 2) / WAD;
}
}
contract Setup is DSMath {
/**
* @dev Return InstAaMemory Address.
*/
function getMemoryAddr() internal pure returns (address) {
return 0x8a5419CfC711B2343c17a6ABf4B2bAFaBb06957F;
}
/**
* @dev Get Uint value from InstaMemory Contract.
*/
function getUint(uint getId, uint val) internal returns (uint returnVal) {
returnVal = getId == 0 ? val : MemoryInterface(getMemoryAddr()).getUint(getId);
}
/**
* @dev Set Uint value in InstaMemory Contract.
*/
function setUint(uint setId, uint val) internal {
if (setId != 0) MemoryInterface(getMemoryAddr()).setUint(setId, val);
}
/**
2020-12-17 14:25:09 +00:00
* @dev Connector ID and Type
2020-12-16 14:34:24 +00:00
*/
function connectorID() public pure returns(uint _type, uint _id) {
2021-01-04 20:07:01 +00:00
(_type, _id) = (1, 70);
2020-12-16 14:34:24 +00:00
}
}
contract FeeResolver is Setup {
/**
* @dev Calculate fee
2020-12-17 14:25:09 +00:00
* @param amount token amount to caculate fee.
* @param fee fee percentage. Eg: 1% => 1e17, 100% => 1e18.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set total amount at this ID in `InstaMemory` Contract.
* @param setIdFee Set only fee amount at this ID in `InstaMemory` Contract.
2020-12-16 14:34:24 +00:00
*/
2021-01-04 20:07:01 +00:00
function calculateFee(
uint amount,
uint fee,
uint getId,
uint setId,
uint setIdFee
) external payable {
2020-12-16 14:34:24 +00:00
uint _amt = getUint(getId, amount);
uint feeAmt = wmul(_amt, fee);
uint totalAmt = add(_amt, feeAmt);
setUint(setId, totalAmt);
2020-12-16 14:56:02 +00:00
setUint(setIdFee, feeAmt);
2020-12-16 14:34:24 +00:00
}
2021-01-04 20:07:01 +00:00
/**
* @dev Calculate amount minus fee
* @param amount token amount to caculate fee.
* @param fee fee percentage. Eg: 1% => 1e17, 100% => 1e18.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setIdAmtMinusFee Set amount minus fee amount at this ID in `InstaMemory` Contract.
* @param setIdFee Set only fee amount at this ID in `InstaMemory` Contract.
*/
function calculateAmtMinusFee(
uint amount,
uint fee,
uint getId,
uint setIdAmtMinusFee,
uint setIdFee
) external payable {
uint _amt = getUint(getId, amount);
uint feeAmt = wmul(_amt, fee);
uint amountMinusFee = sub(_amt, feeAmt);
setUint(setIdAmtMinusFee, amountMinusFee);
setUint(setIdFee, feeAmt);
}
2020-12-16 14:34:24 +00:00
}
contract ConnectFee is FeeResolver {
2021-01-04 20:07:01 +00:00
string public constant name = "Fee-v1.1";
2020-12-16 14:34:24 +00:00
}