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

Rewards #417

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Rewards #417

wants to merge 5 commits into from

Conversation

jrwbabylonlab
Copy link
Collaborator

No description provided.

const tmClient = await Tendermint34Client.connect(
"https://rpc.devnet.babylonlabs.io",
);
const queryClient = QueryClient.withExtensions(tmClient);
Copy link
Collaborator Author

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

Copy link
Contributor

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?

Copy link
Contributor

@totraev totraev Nov 30, 2024

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

Comment on lines +26 to +46
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]);
Copy link
Contributor

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;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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 }
}

Copy link
Contributor

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();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const { signingStargateClient, bech32Address } = useCosmosWallet();
const { simulate } = useSigningStargateClient();

Comment on lines +22 to +43
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],
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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],
);

Comment on lines +50 to +79
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],
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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],
);

Copy link
Contributor

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);
Copy link
Contributor

@totraev totraev Nov 30, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants