-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25ae42a
commit 42ffac9
Showing
10 changed files
with
175 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const BBN_RPC_URL = "https://rpc-dapp.devnet.babylonlabs.io"; | ||
export const BBN_LCD_URL = "https://lcd-dapp.devnet.babylonlabs.io"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { incentivequery } from "@babylonlabs-io/babylon-proto-ts"; | ||
import { | ||
QueryClient, | ||
createProtobufRpcClient, | ||
setupBankExtension, | ||
} from "@cosmjs/stargate"; | ||
import { Tendermint34Client } from "@cosmjs/tendermint-rpc"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
|
||
import { BBN_RPC_URL } from "@/app/common/rpc"; | ||
import { useCosmosWallet } from "@/app/context/wallet/CosmosWalletProvider"; | ||
|
||
/** | ||
* Query service for Babylon which contains all the queries for | ||
* interacting with Babylon RPC nodes | ||
*/ | ||
export const useBbnQueryClient = () => { | ||
const { bech32Address } = useCosmosWallet(); | ||
const [queryClient, setQueryClient] = useState<QueryClient>(); | ||
|
||
useEffect(() => { | ||
const initQueryClient = async () => { | ||
const tmClient = await Tendermint34Client.connect(BBN_RPC_URL); | ||
const queryClient = QueryClient.withExtensions(tmClient); | ||
setQueryClient(queryClient); | ||
}; | ||
|
||
initQueryClient(); | ||
}, []); | ||
|
||
/** | ||
* Gets the rewards from the user's account. | ||
* @returns {Promise<Object>} - The rewards from the user's account. | ||
*/ | ||
const getRewards = useCallback(async (): Promise< | ||
incentivequery.QueryRewardGaugesResponse | undefined | ||
> => { | ||
if (!queryClient || !bech32Address) { | ||
return undefined; | ||
} | ||
const { incentive } = setupIncentiveExtension(queryClient); | ||
|
||
const req: incentivequery.QueryRewardGaugesRequest = | ||
incentivequery.QueryRewardGaugesRequest.fromPartial({ | ||
address: bech32Address, | ||
}); | ||
|
||
return incentive.RewardGauges(req); | ||
}, [queryClient, bech32Address]); | ||
|
||
const getBalance = useCallback(async (): Promise<number> => { | ||
if (!queryClient || !bech32Address) { | ||
return 0; | ||
} | ||
|
||
const { bank } = setupBankExtension(queryClient); | ||
const balance = await bank.balance(bech32Address, "ubbn"); | ||
return Number(balance?.amount ?? 0); | ||
}, [queryClient, bech32Address]); | ||
|
||
return { | ||
getRewards, | ||
getBalance, | ||
}; | ||
}; | ||
|
||
// Extend the QueryClient with the Incentive module | ||
const setupIncentiveExtension = ( | ||
base: QueryClient, | ||
): { | ||
incentive: incentivequery.QueryClientImpl; | ||
} => { | ||
const rpc = createProtobufRpcClient(base); | ||
const incentiveQueryClient = new incentivequery.QueryClientImpl(rpc); | ||
return { incentive: incentiveQueryClient }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { StdFee } from "@cosmjs/stargate"; | ||
import { useCallback } from "react"; | ||
|
||
import { useCosmosWallet } from "@/app/context/wallet/CosmosWalletProvider"; | ||
|
||
/** | ||
* Hook for signing and broadcasting transactions with the Cosmos wallet | ||
*/ | ||
export const useSigningStargateClient = () => { | ||
const { signingStargateClient, bech32Address } = useCosmosWallet(); | ||
|
||
/** | ||
* Simulates a transaction to estimate the gas fee | ||
* @param msg - The transaction message | ||
* @returns The gas fee | ||
*/ | ||
const simulate = useCallback( | ||
<T>(msg: { typeUrl: string; value: T }): Promise<number> => { | ||
if (!signingStargateClient || !bech32Address) { | ||
throw new Error("Wallet not connected"); | ||
} | ||
|
||
// estimate gas | ||
return signingStargateClient.simulate( | ||
bech32Address, | ||
[msg], | ||
`estimate transaction fee for ${msg.typeUrl}`, | ||
); | ||
}, | ||
[signingStargateClient, bech32Address], | ||
); | ||
|
||
/** | ||
* Signs and broadcasts a transaction | ||
* @param msg - The transaction message | ||
* @param fee - The gas fee | ||
* @returns The transaction hash and gas used | ||
*/ | ||
const signAndBroadcast = useCallback( | ||
async <T>( | ||
msg: { | ||
typeUrl: string; | ||
value: T; | ||
}, | ||
fee: StdFee, | ||
): Promise<{ | ||
txHash: string; | ||
gasUsed: string; | ||
}> => { | ||
if (!signingStargateClient || !bech32Address) { | ||
throw new Error("Wallet not connected"); | ||
} | ||
|
||
const res = await signingStargateClient.signAndBroadcast( | ||
bech32Address, | ||
[msg], | ||
fee, | ||
); | ||
|
||
if (res.code !== 0) { | ||
throw new Error( | ||
`Failed to send ${msg.typeUrl} transaction, code: ${res.code}, txHash: ${res.transactionHash}`, | ||
); | ||
} | ||
return { | ||
txHash: res.transactionHash, | ||
gasUsed: res.gasUsed.toString(), | ||
}; | ||
}, | ||
[signingStargateClient, bech32Address], | ||
); | ||
|
||
return { simulate, signAndBroadcast }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
// Temporary solution until we have a stable chain registry | ||
|
||
import { BBN_LCD_URL, BBN_RPC_URL } from "@/app/common/rpc"; | ||
|
||
// The values here shall match from https://rpc.devnet.babylonlabs.io/genesis? | ||
export const bbnDevnet = { | ||
chainId: "devnet-7", | ||
chainName: "Babylon Devnet 7", | ||
chainSymbolImageUrl: | ||
"https://raw.githubusercontent.com/chainapsis/keplr-chain-registry/main/images/bbn-dev/chain.png", | ||
rpc: "https://rpc-dapp.devnet.babylonlabs.io", | ||
rest: "https://lcd-dapp.devnet.babylonlabs.io", | ||
rpc: BBN_RPC_URL, | ||
rest: BBN_LCD_URL, | ||
nodeProvider: { | ||
name: "Babylonlabs", | ||
email: "[email protected]", | ||
|