Compare commits
3 Commits
380c4acb09
...
c0af84f886
| Author | SHA1 | Date | |
|---|---|---|---|
| c0af84f886 | |||
| 9d91ac234d | |||
| 2b241a1995 |
85
contracts/Bank.sol
Normal file
85
contracts/Bank.sol
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity >=0.4.22 <0.9.0;
|
||||||
|
import "@openzeppelin/contracts/utils/Counters.sol";
|
||||||
|
import "./SoulboundToken.sol";
|
||||||
|
|
||||||
|
contract Bank {
|
||||||
|
using Counters for Counters.Counter;
|
||||||
|
Counters.Counter private id_counter;
|
||||||
|
|
||||||
|
struct Order {
|
||||||
|
bool isFinished;
|
||||||
|
uint amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
SoulboundToken sbt;
|
||||||
|
address owner;
|
||||||
|
mapping(address => uint) arrears;
|
||||||
|
mapping(uint => Order) order_amount;
|
||||||
|
mapping(address => uint[]) client_orders;
|
||||||
|
mapping(address => uint) credits;
|
||||||
|
mapping(address => uint) sbt_number;
|
||||||
|
|
||||||
|
constructor(address SBT_addr) {
|
||||||
|
sbt = SoulboundToken(SBT_addr);
|
||||||
|
owner = msg.sender;
|
||||||
|
}
|
||||||
|
|
||||||
|
receive() external payable {}
|
||||||
|
|
||||||
|
modifier onlyBank {
|
||||||
|
require(msg.sender == owner, "Only the owner can access this function.");
|
||||||
|
_;
|
||||||
|
}
|
||||||
|
|
||||||
|
modifier onlyClient {
|
||||||
|
require(sbt_number[msg.sender] != 0);
|
||||||
|
_;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setCredit(address client, uint amount) public onlyBank {
|
||||||
|
credits[client] = amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
function register(uint number) public {
|
||||||
|
uint target = sbt.getAccountNumber(msg.sender);
|
||||||
|
require(target != 0, "You don't have SBT.");
|
||||||
|
require(target == number, "This is not your SBT number.");
|
||||||
|
sbt_number[msg.sender] = target;
|
||||||
|
}
|
||||||
|
|
||||||
|
function pay(address shop, uint amount) public onlyClient {
|
||||||
|
require(amount <= credits[msg.sender], "You don't have enough credit.");
|
||||||
|
id_counter.increment();
|
||||||
|
uint id = id_counter.current();
|
||||||
|
arrears[msg.sender] += amount;
|
||||||
|
order_amount[id] = Order(false, amount);
|
||||||
|
client_orders[msg.sender].push(id);
|
||||||
|
payable(shop).transfer(amount);
|
||||||
|
sbt.logBorrowing(msg.sender, id, amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
function repay() public payable onlyClient returns (uint[] memory, uint) {
|
||||||
|
uint value = msg.value;
|
||||||
|
uint repay_amount = 0;
|
||||||
|
uint should_pay;
|
||||||
|
uint[] memory unfinished = new uint[](client_orders[msg.sender].length);
|
||||||
|
uint refund;
|
||||||
|
uint item_counter = 0;
|
||||||
|
for (uint i=0; i<client_orders[msg.sender].length; i++) {
|
||||||
|
should_pay = order_amount[client_orders[msg.sender][i]].amount;
|
||||||
|
if ((value - repay_amount) >= should_pay){
|
||||||
|
repay_amount += should_pay;
|
||||||
|
order_amount[client_orders[msg.sender][i]].isFinished = true;
|
||||||
|
} else {
|
||||||
|
unfinished[item_counter] = (client_orders[msg.sender][i]);
|
||||||
|
item_counter += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
refund = value - repay_amount;
|
||||||
|
if (refund != 0){
|
||||||
|
payable(msg.sender).transfer(refund);
|
||||||
|
}
|
||||||
|
return (unfinished, refund);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -17,9 +17,10 @@ contract SoulboundToken is ERC721, Ownable {
|
|||||||
|
|
||||||
mapping(address => bool) private isReliableBank;
|
mapping(address => bool) private isReliableBank;
|
||||||
mapping(address => Certificate[]) private certificates;
|
mapping(address => Certificate[]) private certificates;
|
||||||
|
mapping(address => uint) private address_to_number;
|
||||||
|
|
||||||
event Borrow(address client, address bank, uint id, uint amount);
|
event Borrow(address client, address bank, uint id, uint amount);
|
||||||
event Repay(address client, address bank, uint id, uint amount, bool finish);
|
event Repay(address client, address bank, uint id, uint amount);
|
||||||
event Warning(address client, address bank);
|
event Warning(address client, address bank);
|
||||||
|
|
||||||
constructor() ERC721("Credit System Soulbound Token", "CS_SBT") {}
|
constructor() ERC721("Credit System Soulbound Token", "CS_SBT") {}
|
||||||
@ -28,6 +29,7 @@ contract SoulboundToken is ERC721, Ownable {
|
|||||||
_tokenIds.increment();
|
_tokenIds.increment();
|
||||||
uint256 newItemId = _tokenIds.current();
|
uint256 newItemId = _tokenIds.current();
|
||||||
_mint(player, newItemId);
|
_mint(player, newItemId);
|
||||||
|
address_to_number[player] = newItemId;
|
||||||
|
|
||||||
return newItemId;
|
return newItemId;
|
||||||
}
|
}
|
||||||
@ -88,8 +90,8 @@ contract SoulboundToken is ERC721, Ownable {
|
|||||||
emit Borrow(client, msg.sender, id, amount);
|
emit Borrow(client, msg.sender, id, amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
function logRepaying(address client, uint id, uint amount, bool finish) public onlyBank {
|
function logRepaying(address client, uint id, uint amount) public onlyBank {
|
||||||
emit Repay(client, msg.sender, id, amount, finish);
|
emit Repay(client, msg.sender, id, amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
function logWarning(address client) public onlyBank {
|
function logWarning(address client) public onlyBank {
|
||||||
@ -103,4 +105,8 @@ contract SoulboundToken is ERC721, Ownable {
|
|||||||
function listCertificate(address client) public view onlyBank returns (Certificate[] memory){
|
function listCertificate(address client) public view onlyBank returns (Certificate[] memory){
|
||||||
return certificates[client];
|
return certificates[client];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getAccountNumber(address client) public view onlyBank returns (uint) {
|
||||||
|
return address_to_number[client];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,6 +1,11 @@
|
|||||||
var MyNFT = artifacts.require("SoulboundToken");
|
var SBT = artifacts.require("SoulboundToken");
|
||||||
|
var Bank = artifacts.require("Bank");
|
||||||
|
|
||||||
module.exports = function(_deployer) {
|
module.exports = function(_deployer) {
|
||||||
// Use deployer to state migration tasks.
|
// Use deployer to state migration tasks.
|
||||||
_deployer.deploy(MyNFT, );
|
_deployer.deploy(SBT).then((SBT_instance) => {
|
||||||
|
return _deployer.deploy(Bank, SBT_instance.address).then((Bank_instance) => {
|
||||||
|
return SBT_instance.addReliableBank(Bank_instance.address);
|
||||||
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user