fix authentication and refund on delete vm refactor app daemon register to get delete app req db function for delete app sample environment variable extensive tests for delete app and vm refactor test utilities
		
			
				
	
	
		
			31 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			SQL
		
	
	
	
	
	
			
		
		
	
	
			31 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			SQL
		
	
	
	
	
	
| 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
 | |
|     };
 | |
|     LET $escrow_multiplier = IF $operator.escrow < 5_000_000_000_000 { 1 } ELSE { 5 };
 | |
|     IF $node_is_online {
 | |
|         UPDATE $operator.id SET balance += $amount_paid * $escrow_multiplier;
 | |
|         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.in SET balance += $compensation;
 | |
|     };
 | |
|     IF $amount_paid >= $contract.locked_nano {
 | |
|         fn::delete_vm($contract.id);
 | |
|     };
 | |
| };
 | |
| 
 | |
| -- TODO: implement for active_app |