Skip to content

Commit c65e59e

Browse files
authored
feat: implement viewConsumed to see orders consumption level on-chain (#147)
1 parent c07cf70 commit c65e59e

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

src/main/java/com/iexec/commons/poco/chain/IexecHubAbstractService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,19 @@ public String getOwner(final String address) {
577577
return "";
578578
}
579579

580+
/**
581+
* Read on-chain the consumption level of an order from its EIP-712 hash
582+
*
583+
* @param typedHash The order EIP-712 hash whose consumption level is queried
584+
* @return The consumed value which is less or equal to the order volume. It will be {@literal BigInteger.ZERO} if the
585+
* hash is not present on-chain and has never been matched in a deal.
586+
* @throws IOException on communication error with the blockchain network
587+
*/
588+
public BigInteger viewConsumed(final String typedHash) throws IOException {
589+
return Numeric.toBigInt(
590+
txManager.sendCall(iexecHubAddress, VIEW_CONSUMED_SELECTOR + Numeric.cleanHexPrefix(typedHash), DefaultBlockParameterName.LATEST));
591+
}
592+
580593
// endregion
581594

582595
// region Purge

src/main/java/com/iexec/commons/poco/encoding/AccessorsEncoder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public class AccessorsEncoder {
5050
*/
5151
public static final String OWNER_SELECTOR = "0x8da5cb5b";
5252

53+
public static final String VIEW_CONSUMED_SELECTOR = "0x4b2bec8c";
54+
5355
// app
5456
public static final String M_APPCHECKSUM_SELECTOR = "0x84aaf12e";
5557
public static final String M_APPMRENCLAVE_SELECTOR = "0xe30d26a8";

src/test/java/com/iexec/commons/poco/itest/IexecHubTestService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
public class IexecHubTestService extends IexecHubAbstractService {
4141
static final BigInteger GAS_PRICE = BigInteger.valueOf(22_000_000_000L);
4242
static final BigInteger GAS_LIMIT = BigInteger.valueOf(1_000_000L);
43+
static final long CHAIN_ID = 65535L;
4344
static final String IEXEC_HUB_ADDRESS = "0xc4b11f41746D3Ad8504da5B383E1aB9aa969AbC7";
4445

4546
static final String ASSET_MULTI_ADDRESS = "multiAddress";

src/test/java/com/iexec/commons/poco/itest/MatchOrdersTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,16 @@ void shouldMatchOrdersWithSignerService() throws IOException {
150150
assertThat(iexecHubService.fetchLogTopics(matchOrdersTxHash))
151151
.isEqualTo(List.of("Transfer", "Lock", "Transfer", "Lock", "SchedulerNotice", "OrdersMatched"));
152152

153+
// orders consumption
154+
assertThat(iexecHubService.viewConsumed(signedAppOrder.computeHash(ordersService.getDomain()))).isEqualTo(BigInteger.ONE);
155+
assertThat(iexecHubService.viewConsumed(signedDatasetOrder.computeHash(ordersService.getDomain()))).isEqualTo(BigInteger.ONE);
156+
assertThat(iexecHubService.viewConsumed(signedWorkerpoolOrder.computeHash(ordersService.getDomain()))).isEqualTo(BigInteger.ONE);
157+
assertThat(iexecHubService.viewConsumed(signedRequestOrder.computeHash(ordersService.getDomain()))).isEqualTo(BigInteger.ONE);
158+
153159
// estimate gas revert
160+
// 0x694578656356352d6d617463684f72646572732d30783630 is hex string corresponding to the 'iExecV5-matchOrders-0x60' string
161+
// The estimateGas calls tells us that not enough volumes are available across the 4 orders
162+
// https://github.com/iExecBlockchainComputing/PoCo/blob/v6.0.0/contracts/facets/IexecPoco1Facet.sol#L311
154163
assertThatThrownBy(() -> signerService.estimateGas(IEXEC_HUB_ADDRESS, matchOrdersTxData))
155164
.isInstanceOf(JsonRpcError.class)
156165
.hasMessage("Reverted 0x694578656356352d6d617463684f72646572732d30783630");

src/test/java/com/iexec/commons/poco/itest/OrdersService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.iexec.commons.poco.encoding.MatchOrdersDataEncoder;
2323
import com.iexec.commons.poco.order.*;
2424
import com.iexec.commons.poco.utils.BytesUtils;
25+
import lombok.Getter;
2526
import org.apache.commons.lang3.RandomStringUtils;
2627
import org.web3j.crypto.Hash;
2728

@@ -30,17 +31,16 @@
3031
import java.util.Map;
3132
import java.util.TreeMap;
3233

33-
import static com.iexec.commons.poco.itest.IexecHubTestService.GAS_PRICE;
34-
import static com.iexec.commons.poco.itest.IexecHubTestService.IEXEC_HUB_ADDRESS;
34+
import static com.iexec.commons.poco.itest.IexecHubTestService.*;
3535

3636
public class OrdersService {
3737

3838
static final String APP_NAME = "my-app";
3939
static final String DATASET_NAME = "my-dataset";
4040
static final String WORKERPOOL_NAME = "my-workerpool";
4141

42-
private static final long CHAIN_ID = 65535L;
4342

43+
@Getter
4444
private final EIP712Domain domain;
4545
private final SignerService signerService;
4646

0 commit comments

Comments
 (0)