Skip to content

Instantly share code, notes, and snippets.

@jsbeaudry
Last active August 21, 2023 16:57
Show Gist options
  • Save jsbeaudry/d61de7c738cc31018e210f3a96ba1508 to your computer and use it in GitHub Desktop.
Save jsbeaudry/d61de7c738cc31018e210f3a96ba1508 to your computer and use it in GitHub Desktop.
Create a Bank on the blockchain
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;
interface IERC20 {
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);
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
}
contract DefiBank {
// call it DefiBank
string public name = "DefiBank";
//Owner of the bank
address public owner;
// create 2 state variables
IERC20 public usdc; // The token you will accept
IERC20 public bankToken; // The token that represents your bank that will be used to pay interest
uint public rates = 10; // Rates 10% APR
// create 1 array to add all your clients
address[] public clients;
// create a 3 maps
mapping(address => uint) public stakingBalance; //Clients balance
mapping(address => bool) public hasStaked; // Find out if this customer has created an account
mapping(address => bool) public isStaking; // Find out if this customer is using their account
// In constructor pass in the address for USDC token, set your custom bank token and the owner will be who will deploy the contract
constructor(address stable, address token){
usdc = IERC20(stable);
bankToken = IERC20(token);
owner = msg.sender;
}
// Change the ownership
function changeOwner(address newOwner) public {
// require the permission of the current owner
require(owner == msg.sender, "Your are not the current owner");
owner = newOwner;
}
// allow user to deposit usdc tokens in your contract
// 000000000000000000
function deposit(uint _amount) public returns(bool){
// Transfer usdc tokens to contract
IERC20(usdc).transferFrom(msg.sender, address(this), _amount);
// Update the account balance in map
stakingBalance[msg.sender] = stakingBalance[msg.sender] + _amount;
// Add user to clients array if they haven't staked already
if(!hasStaked[msg.sender]) {
clients.push(msg.sender);
}
// Update staking status to track
isStaking[msg.sender] = true;
hasStaked[msg.sender] = true;
return true;
}
// allow user to withdraw total balance and withdraw USDC from the contract
function withdrawTokens(uint _amount) public returns(bool){
// get the users staking balance in usdc
uint balance = stakingBalance[msg.sender];
// require the amount staked needs to be greater then 0 and than the amount needed
require(balance > 0, "Your balance is 0");
require(_amount<=balance, "Not enough fund for this transaction");
// reset staking balance map to 0
stakingBalance[msg.sender] = balance - _amount;
// transfer usdc tokens out of this contract to the msg.sender (client)
IERC20(usdc).transfer(msg.sender, _amount);
balance = stakingBalance[msg.sender];
if(balance == 0)
{
// update the staking status
isStaking[msg.sender] = false;
}
return true;
}
// Send bank tokens as a reward for staking. You can change the way you need to give interest if you want
function sendInterestToken() public {
// require the permission of the current owner
require(owner == msg.sender, "Your are not the current owner");
for (uint i=0; i<clients.length; i++) {
address recipient = clients[i];
uint balance = stakingBalance[recipient];
// if there is a balance transfer the same amount of bank tokens to the account that is staking as interest
if(balance >0 ) {
uint interest = ((balance*rates)/100)/12; //12 is for the number of months in the year. 10% / Year
IERC20(bankToken).transfer(recipient, interest);
}
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract Bankoin is ERC20, ERC20Burnable, Ownable {
constructor() ERC20("Bankoin", "BK") {
_mint(msg.sender, 1000 * 10 ** decimals());
}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract Fiat is ERC20, ERC20Burnable, Ownable {
constructor() ERC20("Fiat", "FIAT") {
_mint(msg.sender, 1000 * 10 ** decimals());
}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
}
@Uzumakos
Copy link

Interesting!!!

@srivera145
Copy link

I would like to talk with you more in depth about this. I've got some ideas and I think we may be able to come with something really good! My telegram is @ThePeoplesTokenFounder

@jsbeaudry
Copy link
Author

jsbeaudry commented Jul 31, 2022

Feel free to send me an email at jeansauvenel.beaudry@gmail.com for any suggestion, comment or critics. Thank You

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment