|
| 1 | +--- |
| 2 | +title: "Building a Website with the Gluon Gold SDK: A Step-by-Step Guide" |
| 3 | +author: "anon_real" |
| 4 | +date: "2024-06-24" |
| 5 | +image: "/images/gluon-sdk-tutorial.webp" |
| 6 | +excerpt: "Discover how to integrate Gluon Gold SDK to build a website enabling gold‑pegged ERG‑stablecoin operations directly in the browser." |
| 7 | +--- |
| 8 | + |
| 9 | +# Building a Website with the Gluon Gold SDK: A Step-by-Step Guide |
| 10 | + |
| 11 | +The [Gluon Gold SDK](https://github.com/StabilityNexus/Gluon-Ergo-SDK) is a powerful tool designed to facilitate interaction with Gluon Gold, a Gold-pegged ERG-backed stablecoin based on the Gluon protocol, running on the Ergo blockchain. This SDK allows developers to easily perform complex operations such as fission, fusion, and transmutations of stable and volatile tokeons (a.k.a. neutrons and protons, respectively and generally, or GAU and GAUC, in the case of Gluon Gold on Ergo). The SDK is particularly useful for developers looking to integrate these functionalities into web applications, bots, etc. This enables users to interact with the Gluon protocol directly from their browsers. |
| 12 | + |
| 13 | +In this blog post, we will explore how to use the Gluon Gold SDK in a website to interact with the Gluon protocol. We will focus on utilizing the SDK’s functionalities to perform the four main operations: fission, fusion, transmuting to gold, and transmuting from gold. Additionally, we will discuss how to display relevant Gluon Gold data, such as fusion ratio, prices, fees, etc. to users. |
| 14 | + |
| 15 | +## Getting Started with the Gluon Gold SDK |
| 16 | + |
| 17 | +### Installation |
| 18 | + |
| 19 | +To begin, install the Gluon Gold SDK using npm: |
| 20 | + |
| 21 | +``` |
| 22 | +npm install gluon-gold-sdk@1.0.0 |
| 23 | +``` |
| 24 | + |
| 25 | +### Importing the SDK in your code |
| 26 | +To use the SDK in a browser environment, you can dynamically import it as follows: |
| 27 | + |
| 28 | +``` |
| 29 | +if (typeof window !== 'undefined') { |
| 30 | + gluon = import('gluon-gold-sdk'); |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### Creating an Instance of the SDK |
| 35 | +You can create a new instance of Gluon which let’s you interact with the protocol. |
| 36 | + |
| 37 | +``` |
| 38 | +const resolvedGluon = await gluon; |
| 39 | +const gluonInstance = new resolvedGluon.Gluon(); |
| 40 | +``` |
| 41 | + |
| 42 | +### Adjusting the Gluon Gold SDK Parameters |
| 43 | +Before you start performing operations with the Gluon Gold SDK, it’s important to configure the SDK with the appropriate parameters as you see fit. This ensures that the SDK operates correctly within your desired network environment and with the specified fees. |
| 44 | + |
| 45 | +To adjust the constant parameters, you can create a new instance of the Config class and set the necessary properties. Here's an example of how to do this: |
| 46 | + |
| 47 | +``` |
| 48 | +// Create a new configuration instance |
| 49 | +const config = new Config(); |
| 50 | +``` |
| 51 | + |
| 52 | +``` |
| 53 | +// Set the network to the desired environment (e.g., 'mainnet' or 'testnet') |
| 54 | +config.NETWORK = 'mainnet'; // or 'testnet' |
| 55 | +// Set the miner fee (in nanoERGs) |
| 56 | +config.MINER_FEE = 1000000; // Example: 0.001 ERG |
| 57 | +// Set the node URL for connecting to the Ergo blockchain |
| 58 | +config.NODE_URL = 'https://ergo-node.example.com'; |
| 59 | +// Set the UI fee percentage: 1000 means 1% of the value of each operation that users perform will be sent to the UI devs |
| 60 | +config.UI_FEE = 1000; // Example: 1% fee |
| 61 | +// Set the UI tree where a predefined percentage of each operation will be sent to |
| 62 | +config.UI_TREE = 'your-ui-tree-value-here'; |
| 63 | +``` |
| 64 | + |
| 65 | +By configuring these parameters, you ensure that the SDK is tailored to your application’s requirements, allowing for seamless interaction with the Gluon protocol. Adjust these values according to your specific needs and the environment in which your application will operate. |
| 66 | + |
| 67 | +### Setting Up the Environment |
| 68 | +To perform operations with the Gluon Gold SDK, it’s crucial to first obtain the necessary boxes from the Ergo blockchain. These boxes represent the current state of the protocol on the blockchain and are essential for executing transactions like fission, fusion, and transmutation. |
| 69 | + |
| 70 | +The Gluon protocol operates on the Ergo blockchain, and to interact with it, your application needs to fetch the latest state of these boxes. This is done by connecting to a node, which acts as a gateway to the blockchain, providing access to the most recent data. |
| 71 | + |
| 72 | +Here’s how you can set up the environment by fetching the required boxes: |
| 73 | + |
| 74 | +``` |
| 75 | +// Fetch the current unspent Gold Oracle Box |
| 76 | +const oracleBoxJs = await gluonInstance.getGoldOracleBox(); |
| 77 | +// Fetch the current unspent Gluon Box |
| 78 | +const gluonBoxJs = await gluonInstance.getGluonBox(); |
| 79 | +``` |
| 80 | + |
| 81 | +These boxes will be used in the rest of the tutorial. |
| 82 | + |
| 83 | +Why Refresh the Boxes? |
| 84 | + |
| 85 | +The state of the blockchain changes. Either the Gold price changes or Gluon parameters change with transactions occurring continuously. By periodically refreshing these boxes, you ensure that your application is working with the most up-to-date information. Consequently, it’s important to keep them updated to avoid using stale data. |
| 86 | + |
| 87 | +### User Interaction with the Nautilus Wallet |
| 88 | +To perform Gluon operations (e.g., minting gold), you need to fetch users’ boxes (funds). You can use any approach to get these depending on your application. One of the most common approaches is to use user’s wallet to fetch these boxes. For example, to use this in Nautilus: |
| 89 | + |
| 90 | +``` |
| 91 | +const userBoxes = await ergo.get_utxos(); |
| 92 | +``` |
| 93 | + |
| 94 | +It is desired to use as few boxes as possible to construct our necessary transactions. Too many boxes will make the constructed transaction too big which could cause several issues such as needing a high mining fee. |
| 95 | + |
| 96 | +## Performing Gluon operation with the SDK |
| 97 | + |
| 98 | +### Fission |
| 99 | +Fission is the process of converting ERGs into Neutrons (GAUs, stable coins) and Protons (GAUCs, volatile coins). Here’s how you can perform a fission operation: |
| 100 | + |
| 101 | +``` |
| 102 | +const amountToFission = ... // amount in nanoERGs |
| 103 | +const unsignedTransaction = await gluonInstance.fissionForEip12(gluonBoxJs, oracleBoxJs, userBoxes, amountToFission); |
| 104 | +``` |
| 105 | + |
| 106 | +### Fusion |
| 107 | +Fusion is the reverse process, where Neutrons (GAUs, stable coins) and Protons (GAUCs, volatile coins) are converted back into ERGs: |
| 108 | + |
| 109 | +``` |
| 110 | +const amountToFusion = ... // amount in nanoERGs |
| 111 | +const unsignedTransaction = await gluonInstance.fusionForEip12(gluonBoxJs, oracleBoxJs, userBoxes, amountToFusion); |
| 112 | +``` |
| 113 | + |
| 114 | +### Transmuting to Gold |
| 115 | +This operation involves sending Protons (GAUCs) to the reactor to receive Neutrons (GAUs): |
| 116 | + |
| 117 | +``` |
| 118 | +const protonsToTransmute = 5000000; // Example amount |
| 119 | +const height = await nodeService.getNetworkHeight(); // Get current network height |
| 120 | +const oracleBuyBackJs = await gluonInstance.getOracleBuyBackBoxJs(); |
| 121 | +const unsignedTransaction = await gluonInstance.transmuteToGoldForEip12(gluonBoxJs, oracleBoxJs, userBoxes, oracleBuyBackJs, protonsToTransmute, height); |
| 122 | +``` |
| 123 | + |
| 124 | +### Transmuting from Gold |
| 125 | +This operation involves sending Neutrons (GAUs) to the reactor to receive Protons (GAUCs): |
| 126 | + |
| 127 | +``` |
| 128 | +const neutronsToDecay = 2700000; // Example amount |
| 129 | +const unsignedTransaction = await gluonInstance.transmuteFromGoldForEip12(gluonBoxJs, oracleBoxJs, userBoxes, oracleBuyBackJs, neutronsToDecay, height); |
| 130 | +``` |
| 131 | + |
| 132 | +### Signing the Transaction |
| 133 | +You can use any method to sign the transaction. One of the most common methods is to use the user’s wallet to sign the desired transaction. For example, to do this in Nautilus: |
| 134 | + |
| 135 | +``` |
| 136 | +const signedTransaction = await ergo.sign_tx(unsignedTransaction); |
| 137 | +``` |
| 138 | + |
| 139 | +The signed transaction can then be submitted to the network using the `submit_tx` method provided by the Nautilus wallet. |
| 140 | + |
| 141 | +``` |
| 142 | +const txId = await ergo.submit_tx(signedTransaction); |
| 143 | +``` |
| 144 | + |
| 145 | +It is important to catch any errors that may occur during the transaction signing and submission process and display them to the user. |
| 146 | + |
| 147 | +### Calculating Fees |
| 148 | +When the user is requesting one of these operations (e.g., transmuting to Gold), it is important to show any fee that he will pay. In the Gluon protocol these fees could be |
| 149 | + |
| 150 | +- Gluon dev fee |
| 151 | +- UI fee (which you can set to receive yourself) |
| 152 | +- Oracle fee |
| 153 | + |
| 154 | +For each operation, you can calculate the total fees and their breakdown: |
| 155 | + |
| 156 | +``` |
| 157 | +const fees = await gluonInstance.getTotalFeeAmountFission(gluonBoxJs, amountToFission); |
| 158 | +console.log(`Developer Fee: ${fees.devFee}, UI Fee: ${fees.uiFee}, Oracle Fee: ${fees.oracleFee}, Total Fee: ${fees.totalFee}`); |
| 159 | +``` |
| 160 | + |
| 161 | +You can then show these fees (or the total amount) to the user. All these fees are in nanoErg. |
| 162 | + |
| 163 | +### Displaying Gluon Gold Data |
| 164 | +To provide users with detailed information about the Gluon Gold protocol, such as fusion ratio, prices, fees, etc., you can use the SDK’s methods to calculate and display these values: |
| 165 | + |
| 166 | +### Displaying Prices |
| 167 | +You can also display the current prices of Neutrons and Protons as well as the oracle price for Gold: |
| 168 | + |
| 169 | +``` |
| 170 | +const neutronPrice = await gluonInstance.neutronPrice(oracleBoxJs); |
| 171 | +const protonPrice = await gluonInstance.protonPrice(oracleBoxJs); |
| 172 | +const oracleGoldPricePerKg = await oracleBoxJs.getPrice(); |
| 173 | +const oracleGoldPricePerGram = await oracleBoxJs.getPricePerGram(); |
| 174 | +console.log(`Neutron Price: ${neutronPrice}, Proton Price: ${protonPrice}, Gold Price: ${oracleGoldPricePerKg}, Gold Price Per Gram: ${oracleGoldPricePerGram}`); |
| 175 | +``` |
| 176 | + |
| 177 | +These values should be displayed in the website to users so they know what the current price of the assets are. They can easily be called with the updated oracle box to get the latest price periodically. |
| 178 | + |
| 179 | +### N-Day Volume Calculation |
| 180 | +The SDK provides methods to calculate volume of gold conversions over a specified number of days (up to 14 days). You can calculate the volume of protons to neutrons or neutrons to protons. |
| 181 | + |
| 182 | +``` |
| 183 | +// Calculate the n-day volume of protons to neutrons |
| 184 | +const n = 7; // specify the number of days, up to 14 days |
| 185 | +const volumeProtonsToNeutrons = await gluon.accumulateVolumeProtonsToNeutrons(n); |
| 186 | +// Calculate the n-day volume of neutrons to protons |
| 187 | +const volumeNeutronsToProtons = await gluon.accumulateVolumeNeutronsToProtons(n); |
| 188 | +console.log(`Volume of protons to neutrons over ${n} days:`, volumeProtonsToNeutrons); |
| 189 | +console.log(`Volume of neutrons to protons over ${n} days:`, volumeNeutronsToProtons); |
| 190 | +``` |
| 191 | + |
| 192 | +Displaying these to the user will give them a good idea of the trend of the protocol. |
| 193 | + |
| 194 | +### More Stats |
| 195 | +For a more complete list of statistics you can use (for example TVL), take a look at the README in the SDK’s GitHub repository: [here](https://github.com/StabilityNexus/Gluon-Ergo-SDK/blob/main/README.md) |
| 196 | + |
| 197 | +## Conclusion |
| 198 | +The Gluon Gold SDK provides a comprehensive set of tools for interacting with the Gluon protocol on the Ergo blockchain. By integrating this SDK into your website, you can offer users the ability to perform complex financial operations directly from their browsers. Whether it’s fission, fusion, or transmutation, the SDK simplifies these processes and provides detailed transaction data to enhance user experience. |
| 199 | + |
| 200 | +By following the steps outlined in this blog post, you can effectively leverage the Gluon Gold SDK to build a robust web application that interacts seamlessly with the Gluon protocol. |
0 commit comments