-
Notifications
You must be signed in to change notification settings - Fork 2
Home
Yenwen Feng edited this page Mar 2, 2018
·
14 revisions
Etherspace is a type-safe Ethereum client to interact with Ethereum Smart Contract.
For example, to access greeter
smart contract from ETHEREUM » Create a digital greeter (Slightly modified because we need a setter):
contract greeter {
/* Define variable greeting of the type string */
string greeting;
/* This runs when the contract is executed */
function greeter(string _greeting) public {
greeting = _greeting;
}
/* Main function */
function greet() constant returns (string) {
return greeting;
}
/* Update greeting */
function newGreeting(string _greeting) public returns (string) {
greeting = _greeting;
return greeting;
}
}
By defining a Smart Contract interface in Kotlin / Java: (see: Smart Contract Interface)
// Kotlin
interface Greeter {
@Throws(IOException::class)
@Send
fun newGreeting(greeting: String): TransactionReceipt
@Throws(IOException::class)
@Call
fun greet(): String
}
// Java
public interface Greeter {
@Send
TransactionReceipt newGreeting(String greeting) throws IOException;
@Call
String greet() throws IOException;
}
Etherspace generates an implementation of Greeter
interface.
You can than use greeter
to interact with Smart Contract on Ethereum!
// Kotlin
val etherSpace = EtherSpace.build {
provider = "https://rinkeby.infura.io/" // Or your local node
credentials = Credentials(YOUR_PRIVATE_KEY_OR_WALLET)
}
var greeter = etherSpace.create(SMART_CONTRACT_ADDRESS, Greeter::class.java)
val receipt = greeter.newGreeting("Hello World")
println(greeter.greet()) // Should be "Hello World"
// Java
EtherSpace etherSpace = new EtherSpace.Builder()
.provider("https://rinkeby.infura.io/") // Or your local node
.credentials(new Credentials(YOUR_PRIVATE_KEY_OR_WALLET))
.build();
Greeter greeter = etherSpace.create(SMART_CONTRACT_ADDRESS, Greeter.class);
TransactionReceipt receipt = greeter.newGreeting("Hello World");
System.out.println(greeter.greet()); // Should be "Hello World"
-
Gradle
repositories { ... maven { url 'https://jitpack.io' } // should be the last entry } dependencies { ... compile 'com.github.Wraecca:etherspace-java:0.1.0' }
-
Maven
<repositories> <repository> <id>jitpack.io</id> <url>https://jitpack.io</url> </repository> </repositories> <dependency> <groupId>com.github.Wraecca</groupId> <artifactId>etherspace-java</artifactId> <version>0.1.0</version> </dependency>