Skip to content

Commit

Permalink
Merge pull request #247 from InjectiveLabs/docs/update-examples
Browse files Browse the repository at this point in the history
docs/update examples
  • Loading branch information
bangjelkoski authored Oct 13, 2023
2 parents 285e9a7 + 189e1a2 commit f17e8e3
Show file tree
Hide file tree
Showing 26 changed files with 1,728 additions and 104 deletions.
1 change: 1 addition & 0 deletions .gitbook/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ _Note: Reading the Technical Concepts section after reading the overview below i
* [Core Modules](core-modules/) - In this section we are going to have a quick summary of the core modules on Injective and show examples of how to create some Messages (+ pack them into a transaction, sign them using a private key, and broadcast them on Injective) within these core modules.
* [Bridge](bridge/) - In this section, we are going to have a look at Injective's interoperability and explain how developers can utilize the Peggy bridge and the IBC bridge to bridge assets over to Injective.
* [Networks](readme/networks.md) - In this section, we will look at different (pre-defined) available Networks for developers to utilize while building dApps on top of Injective, allowing them to start building without the need to make their own infrastructure.
* [Calculations](calculations/) - In this section, we will look at different calculations formula converting values between UI human-readable and chain format.
2 changes: 2 additions & 0 deletions .gitbook/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
* [Core Modules](core-modules/README.md)
* [Auction](core-modules/auction.md)
* [AuthZ](core-modules/authz.md)
- [Feegrant](core-modules/feegrant.md)
* [Bank](core-modules/bank.md)
* [Distribution](core-modules/distribution.md)
* [Exchange](core-modules/exchange.md)
Expand All @@ -84,3 +85,4 @@
* [Smart Contract](building-dapps/smart-contract.md)
* [DEX](building-dapps/dex.md)
* [Bridge](building-dapps/bridge.md)
* [Calculations](calculations/README.md)
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions .gitbook/core-modules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Within this section, we are going to explore the core modules of the Injective c
| ------------------------------- | ------------------------------------------------ |
| [Auction](auction.md) | Use for the buy-back-and-burn on chain mechanism |
| [AuthZ](authz.md) | Used for granting account priveledges |
| [Feegrant](feegrant.md) | Used for granting fee allowance priveledges |
| [Bank](bank.md) | Used for managing users assets (funds) |
| [Exchange](exchange.md) | Used for the exchange primitives |
| [Distribution](distribution.md) | Used for on-chain distribution/minting |
Expand Down
75 changes: 75 additions & 0 deletions .gitbook/core-modules/feegrant.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Fee Grant

The `feegrant` module allows accounts (granters) to grant fee allowances to other accounts (grantees). This allows the grantee to use the granter's funds to pay for transaction fees.

## Messages

### MsgGrantAllowance

A fee allowance grant is created using the `MsgGrantAllowance` message. If there is already a grant for the (granter, grantee) pair, then the new grant will overwrite the previous one.

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


const privateKeyOfGranter = '0x...'

const date = new Date('2023-10-02T00:00:00Z')
const expiration = date.getTime() / 1000
const granter = 'inj...'
const grantee = 'inj...'
const allowance = {
spendLimit: [
{
denom: 'inj',
amount: '10000',
},
],
expiration
}

const msg = MsgGrantAllowance.fromJSON({
granter,
grantee,
allowance,
})

const txHash = await new MsgBroadcasterWithPk({
privateKey: privateKeyOfGranter,
network: Network.Testnet,
}).broadcast({
msgs: msg,
})

console.log(txHash)

```

### MsgRevokeAllowance
A grant can be removed using the MsgRevokeAllowance message. The grantee will no longer be able to use the granter's funds to pay for transaction fees.

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

const privateKey= "0x..."
const granteeAddress = 'inj...'
const granterAddress = 'inj...'

const params = {
grantee: granteeAddress,
granter: granterAddress,
}

const msg = MsgRevokeAllowance.fromJSON(params);

const txHash = await new MsgBroadcasterWithPk({
privateKey,
network: Network.Testnet,
}).broadcast({
msgs: msg,
})

console.log(txHash)
```
4 changes: 3 additions & 1 deletion .gitbook/core-modules/wasm.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ console.log(txHash);

### MsgExecuteContractCompact

