fix: pay & repay but
This commit is contained in:
parent
c0af84f886
commit
bd40ab67d8
@ -12,31 +12,38 @@ contract Bank {
|
|||||||
uint amount;
|
uint amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
SoulboundToken sbt;
|
SoulboundToken public sbt;
|
||||||
address owner;
|
address public owner;
|
||||||
mapping(address => uint) arrears;
|
bool public recv = false;
|
||||||
mapping(uint => Order) order_amount;
|
mapping(address => uint) private arrears;
|
||||||
mapping(address => uint[]) client_orders;
|
mapping(uint => Order) private order_info;
|
||||||
mapping(address => uint) credits;
|
mapping(address => uint[]) private client_orders;
|
||||||
mapping(address => uint) sbt_number;
|
mapping(address => uint) private credits;
|
||||||
|
mapping(address => uint) private sbt_number;
|
||||||
|
|
||||||
constructor(address SBT_addr) {
|
constructor(address SBT_addr) {
|
||||||
sbt = SoulboundToken(SBT_addr);
|
sbt = SoulboundToken(SBT_addr);
|
||||||
owner = msg.sender;
|
owner = msg.sender;
|
||||||
}
|
}
|
||||||
|
|
||||||
receive() external payable {}
|
|
||||||
|
|
||||||
modifier onlyBank {
|
modifier onlyBank {
|
||||||
require(msg.sender == owner, "Only the owner can access this function.");
|
require(msg.sender == owner, "Only the owner can access this function.");
|
||||||
_;
|
_;
|
||||||
}
|
}
|
||||||
|
|
||||||
modifier onlyClient {
|
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 {
|
function setCredit(address client, uint amount) public onlyBank {
|
||||||
credits[client] = amount;
|
credits[client] = amount;
|
||||||
}
|
}
|
||||||
@ -45,41 +52,56 @@ contract Bank {
|
|||||||
uint target = sbt.getAccountNumber(msg.sender);
|
uint target = sbt.getAccountNumber(msg.sender);
|
||||||
require(target != 0, "You don't have SBT.");
|
require(target != 0, "You don't have SBT.");
|
||||||
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.");
|
||||||
sbt_number[msg.sender] = target;
|
sbt_number[msg.sender] = target;
|
||||||
}
|
}
|
||||||
|
|
||||||
function pay(address shop, uint amount) public onlyClient {
|
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();
|
id_counter.increment();
|
||||||
uint id = id_counter.current();
|
uint id = id_counter.current();
|
||||||
arrears[msg.sender] += amount;
|
arrears[msg.sender] += amount;
|
||||||
order_amount[id] = Order(false, amount);
|
order_info[id] = Order(false, amount);
|
||||||
client_orders[msg.sender].push(id);
|
client_orders[msg.sender].push(id);
|
||||||
payable(shop).transfer(amount);
|
payable(shop).transfer(amount);
|
||||||
sbt.logBorrowing(msg.sender, id, 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 value = msg.value;
|
||||||
uint repay_amount = 0;
|
uint repay_amount = 0;
|
||||||
uint should_pay;
|
uint should_pay;
|
||||||
uint[] memory unfinished = new uint[](client_orders[msg.sender].length);
|
|
||||||
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_amount[client_orders[msg.sender][i]].amount;
|
should_pay = order_info[client_orders[msg.sender][i]].amount;
|
||||||
if ((value - repay_amount) >= should_pay){
|
if ((value - repay_amount) >= should_pay){
|
||||||
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 {
|
} else {
|
||||||
unfinished[item_counter] = (client_orders[msg.sender][i]);
|
client_orders[msg.sender][item_counter] = client_orders[msg.sender][i];
|
||||||
item_counter += 1;
|
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;
|
refund = value - repay_amount;
|
||||||
|
arrears[msg.sender] -= repay_amount;
|
||||||
if (refund != 0){
|
if (refund != 0){
|
||||||
payable(msg.sender).transfer(refund);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,9 @@ module.exports = function(_deployer) {
|
|||||||
// Use deployer to state migration tasks.
|
// Use deployer to state migration tasks.
|
||||||
_deployer.deploy(SBT).then((SBT_instance) => {
|
_deployer.deploy(SBT).then((SBT_instance) => {
|
||||||
return _deployer.deploy(Bank, SBT_instance.address).then((Bank_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"});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user