Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: updated blaze docs #338

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 91 additions & 18 deletions docs/pages/apis/blaze.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,108 @@ import { Callout } from "nextra/components";

Blaze is a NodeJS library for creating Cardano transactions and off-chain code for your Aiken contracts in JavaScript. You can learn more about Blaze [in their documentation site](https://blaze.butane.dev/).

Blaze supports multiple backend providers for interacting with the Cardano network. One of those providers is `U5C` (short for UtxoRPC) which is one of the APIs suppored by Dolos. You can learn more about U5C in the [U5C documentation](./grpc.mdx).
Blaze supports multiple backend providers for interacting with the Cardano network. One of those providers is `U5C` (short for UtxoRPC) which is one of the APIs supported by Dolos. You can learn more about U5C in the [U5C documentation](./grpc.mdx).

## Example
## Installation

The following example demonstrates how to use the Blaze SDK to query UTXOs for a given address using a local Dolos instance.
To install the UTxORPC provider for blaze, use npm:

```ts
// Import Blaze SDK and U5C provider
import { Core, U5C } from "@blaze-cardano/sdk";
```bash
npm install @utxorpc/blaze-provider
```

You also need to install the **Blaze SDK**:

```bash
npm install @blaze-cardano/sdk
```

## Sample Usage

### Step 1: Import Blaze SDK and the UTxORPC Provider for Blaze

```javascript
import {
Bip32PrivateKey,
mnemonicToEntropy,
wordlist,
} from "@blaze-cardano/core";
import {
HotWallet,
Core,
Blaze,
} from "@blaze-cardano/sdk";
import { U5C } from "@utxorpc/blaze-provider";
```

// Create a new U5C provider pointing to the local Dolos instance
### Step 2: Create a New U5C Provider with Dolos
Now this is where we can utilize our local Dolos node and use it for our UTxORPC provider. We can initiliaze our node through this [link](/quickstart).

Here's how to create the U5C Provider:
```javascript
const provider = new U5C({
url: "http://localhost:50051",
url: "http://localhost:50051",
});
```

// Query Utxos for the given address (the address in the example is a preview address randomly chosen from an explorer, use your own address)
const utxos = await provider.getUnspentOutputs(
Core.Address.fromBech32(
"addr_test1vpetczxy5uc9tkkqhrxgj6t0sggthyg8dd0qp22fte6wdtgvau4rn"
)
);
### Step 3: Create a New Wallet from a Mnemonic

// Log the UTXOs to the console
utxos.map((utxo) => {
console.log(utxo.toCbor());
});
```javascript
const mnemonic = "your 24-word mnemonic here";
const entropy = mnemonicToEntropy(mnemonic, wordlist);
const masterkey = Bip32PrivateKey.fromBip39Entropy(Buffer.from(entropy), "");
const wallet = await HotWallet.fromMasterkey(masterkey.hex(), provider);
```

### Step 4: Create a Blaze Instance from the Wallet and Provider

```javascript
const blaze = await Blaze.from(provider, wallet);
```

Optional: Print the wallet address

```javascript
console.log("Wallet address", wallet.address.toBech32());
```

Optional: Print the wallet balance

```javascript
console.log("Wallet balance", (await wallet.getBalance()).toCore());
```

### Step 5: Create an Example Transaction

```javascript
const tx = await blaze
.newTransaction()
.payLovelace(
Core.Address.fromBech32(
"addr_test1qrnrqg4s73skqfyyj69mzr7clpe8s7ux9t8z6l55x2f2xuqra34p9pswlrq86nq63hna7p4vkrcrxznqslkta9eqs2nsmlqvnk",
),
5_000_000n,
)
.complete();
```

### Step 6: Sign the Transaction

```javascript
const signedTx = await blaze.signTransaction(tx);
```

### Step 7: Submit the Transaction to the Blockchain Network

```javascript
const txId = await blaze.provider.postTransactionToChain(signedTx);
```

## Conclusion

This showcases a use-case for Dolos which is through The **UTxORPC (u5c)** provider for [Blaze](https://github.com/butaneprotocol/blaze-cardano). For further customization and advanced usage, refer to the documentation for [Blaze](https://github.com/butaneprotocol/blaze-cardano). By understanding and utilizing these tools, you can develop robust applications that interact with Cardano efficiently.

<Callout type="info">
Check the [Blaze client example](https://github.com/txpipe/dolos/tree/main/examples/blaze-client) in the Dolos repository for a working version of the above snippet.
</Callout>

83 changes: 65 additions & 18 deletions examples/blaze-client/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,66 @@
// Step #1
// Import Blaze SDK and U5C provider
import { Core, U5C } from "@blaze-cardano/sdk";

// Create a new U5C provider pointing to the local Dolos instance
const provider = new U5C({
url: "http://localhost:50051",
});

// Query Utxos for the given address (the address in the example is a preview address randomly chosen from an explorer, use your own address)
const utxos = await provider.getUnspentOutputs(
Core.Address.fromBech32(
"addr_test1vpetczxy5uc9tkkqhrxgj6t0sggthyg8dd0qp22fte6wdtgvau4rn"
)
);

// Log the UTXOs to the console
utxos.map((utxo) => {
console.log(utxo.toCbor());
});
import {
Bip32PrivateKey,
mnemonicToEntropy,
wordlist,
} from "@blaze-cardano/core";
import { HotWallet, Core, Blaze } from "@blaze-cardano/sdk";
import { U5C } from "@utxorpc/blaze-provider";

async function main() {
// Step #2
// Create a new U5C provider
// In this example we use Demeter hosted UTXO provider
// but you can run a local Dolos https://github.com/txpipe/dolos instance and connect to its UTxO endpoint
// If this is the case then you can remove the headers field
const provider = new U5C({
url: "https://preview.utxorpc-v0.demeter.run",
headers: {
"dmtr-api-key": "dmtr_utxorpc19r0r7x8stkzejplyyra8n6d70gw276un",
},
});

// Step #3
// Create a new wallet from a mnemonic
const mnemonic =
"end link visit estate sock hurt crucial forum eagle earn idle laptop wheat rookie when hard suffer duty kingdom clerk glide mechanic debris jar";
const entropy = mnemonicToEntropy(mnemonic, wordlist);
const masterkey = Bip32PrivateKey.fromBip39Entropy(Buffer.from(entropy), "");
const wallet = await HotWallet.fromMasterkey(masterkey.hex(), provider);

// Step #4
// Create a Blaze instance from the wallet and provider
const blaze = await Blaze.from(provider, wallet);

// Optional: Print the wallet address
console.log("Wallet address", wallet.address.toBech32());

// Optional: Print the wallet balance
console.log("Wallet balance", (await wallet.getBalance()).toCore());

// Step #5
// Create a example transaction that sends 5 ADA to an address
const tx = await blaze
.newTransaction()
.payLovelace(
Core.Address.fromBech32(
"addr_test1qrnrqg4s73skqfyyj69mzr7clpe8s7ux9t8z6l55x2f2xuqra34p9pswlrq86nq63hna7p4vkrcrxznqslkta9eqs2nsmlqvnk",
),
5_000_000n,
)
.complete();

// Step #6
// Sign the transaction
const signexTx = await blaze.signTransaction(tx);

// Step #7
// Submit the transaction to the blockchain network
const txId = await blaze.provider.postTransactionToChain(signexTx);

// Optional: Print the transaction ID
console.log("Transaction ID", txId);
}

main().catch(console.error);
Loading