There are some compatibility issue parsing the funds array in the previous example with EIP712, hence we introduced MsgExecuteContractCompact which converts the funds into a string
There are some compatibility issue parsing the funds array in the previous example with EIP712.
Since MsgExecuteContract can't be converted to EIP712 and then signed by ethereum wallets, we introduced MsgExecuteContractCompact which converts the funds into a string and therefore allows for EIP712 transformation.
Note that the MsgExecuteContract and MsgExecuteContractCompat underlying messages are the same. MsgExecuteContractCompat will just format for EIP712 compatibility.

An array of funds:

Expand Down
88 changes: 43 additions & 45 deletions .gitbook/transactions/ethereum.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,40 @@ First of, we need to prepare the transaction for signing. To use Ethereum native
Using our custom abstraction for the Messages which allows the developer to get EIP712 TypedData straight from the proto file of the particular message.

```ts
import {
import {
MsgSend,
ChainRestAuthApi,
ChainRestTendermintApi,
BaseAccount,
DEFAULT_STD_FEE,
getEip712TypedData,
} from '@injectivelabs/sdk-ts'
import { DEFAULT_STD_FEE, DEFAULT_BLOCK_TIMEOUT_HEIGHT } from '@injectivelabs/utils'
import {
DEFAULT_STD_FEE,
DEFAULT_BLOCK_TIMEOUT_HEIGHT,
} from '@injectivelabs/utils'
import { ChainId } from '@injectivelabs/ts-types'
import { Network, getNetworkEndpoints } from '@injectivelabs/networks'

const injectiveAddress = 'inj1'
const chainId = 'injective-1' /* ChainId.Mainnet */
const restEndpoint = 'https://lcd.injective.network' /* getNetworkEndpoints(Network.Mainnet).rest */
const restEndpoint =
'https://lcd.injective.network' /* getNetworkEndpoints(Network.Mainnet).rest */
const amount = {
amount: new BigNumberInBase(0.01).toWei().toFixed(),
denom: "inj",
};
denom: 'inj',
}

/** Account Details **/
const chainRestAuthApi = new ChainRestAuthApi(
restEndpoint,
)
const chainRestAuthApi = new ChainRestAuthApi(restEndpoint)
const accountDetailsResponse = await chainRestAuthApi.fetchAccount(
injectiveAddress,
)
const baseAccount = BaseAccount.fromRestApi(accountDetailsResponse)
const accountDetails = baseAccount.toAccountDetails()

/** Block Details */
const chainRestTendermintApi = new ChainRestTendermintApi(
restEndpoint,
)
const chainRestTendermintApi = new ChainRestTendermintApi(restEndpoint)
const latestBlock = await chainRestTendermintApi.fetchLatestBlock()
const latestHeight = latestBlock.header.height
const timeoutHeight = new BigNumberInBase(latestHeight).plus(
Expand All @@ -54,7 +54,7 @@ const msg = MsgSend.fromJSON({
amount,
srcInjectiveAddress: injectiveAddress,
dstInjectiveAddress: injectiveAddress,
});
})

/** EIP712 for signing on Ethereum wallets */
const eip712TypedData = getEip712TypedData({
Expand All @@ -77,14 +77,14 @@ Once we have prepared the EIP712 typed data, we proceed to signing.
/** Use your preferred approach to sign EIP712 TypedData, example with Metamask */
const signature = await window.ethereum.request({
method: 'eth_signTypedData_v4',
params: [ethereumAddress, JSON.stringify(eip712TypedData /* from previous step */)],
params: [
ethereumAddress,
JSON.stringify(eip712TypedData /* from previous step */),
],
})

/** Get Public Key of the signer */
const publicKeyHex = recoverTypedSignaturePubKey(
eip712TypedData,
signature,
)
const publicKeyHex = recoverTypedSignaturePubKey(eip712TypedData, signature)
const publicKeyBase64 = hexToBase64(publicKeyHex)
```

