-
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
30ed0cc
commit 7501af3
Showing
4 changed files
with
160 additions
and
6 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
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,52 @@ | ||
import { useCallback } from "react"; | ||
|
||
import { useCosmosWallet } from "@/app/context/wallet/CosmosWalletProvider"; | ||
|
||
export const useRewardsService = () => { | ||
const { | ||
connected: cosmosConnected, | ||
bech32Address, | ||
signingStargateClient, | ||
distributionQueryClient, | ||
} = useCosmosWallet(); | ||
|
||
const getRewards = useCallback(async (): Promise<number> => { | ||
if (!cosmosConnected || !bech32Address || !signingStargateClient) { | ||
return 0; | ||
} | ||
// get public key | ||
const account = await signingStargateClient.getAccount(bech32Address); | ||
const publicKeyHex = Buffer.from(account?.pubkey?.value ?? "").toString( | ||
"hex", | ||
); | ||
console.log("publicKey", publicKeyHex); | ||
|
||
const result = | ||
await distributionQueryClient?.distribution.delegationTotalRewards( | ||
bech32Address, | ||
); | ||
if (!result) { | ||
throw new Error("Unable to fetch rewards"); | ||
} | ||
console.log("result", result); | ||
// Sum up all the rewards into a single number | ||
const total = result.total.reduce((sum, coin) => { | ||
return sum + Number(coin.amount); | ||
}, 0); | ||
return total; | ||
}, [ | ||
cosmosConnected, | ||
bech32Address, | ||
signingStargateClient, | ||
distributionQueryClient, | ||
]); | ||
|
||
const claimRewards = useCallback(async () => { | ||
throw new Error("Not implemented"); | ||
}, []); | ||
|
||
return { | ||
getRewards, | ||
claimRewards, | ||
}; | ||
}; |
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,19 @@ | ||
/** | ||
* Converts BBN to uBBN (micro BBN). | ||
* should be used internally in the app | ||
* @param bbn The amount in BBN. | ||
* @returns The equivalent amount in uBBN. | ||
*/ | ||
export function bbnToUbbn(bbn: number): number { | ||
return Math.round(bbn * 1e6); | ||
} | ||
|
||
/** | ||
* Converts uBBN (micro BBN) to BBN. | ||
* should be used only in the UI | ||
* @param ubbn The amount in uBBN. | ||
* @returns The equivalent amount in BBN. | ||
*/ | ||
export function ubbnToBbn(ubbn: number): number { | ||
return ubbn / 1e6; | ||
} |