-
Notifications
You must be signed in to change notification settings - Fork 11
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
Rewards #417
base: main
Are you sure you want to change the base?
Rewards #417
Conversation
const tmClient = await Tendermint34Client.connect( | ||
"https://rpc.devnet.babylonlabs.io", | ||
); | ||
const queryClient = QueryClient.withExtensions(tmClient); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@totraev once your PR is merged which should hopefully contain the latest version of stargate, then i can derive the queryClient directly from the signingStargateClient which we already have above.
The current version of tomo is using an older version of the stargate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jrwbabylonlab does it mean that this PR is in Draft/WIP state atm?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jrwbabylonlab I don't think that instances of QueryClient and SigningStargetClient should be inside CosmosWalletProvider. Instead CosmosWalletProvider just provides factory methods to create such instances somewhere else in the code. CosmosWalletProvider is just a thin adapter for Tomo/Native wallet's network providers to avoid tight coupling between DApp and current wallet provider
useEffect(() => { | ||
const fetchRewards = async () => { | ||
const result = await getRewards(); | ||
setRewards(result); | ||
}; | ||
const fetchBTCBalance = async () => { | ||
const balance = await getBTCBalance(); | ||
setBTCBalance(balance); | ||
}; | ||
const fetchCosmosBalance = async () => { | ||
const balance = await signingStargateClient?.getBalance( | ||
bech32Address, | ||
"ubbn", | ||
); | ||
const bbnAmount = Number(balance?.amount ?? 0); | ||
setCosmosBalance(bbnAmount); | ||
}; | ||
fetchRewards(); | ||
fetchBTCBalance(); | ||
fetchCosmosBalance(); | ||
}, [getRewards, getBTCBalance, signingStargateClient, bech32Address]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason why you do this manually instead of using react-query
? @jrwbabylonlab
amount: { denom: string; amount: string }[]; | ||
gas: string; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export const useSigningStargateClient = () => { | |
const { signingStargateClient, bech32Address } = useCosmosWallet(); | |
const simulate = useCallback( | |
<T>(msg: { typeUrl: string; value: T }, value: string): Promise<BbnGasFee> => { | |
if (!signingStargateClient || !bech32Address) { | |
throw new Error("Wallet not connected"); | |
} | |
// estimate gas | |
return signingStargateClient.simulate( | |
bech32Address, | |
[msg], | |
value, | |
); | |
}, | |
[signingStargateClient, bech32Address], | |
); | |
return { simulate } | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jrwbabylonlab initialisation of SigningStargetClient can be implemented here. Pass rpc url as param of this hook
* interacting with Babylon RPC nodes | ||
*/ | ||
export const useBbnTransaction = () => { | ||
const { signingStargateClient, bech32Address } = useCosmosWallet(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const { signingStargateClient, bech32Address } = useCosmosWallet(); | |
const { simulate } = useSigningStargateClient(); |
const estimateBbnGasFee = useCallback( | ||
async <T>(msg: { typeUrl: string; value: T }): Promise<BbnGasFee> => { | ||
if (!signingStargateClient || !bech32Address) { | ||
throw new Error("Wallet not connected"); | ||
} | ||
|
||
// estimate gas | ||
const gasEstimate = await signingStargateClient.simulate( | ||
bech32Address, | ||
[msg], | ||
`estimate transaction fee for ${msg.typeUrl}`, | ||
); | ||
// TODO: The gas calculation need to be improved | ||
// https://github.com/babylonlabs-io/simple-staking/issues/320 | ||
const gasWanted = Math.ceil(gasEstimate * 1.5); | ||
return { | ||
amount: [{ denom: "ubbn", amount: (gasWanted * 0.01).toFixed(0) }], | ||
gas: gasWanted.toString(), | ||
}; | ||
}, | ||
[signingStargateClient, bech32Address], | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const estimateBbnGasFee = useCallback( | |
async <T>(msg: { typeUrl: string; value: T }): Promise<BbnGasFee> => { | |
if (!signingStargateClient || !bech32Address) { | |
throw new Error("Wallet not connected"); | |
} | |
// estimate gas | |
const gasEstimate = await signingStargateClient.simulate( | |
bech32Address, | |
[msg], | |
`estimate transaction fee for ${msg.typeUrl}`, | |
); | |
// TODO: The gas calculation need to be improved | |
// https://github.com/babylonlabs-io/simple-staking/issues/320 | |
const gasWanted = Math.ceil(gasEstimate * 1.5); | |
return { | |
amount: [{ denom: "ubbn", amount: (gasWanted * 0.01).toFixed(0) }], | |
gas: gasWanted.toString(), | |
}; | |
}, | |
[signingStargateClient, bech32Address], | |
); | |
const estimateBbnGasFee = useCallback( | |
async <T>(msg: { typeUrl: string; value: T }): Promise<BbnGasFee> => { | |
// estimate gas | |
const gasEstimate = await simulate( | |
msg, | |
`estimate transaction fee for ${msg.typeUrl}`, | |
); | |
const gasWanted = Math.ceil(gasEstimate * 1.5); | |
return { | |
amount: [{ denom: "ubbn", amount: (gasWanted * 0.01).toFixed(0) }], | |
gas: gasWanted.toString(), | |
}; | |
}, | |
[simulate], | |
); |
const sendBbnTx = useCallback( | ||
async <T extends object>(msg: { | ||
typeUrl: string; | ||
value: T; | ||
}): Promise<{ txHash: string; gasUsed: string }> => { | ||
if (!signingStargateClient || !bech32Address) { | ||
throw new Error("Wallet not connected"); | ||
} | ||
|
||
// estimate gas | ||
const fee = await estimateBbnGasFee(msg); | ||
|
||
// sign it | ||
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, estimateBbnGasFee], | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const sendBbnTx = useCallback( | |
async <T extends object>(msg: { | |
typeUrl: string; | |
value: T; | |
}): Promise<{ txHash: string; gasUsed: string }> => { | |
if (!signingStargateClient || !bech32Address) { | |
throw new Error("Wallet not connected"); | |
} | |
// estimate gas | |
const fee = await estimateBbnGasFee(msg); | |
// sign it | |
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, estimateBbnGasFee], | |
); | |
const sendBbnTx = useCallback( | |
async <T extends object>(msg: { | |
typeUrl: string; | |
value: T; | |
}): Promise<{ txHash: string; gasUsed: string }> => { | |
// estimate gas | |
const fee = await estimateBbnGasFee(msg); | |
// sign it | |
const res = await simulate(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(), | |
}; | |
}, | |
[simulate, estimateBbnGasFee], | |
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too many duplicated code here
const tmClient = await Tendermint34Client.connect( | ||
"https://rpc.devnet.babylonlabs.io", | ||
); | ||
const queryClient = QueryClient.withExtensions(tmClient); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jrwbabylonlab I don't think that instances of QueryClient and SigningStargetClient should be inside CosmosWalletProvider. Instead CosmosWalletProvider just provides factory methods to create such instances somewhere else in the code. CosmosWalletProvider is just a thin adapter for Tomo/Native wallet's network providers to avoid tight coupling between DApp and current wallet provider
No description provided.