As part of open sourcing the software product, we should consider that loyalty points are not the best language. Switching to "credits" makes sense from a lot of points of view. At the same time, this change allows an achitectural change towards slots. Slots allow daemon resources to get booked based on the HW ratio configured in the daemon config.
35 lines
1.2 KiB
SQL
35 lines
1.2 KiB
SQL
-- SPDX-License-Identifier: Apache-2.0
|
|
|
|
FOR $contract IN (select * from active_vm fetch out) {
|
|
LET $operator = (select * from $contract.out.operator)[0];
|
|
LET $node_is_online = $contract.out.connected_at > $contract.out.disconnected_at;
|
|
LET $price_per_minute = fn::vm_price_per_minute($contract.id);
|
|
LET $amount_due = (time::now() - $contract.collected_at).mins() * $price_per_minute;
|
|
LET $amount_paid = IF $amount_due > $contract.locked_nano {
|
|
$contract.locked_nano
|
|
} ELSE {
|
|
$amount_due
|
|
};
|
|
IF $node_is_online {
|
|
UPDATE $operator.id SET balance += $amount_paid;
|
|
UPDATE $contract.id SET
|
|
locked_nano -= $amount_paid,
|
|
collected_at = time::now();
|
|
} ELSE {
|
|
LET $compensation = IF $amount_due > $operator.escrow {
|
|
$operator.escrow
|
|
} ELSE {
|
|
$amount_due
|
|
};
|
|
UPDATE $operator.id SET escrow -= $compensation;
|
|
UPDATE $contract.id SET
|
|
locked_nano += $compensation,
|
|
collected_at = time::now();
|
|
};
|
|
IF $amount_paid >= $contract.locked_nano {
|
|
fn::delete_vm($contract.id);
|
|
};
|
|
};
|
|
|
|
-- TODO: implement for active_app
|