Skip to content
This repository has been archived by the owner on Feb 8, 2022. It is now read-only.

Commit

Permalink
Adding faucet functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
aaitor committed Sep 10, 2020
1 parent c059957 commit 4fe4c5f
Show file tree
Hide file tree
Showing 18 changed files with 242 additions and 36 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

## Table of Contents

* [Keyko Java API for Nevermined](#keyko-java-api-for-nevermined)
* [Java API for Nevermined Data platform](#java-api-for-nevermined-data-platform)
* [Table of Contents](#table-of-contents)
* [Features](#features)
* [Installation](#installation)
* [Configuration](#configuration)
* [Using Squid-Java with Barge](#using-squid-java-with-barge)
* [Using the SDK with the Nevermined Tools](#using-the-sdk-with-the-nevermined-tools)
* [Dealing with Flowables](#dealing-with-flowables)
* [Documentation](#documentation)
* [Testing](#testing)
Expand All @@ -32,6 +32,7 @@
* [Attribution](#attribution)
* [License](#license)


---

## Features
Expand All @@ -46,7 +47,7 @@ Typically in Maven you can add nevermined sdk as a dependency:
<dependency>
<groupId>io.keyko.nevermined</groupId>
<artifactId>api</artifactId>
<version>0.3.0</version>
<version>0.3.2</version>
</dependency>
```

Expand Down Expand Up @@ -259,7 +260,7 @@ mvn clean verify -P all-tests

You can run the integration tests in Nile environment using the command:
```bash
mvn verify -P integration-test -Dconfig.file=src/test/resources/networks/nile-application.conf
mvn verify -P integration-test -Dconfig.file=src/test/resources/networks/integration-application.conf
```

### Code Coverage
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/io/keyko/nevermined/api/AccountsAPI.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.keyko.nevermined.api;

import io.keyko.nevermined.exceptions.EthereumException;
import io.keyko.nevermined.exceptions.ServiceException;
import io.keyko.nevermined.models.Account;
import io.keyko.nevermined.models.Balance;
import io.keyko.nevermined.models.faucet.FaucetResponse;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

import java.math.BigInteger;
Expand All @@ -19,7 +21,7 @@ public interface AccountsAPI {
* @return a List of all Account registered in Keeper
* @throws EthereumException EthereumException
*/
public List<Account> list() throws EthereumException;
List<Account> list() throws EthereumException;

/**
* Returns the Balance of an account
Expand All @@ -28,15 +30,24 @@ public interface AccountsAPI {
* @return the Balance of the account
* @throws EthereumException EthereumException
*/
public Balance balance(Account account) throws EthereumException;
Balance balance(Account account) throws EthereumException;

/**
* Requests Ocean Tokens
* Requests Ether to faucet for paying transactions gas
*
* @param address the account address requesting ETH
* @return FaucetResponse response status and message
* @throws ServiceException ServiceException
*/
FaucetResponse requestEthFromFaucet(String address) throws ServiceException;

/**
* Requests Nevermined Tokens
*
* @param amount the amount of tokens
* @return a TransactionReceipt from the transaction sent to the smart contract
* @throws EthereumException EthereumException
*/
public TransactionReceipt requestTokens(BigInteger amount) throws EthereumException;
TransactionReceipt requestTokens(BigInteger amount) throws EthereumException;

}
11 changes: 11 additions & 0 deletions src/main/java/io/keyko/nevermined/api/config/NeverminedConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class NeverminedConfig {
public static final String KEEPER_TX_SLEEPDURATION = "keeper.tx.sleepDuration";
public static final String METADATA_URL = "metadata.url";
public static final String SECRETSTORE_URL = "secretstore.url";
public static final String FAUCET_URL = "faucet.url";
public static final String PROVIDER_ADDRESS = "provider.address";
public static final String MAIN_ACCOUNT_ADDRESS = "account.main.address";
public static final String MAIN_ACCOUNT_PASSWORD = "account.main.password";
Expand All @@ -42,6 +43,7 @@ public class NeverminedConfig {
private long keeperTxSleepDuration;
private String metadataUrl;
private String secretStoreUrl;
private String faucetUrl;
private String providerAddress;
private String mainAccountAddress;
private String mainAccountPassword;
Expand Down Expand Up @@ -251,6 +253,15 @@ public NeverminedConfig setSecretStoreUrl(String secretStoreUrl) {
return this;
}

public NeverminedConfig setFaucetUrl(String faucetUrl) {
this.faucetUrl = faucetUrl;
return this;
}

public String getFaucetUrl() {
return faucetUrl;
}

public String getDidRegistryAddress() {
return didRegistryAddress;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class NeverminedConfigFactory {
private static final BigInteger DEFAULT_KEEPER_GAS_PRICE = BigInteger.valueOf(100000000000l);
private static final String DEFAULT_METADATA_URL = "http://localhost:5000";
private static final String DEFAULT_SECRET_STORE_URL = "http://localhost:12001";
private static final String DEFAULT_FAUCET_URL = "http://localhost:3001";
private static final String DEFAULT_CONSUME_PATH = "/tmp";


Expand Down Expand Up @@ -47,6 +48,7 @@ public static NeverminedConfig getNeverminedConfig(Properties properties) {

neverminedConfig.setMetadataUrl((String) properties.getOrDefault(NeverminedConfig.METADATA_URL, DEFAULT_METADATA_URL));
neverminedConfig.setSecretStoreUrl((String) properties.getOrDefault(NeverminedConfig.SECRETSTORE_URL, DEFAULT_SECRET_STORE_URL));
neverminedConfig.setFaucetUrl((String) properties.getOrDefault(NeverminedConfig.FAUCET_URL, DEFAULT_FAUCET_URL));
neverminedConfig.setProviderAddress((String) properties.getOrDefault(NeverminedConfig.PROVIDER_ADDRESS, ""));
neverminedConfig.setDidRegistryAddress((String) properties.getOrDefault(NeverminedConfig.DID_REGISTRY_ADDRESS, ""));
neverminedConfig.setEscrowRewardConditionsAddress((String) properties.getOrDefault(NeverminedConfig.ESCROWREWARD_CONDITIONS_ADDRESS, ""));
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/io/keyko/nevermined/api/helper/AccountsHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.keyko.nevermined.api.helper;

import org.web3j.crypto.CipherException;
import org.web3j.crypto.WalletUtils;

import java.io.File;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

public abstract class AccountsHelper {

public static String createAccount(String password, String destination) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, CipherException, IOException {
return WalletUtils.generateNewWalletFile(password, new File(destination));
}

public static String getAddressFromFilePath(String walletFileName) {
try {
String[] fetchAddress = walletFileName.split("--");
return "0x" + fetchAddress[fetchAddress.length - 1].split("\\.")[0];
} catch (Exception e) {
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public NeverminedManager getNeverminedManager(KeeperService keeperService, Metad
* @throws CipherException CipherException
*/
public AccountsManager getAccountsManager(KeeperService keeperService, MetadataApiService metadataApiService) throws IOException, CipherException {
return AccountsManager.getInstance(keeperService, metadataApiService);
return AccountsManager.getInstance(keeperService, metadataApiService)
.setFaucetUrl(neverminedConfig.getFaucetUrl());
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/io/keyko/nevermined/api/impl/AccountsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import io.keyko.nevermined.api.AccountsAPI;
import io.keyko.nevermined.exceptions.EthereumException;
import io.keyko.nevermined.exceptions.ServiceException;
import io.keyko.nevermined.external.FaucetService;
import io.keyko.nevermined.manager.AccountsManager;
import io.keyko.nevermined.models.Account;
import io.keyko.nevermined.models.Balance;
import io.keyko.nevermined.models.faucet.FaucetResponse;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

import java.math.BigInteger;
Expand Down Expand Up @@ -40,6 +43,12 @@ public Balance balance(Account account) throws EthereumException {
return accountsManager.getAccountBalance(account.address);
}

@Override
public FaucetResponse requestEthFromFaucet(String address) throws ServiceException {
return FaucetService.requestEthFromFaucet(this.accountsManager.getFaucetUrl(), address);
}


@Override
public TransactionReceipt requestTokens(BigInteger amount) throws EthereumException {

Expand Down
53 changes: 53 additions & 0 deletions src/main/java/io/keyko/nevermined/external/FaucetService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.keyko.nevermined.external;

import com.fasterxml.jackson.core.type.TypeReference;
import io.keyko.common.helpers.HttpHelper;
import io.keyko.common.models.HttpResponse;
import io.keyko.nevermined.exceptions.ServiceException;
import io.keyko.nevermined.models.faucet.FaucetRequest;
import io.keyko.nevermined.models.faucet.FaucetResponse;
import org.apache.http.HttpStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;

/**
* Service for Gateway's Integration
*/
public class FaucetService {

private static final Logger log = LogManager.getLogger(FaucetService.class);

private static final String FAUCET_URI = "/faucet";


/**
* Requests Network ETH to the faucet for paying the transactions gas
*
* @param address address requesting ETH from the faucet
* @return boolean
* @throws ServiceException if there is an error communicating with the Faucet
*/
public static FaucetResponse requestEthFromFaucet(String faucetUrl, String address) throws ServiceException {
try {

final FaucetRequest faucetRequest = new FaucetRequest(address);
HttpResponse response = HttpHelper.httpClientPost(
faucetUrl + FAUCET_URI, new ArrayList<>(), faucetRequest.toJson());

if (response.getStatusCode() != HttpStatus.SC_OK && response.getStatusCode() != HttpStatus.SC_CREATED) {
log.warn("Error getting funds from faucet " + response.getBody());
}

return FaucetResponse.fromJSON(new TypeReference<>() {
}, response.getBody());

} catch (Exception ex) {
String msg = "Error requesting eth from faucet for address " + address;
log.error(msg + ": " + ex.getMessage());
throw new ServiceException("Exception getting ETH from faucet: " + ex.getMessage());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.keyko.common.helpers.StringsHelper;
import io.keyko.common.models.HttpResponse;
import io.keyko.nevermined.exceptions.ServiceException;
import io.keyko.nevermined.models.AbstractModel;
import io.keyko.nevermined.models.gateway.*;
import io.keyko.nevermined.models.service.Service;
import io.keyko.nevermined.models.service.types.AuthorizationService;
Expand Down Expand Up @@ -57,7 +58,7 @@ public void setCode(Integer code) {
}
}

public static class ServiceExecutionResult {
public static class ServiceExecutionResult extends AbstractModel {

private Boolean ok;
private String workflowId;
Expand Down Expand Up @@ -263,7 +264,7 @@ public static Status getStatus(String serviceEndpoint) throws IOException {
if (httpResponse.getStatusCode() != 200) {
throw new IOException("Invalid http response from Gateway: " + httpResponse.getStatusCode());
}
return Status.fromJSON(new TypeReference<Status>() {}, httpResponse.getBody());
return Status.fromJSON(new TypeReference<>() {}, httpResponse.getBody());
} catch (HttpException e) {
throw new IOException("Unable to fetch status page", e);
} catch (Exception e) {
Expand Down Expand Up @@ -317,7 +318,7 @@ public static EncryptionResponse encrypt(String gatewayUrl, String message, Auth
log.error("Unable to Encrypt Message: " + response.toString());
throw new ServiceException("Unable to Encrypt Message");
}
return EncryptionResponse.fromJSON(new TypeReference<EncryptionResponse>() {}, response.getBody());
return EncryptionResponse.fromJSON(new TypeReference<>() {}, response.getBody());

} catch (Exception e) {
log.error("Error encrypting message: " + e.getMessage());
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/io/keyko/nevermined/manager/AccountsManager.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package io.keyko.nevermined.manager;

import com.fasterxml.jackson.core.type.TypeReference;
import io.keyko.common.helpers.HttpHelper;
import io.keyko.common.models.HttpResponse;
import io.keyko.common.web3.KeeperService;
import io.keyko.nevermined.exceptions.EthereumException;
import io.keyko.nevermined.exceptions.ServiceException;
import io.keyko.nevermined.external.MetadataApiService;
import io.keyko.nevermined.models.Account;
import io.keyko.nevermined.models.Balance;
import io.keyko.nevermined.models.faucet.FaucetRequest;
import io.keyko.nevermined.models.faucet.FaucetResponse;
import org.apache.http.HttpStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.web3j.protocol.core.DefaultBlockParameterName;
Expand All @@ -23,6 +30,8 @@ public class AccountsManager extends BaseManager {

private static final Logger log = LogManager.getLogger(AccountsManager.class);

private String faucetUrl;

private AccountsManager(KeeperService keeperService, MetadataApiService metadataApiService) {
super(keeperService, metadataApiService);
}
Expand Down Expand Up @@ -77,7 +86,7 @@ public List<Account> getAccounts() throws EthereumException {
public Balance getAccountBalance(String accountAddress) throws EthereumException {
return new Balance(
getEthAccountBalance(accountAddress),
getOceanAccountBalance(accountAddress)
getNeverminedAccountBalance(accountAddress)
);
}

Expand Down Expand Up @@ -110,7 +119,7 @@ public BigInteger getEthAccountBalance(String accountAddress) throws EthereumExc
* @return nevermined balance
* @throws EthereumException if the EVM throws an exception
*/
public BigInteger getOceanAccountBalance(String accountAddress) throws EthereumException {
public BigInteger getNeverminedAccountBalance(String accountAddress) throws EthereumException {
try {
return tokenContract.balanceOf(accountAddress).send();
} catch (Exception ex) {
Expand All @@ -120,11 +129,8 @@ public BigInteger getOceanAccountBalance(String accountAddress) throws EthereumE
}
}


/**
* Requests Ocean Tokens from the Dispenser Smart Contract
* Contract: OceanMarket
* Method: requestTokens
* Requests Nevermined Tokens from the Dispenser Smart Contract
*
* @param amount amount of tokens requests
* @return TransactionReceipt
Expand Down Expand Up @@ -158,4 +164,11 @@ public TransactionReceipt transfer(String receiverAccount, BigInteger amount) th
}
}

public String getFaucetUrl() {
return this.faucetUrl;
}
public AccountsManager setFaucetUrl(String faucetUrl) {
this.faucetUrl = faucetUrl;
return this;
}
}
1 change: 0 additions & 1 deletion src/main/java/io/keyko/nevermined/manager/BaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,6 @@ public BaseManager setMainAccount(Account mainAccount) {
return this;
}


public String getProviderAddress() {
return providerAddress;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public DDO registerComputeService(AssetMetadata metadata, ProviderConfig provide
.getServiceBuilder(Service.ServiceTypes.COMPUTE)
.buildService(configuration);

computingService.serviceEndpoint = providerConfig.getExecuteEndpoint();
return registerAsset(metadata, providerConfig, computingService, new AuthConfig(providerConfig.getGatewayUrl()));

} catch ( ServiceException e) {
Expand Down Expand Up @@ -408,7 +409,7 @@ public OrderResult purchaseAssetDirect(DID did, int serviceIndex, Service.Servic
Service service;
if (serviceIndex >= 0)
service = ddo.getService(serviceIndex);
else if (serviceType.toString().equals(Service.ServiceTypes.COMPUTE)) {
else if (serviceType.toString().equalsIgnoreCase(Service.ServiceTypes.COMPUTE.toString())) {
service = ddo.getComputeService();
serviceIndex = service.index;
} else {
Expand Down

0 comments on commit 4fe4c5f

Please sign in to comment.