/
ReturnNftJob.java
143 lines (110 loc) · 5.19 KB
/
ReturnNftJob.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.fluidtokens.nft.borrow.service;
import com.bloxbean.cardano.client.account.Account;
import com.bloxbean.cardano.client.address.AddressProvider;
import com.bloxbean.cardano.client.api.model.Result;
import com.bloxbean.cardano.client.api.model.Utxo;
import com.bloxbean.cardano.client.backend.blockfrost.service.BFBackendService;
import com.bloxbean.cardano.client.common.model.Networks;
import com.bloxbean.cardano.client.function.helper.SignerProviders;
import com.bloxbean.cardano.client.plutus.spec.BigIntPlutusData;
import com.bloxbean.cardano.client.plutus.spec.ConstrPlutusData;
import com.bloxbean.cardano.client.quicktx.QuickTxBuilder;
import com.bloxbean.cardano.client.quicktx.ScriptTx;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fluidtokens.nft.borrow.model.UtxoRent;
import jakarta.annotation.PostConstruct;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.Comparator;
import java.util.concurrent.TimeUnit;
@Service
@Slf4j
//@AllArgsConstructor
public class ReturnNftJob implements Runnable {
private static final String SCRIPT_REF_INPUT_HASH = "2c812d5ba6d240eea79dca528f22a3854adcaac140f3151ecbcf5d945c5981e3";
private static final int SCRIPT_REF_INPUT_INDEX = 0;
@Value("${dryRun}")
private boolean dryRun;
@Autowired
private Account account;
@Autowired
private BFBackendService bfBackendService;
@Autowired
private DatumService datumService;
@Autowired
private RentService rentService;
@Autowired
private FluidtokensRentContractService fluidtokensRentContractService;
private final ObjectMapper objectMapper = new ObjectMapper();
@PostConstruct
public void init() {
log.info("Running in dry-run mode: {}", dryRun);
}
@SneakyThrows
@Override
@Scheduled(timeUnit = TimeUnit.MINUTES, initialDelay = 2, fixedDelay = 15)
public void run() {
log.info("Running");
var allRents = rentService.findAllRents();
var nextLoan = allRents.stream()
.filter(utxoRent -> utxoRent.rent().isLent())
.min(Comparator.comparing(utxoRent -> utxoRent.rent().deadline()));
nextLoan.ifPresent(loan -> log.info("Next loan: {}", loan));
var expiredRents = allRents
.stream()
.filter(utxoRent -> utxoRent.rent().canBeReturned())
.sorted(Comparator.comparing(UtxoRent::transactionOutput))
.toList();
if (expiredRents.size() > 10) {
expiredRents = expiredRents.subList(0, 9);
}
if (!expiredRents.isEmpty()) {
final ScriptTx scriptTx = new ScriptTx()
.readFrom(SCRIPT_REF_INPUT_HASH, SCRIPT_REF_INPUT_INDEX);
for (int i = 0; i < expiredRents.size(); i++) {
var rent = expiredRents.get(i);
final var j = i;
var utxoHash = rent.transactionOutput().hash();
var utxoIndex = rent.transactionOutput().index();
try {
Result<Utxo> txOutputResult = bfBackendService.getUtxoService().getTxOutput(utxoHash, utxoIndex);
Utxo utxo = txOutputResult.getValue();
String inlineDatum = utxo.getInlineDatum();
var delegationCredentials = rent.rent().owner().getDelegationCredential().get();
var addressOwner = AddressProvider.getBaseAddress(fluidtokensRentContractService.getPaymentCredentials(),
delegationCredentials,
Networks.mainnet());
datumService.getNewDatum(inlineDatum)
.ifPresent(newDatum -> {
scriptTx.collectFrom(utxo, ConstrPlutusData.of(4, BigIntPlutusData.of(j)));
scriptTx.payToContract(addressOwner.getAddress(), utxo.getAmount(), newDatum);
});
} catch (Exception e) {
log.warn("Error while processing utxo: {}:{}", utxoHash, utxoIndex);
throw new RuntimeException(e);
}
}
scriptTx.withChangeAddress(account.baseAddress());
QuickTxBuilder quickTxBuilder = new QuickTxBuilder(bfBackendService);
var slot = bfBackendService.getBlockService().getLatestBlock().getValue().getSlot();
var transaction = quickTxBuilder
.compose(scriptTx)
.mergeOutputs(false)
.withSigner(SignerProviders.signerFrom(account))
.validFrom(slot - 120L)
.validTo(slot + 600L)
.feePayer(account.baseAddress());
if (dryRun) {
var tx = transaction.build();
log.info("tx: {}", objectMapper.writeValueAsString(tx));
} else {
transaction.completeAndWait();
}
}
log.info("Completed");
}
}