Deploying a Soroban Contract from a .wasm File using js-stellar-sdk
Tags: soroban, contract, stellar, stellarsdk
Published: 2023-12-14 20:06:00

Soroban is a blockchain platform that allows developers to build and deploy smart contracts. In this article, we will explore how to deploy a Soroban contract from a .wasm file using typescript.

In this example we will deploy a token contract

## Prerequisites

Before we begin, make sure you have the following:

- Node.js installed on your machine
- You have installed `stellar-sdk`
- You have a .wasm file containing the contract code

## Understanding the Code

The code provided consists of two functions: `uploadTokenContractWasm` and `createTokenContract`. Because we need to upload the wasm code before we can create the contract, we will first look at the `uploadTokenContractWasm` function.

The `createTokenContract` function work as follow: It reads the .wasm file, calculates the hash of the wasm code, and creates a custom contract operation. It then builds and submits a transaction to create the contract.

## Deploying the Soroban Contract

To deploy a Soroban contract from a .wasm file, follow these steps:

1. Import the necessary modules and dependencies:
```typescript
import fs from 'fs';
import sdk from 'stellar-sdk';
import axios from 'axios';
```
2. Define the `uploadTokenContractWasm` function:
```typescript
export async function uploadTokenContractWasm(signer) {
  const wasmBuffer = fs.readFileSync("/path/to/soroban_token_contract.optimized.wasm");
  const hf = sdk.xdr.HostFunction.hostFunctionTypeUploadContractWasm(wasmBuffer);
  const op = sdk.Operation.uploadContractWasm({ wasm: wasmBuffer });
  // ... continue with the transaction building and submission
}
```

3. Define the `createTokenContract` function:
```typescript
export async function createTokenContract(signer) {
  const wasmBuffer = fs.readFileSync("/path/to/soroban_token_contract.optimized.wasm");
  const hash = sdk.hash(wasmBuffer);
  const op = sdk.Operation.createCustomContract({
    address: new sdk.Address(signer.publicKey),
    wasmHash: hash,
  });
  // ... continue with the transaction building and submission
}
```

4. Replace `/path/to/soroban_token_contract.optimized.wasm` with the actual path to your .wasm file.

5. Build and submit the transaction:
```typescript
const source = await sorobanServer.getAccount(signer.publicKey);
const sourceKeypair = sdk.Keypair.fromSecret(signer.privateKey);

let tx = new sdk.TransactionBuilder(source, { fee: sdk.BASE_FEE })
  .setNetworkPassphrase(sdk.Networks.TESTNET)
  .setTimeout(30)
  .addOperation(op)
  .build();

try {
  const preparedTransaction = await sorobanServer.prepareTransaction(tx);
  preparedTransaction.sign(sourceKeypair);
  const submitTransactionResponse = await sorobanServer.sendTransaction(preparedTransaction);
  return submitTransactionResponse;
} catch (error) {
  // Handle error cases
}
```

6. Replace `sorobanServer` with the appropriate Soroban server instance.

7. Run the desired function to deploy the contract:
```typescript
const signer = {
  publicKey: 'YOUR_PUBLIC_KEY',
  privateKey: 'YOUR_PRIVATE_KEY',
};

await uploadTokenContractWasm(signer);
// or
await createTokenContract(signer);
```

8. Replace `'YOUR_PUBLIC_KEY'` and `'YOUR_PRIVATE_KEY'` with your Soroban account's public and private keys.
9. You will need to initialize it afterwards

## Conclusion

Deploying a Soroban contract from a .wasm file is a straightforward process. By utilizing the provided code and following the steps outlined in this article, you can easily deploy your own Soroban contracts and leverage the power of blockchain technology.

Remember to handle any potential errors and customize the code according to your specific requirements. Happy coding!