Expand All @@ -97,7 +97,11 @@ Once we have the signature ready, we need to broadcast the transaction to the In
```ts
import { ChainId } from '@injectivelabs/ts-types'
import { createTransaction, TxRestClient } from '@injectivelabs/sdk-ts'
import { SIGN_AMINO, Network, getNetworkEndpoints } from '@injectivelabs/networks'
import {
SIGN_AMINO,
Network,
getNetworkEndpoints,
} from '@injectivelabs/networks'

const { txRaw } = createTransaction({
message: msgs,
Expand All @@ -119,19 +123,20 @@ const txRawEip712 = createTxRawEIP712(txRaw, web3Extension)
txRawEip712.signatures = [signatureBuff /* From previous step */]

/** Broadcast the Transaction */
const restEndpoint = 'https://lcd.injective.network' /* getNetworkEndpoints(Network.Mainnet).rest */
const restEndpoint =
'https://lcd.injective.network' /* getNetworkEndpoints(Network.Mainnet).rest */
const txRestClient = new TxRestClient(restEndpoint)

const txHash = await txRestClient.broadcast(txRawEip712)

/**
* Once we get the txHash, because we use the Sync mode we
* are not sure that the transaction is included in the block,
* it can happen that it's still in the mempool so we need to query
/**
* Once we get the txHash, because we use the Sync mode we
* are not sure that the transaction is included in the block,
* it can happen that it's still in the mempool so we need to query
* the chain to see when the transaction will be included
*/

/** This will poll querying the transaction and await for it's inclusion in the block */
/** This will poll querying the transaction and await for it's inclusion in the block */
const response = await txRestClient.fetchTxPoll(txHash)
```

Expand All @@ -140,40 +145,40 @@ const response = await txRestClient.fetchTxPoll(txHash)
Let's have a look at the whole flow (using Metamask as a signing wallet)

```ts
import {
import {
MsgSend,
ChainRestAuthApi,
ChainRestTendermintApi,
BaseAccount,
DEFAULT_STD_FEE,
getEip712TypedData,
} from '@injectivelabs/sdk-ts'
import { DEFAULT_STD_FEE, DEFAULT_BLOCK_TIMEOUT_HEIGHT } from '@injectivelabs/utils'
import {
DEFAULT_STD_FEE,
DEFAULT_BLOCK_TIMEOUT_HEIGHT,
} from '@injectivelabs/utils'
import { ChainId } from '@injectivelabs/ts-types'
import { Network, getNetworkEndpoints } from '@injectivelabs/networks'

const injectiveAddress = 'inj1'
const chainId = 'injective-1' /* ChainId.Mainnet */
const restEndpoint = 'https://lcd.injective.network' /* getNetworkEndpoints(Network.Mainnet).rest */
const restEndpoint =
'https://lcd.injective.network' /* getNetworkEndpoints(Network.Mainnet).rest */
const amount = {
amount: new BigNumberInBase(0.01).toWei().toFixed(),
denom: "inj",
};
denom: 'inj',
}

/** Account Details **/
const chainRestAuthApi = new ChainRestAuthApi(
restEndpoint,
)
const chainRestAuthApi = new ChainRestAuthApi(restEndpoint)
const accountDetailsResponse = await chainRestAuthApi.fetchAccount(
injectiveAddress,
)
const baseAccount = BaseAccount.fromRestApi(accountDetailsResponse)
const accountDetails = baseAccount.toAccountDetails()

/** Block Details */
const chainRestTendermintApi = new ChainRestTendermintApi(
restEndpoint,
)
const chainRestTendermintApi = new ChainRestTendermintApi(restEndpoint)
const latestBlock = await chainRestTendermintApi.fetchLatestBlock()
const latestHeight = latestBlock.header.height
const timeoutHeight = new BigNumberInBase(latestHeight).plus(
Expand All @@ -185,7 +190,7 @@ const msg = MsgSend.fromJSON({
amount,
srcInjectiveAddress: injectiveAddress,
dstInjectiveAddress: injectiveAddress,
});
})

/** EIP712 for signing on Ethereum wallets */
const eip712TypedData = getEip712TypedData({
Expand All @@ -206,10 +211,7 @@ const signature = await window.ethereum.request({
})

/** Get Public Key of the signer */
const publicKeyHex = recoverTypedSignaturePubKey(
eip712TypedData,
signature,
)
const publicKeyHex = recoverTypedSignaturePubKey(eip712TypedData, signature)
const publicKeyBase64 = hexToBase64(publicKeyHex)

const { txRaw } = createTransaction({
Expand Down Expand Up @@ -240,8 +242,4 @@ const response = await txRestClient.fetchTxPoll(txHash)

### Example with WalletStrategy (Prepare + Sign + Broadcast)

🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧

This part is currently under work in progress.

🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧 🚧
Example can be found [here](https://github.com/InjectiveLabs/injective-ts/blob/862e7c30d96120947b056abffbd01b4f378984a1/packages/wallet-ts/src/broadcaster/MsgBroadcaster.ts#L166-L248).
Loading

0 comments on commit f17e8e3

Please sign in to comment.