Compare commits
No commits in common. "c0af84f88631c0ab8113a1a3e5dd30f968bac3fc" and "380c4acb09383b1c1420260d30945eccc541c5d0" have entirely different histories.
c0af84f886
...
380c4acb09
@ -1,85 +0,0 @@
|
|||||||
// 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,10 +17,9 @@ 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);
|
event Repay(address client, address bank, uint id, uint amount, bool finish);
|
||||||
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") {}
|
||||||
@ -29,7 +28,6 @@ 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;
|
||||||
}
|
}
|
||||||
@ -90,8 +88,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) public onlyBank {
|
function logRepaying(address client, uint id, uint amount, bool finish) public onlyBank {
|
||||||
emit Repay(client, msg.sender, id, amount);
|
emit Repay(client, msg.sender, id, amount, finish);
|
||||||
}
|
}
|
||||||
|
|
||||||
function logWarning(address client) public onlyBank {
|
function logWarning(address client) public onlyBank {
|
||||||
@ -105,8 +103,4 @@ 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,11 +1,6 @@
|
|||||||
var SBT = artifacts.require("SoulboundToken");
|
var MyNFT = 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(SBT).then((SBT_instance) => {
|
_deployer.deploy(MyNFT, );
|
||||||
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