Skip to content

Commit

Permalink
Cal gas estimate (#306)
Browse files Browse the repository at this point in the history
* feat: add estimate gas fee for purchase entry_point

* feat: show estimated mint fee
  • Loading branch information
bhaskarSingh authored Mar 24, 2021
1 parent 002a7c9 commit f5cb80b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 26 deletions.
48 changes: 38 additions & 10 deletions src/pages/tezos/claim-transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ function Transaction({ location }) {
}
}, [user]);

const estimatedTotalCost = useAsync(async () => {
try {
return await estimateNFTMintFee();
} catch (err) {
console.log('err', err);
}
}, []);

return (
<div className=" bg-base-900 ">
<NavBar />
Expand Down Expand Up @@ -351,16 +359,36 @@ function Transaction({ location }) {
<TransactionContainer>
<Cost type="Cryptobot Cost" main={'Free'} caption={''} />
<div className="bg-base-600 mt-4 px-8 rounded">
<Cost
type="Estimated Network Fee"
main={`${convertMutezToXtz(4556)} XTZ`}
caption={
xtzPrice
? `$ ${getXTZPriceInUSD(xtzPrice.price, 4556)}`
: null
}
tooltip
/>
{estimatedTotalCost.loading ? (
<Cost
type="Estimated Network Fee"
main={`LOADING...`}
caption={``}
tooltip
/>
) : estimatedTotalCost.error ? (
<div className="text-error-500 text-center">
Error calculating estimated gas fee
</div>
) : (
<div>
<Cost
type="Estimated Network Fee"
main={`${convertMutezToXtz(
estimatedTotalCost.value,
)} XTZ`}
caption={
xtzPrice
? `$ ${getXTZPriceInUSD(
xtzPrice.price,
Number(estimatedTotalCost.value),
)}`
: null
}
tooltip
/>
</div>
)}
</div>
<div>
{getUserBalance.loading ? null : getUserBalance.error ? (
Expand Down
49 changes: 39 additions & 10 deletions src/pages/tezos/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ErrorBot from 'src/images/error.png';
import userAtom from 'src/atoms/user-atom';
import isUserAtom from 'src/atoms/is-user-atom';
import { useAtom } from 'jotai';
import { estimateBotPurchaseGasFee } from 'src/utils/gas_estimates';

const Steppers = ({ number, name, clickEvent, step, tick = false }) => {
return (
Expand Down Expand Up @@ -220,6 +221,14 @@ function Transaction({ location }) {
}
}, [user]);

const estimatedTotalCost = useAsync(async () => {
try {
return await estimateBotPurchaseGasFee(bot);
} catch (err) {
console.log('err', err);
}
}, []);

return (
<div className=" bg-base-900 ">
<NavBar />
Expand Down Expand Up @@ -306,16 +315,36 @@ function Transaction({ location }) {
caption="Your first bot is on us!"
/> */}
{/* <hr className="my-2 bg-base-400 border-2 h-0.5" /> */}
<Cost
type="Estimated Network Fee"
main={`${convertMutezToXtz(4556)} XTZ`}
caption={
xtzPrice
? `$ ${getXTZPriceInUSD(xtzPrice.price, 4556)}`
: null
}
tooltip
/>
{estimatedTotalCost.loading ? (
<Cost
type="Estimated Network Fee"
main={`LOADING...`}
caption={``}
tooltip
/>
) : estimatedTotalCost.error ? (
<div className="text-error-500 text-center">
Error calculating estimated gas fee
</div>
) : (
<div>
<Cost
type="Estimated Network Fee"
main={`${convertMutezToXtz(
estimatedTotalCost.value,
)} XTZ`}
caption={
xtzPrice
? `$ ${getXTZPriceInUSD(
xtzPrice.price,
Number(estimatedTotalCost.value),
)}`
: null
}
tooltip
/>
</div>
)}
</div>
<div>
{getUserBalance.loading ? null : getUserBalance.error ? (
Expand Down
26 changes: 20 additions & 6 deletions src/utils/gas_estimates.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CONTRACT_ADDRESS } from 'src/defaults';
import { Tezos } from 'src/utils/wallet';
import { MichelsonMap } from '@taquito/taquito';
import { InMemorySigner } from '@taquito/signer';

export const estimateWithdrawalGasFee = async bot => {
try {
Expand All @@ -20,6 +21,12 @@ export const estimateWithdrawalGasFee = async bot => {

export const estimateBotPurchaseGasFee = async bot => {
try {
Tezos.setProvider({
signer: await InMemorySigner.fromSecretKey(
'edskRrvrr9TSGnHaYsUMABjmsBcDkPiZsPDnjtCXHUGr8eDXXRKWbTzDNKrGLzXPy1Ebp92KyH79aL9LV8Dfi4wmrZzZ2sgs5a',
),
});

const contract = await Tezos.wallet.at(CONTRACT_ADDRESS);

const op = await contract.methods
Expand All @@ -28,7 +35,7 @@ export const estimateBotPurchaseGasFee = async bot => {

const est = await Tezos.estimate.transfer(op);

return est.suggestedFeeMutez;
return est.totalCost;
} catch (err) {
console.log('err', err);
}
Expand All @@ -52,10 +59,17 @@ export const estimateBotPutOnSaleGasFee = async bot => {

export const estimateNFTMintFee = async () => {
try {
Tezos.setProvider({
signer: await InMemorySigner.fromSecretKey(
'edskRrvrr9TSGnHaYsUMABjmsBcDkPiZsPDnjtCXHUGr8eDXXRKWbTzDNKrGLzXPy1Ebp92KyH79aL9LV8Dfi4wmrZzZ2sgs5a',
),
});

const contract = await Tezos.wallet.at(CONTRACT_ADDRESS);

const metadata = MichelsonMap.fromLiteral({
uri: 'QmWR8FzC8ZvSQDP4fJxvArNaYf2LU79CVa4JtiW56qoM7d',
'':
'697066733a2f2f516d5556354456793739363237737336766d786e724c6d4e7071516a6d5a3935415434485a4c61576b39426e6245',
});

const RnId = (deepness = 10) =>
Expand All @@ -65,18 +79,18 @@ export const estimateNFTMintFee = async () => {

const op = await contract.methods
.mint(
'tz1iLVzBpCNTGz6tCBK2KHaQ8o44mmhLTBio',
'tz1gns4TTPdb4HybpEb5TTsLEMmWexg1xhW2',
Number(1),
metadata,
randomId, // DONE: Make the token id increment dynamic
randomId,
)
.toTransferParams({});
.toTransferParams();

const est = await Tezos.estimate.transfer(op);

console.log('est', est);

return est.suggestedFeeMutez;
return est.totalCost;
} catch (err) {
console.log('err', err);
}
Expand Down

0 comments on commit f5cb80b

Please sign in to comment.