fix: fix simplify repay() function

This commit is contained in:
snsd0805 2023-06-12 20:19:28 +08:00
parent c7a26f1a7e
commit 260568d62d
Signed by: snsd0805
GPG Key ID: 569349933C77A854

View File

@ -11,7 +11,7 @@ struct Order {
contract Bank { contract Bank {
using Counters for Counters.Counter; using Counters for Counters.Counter;
Counters.Counter private id_counter; // Counters.Counter private id_counter;
SoulboundToken public sbt; SoulboundToken public sbt;
address public owner; address public owner;
@ -55,12 +55,13 @@ contract Bank {
require(target == number, "This is not your SBT number."); require(target == number, "This is not your SBT number.");
require(sbt_number[msg.sender] == 0, "You have registered."); require(sbt_number[msg.sender] == 0, "You have registered.");
sbt_number[msg.sender] = target; sbt_number[msg.sender] = target;
credits[msg.sender] = 1000000000000000000;
} }
function pay(address shop, uint amount) public onlyClient { function pay(uint id, address shop, uint amount) public onlyClient {
require(amount <= (credits[msg.sender]-arrears[msg.sender]), "You don't have enough credit."); require(amount <= (credits[msg.sender]-arrears[msg.sender]), "You don't have enough credit.");
id_counter.increment(); // id_counter.increment();
uint id = id_counter.current(); // uint id = id_counter.current();
arrears[msg.sender] += amount; arrears[msg.sender] += amount;
order_info[id] = Order(false, amount, shop); order_info[id] = Order(false, amount, shop);
client_orders[msg.sender].push(id); client_orders[msg.sender].push(id);
@ -68,34 +69,28 @@ contract Bank {
sbt.logBorrowing(msg.sender, shop, id, amount); sbt.logBorrowing(msg.sender, shop, id, amount);
} }
function repay() public payable onlyClient returns (uint, uint) { function repay() public payable onlyClient {
require(recv, "It's not the time to repay."); require(recv, "It's not the time to repay.");
uint value = msg.value; uint value = msg.value;
uint repay_amount = 0; // uint repay_amount = 0;
uint should_pay; uint should_pay = 0;
uint refund; // uint refund;
uint item_counter = 0; // uint item_counter = 0;
for (uint i=0; i<client_orders[msg.sender].length; i++) { for (uint i=0; i<client_orders[msg.sender].length; i++) {
should_pay = order_info[client_orders[msg.sender][i]].amount; should_pay += order_info[client_orders[msg.sender][i]].amount;
if ((value - repay_amount) >= should_pay){
repay_amount += should_pay;
order_info[client_orders[msg.sender][i]].isFinished = true;
sbt.logRepaying(msg.sender, client_orders[msg.sender][i], should_pay);
} else {
client_orders[msg.sender][item_counter] = client_orders[msg.sender][i];
item_counter += 1;
}
} }
for(uint i=0; i<(client_orders[msg.sender].length-item_counter); i++){ require(value>=should_pay, "You dont have enough ETH to repay");
client_orders[msg.sender].pop(); for (uint i=0; i<client_orders[msg.sender].length; i++) {
order_info[client_orders[msg.sender][i]].isFinished = true;
sbt.logRepaying(msg.sender, client_orders[msg.sender][i], should_pay);
} }
refund = value - repay_amount; client_orders[msg.sender] = new uint[](0);
arrears[msg.sender] -= repay_amount; arrears[msg.sender] = 0;
if (refund != 0){ }
payable(msg.sender).transfer(refund);
} function warning(address client) public onlyBank {
return (client_orders[msg.sender].length, refund); sbt.logWarning(client);
} }
function start_recv() public onlyBank { function start_recv() public onlyBank {
@ -114,11 +109,15 @@ contract Bank {
return arrears[client]; return arrears[client];
} }
function getClientOrders(address client) public view onlySelfOrBank(client) returns (Order[] memory){ function getClientOrders(address client) public view onlySelfOrBank(client) returns (uint[] memory){
Order[] memory orders = new Order[]( client_orders[client].length ); // Order[] memory orders = new Order[]( client_orders[client].length );
for(uint i=0; i<client_orders[client].length; i++){ // for(uint i=0; i<client_orders[client].length; i++){
orders[i] = order_info[client_orders[client][i]]; // orders[i] = order_info[client_orders[client][i]];
} // }
return orders; return client_orders[client];
}
function getSBTNumber(address client) public view onlySelfOrBank(client) returns (uint) {
return sbt_number[client];
} }
} }