Skip to content

Commit

Permalink
[docs] Add deploy account contract example (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
DelevoXDG authored Aug 2, 2023
1 parent 4480c1b commit f768537
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 15 deletions.
93 changes: 93 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,99 @@ public class Main {
}
```

### Deploy Account example

```java
import org.junit.jupiter.api.Test;
import com.swmansion.starknet.account.StandardAccount;
import com.swmansion.starknet.crypto.StarknetCurve;
import com.swmansion.starknet.data.ContractAddressCalculator;
import com.swmansion.starknet.data.types.*;
import com.swmansion.starknet.data.types.transactions.*;
import com.swmansion.starknet.data.types.transactions.TransactionReceipt;
import com.swmansion.starknet.provider.rpc.JsonRpcProvider;

import java.math.BigInteger;
import java.util.List;

public class Main {
public static void main(String[] args) {
// Create a provider for interacting with StarkNet
JsonRpcProvider provider = new JsonRpcProvider("https://example-node-url.com/rpc", StarknetChainId.TESTNET);

// Create an account interface
Felt privateKey = Felt.fromHex("0x12345");
Felt publicKey = StarknetCurve.getPublicKey(privateKey);

// Use the class hash of the desired account contract (i.e. the class hash of OpenZeppelin account contract)
Felt classHash = Felt.fromHex("0x058d97f7d76e78f44905cc30cb65b91ea49a4b908a76703c54197bca90f81773");
Felt salt = new Felt(789);
List<Felt> calldata = List.of(publicKey);
Felt address = ContractAddressCalculator.calculateAddressFromHash(
classHash,
calldata,
salt
);

StandardAccount account = new StandardAccount(address, privateKey, provider, Felt.ZERO);

Felt maxFee = Felt.fromHex("0x11fcc58c7f7000"); // should be 10*fee from estimate deploy account fee

// Make sure to prefund the address with at least maxFee

// Create and sign deploy account transaction
DeployAccountTransactionPayload payload = account.signDeployAccount(
classHash,
calldata,
salt,
maxFee
);

DeployAccountResponse response = provider.deployAccount(payload).send();
}
}
```

or in Kotlin

```kotlin
fun main(args: Array<String>) {
// Create a provider for interacting with StarkNet
val provider = JsonRpcProvider("https://example-node-url.com/rpc", StarknetChainId.TESTNET)

// Create an account interface
val privateKey = Felt.fromHex("0x12345")
val publicKey = StarknetCurve.getPublicKey(privateKey)

// Use the class hash of desired account contract (i.e. the class hash of OpenZeppelin account contract)
val classHash = Felt.fromHex("0x058d97f7d76e78f44905cc30cb65b91ea49a4b908a76703c54197bca90f81773")
val salt = Felt(789)
val calldata = listOf(publicKey)
val address = ContractAddressCalculator.calculateAddressFromHash(
classHash = classHash,
calldata = calldata,
salt = salt,
)

val account = StandardAccount(
address,
privateKey,
provider,
)

val payload = account.signDeployAccount(
classHash = classHash,
salt = salt,
calldata = calldata,
maxFee = Felt.fromHex("0x11fcc58c7f7000"), // should be 10*fee from estimate deploy account fee
)

// Create and sign deploy account transaction
val response = provider.deployAccount(payload).send()
}
```


## Reusing http clients

Make sure you don't create a new provider every time you want to use one. Instead, you should reuse existing instance.
Expand Down
152 changes: 137 additions & 15 deletions lib/starknet-jvm.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ querying starknet state, executing transactions and deploying contracts.

Although written in Kotlin, StarkNet-jvm has been created with compatibility with Java in mind.

⚠️Gateway provider is currently marked as deprecated and will soon be removed.
⚠️Gateway provider is currently marked as deprecated and will soon be removed. Please use JSON-RPC provider instead.

## Making synchronous requests

Expand All @@ -18,12 +18,12 @@ import com.swmansion.starknet.data.types.BlockTag;
import com.swmansion.starknet.data.types.Felt;
import com.swmansion.starknet.provider.Provider;
import com.swmansion.starknet.provider.Request;
import com.swmansion.starknet.provider.gateway.GatewayProvider;
import com.swmansion.starknet.provider.rpc.JsonRpcProvider;

public class Main {
public static void main(String[] args) {
// Create a provider for interacting with StarkNet
Provider provider = GatewayProvider.makeTestnetProvider();
Provider provider = new JsonRpcProvider("https://example-node-url.com/rpc", StarknetChainId.TESTNET);

// Create an account interface
Felt accountAddress = Felt.fromHex("0x13241455");
Expand All @@ -47,11 +47,11 @@ public class Main {
import com.swmansion.starknet.account.StandardAccount
import com.swmansion.starknet.data.types.BlockTag
import com.swmansion.starknet.data.types.Felt
import com.swmansion.starknet.provider.gateway.GatewayProvider
import com.swmansion.starknet.provider.rpc.JsonRpcProvider

fun main() {
// Create a provider for interacting with StarkNet
val provider = GatewayProvider.makeTestnetProvider()
val provider = JsonRpcProvider("https://example-node-url.com/rpc", StarknetChainId.TESTNET)

// Create an account interface
val accountAddress = Felt.fromHex("0x1052524524")
Expand Down Expand Up @@ -82,14 +82,14 @@ import com.swmansion.starknet.data.types.BlockTag;
import com.swmansion.starknet.data.types.Felt;
import com.swmansion.starknet.provider.Provider;
import com.swmansion.starknet.provider.Request;
import com.swmansion.starknet.provider.gateway.GatewayProvider;
import com.swmansion.starknet.provider.rpc.JsonRpcProvider;

import java.util.concurrent.CompletableFuture;

public class Main {
public static void main(String[] args) {
// Create a provider for interacting with StarkNet
Provider provider = GatewayProvider.makeTestnetProvider();
Provider provider = new JsonRpcProvider("https://example-node-url.com/rpc", StarknetChainId.TESTNET);

// Create an account interface
Felt accountAddress = Felt.fromHex("0x13241455");
Expand All @@ -113,12 +113,11 @@ public class Main {
import com.swmansion.starknet.account.StandardAccount
import com.swmansion.starknet.data.types.BlockTag
import com.swmansion.starknet.data.types.Felt
import com.swmansion.starknet.provider.gateway.GatewayProvider
import com.swmansion.starknet.provider.rpc.JsonRpcProvider

fun main() {
// Create a provider for interacting with StarkNet
val provider = GatewayProvider.makeTestnetProvider()

val provider = JsonRpcProvider("https://example-node-url.com/rpc", StarknetChainId.TESTNET)
// Create an account interface
val accountAddress = Felt.fromHex("0x1052524524")
val privateKey = Felt.fromHex("0x4232362662")
Expand All @@ -138,7 +137,7 @@ fun main() {

Account interface used to simplify preparing, signing StarkNet transactions and automatic fee estimation.

Example usages of `StandardAccount`
### Example usages of `StandardAccount`

```java
import com.swmansion.starknet.account.Account;
Expand All @@ -147,13 +146,13 @@ import com.swmansion.starknet.data.types.*;
import com.swmansion.starknet.data.types.transactions.InvokeFunctionPayload;
import com.swmansion.starknet.provider.Provider;
import com.swmansion.starknet.provider.Request;
import com.swmansion.starknet.provider.gateway.GatewayProvider;
import com.swmansion.starknet.provider.rpc.JsonRpcProvider;

import java.util.List;

public class Main {
public static void main(String[] args) {
Provider provider = GatewayProvider.makeTestnetProvider();
Provider provider = new JsonRpcProvider("https://example-node-url.com/rpc", StarknetChainId.TESTNET);
Felt address = new Felt(0x1234);
Felt privateKey = new Felt(0x1);
Account account = new StandardAccount(address, privateKey, provider);
Expand Down Expand Up @@ -238,6 +237,129 @@ fun main(args: Array<String>) {
}
```

### Deploy Account example

```java
import org.junit.jupiter.api.Test;
import com.swmansion.starknet.account.StandardAccount;
import com.swmansion.starknet.crypto.StarknetCurve;
import com.swmansion.starknet.data.ContractAddressCalculator;
import com.swmansion.starknet.data.types.*;
import com.swmansion.starknet.data.types.transactions.*;
import com.swmansion.starknet.data.types.transactions.TransactionReceipt;
import com.swmansion.starknet.provider.rpc.JsonRpcProvider;

import java.math.BigInteger;
import java.util.List;

public class Main {
public static void main(String[] args) {
// Create a provider for interacting with StarkNet
JsonRpcProvider provider = new JsonRpcProvider("https://example-node-url.com/rpc", StarknetChainId.TESTNET);

// Create an account interface
Felt privateKey = Felt.fromHex("0x12345");
Felt publicKey = StarknetCurve.getPublicKey(privateKey);

// Use the class hash of the desired account contract (i.e. the class hash of OpenZeppelin account contract)
Felt classHash = Felt.fromHex("0x058d97f7d76e78f44905cc30cb65b91ea49a4b908a76703c54197bca90f81773");
Felt salt = new Felt(789);
List<Felt> calldata = List.of(publicKey);
Felt address = ContractAddressCalculator.calculateAddressFromHash(
classHash,
calldata,
salt
);

StandardAccount account = new StandardAccount(address, privateKey, provider, Felt.ZERO);

// Estimate the fee for deploying the account
DeployAccountTransactionPayload payloadForFeeEstimation = account.signDeployAccount(
classHash,
calldata,
salt,
Felt.ZERO,
Felt.ZERO,
true
);

List<EstimateFeeResponse> feePayload = provider.getEstimateFee(
List.of(payloadForFeeEstimation)
).send();
Felt estimateDeployAccountFee = feePayload.get(0).getOverallFee();
// Multiply the estimated fee by 10 to ensure the transaction will not fail due to insufficient funds
Felt maxFee = new Felt(estimateDeployAccountFee.getValue().multiply(BigInteger.TEN));

// Make sure to prefund the address with at least maxFee

// Create and sign deploy account transaction
DeployAccountTransactionPayload payload = account.signDeployAccount(
classHash,
calldata,
salt,
maxFee
);

DeployAccountResponse response = provider.deployAccount(payload).send();
}
}
```

or in Kotlin

```kotlin
fun main(args: Array<String>) {
// Create a provider for interacting with StarkNet
val provider = JsonRpcProvider("https://example-node-url.com/rpc", StarknetChainId.TESTNET)

// Create an account interface
val privateKey = Felt.fromHex("0x12345")
val publicKey = StarknetCurve.getPublicKey(privateKey)

// Use the class hash of desired account contract (i.e. the class hash of OpenZeppelin account contract)
val classHash = Felt.fromHex("0x058d97f7d76e78f44905cc30cb65b91ea49a4b908a76703c54197bca90f81773")
val salt = Felt(789)
val calldata = listOf(publicKey)
val address = ContractAddressCalculator.calculateAddressFromHash(
classHash = classHash,
calldata = calldata,
salt = salt,
)

val account = StandardAccount(
address,
privateKey,
provider,
)

// Estimate the fee for deploying the account
val payloadForFeeEstimation = account.signDeployAccount(
classHash = classHash,
salt = salt,
calldata = calldata,
maxFee = Felt.ZERO,
nonce = Felt.ZERO,
forFeeEstimate = true,
)
val feePayload = provider.getEstimateFee(listOf(payloadForFeeEstimation)).send()
val fee = feePayload.first().overallFee
// Multiply the estimated fee by 10 to ensure the transaction will not fail due to insufficient funds
val maxFee = Felt(fee.value.multiply(BigInteger.TEN))

// Make sure to prefund the address with at least maxFee

val payload = account.signDeployAccount(
classHash = classHash,
salt = salt,
calldata = calldata,
maxFee = maxFee,
)

// Create and sign deploy account transaction
val response = provider.deployAccount(payload).send()
}
```

# Package com.swmansion.starknet.crypto

Cryptography and signature related classes.
Expand Down Expand Up @@ -360,9 +482,9 @@ to communicate with the network.

```java
// JsonRpcProvider can only be created using constructor
new JsonRpcProvider("rpcNodeUrl", StarknetChainId.TESTNET);
new JsonRpcProvider("https://example-node-url.com/rpc", StarknetChainId.TESTNET);
// or with a custom HttpService
new JsonRpcProvider("rpcNodeUrl", StarknetChainId.TESTNET, myHttpService);
new JsonRpcProvider("https://example-node-url.com/rpc", StarknetChainId.TESTNET, myHttpService);
```

# Package com.swmansion.starknet.service.http
Expand Down

0 comments on commit f768537

Please sign in to comment.