Skip to content

Instantly share code, notes, and snippets.

@ice09

ice09/FinancingPlatform.sol Secret

Last active Jan 29, 2021
Embed
What would you like to do?
Asset Financing with Accounts Receivable in Solidity
pragma solidity ^0.5;
contract FinancingPlatform {
address public admin;
mapping(bytes32 => FinancingConditions) public financingRegistry;
struct FinancingConditions {
uint256 amount;
uint256 discountedAmount;
address payable receiver;
address payable financer;
bool financed;
bool paid;
}
event Registered(bytes32 indexed _financing);
event Financed(bytes32 indexed _financing);
event Paid(bytes32 indexed _financing);
constructor() public {
admin = msg.sender;
}
function registerAssetForFinancing(bytes32 asset, uint256 amount, uint8 discount) public {
if (financingRegistry[asset].amount > 0) {
revert("Asset already registered or financed.");
}
if ((discount < 0) || (discount > 100)) {
revert("Discount must be between 0% and 100%.");
}
uint256 discountedAmount = amount * discount / 100;
FinancingConditions memory conds = FinancingConditions(
{
amount: amount,
discountedAmount: discountedAmount,
receiver: msg.sender,
financer: address(0x0),
financed: false,
paid: false
});
financingRegistry[asset] = conds;
emit Registered(asset);
}
function financeAsset(bytes32 asset) public payable {
if (financingRegistry[asset].amount == 0) {
revert("Asset not registered.");
}
FinancingConditions memory conds = financingRegistry[asset];
if (((conds.amount - conds.discountedAmount) * 10**18) != msg.value) {
revert("Submitted value is not correct.");
}
conds.financed = true;
conds.financer = msg.sender;
financingRegistry[asset] = conds;
conds.receiver.transfer((conds.amount - conds.discountedAmount) * 10**18);
emit Financed(asset);
}
function settleFinancedAsset(bytes32 asset) public payable {
if (financingRegistry[asset].amount == 0) {
revert("Asset not registered.");
}
FinancingConditions memory conds = financingRegistry[asset];
if (!conds.financed) {
revert("Asset has not been financed.");
}
if (msg.value != conds.amount * 10**18) {
revert("Submitted value is not Asset amount.");
}
conds.paid = true;
financingRegistry[asset] = conds;
conds.financer.transfer(conds.amount * 10**18);
emit Financed(asset);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment