fix: pay & repay but

This commit is contained in:
snsd0805 2023-06-06 16:45:49 +08:00
parent c0af84f886
commit bd40ab67d8
Signed by: snsd0805
GPG Key ID: 569349933C77A854
2 changed files with 43 additions and 19 deletions

View File

@ -12,31 +12,38 @@ contract Bank {
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;
SoulboundToken public sbt;
address public owner;
bool public recv = false;
mapping(address => uint) private arrears;
mapping(uint => Order) private order_info;
mapping(address => uint[]) private client_orders;
mapping(address => uint) private credits;
mapping(address => uint) private 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);
require(sbt_number[msg.sender] != 0, "You should call register() first.");
_;
}
modifier onlySelfOrBank(address addr) {
require((msg.sender == owner) || (msg.sender==addr), "Only the owner can access this function.");
_;
}
receive() external payable onlyBank {}
function setCredit(address client, uint amount) public onlyBank {
credits[client] = amount;
}
@ -45,41 +52,56 @@ contract Bank {
uint target = sbt.getAccountNumber(msg.sender);
require(target != 0, "You don't have SBT.");
require(target == number, "This is not your SBT number.");
require(sbt_number[msg.sender] == 0, "You have registered.");
sbt_number[msg.sender] = target;
}
function pay(address shop, uint amount) public onlyClient {
require(amount <= credits[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();
uint id = id_counter.current();
arrears[msg.sender] += amount;
order_amount[id] = Order(false, amount);
order_info[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) {
function repay() public payable onlyClient returns (uint, uint) {
require(recv, "It's not the time to repay.");
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;
should_pay = order_info[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;
order_info[client_orders[msg.sender][i]].isFinished = true;
sbt.logRepaying(msg.sender, client_orders[msg.sender][i], should_pay);
} else {
unfinished[item_counter] = (client_orders[msg.sender][i]);
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++){
client_orders[msg.sender].pop();
}
refund = value - repay_amount;
arrears[msg.sender] -= repay_amount;
if (refund != 0){
payable(msg.sender).transfer(refund);
}
return (unfinished, refund);
return (client_orders[msg.sender].length, refund);
}
function start_recv() public onlyBank {
recv = true;
}
function stop_recv() public onlyBank {
recv = false;
}
}

View File

@ -5,7 +5,9 @@ module.exports = function(_deployer) {
// Use deployer to state migration tasks.
_deployer.deploy(SBT).then((SBT_instance) => {
return _deployer.deploy(Bank, SBT_instance.address).then((Bank_instance) => {
return SBT_instance.addReliableBank(Bank_instance.address);
return SBT_instance.addReliableBank(Bank_instance.address).then(() => {
return Bank_instance.sendTransaction({value: "5000000000000000000"});
});
});
});
};