Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
intergalacticspacehighway committed Nov 1, 2023
1 parent 41036c8 commit 9f57119
Showing 1 changed file with 132 additions and 123 deletions.
255 changes: 132 additions & 123 deletions packages/app/hooks/creator-token/use-creator-token-buy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,151 +65,160 @@ export const useCreatorTokenBuy = (params: {
if (walletAddress && profileData?.data?.profile.creator_token) {
const switchChainSuccess = await switchChain.trigger();

const { maxFeePerGas, maxPriorityFeePerGas } =
const { maxFeePerGas: estimatedMaxFeePerGas, maxPriorityFeePerGas } =
await publicClient.estimateFeesPerGas({
type: "eip1559",
});

console.log("gas price buy", {
maxFeePerGas,
maxPriorityFeePerGas,
});

let transactionHash: `0x${string}` | undefined;
let requestPayload: any;

if (switchChainSuccess && maxFeePerGas) {
if (params.paymentMethod === "ETH") {
if (tokenAmount === 1) {
const { request } = await publicClient.simulateContract({
// @ts-ignore
address: creatorTokenSwapRouterAddress,
account: walletAddress,
abi: creatorTokenSwapRouterAbi,
functionName: "buyWithEth",
value: ethPriceToBuyNext.data?.value,
args: [
profileData.data.profile.creator_token.address,
priceToBuyNext.data?.totalPrice + 500000n,
],
chain: baseChain,
});
requestPayload = request;
console.log("buy with eth request payload ", request);
} else {
const { request } = await publicClient.simulateContract({
// @ts-ignore
address: creatorTokenSwapRouterAddress,
account: walletAddress,
abi: creatorTokenSwapRouterAbi,
functionName: "bulkBuyWithEth",
value: ethPriceToBuyNext.data?.value,
args: [
profileData.data.profile.creator_token.address,
tokenAmount,
priceToBuyNext.data?.totalPrice + 500000n,
],
chain: baseChain,
});
requestPayload = request;
console.log("buy with eth bulk request payload ", request);
}
} else {
// @ts-ignore
const approveSuccess = await approveToken.trigger({
creatorTokenContract:
profileData?.data?.profile.creator_token.address,
// add 10 cents more to cover for weird fluctuation
// TODO: remove if not needed after more testing
maxPrice: priceToBuyNext.data?.totalPrice + 100000n,
});
if (approveSuccess) {
const latestBlockBaseFeePerGas = (await publicClient.getBlock())
.baseFeePerGas;

if (maxPriorityFeePerGas) {
const maxFeePerGas = latestBlockBaseFeePerGas
? latestBlockBaseFeePerGas * 2n + maxPriorityFeePerGas
: estimatedMaxFeePerGas;

console.log("gas price buy", {
maxFeePerGas,
maxPriorityFeePerGas,
});

let transactionHash: `0x${string}` | undefined;
let requestPayload: any;

if (switchChainSuccess && maxFeePerGas) {
if (params.paymentMethod === "ETH") {
if (tokenAmount === 1) {
const { request } = await publicClient.simulateContract({
address: profileData?.data?.profile.creator_token.address,
// @ts-ignore
address: creatorTokenSwapRouterAddress,
account: walletAddress,
abi: creatorTokenAbi,
functionName: "buy",
args: [priceToBuyNext.data?.totalPrice],
abi: creatorTokenSwapRouterAbi,
functionName: "buyWithEth",
value: ethPriceToBuyNext.data?.value,
args: [
profileData.data.profile.creator_token.address,
priceToBuyNext.data?.totalPrice + 500000n,
],
chain: baseChain,
});
requestPayload = request;
console.log("buy with usdc request payload ", request);
console.log("buy with eth request payload ", request);
} else {
const { request } = await publicClient.simulateContract({
address: profileData?.data?.profile.creator_token.address,
// @ts-ignore
address: creatorTokenSwapRouterAddress,
account: walletAddress,
abi: creatorTokenAbi,
functionName: "bulkBuy",
args: [tokenAmount, priceToBuyNext.data?.totalPrice],
abi: creatorTokenSwapRouterAbi,
functionName: "bulkBuyWithEth",
value: ethPriceToBuyNext.data?.value,
args: [
profileData.data.profile.creator_token.address,
tokenAmount,
priceToBuyNext.data?.totalPrice + 500000n,
],
chain: baseChain,
});
requestPayload = request;
console.log("buy bulk with usdc request payload ", request);
console.log("buy with eth bulk request payload ", request);
}
} else {
// @ts-ignore
const approveSuccess = await approveToken.trigger({
creatorTokenContract:
profileData?.data?.profile.creator_token.address,
// add 10 cents more to cover for weird fluctuation
// TODO: remove if not needed after more testing
maxPrice: priceToBuyNext.data?.totalPrice + 100000n,
});
if (approveSuccess) {
if (tokenAmount === 1) {
const { request } = await publicClient.simulateContract({
address: profileData?.data?.profile.creator_token.address,
account: walletAddress,
abi: creatorTokenAbi,
functionName: "buy",
args: [priceToBuyNext.data?.totalPrice],
chain: baseChain,
});
requestPayload = request;
console.log("buy with usdc request payload ", request);
} else {
const { request } = await publicClient.simulateContract({
address: profileData?.data?.profile.creator_token.address,
account: walletAddress,
abi: creatorTokenAbi,
functionName: "bulkBuy",
args: [tokenAmount, priceToBuyNext.data?.totalPrice],
chain: baseChain,
});
requestPayload = request;
console.log("buy bulk with usdc request payload ", request);
}
}
}
}

transactionHash = await walletClient?.writeContract?.({
...requestPayload,
type: "eip1559",
maxFeePerGas,
maxPriorityFeePerGas,
});
console.log("buy transaction hash ", transactionHash);

if (transactionHash) {
const transaction = await publicClient.waitForTransactionReceipt({
hash: transactionHash,
pollingInterval: 2000,
transactionHash = await walletClient?.writeContract?.({
...requestPayload,
type: "eip1559",
maxFeePerGas,
maxPriorityFeePerGas,
});
console.log("buy transaction hash ", transactionHash);

if (transaction.status === "success") {
mutate(
getTotalCollectedKey(
profileData?.data?.profile.creator_token.address
)
);
mutate(
getPriceToBuyNextKey({
address: profileData?.data?.profile.creator_token.address,
tokenAmount: 1,
})
);

mutate(
getContractBalanceOfTokenKey({
ownerAddress: walletAddress,
contractAddress:
profileData?.data?.profile.creator_token.address,
})
);

await axios({
url: "/v1/creator-token/poll-buy",
method: "POST",
data: {
creator_token_id: profileData.data.profile.creator_token.id,
quantity: tokenAmount,
tx_hash: transactionHash,
},
if (transactionHash) {
const transaction = await publicClient.waitForTransactionReceipt({
hash: transactionHash,
pollingInterval: 2000,
});
mutate(
(key: any) => {
const channelId = profileData.data?.profile.channels[0]?.id;
if (
typeof key === "string" &&
typeof channelId === "number" &&
(key.startsWith(getChannelByIdCacheKey(channelId)) ||
key.startsWith(getChannelMessageKey(channelId)))
) {
return true;
}
},
undefined,
{ revalidate: true }
);
return true;

if (transaction.status === "success") {
mutate(
getTotalCollectedKey(
profileData?.data?.profile.creator_token.address
)
);
mutate(
getPriceToBuyNextKey({
address: profileData?.data?.profile.creator_token.address,
tokenAmount: 1,
})
);

mutate(
getContractBalanceOfTokenKey({
ownerAddress: walletAddress,
contractAddress:
profileData?.data?.profile.creator_token.address,
})
);

await axios({
url: "/v1/creator-token/poll-buy",
method: "POST",
data: {
creator_token_id: profileData.data.profile.creator_token.id,
quantity: tokenAmount,
tx_hash: transactionHash,
},
});
mutate(
(key: any) => {
const channelId = profileData.data?.profile.channels[0]?.id;
if (
typeof key === "string" &&
typeof channelId === "number" &&
(key.startsWith(getChannelByIdCacheKey(channelId)) ||
key.startsWith(getChannelMessageKey(channelId)))
) {
return true;
}
},
undefined,
{ revalidate: true }
);
return true;
}
}
}
}
Expand Down

0 comments on commit 9f57119

Please sign in to comment.