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: cw20 to bank convert docs #516

Merged
merged 15 commits into from
Nov 29, 2024
Merged
Changes from 5 commits
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
1 change: 1 addition & 0 deletions .gitbook/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -86,6 +86,7 @@
- [Contracts](contracts/README.md)
- [Injective Name Service](contracts/injective-name-service.md)
- [Neptune Service](contracts/neptune-service.md)
- [CW20 to Bank & Market Order in One Transaction](contracts/cw20-convert-and-market-order-example.md)
- [Building dApps](building-dapps/README.md)
- [Configuring Nuxt](building-dapps/configuring-nuxt.md)
- [Configuring React](building-dapps/configuring-react.md)
11 changes: 6 additions & 5 deletions .gitbook/contracts/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Contracts

#### [What is CosmWasm?](./#what-is-cosmwasm-)[​](https://docs.injective.network/develop/guides/cosmwasm-dapps/#what-is-cosmwasm) <a href="#user-content-what-is-cosmwasm" id="user-content-what-is-cosmwasm"></a>
#### What is CosmWasm?

CosmWasm is a novel smart contracting platform built for the Cosmos ecosystem. You can learn more about CosmWasm [here](https://docs.cosmwasm.com/docs/), or see the [CosmWasm Book](https://book.cosmwasm.com/index.html) for a guide on creating CosmWasm smart contracts.

#### Specific Cosmwasm Contracts

| Topic | Description |
| --------------------------------------------------- | ---------------------- |
| [Injective Name Service](injective-name-service.md) | Injective Name Service |
| [Neptune Service](neptune-service.md) | Injective Name Service |
| Topic | Description |
| ------------------------------------------------------------------------------------------ | ---------------------- |
| [Injective Name Service](injective-name-service.md) | Injective Name Service |
| [Neptune Service](neptune-service.md) | Neptune Service |
| [CW20 to Bank & Market Order in One Transaction](cw20-convert-and-market-order-example.md) | Convert Cw20 Example |
54 changes: 54 additions & 0 deletions .gitbook/contracts/cw20-convert-and-market-order-example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Convert Cw20 to Bank and Place Market Order In One Transaction Example

This example helps you create messages to convert CW20 tokens to bank tokens on the Injective blockchain. This is particularly useful when you have CW20 tokens and need to convert them to their bank equivalents to perform operations like placing market orders. Note that this flow only works for cw20 tokens and their corresponding [factory tokens](../readme/application-concepts.md).

This guide will walk you through:

- Obtaining the user's CW20 token balance.
- Creating a message to convert CW20 tokens to bank tokens using ConvertCw20ToBankService
- Executing a market order using the converted bank balance and existing bank balance

## Get User's CW20 Balance

[Detailed here](../querying/querying-api/querying-indexer-explorer.md#fetch-cw20-balances)

- Find the cw20 address and balance from the result set that you want to convert to a bank factory token

## Create CW20 to Bank Conversion Message

- create the `convertMsg` using the steps detailed [here](../readme/application-concepts#example-on-how-to-convert-cw20-to-a-factory-denom) in order to convert your cw20 token to a bank factory token. No need to submit the tsx yet.

## Create a `MsgCreateSpotMarketOrder` message

- Create the `msg` using the steps detailed in [MsgCreateSpotMarketOrder](../core-modules/exchange.md#msgcreatespotmarketorder). No need to submit the tsx yet.
- Note that the buy order you create will have access to your converted cw20 balance + existing bank balance. Example:

```ts
const order = {
price: 1,
quantity: 10,
}
```

- If you had 5 Cw20 tokens and 5 bank tokens at a price of $1 each, then the order above will go through because we will convert the cw20 to bank before the chain executes this market order. This will be more clear in the next step.

## Place a Market Order Using Converted CW20 Balance and your existing bank balance

Now that you have both messages formatted, you can convert your cw20 tokens to bank factory tokens and then place a market order using the combined balance, all in one transaction

```ts
import { MsgBroadcasterWithPk } from '@injectivelabs/sdk-ts'
import { Network } from '@injectivelabs/networks'

const privateKey = '0x...'
const injectiveAddress = 'inj1...'
Comment on lines +43 to +44
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve security practices in code examples

The example shows sensitive information directly in the code. Consider:

  1. Adding a comment about secure key management
  2. Mentioning environment variables for private keys

Update the example:

-const privateKey = '0x...'
-const injectiveAddress = 'inj1...'
+// IMPORTANT: Never hardcode private keys in your code
+// Use secure environment variables or key management solutions
+const privateKey = process.env.INJECTIVE_PRIVATE_KEY
+const injectiveAddress = process.env.INJECTIVE_ADDRESS
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const privateKey = '0x...'
const injectiveAddress = 'inj1...'
// IMPORTANT: Never hardcode private keys in your code
// Use secure environment variables or key management solutions
const privateKey = process.env.INJECTIVE_PRIVATE_KEY
const injectiveAddress = process.env.INJECTIVE_ADDRESS


const txHash = await new MsgBroadcasterWithPk({
privateKey,
network: Network.MainnetSentry,
}).broadcast({
msgs: [convertMsg, msg], // the convert to bank message executes first, Then, you will have that additional balance to complete your market order in the following msg
})

Comment on lines +46 to +52
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling and gas estimation details

The broadcast example should include error handling and information about gas estimation.

Consider updating the example:

 const txHash = await new MsgBroadcasterWithPk({
   privateKey,
   network: Network.MainnetSentry,
-}).broadcast({
-  msgs: [convertMsg, msg],
-})
+}).broadcast({
+  msgs: [convertMsg, msg],
+  // Add comment explaining gas estimation strategy
+  gasMultiplier: 1.2 // Adjust gas limit by 20% to ensure transaction success
+}).catch(error => {
+  console.error('Transaction failed:', error);
+  // Handle specific error cases (insufficient funds, slippage, etc.)
+  throw error;
+});

Committable suggestion skipped: line range outside the PR's diff.

console.log(txHash)
```
2 changes: 1 addition & 1 deletion .gitbook/core-modules/auction.md
Original file line number Diff line number Diff line change
@@ -68,7 +68,7 @@ How to use funds that are currently associated with your Injective Address in ba
- If you have existing non-default subaccounts, you'll want to do a[ MsgDeposit ](exchange.md#msgdeposit)to one of your existing non-default subaccountIds and use that subaccountId as the `srcSubaccountId` below.
- If you don't have existing non-default subaccounts, you can do a [MsgDeposit](exchange.md#msgdeposit) to a new default subaccountId, which would be done via importing `getSubaccountId` from `sdk-ts` and setting the `subaccountId` field in [MsgDeposit](exchange.md#msgdeposit) to `getSubaccountId(injectiveAddress, 1)`.

For more info, check out the [burn auction pool docs](https://docs.injective.network/develop/tech-concepts/auction_pool/).
For more info, check out the [burn auction pool docs](https://docs.injective.network/developers/modules/injective/auction).
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Documentation links need to be updated consistently

There's an inconsistency in the documentation links. While the burn auction pool link has been updated to the new docs.injective.network domain, there's still an old reference to hub.injective.network in the same file:

  • Line 7: [auction](https://hub.injective.network/auction/) should be updated to use the new documentation domain pattern
🔗 Analysis chain

Verify the updated documentation link

The link to the burn auction pool documentation has been updated. While the change itself is straightforward, let's verify the link's validity.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify if the new documentation link is accessible
# and check for any other references to the old link pattern

# Check for any other instances of the old documentation pattern that might need updating
rg -i "hub\.injective\.network/auction"

# Note: The actual URL verification would typically be done via curl,
# but since this is an internal documentation site, we'll focus on consistency

Length of output: 41


Script:

#!/bin/bash
# Let's search for any references to documentation links about auction
rg -i "docs.*auction|hub.*auction" .gitbook/

# Also check for any other potential documentation links in the auction.md file
cat .gitbook/core-modules/auction.md

Length of output: 4786


```ts
import {
22 changes: 14 additions & 8 deletions .gitbook/readme/application-concepts.md
Original file line number Diff line number Diff line change
@@ -11,7 +11,10 @@ Combined with the `CW20AdapterContract` which acts as a creator, we allow CW20 a
Example on how to redeem a factory denom to CW20:

```ts
import { MsgExecuteContractCompat, ExecArgCW20AdapterRedeemAndTransfer } from '@injectivelabs/sdk-ts'
import {
MsgExecuteContractCompat,
ExecArgCW20AdapterRedeemAndTransfer,
} from '@injectivelabs/sdk-ts'

const CW20_ADAPTER_CONTRACT = 'inj...'
const contractCw20Address = 'inj...'
@@ -22,20 +25,23 @@ const message = MsgExecuteContractCompat.fromJSON({
contractAddress: CW20_ADAPTER_CONTRACT,
funds: {
denom: `factory/${CW20_ADAPTER_CONTRACT}/${contractCw20Address}`,
amount: actualAmount.toFixed()
amount: actualAmount.toFixed(),
},
execArgs: ExecArgCW20AdapterRedeemAndTransfer.fromJSON({
recipient: injectiveAddress
})
recipient: injectiveAddress,
}),
})

// Then pack the message in a transaction, sign it and broadcast to the chain
```

Example on how to convert CW20 to a factory denom:
### Example on how to convert CW20 to a factory denom:

```ts
import { MsgExecuteContractCompat, ExecArgCW20Send } from '@injectivelabs/sdk-ts'
import {
ExecArgCW20Send,
MsgExecuteContractCompat,
} from '@injectivelabs/sdk-ts'

const CW20_ADAPTER_CONTRACT = 'inj...'
const contractCw20Address = 'inj...'
@@ -47,8 +53,8 @@ const message = MsgExecuteContractCompat.fromJSON({
sender: injectiveAddress,
execArgs: ExecArgCW20Send.fromJSON({
amount,
contractAddress: CW20_ADAPTER_CONTRACT
})
contractAddress: CW20_ADAPTER_CONTRACT,
}),
})

// Then pack the message in a transaction, sign it and broadcast to the chain
2 changes: 1 addition & 1 deletion .gitbook/transactions/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Transactions

_Pre-requisite reading:_ [Cosmos SDK Transactions](https://docs.cosmos.network/main/core/transactions.html)
_Pre-requisite reading:_ [Cosmos SDK Transactions](https://docs.cosmos.network/main/learn/advanced/transactions)

State changes on Injective can be done through transactions. Users create transactions, sign them and broadcast them to Injective.