Skip to content

Commit

Permalink
fix wallet id undefined bug
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Feb 12, 2024
1 parent 2ecddc7 commit 686d811
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
19 changes: 15 additions & 4 deletions trade.renegade.fi/app/(desktop)/[base]/[quote]/deposit-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
useDisclosure
} from "@chakra-ui/react"
import { Token } from "@renegade-fi/renegade-js"
import { useState } from "react"
import { useAccount } from "wagmi"

const MAX_INT = BigInt("115792089237316195423570985008687907853269984665640564039457584007913129639935")
Expand Down Expand Up @@ -43,16 +44,25 @@ export default function DepositButton() {
const { address } = useAccount()
const { data: allowance } = useErc20Allowance({
address: Token.findAddressByTicker(baseTicker) as `0x${string}`,
args: [address ? address : "0x", env.NEXT_PUBLIC_DARKPOOL_CONTRACT as `0x${string}`]
, watch: true
args: [address ? address : "0x", env.NEXT_PUBLIC_DARKPOOL_CONTRACT as `0x${string}`],
watch: true
})
const needsApproval = allowance === BigInt(0)

const { config } = usePrepareErc20Approve({
address: Token.findAddressByTicker(baseTicker) as `0x${string}`,
args: [env.NEXT_PUBLIC_DARKPOOL_CONTRACT as `0x${string}`, MAX_INT]
args: [env.NEXT_PUBLIC_DARKPOOL_CONTRACT as `0x${string}`, MAX_INT],
onSettled: () => {
console.log("settled")
setIsLoading(false)
},
onSuccess: () => {
console.log("success")
setIsLoading(false)
}
})
const { write: approve, isLoading } = useErc20Approve(config)
const { write: approve } = useErc20Approve(config)
const [isLoading, setIsLoading] = useState(false)

const handleApprove = async () => {
if (!accountId || !approve) return
Expand All @@ -63,6 +73,7 @@ export default function DepositButton() {
if (shouldUse) {
buttonOnClick()
} else if (needsApproval) {
setIsLoading(true)
handleApprove()
} else {
onOpenStepper()
Expand Down
8 changes: 7 additions & 1 deletion trade.renegade.fi/app/api/fund/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ export async function GET(request: Request) {
const wethAmount = parseEther("1")
const usdcAmount = parseUnits("3000", 18)

// TODO: Parallelize
const transactionCount = await publicClient.getTransactionCount({
address: account.address,
})

// Fund with ETH
const hash = await walletClient.sendTransaction({
account,
to: recipient,
value: ethAmount,
nonce: transactionCount,
})
const transaction = await publicClient.waitForTransactionReceipt({
hash
Expand All @@ -64,6 +68,7 @@ export async function GET(request: Request) {
abi,
functionName: "transfer",
args: [recipient, wethAmount],
nonce: transactionCount + 1
})

const wethHash = await walletClient.writeContract(wethRequest)
Expand All @@ -79,6 +84,7 @@ export async function GET(request: Request) {
abi,
functionName: "transfer",
args: [recipient, usdcAmount],
nonce: transactionCount + 2
})

const usdcHash = await walletClient.writeContract(usdcRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export function DefaultStep() {
throw new Error("Invalid signature")
}
setAccount(accountId, new Keychain({ seed: data })).then(() => setSeed(data))
// setSeed(data)
setIsOnboarding(false)
onClose()
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useRenegade } from "@/contexts/Renegade/renegade-context"
import { Flex, ModalBody, ModalFooter, Spinner, Text } from "@chakra-ui/react"
import { useEffect } from "react"
import { useAccount } from "wagmi"
Expand All @@ -7,15 +6,17 @@ import { useStepper } from "../testnet-stepper"

export function LoadingStep() {
const { onNext } = useStepper()
const { taskType, taskState } = useRenegade()
const { address } = useAccount()


useEffect(() => {
// TODO: Use faucet contract events
setTimeout(() => {
onNext();
}, 2000);
}, [address, onNext, taskState, taskType])
const handleFund = async () => {
await fetch(`/api/fund?address=${address}`).then(() => {
onNext()
})
}
handleFund()
}, [address, onNext])

return (
<>
Expand Down
8 changes: 3 additions & 5 deletions trade.renegade.fi/contexts/Renegade/renegade-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,16 @@ function RenegadeProvider({ children }: React.PropsWithChildren) {
await renegade.task
.initializeAccount(accountId)
.then(([taskId, taskJob]) => {
if (taskId !== "DONE") {
fetch(`http://localhost:3001/api/fund?address=${address}`, {
method: 'GET',
})
}
setTask(taskId, TaskType.InitializeAccount)
return taskJob
})
.then(() => {
attemptedAutoSignin.current = accountId
setAccountId(accountId)
refreshAccount(accountId)
fetch(`/api/fund?address=${address}`, {
method: 'GET',
})
})
}

Expand Down

0 comments on commit 686d811

Please sign in to comment.