feat: pay & repay
This commit is contained in:
parent
9d91ac234d
commit
c0af84f886
@ -7,15 +7,26 @@ 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.");
|
||||
_;
|
||||
@ -26,6 +37,10 @@ contract Bank {
|
||||
_;
|
||||
}
|
||||
|
||||
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.");
|
||||
@ -33,4 +48,38 @@ contract Bank {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ contract SoulboundToken is ERC721, Ownable {
|
||||
mapping(address => uint) private address_to_number;
|
||||
|
||||
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);
|
||||
|
||||
constructor() ERC721("Credit System Soulbound Token", "CS_SBT") {}
|
||||
@ -90,8 +90,8 @@ contract SoulboundToken is ERC721, Ownable {
|
||||
emit Borrow(client, msg.sender, id, amount);
|
||||
}
|
||||
|
||||
function logRepaying(address client, uint id, uint amount, bool finish) public onlyBank {
|
||||
emit Repay(client, msg.sender, id, amount, finish);
|
||||
function logRepaying(address client, uint id, uint amount) public onlyBank {
|
||||
emit Repay(client, msg.sender, id, amount);
|
||||
}
|
||||
|
||||
function logWarning(address client) public onlyBank {
|
||||
|
||||
@ -6,6 +6,6 @@ module.exports = function(_deployer) {
|
||||
_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