From aaa701c01525a980f3aca091e34e012523dad4ad Mon Sep 17 00:00:00 2001 From: David Asamonye Date: Tue, 27 Sep 2022 09:53:07 +0100 Subject: [PATCH 1/9] fixed issue with fees --- contracts/Vault.sol | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/contracts/Vault.sol b/contracts/Vault.sol index 496a3ec..731b1ec 100644 --- a/contracts/Vault.sol +++ b/contracts/Vault.sol @@ -53,7 +53,7 @@ contract Vault is ReentrancyGuard, Ownable { * Net User Mint * Maps user address => cumulative mint value */ - mapping(address => uint256) private netMintUser; + mapping(address => uint256) public netMintUser; mapping(address => uint256) private grossMintUser; @@ -62,12 +62,13 @@ contract Vault is ReentrancyGuard, Ownable { /** * Net Global Mint */ - uint256 private netMintGlobal; + uint256 public netMintGlobal; + uint256 public globalDebt; /** * map users to accrued fee balance * store 75% swap fee to be shared by minters - * store 25% swap fee seaparately + * store 25% swap fee separately * user => uint256 */ @@ -226,27 +227,19 @@ contract Vault is ReentrancyGuard, Ownable { MULTIPLIER; /** - * Send the treasury amount from User to a treasury wallet + * Send the treasury amount to a treasury wallet */ - IERC20(zUSD).transferFrom( - msg.sender, - treasuryWallet, - treasuryFeePerTransaction - ); + bool treasuryFeeMint = _mint(zUSD, treasuryWallet, treasuryFeePerTransaction); + + if (!treasuryFeeMint) revert MintFailed(); /** - * @TODO - Implement a more elegent solution * Send the global minters fee from User to the global minters fee wallet */ - IERC20(zUSD).transferFrom( - msg.sender, - mintersWallet, - globalMintersFeePerTransaction - ); + bool GlobalMintersFee = _mint(zUSD, address(this), globalMintersFeePerTransaction); + + if (!GlobalMintersFee) revert MintFailed(); - /** - * @TODO - Send the remaining fee to all minters - */ for (uint256 i = 0; i < mintersAddresses.length; i++) { mintersRewardPerTransaction[mintersAddresses[i]] = ((netMintUser[mintersAddresses[i]] * MULTIPLIER) / @@ -653,14 +646,13 @@ contract Vault is ReentrancyGuard, Ownable { function _updateUserDebtOutstanding( uint256 _netMintUserzUSDValue, uint256 _netMintGlobalzUSDValue - ) public view returns (uint256) { + ) public returns (uint256) { require( _netMintGlobalzUSDValue > 0, "Global zUSD mint too low, underflow may occur" ); uint256 userDebtOutstanding; - uint256 globalDebt; uint256 mintRatio; globalDebt = @@ -686,7 +678,7 @@ contract Vault is ReentrancyGuard, Ownable { /** * Helper function to test the impact of a transaction i.e mint, burn, deposit or withdrawal by a user */ - function _testImpact() internal view returns (bool) { + function _testImpact() internal returns (bool) { uint256 userDebt; /** * If the netMintGlobal is 0, then From d9ff9b5e4bf7045904c4dd89ea3f853fbb6fccd4 Mon Sep 17 00:00:00 2001 From: David Asamonye Date: Tue, 27 Sep 2022 10:12:24 +0100 Subject: [PATCH 2/9] made testImpact a view function --- contracts/Vault.sol | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/contracts/Vault.sol b/contracts/Vault.sol index 731b1ec..5c43987 100644 --- a/contracts/Vault.sol +++ b/contracts/Vault.sol @@ -63,8 +63,6 @@ contract Vault is ReentrancyGuard, Ownable { * Net Global Mint */ uint256 public netMintGlobal; - - uint256 public globalDebt; /** * map users to accrued fee balance * store 75% swap fee to be shared by minters @@ -646,12 +644,12 @@ contract Vault is ReentrancyGuard, Ownable { function _updateUserDebtOutstanding( uint256 _netMintUserzUSDValue, uint256 _netMintGlobalzUSDValue - ) public returns (uint256) { + ) public view returns (uint256) { require( _netMintGlobalzUSDValue > 0, "Global zUSD mint too low, underflow may occur" ); - + uint256 globalDebt; uint256 userDebtOutstanding; uint256 mintRatio; @@ -678,7 +676,7 @@ contract Vault is ReentrancyGuard, Ownable { /** * Helper function to test the impact of a transaction i.e mint, burn, deposit or withdrawal by a user */ - function _testImpact() internal returns (bool) { + function _testImpact() internal view returns (bool) { uint256 userDebt; /** * If the netMintGlobal is 0, then From f8138c12f8a2aa3666fa338716c1732f98ac0b80 Mon Sep 17 00:00:00 2001 From: David Asamonye Date: Tue, 27 Sep 2022 13:40:56 +0100 Subject: [PATCH 3/9] fixed multiplier in reward fee --- contracts/Vault.sol | 1 + 1 file changed, 1 insertion(+) diff --git a/contracts/Vault.sol b/contracts/Vault.sol index 5c43987..a3bf0bd 100644 --- a/contracts/Vault.sol +++ b/contracts/Vault.sol @@ -192,6 +192,7 @@ contract Vault is ReentrancyGuard, Ownable { swapFeePerTransaction = (swapFee * _amountWithDecimal) / MULTIPLIER; swapFeePerTransactionInUsd = swapFeePerTransaction / _zTokenFromUSDRate; + swapFeePerTransactionInUsd = swapFeePerTransactionInUsd * HALF_MULTIPLIER; /** * Get the USD values of involved zTokens From eb0f364a57558be86708fd22d81d4a090e37411f Mon Sep 17 00:00:00 2001 From: David Asamonye Date: Tue, 27 Sep 2022 13:57:41 +0100 Subject: [PATCH 4/9] optimized rewards fee --- contracts/Vault.sol | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contracts/Vault.sol b/contracts/Vault.sol index a3bf0bd..be21a0f 100644 --- a/contracts/Vault.sol +++ b/contracts/Vault.sol @@ -190,9 +190,8 @@ contract Vault is ReentrancyGuard, Ownable { uint256 _zTokenFromUSDRate = getZTokenUSDRate(_zTokenFrom); uint256 _zTokenToUSDRate = getZTokenUSDRate(_zTokenTo); - swapFeePerTransaction = (swapFee * _amountWithDecimal) / MULTIPLIER; + swapFeePerTransaction = (swapFee * _amountWithDecimal) / HALF_MULTIPLIER; swapFeePerTransactionInUsd = swapFeePerTransaction / _zTokenFromUSDRate; - swapFeePerTransactionInUsd = swapFeePerTransactionInUsd * HALF_MULTIPLIER; /** * Get the USD values of involved zTokens From c11cd561299e9221d28e8deda17142413450b29d Mon Sep 17 00:00:00 2001 From: David Asamonye Date: Tue, 27 Sep 2022 18:22:50 +0100 Subject: [PATCH 5/9] fix overflow in rewards --- contracts/Vault.sol | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contracts/Vault.sol b/contracts/Vault.sol index be21a0f..a3bf0bd 100644 --- a/contracts/Vault.sol +++ b/contracts/Vault.sol @@ -190,8 +190,9 @@ contract Vault is ReentrancyGuard, Ownable { uint256 _zTokenFromUSDRate = getZTokenUSDRate(_zTokenFrom); uint256 _zTokenToUSDRate = getZTokenUSDRate(_zTokenTo); - swapFeePerTransaction = (swapFee * _amountWithDecimal) / HALF_MULTIPLIER; + swapFeePerTransaction = (swapFee * _amountWithDecimal) / MULTIPLIER; swapFeePerTransactionInUsd = swapFeePerTransaction / _zTokenFromUSDRate; + swapFeePerTransactionInUsd = swapFeePerTransactionInUsd * HALF_MULTIPLIER; /** * Get the USD values of involved zTokens From c2e1355060647edd0ce81fe07e7e0da4525f1aa9 Mon Sep 17 00:00:00 2001 From: David Asamonye Date: Tue, 27 Sep 2022 18:58:06 +0100 Subject: [PATCH 6/9] made rates public --- contracts/Vault.sol | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/contracts/Vault.sol b/contracts/Vault.sol index a3bf0bd..6e35577 100644 --- a/contracts/Vault.sol +++ b/contracts/Vault.sol @@ -33,11 +33,10 @@ contract Vault is ReentrancyGuard, Ownable { * exchange rates of 1 USD to zTokens * TODO These should be fetched from an Oracle */ - uint256 private NGNUSD; - uint256 private ZARUSD; - uint256 private XAFUSD; - uint256 private XRATE; - uint256 private USD = 1e3; + uint256 public NGNUSD; + uint256 public ZARUSD; + uint256 public XAFUSD; + uint256 public USD = 1e3; constructor() {} From cfeeacec3380ca9995151977d61ccb28005569e5 Mon Sep 17 00:00:00 2001 From: David Asamonye Date: Thu, 13 Oct 2022 15:02:31 +0100 Subject: [PATCH 7/9] added blacklist, pause transactions --- contracts/Vault.sol | 83 +++++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 22 deletions(-) diff --git a/contracts/Vault.sol b/contracts/Vault.sol index 6e35577..0a062b9 100644 --- a/contracts/Vault.sol +++ b/contracts/Vault.sol @@ -90,6 +90,30 @@ contract Vault is ReentrancyGuard, Ownable { */ address[] public mintersAddresses; + address[] private _blacklistedAddresses; + bool public transactionsPaused = false; + + /** + * @dev modifier to check for blacklisted addresses + */ + modifier blockBlacklistedAddresses() { + for (uint i = 0; i < _blacklistedAddresses.length; i++) { + if (msg.sender == _blacklistedAddresses[i]) { + revert("This address has been blacklisted"); + } + } + _; + } + + modifier isTransactionsPaused() { + require(transactionsPaused == false, "transactions are paused"); + _; + } + + /** + * @dev + */ + event Deposit( address indexed _account, address indexed _token, @@ -119,7 +143,7 @@ contract Vault is ReentrancyGuard, Ownable { */ function depositAndMint(uint256 _depositAmount, uint256 _mintAmount) external - nonReentrant + nonReentrant blockBlacklistedAddresses() isTransactionsPaused() { uint256 _depositAmountWithDecimal = _getDecimal(_depositAmount); uint256 _mintAmountWithDecimal = _getDecimal(_mintAmount); @@ -173,7 +197,7 @@ contract Vault is ReentrancyGuard, Ownable { uint256 _amount, address _zTokenFrom, address _zTokenTo - ) external nonReentrant { + ) external nonReentrant blockBlacklistedAddresses() isTransactionsPaused() { uint256 _amountWithDecimal = _getDecimal(_amount); uint256 swapFeePerTransactionInUsd; uint256 swapAmount; @@ -258,7 +282,7 @@ contract Vault is ReentrancyGuard, Ownable { uint256 _amountToRepay, uint256 _amountToWithdraw, address _zToken - ) external nonReentrant { + ) external nonReentrant blockBlacklistedAddresses() isTransactionsPaused() { uint256 _amountToRepayWithDecimal = _getDecimal(_amountToRepay); uint256 _amountToWithdrawWithDecimal = _getDecimal(_amountToWithdraw); @@ -316,7 +340,7 @@ contract Vault is ReentrancyGuard, Ownable { emit Withdraw(msg.sender, _zToken, _amountToWithdraw); } - function liquidate(address _user) external nonReentrant { + function liquidate(address _user) external nonReentrant blockBlacklistedAddresses() isTransactionsPaused() { uint256 userDebt; uint256 userCollateralRatio; @@ -370,18 +394,11 @@ contract Vault is ReentrancyGuard, Ownable { uint256 totalRewards = userDebt + rewardFee; - bool transferSuccess = IERC20(collateral).transfer( - msg.sender, - totalRewards - ); - - if (!transferSuccess) revert TransferFailed(); - netMintGlobal = netMintGlobal - netMintUser[_user]; netMintUser[_user] = 0; - /** + /** * Possible overflow */ if (userCollateralBalance[_user] >= totalRewards) { @@ -392,6 +409,13 @@ contract Vault is ReentrancyGuard, Ownable { userCollateralBalance[_user] = 0; } + bool transferSuccess = IERC20(collateral).transfer( + msg.sender, + totalRewards + ); + + if (!transferSuccess) revert TransferFailed(); + emit Liquidate(_user, userDebt, totalRewards, msg.sender); /** @@ -504,6 +528,22 @@ contract Vault is ReentrancyGuard, Ownable { LIQUIDATION_REWARD = value; } + /** + * Add to blacklist + */ + function addAddressToBlacklist(address _address) external onlyOwner { + _blacklistedAddresses.push(_address); + } + + /** + * Pause transactions + */ + function pauseTransactions() external onlyOwner { + if (transactionsPaused == false) + { transactionsPaused = true; } + else { transactionsPaused = false; } +} + /** * Change swap variables */ @@ -515,28 +555,28 @@ contract Vault is ReentrancyGuard, Ownable { mintersWallet = _address; } - function changeSwapFee(uint256 numerator, uint256 denominator) + function changeSwapFee(uint256 a, uint256 b) external onlyOwner { - swapFee = WadRayMath.wadDiv(numerator, denominator); + swapFee = WadRayMath.wadDiv(a, b); } - function changeGlobalMintersFee(uint256 numerator, uint256 denominator) + function changeGlobalMintersFee(uint256 a, uint256 b) external onlyOwner { globalMintersPercentOfSwapFee = WadRayMath.wadDiv( - numerator, - denominator + a, + b ); } - function changeTreasuryFee(uint256 numerator, uint256 denominator) + function changeTreasuryFee(uint256 a, uint256 b) external onlyOwner { - treasuryPercentOfSwapFee = WadRayMath.wadDiv(numerator, denominator); + treasuryPercentOfSwapFee = WadRayMath.wadDiv(a, b); } /** @@ -560,7 +600,7 @@ contract Vault is ReentrancyGuard, Ownable { address _tokenAddress, address _userAddress, uint256 _amount - ) internal virtual returns (bool) { + ) internal returns (bool) { ZTokenInterface(_tokenAddress).mint(_userAddress, _amount); return true; @@ -570,7 +610,7 @@ contract Vault is ReentrancyGuard, Ownable { address _tokenAddress, address _userAddress, uint256 _amount - ) internal virtual returns (bool) { + ) internal returns (bool) { ZTokenInterface(_tokenAddress).burn(_userAddress, _amount); return true; @@ -581,7 +621,6 @@ contract Vault is ReentrancyGuard, Ownable { */ function _repay(uint256 _amount, address _zToken) internal - virtual returns (uint256) { // require(IERC20(_zToken).balanceOf(msg.sender) >= _amount, "Insufficient balance"); From 9dd607190ada61be95c61a0a35750e20886e9c29 Mon Sep 17 00:00:00 2001 From: David Asamonye Date: Thu, 20 Oct 2022 18:50:45 +0100 Subject: [PATCH 8/9] removed functions to add exRates from vault --- contracts/Oracle.sol | 19 ++++---- contracts/Vault.sol | 48 +++++++++----------- contracts/interfaces/BakiOracleInterface.sol | 16 +++++++ 3 files changed, 47 insertions(+), 36 deletions(-) create mode 100644 contracts/interfaces/BakiOracleInterface.sol diff --git a/contracts/Oracle.sol b/contracts/Oracle.sol index f377ded..bb64e78 100644 --- a/contracts/Oracle.sol +++ b/contracts/Oracle.sol @@ -1,17 +1,18 @@ // SPDX-License-Identifier: MIT -pragma solidity ^0.8.0; +pragma solidity ^0.8.17; import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol"; import "@chainlink/contracts/src/v0.8/ConfirmedOwner.sol"; +import "./interfaces/BakiOracleInterface.sol"; -contract PriceOracle is ChainlinkClient, ConfirmedOwner { +contract PriceOracle is BakiOracleInterface, ChainlinkClient, ConfirmedOwner { using Chainlink for Chainlink.Request; bytes32 private jobId; uint256 private fee; uint256 public NGNUSD; - uint256 public CFAUSD; + uint256 public XAFUSD; uint256 public ZARUSD; uint256 public XRATE; string public baseURL; @@ -70,15 +71,15 @@ contract PriceOracle is ChainlinkClient, ConfirmedOwner { NGNUSD = result; } - // ############################################################################# CFAUSD ############################################################################# - function _submitCFAUSD(string memory url) + // ############################################################################# XAFUSD ############################################################################# + function _submitXAFUSD(string memory url) internal returns (bytes32 requestId) { Chainlink.Request memory req = buildChainlinkRequest( jobId, address(this), - this._fulfillCFAUSD.selector + this._fulfillXAFUSD.selector ); // Set the URL to perform the GET request on @@ -96,12 +97,12 @@ contract PriceOracle is ChainlinkClient, ConfirmedOwner { /** * Receive the response in the form of uint256 */ - function _fulfillCFAUSD(bytes32 _requestId, uint256 result) + function _fulfillXAFUSD(bytes32 _requestId, uint256 result) public recordChainlinkFulfillment(_requestId) { emit ResultObtained(_requestId, result); - CFAUSD = result; + XAFUSD = result; } // ############################################################################# ZARUSD ############################################################################# @@ -186,7 +187,7 @@ contract PriceOracle is ChainlinkClient, ConfirmedOwner { keccak256(abi.encodePacked(pair)) == keccak256(abi.encodePacked("CFA/USD")) ) { - _submitCFAUSD(url); + _submitXAFUSD(url); } else if ( keccak256(abi.encodePacked(pair)) == keccak256(abi.encodePacked("ZAR/USD")) diff --git a/contracts/Vault.sol b/contracts/Vault.sol index 0a062b9..02ed043 100644 --- a/contracts/Vault.sol +++ b/contracts/Vault.sol @@ -6,13 +6,14 @@ * Check if the substracting value is greater than or less than the added values i.e check for a negative result */ -pragma solidity 0.8.17; +pragma solidity ^0.8.17; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; import "./interfaces/ZTokenInterface.sol"; import "./libraries/WadRayMath.sol"; +import "./interfaces/BakiOracleInterface.sol"; error TransferFailed(); error MintFailed(); @@ -33,12 +34,12 @@ contract Vault is ReentrancyGuard, Ownable { * exchange rates of 1 USD to zTokens * TODO These should be fetched from an Oracle */ - uint256 public NGNUSD; - uint256 public ZARUSD; - uint256 public XAFUSD; - uint256 public USD = 1e3; + address private Oracle; - constructor() {} + constructor() { + } + + uint256 private constant USD = 1e3; uint256 private constant MULTIPLIER = 1e6; @@ -91,6 +92,7 @@ contract Vault is ReentrancyGuard, Ownable { address[] public mintersAddresses; address[] private _blacklistedAddresses; + bool public transactionsPaused = false; /** @@ -496,21 +498,6 @@ contract Vault is ReentrancyGuard, Ownable { zZAR = _address; } - /** - * Set exchange rates - */ - function setNGNUSD(uint256 _rates) external onlyOwner { - NGNUSD = _rates; - } - - function setXAFUSD(uint256 _rates) external onlyOwner { - XAFUSD = _rates; - } - - function setZARUSD(uint256 _rates) external onlyOwner { - ZARUSD = _rates; - } - /** * set collaterization ratio threshold */ @@ -653,6 +640,13 @@ contract Vault is ReentrancyGuard, Ownable { return decimalAmount; } + /** + * Set Oracle contract address + */ + function setOracleAddress(address _address) public { + Oracle = _address; + } + /** * Returns the appropriate USD exchange rate during a swap/repay */ @@ -664,11 +658,11 @@ contract Vault is ReentrancyGuard, Ownable { uint256 zTokenUSDRate; if (_address == zNGN) { - zTokenUSDRate = NGNUSD; + zTokenUSDRate = BakiOracleInterface(Oracle).NGNUSD(); } else if (_address == zXAF) { - zTokenUSDRate = XAFUSD; + zTokenUSDRate = BakiOracleInterface(Oracle).XAFUSD(); } else if (_address == zZAR) { - zTokenUSDRate = ZARUSD; + zTokenUSDRate = BakiOracleInterface(Oracle).ZARUSD(); } else if (_address == zUSD) { zTokenUSDRate = USD; } @@ -694,9 +688,9 @@ contract Vault is ReentrancyGuard, Ownable { globalDebt = (IERC20(zUSD).totalSupply() * HALF_MULTIPLIER) + - WadRayMath.wadDiv(IERC20(zNGN).totalSupply(), NGNUSD) + - WadRayMath.wadDiv(IERC20(zXAF).totalSupply(), XAFUSD) + - WadRayMath.wadDiv(IERC20(zZAR).totalSupply(), ZARUSD); + WadRayMath.wadDiv(IERC20(zNGN).totalSupply(), BakiOracleInterface(Oracle).NGNUSD()) + + WadRayMath.wadDiv(IERC20(zXAF).totalSupply(), BakiOracleInterface(Oracle).XAFUSD()) + + WadRayMath.wadDiv(IERC20(zZAR).totalSupply(), BakiOracleInterface(Oracle).ZARUSD()); globalDebt = globalDebt / HALF_MULTIPLIER; diff --git a/contracts/interfaces/BakiOracleInterface.sol b/contracts/interfaces/BakiOracleInterface.sol new file mode 100644 index 0000000..7bf58ca --- /dev/null +++ b/contracts/interfaces/BakiOracleInterface.sol @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.17; + +/** +* @dev Oracle interface + */ +interface BakiOracleInterface { + /** + * @dev get each exRates + */ + function NGNUSD() external view returns (uint); + + function XAFUSD() external view returns (uint); + + function ZARUSD() external view returns (uint); +} \ No newline at end of file From bcc2a46d429f5c49d273316bfdd84c837c5fd2a9 Mon Sep 17 00:00:00 2001 From: Deon Achuo Date: Mon, 7 Nov 2022 16:34:53 +0100 Subject: [PATCH 9/9] finished Baki Oracle --- contracts/Oracle.sol | 206 ++++--------------------------------------- 1 file changed, 18 insertions(+), 188 deletions(-) diff --git a/contracts/Oracle.sol b/contracts/Oracle.sol index bb64e78..7176fbd 100644 --- a/contracts/Oracle.sol +++ b/contracts/Oracle.sol @@ -1,205 +1,35 @@ // SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; -pragma solidity ^0.8.17; - -import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol"; -import "@chainlink/contracts/src/v0.8/ConfirmedOwner.sol"; +import "@openzeppelin/contracts/access/Ownable.sol"; import "./interfaces/BakiOracleInterface.sol"; -contract PriceOracle is BakiOracleInterface, ChainlinkClient, ConfirmedOwner { - using Chainlink for Chainlink.Request; - - bytes32 private jobId; - uint256 private fee; - uint256 public NGNUSD; - uint256 public XAFUSD; - uint256 public ZARUSD; - uint256 public XRATE; - string public baseURL; - - event NewQuery(string description); - event ResultObtained(bytes32 indexed requestId, uint256 result); - - /** - * @notice Initialize the link token and target oracle - * - * Alfajores Testnet details: - * Link Token: 0xa36085F69e2889c224210F603D836748e7dC0088 - * Oracle: 0x74EcC8Bdeb76F2C6760eD2dc8A46ca5e581fA656 (Chainlink DevRel) - * jobId: ca98366cc7314957b8c012c72f05aeeb - * - */ - constructor() ConfirmedOwner(msg.sender) { - setChainlinkToken(0x01BE23585060835E02B77ef475b0Cc51aA1e0709); - setChainlinkOracle(0xf3FBB7f3391F62C8fe53f89B41dFC8159EE9653f); - jobId = "ca98366cc7314957b8c012c72f05aeeb"; - fee = 0.25 * 10**18; // 0,1 * 10**18 (Varies by network and job) - } - - // ############################################################################# NGNUSD ############################################################################# - - function _submitNGNUSD(string memory url) - internal - returns (bytes32 requestId) - { - Chainlink.Request memory req = buildChainlinkRequest( - jobId, - address(this), - this._fulfillNGNUSD.selector - ); - - // Set the URL to perform the GET request on - req.add("get", url); - req.add("path", "data"); // Chainlink nodes 1.0.0 and later support this format - - // Multiply the result by 1000000000000000000 to remove decimals - int256 timesAmount = 10**18; - req.addInt("times", timesAmount); - - // Sends the request - return sendChainlinkRequest(req, fee); - } - - /** - * Receive the response in the form of uint256 - */ - function _fulfillNGNUSD(bytes32 _requestId, uint256 result) - public - recordChainlinkFulfillment(_requestId) - { - emit ResultObtained(_requestId, result); - NGNUSD = result; - } - - // ############################################################################# XAFUSD ############################################################################# - function _submitXAFUSD(string memory url) - internal - returns (bytes32 requestId) - { - Chainlink.Request memory req = buildChainlinkRequest( - jobId, - address(this), - this._fulfillXAFUSD.selector - ); - - // Set the URL to perform the GET request on - req.add("get", url); - req.add("path", "data"); // Chainlink nodes 1.0.0 and later support this format +contract BakiOracle is Ownable, BakiOracleInterface { + uint256 _NGNUSD; + uint256 _XAFUSD; + uint256 _ZARUSD; - // Multiply the result by 1000000000000000000 to remove decimals - int256 timesAmount = 10**18; - req.addInt("times", timesAmount); - - // Sends the request - return sendChainlinkRequest(req, fee); + function setNGNUSD(uint256 _value) external onlyOwner { + _NGNUSD = _value; } - /** - * Receive the response in the form of uint256 - */ - function _fulfillXAFUSD(bytes32 _requestId, uint256 result) - public - recordChainlinkFulfillment(_requestId) - { - emit ResultObtained(_requestId, result); - XAFUSD = result; + function setXAFUSD(uint256 _value) external onlyOwner { + _XAFUSD = _value; } - // ############################################################################# ZARUSD ############################################################################# - function _submitZARUSD(string memory url) - internal - returns (bytes32 requestId) - { - Chainlink.Request memory req = buildChainlinkRequest( - jobId, - address(this), - this._fulfillZARUSD.selector - ); - - // Set the URL to perform the GET request on - req.add("get", url); - req.add("path", "data"); // Chainlink nodes 1.0.0 and later support this format - - // Multiply the result by 1000000000000000000 to remove decimals - int256 timesAmount = 10**18; - req.addInt("times", timesAmount); - - // Sends the request - return sendChainlinkRequest(req, fee); - } - - /** - * Receive the response in the form of uint256 - */ - function _fulfillZARUSD(bytes32 _requestId, uint256 result) - public - recordChainlinkFulfillment(_requestId) - { - emit ResultObtained(_requestId, result); - ZARUSD = result; - } - - // ############################################################################# XRATES ############################################################################# - function _submitXRATE(string memory url) - internal - returns (bytes32 requestId) - { - Chainlink.Request memory req = buildChainlinkRequest( - jobId, - address(this), - this._fulfillXRATE.selector - ); - - // Set the URL to perform the GET request on - req.add("get", url); - req.add("path", "data"); // Chainlink nodes 1.0.0 and later support this format - - // Multiply the result by 1000000000000000000 to remove decimals - int256 timesAmount = 10**18; - req.addInt("times", timesAmount); - - // Sends the request - return sendChainlinkRequest(req, fee); + function setZARUSD(uint256 _value) external onlyOwner { + _ZARUSD = _value; } - /** - * Receive the response in the form of uint256 - */ - function _fulfillXRATE(bytes32 _requestId, uint256 result) - public - recordChainlinkFulfillment(_requestId) - { - emit ResultObtained(_requestId, result); - XRATE = result; + function NGNUSD() external view override returns (uint256) { + return _NGNUSD; } - // create a request - function createRequest(string memory _target, string memory base) external { - string memory pair = string(abi.encodePacked(_target, "/", base)); - string memory url = string(abi.encodePacked(baseURL, "/", pair)); - emit NewQuery("Requesting price data"); - if ( - keccak256(abi.encodePacked(pair)) == - keccak256(abi.encodePacked("NGN/USD")) - ) { - _submitNGNUSD(url); - } else if ( - keccak256(abi.encodePacked(pair)) == - keccak256(abi.encodePacked("CFA/USD")) - ) { - _submitXAFUSD(url); - } else if ( - keccak256(abi.encodePacked(pair)) == - keccak256(abi.encodePacked("ZAR/USD")) - ) { - _submitZARUSD(url); - } else { - _submitXRATE(url); - } + function XAFUSD() external view override returns (uint256) { + return _XAFUSD; } - // set Base URL - function updateBaseURL(string memory _url) public onlyOwner { - baseURL = _url; + function ZARUSD() external view override returns (uint256) { + return _ZARUSD; } }