Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions src/app/(root)/farms/[farmId]/_hooks/use-farm-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,37 +55,59 @@ export const useFarmOperations = ({
type: stakeCoinType,
})

if (!account) {
const { tx, account } = await farmsSdk.newAccount({
farm: farmId,
})

const { tx: stakeTx } = await farmsSdk.stakeUnchecked({
tx,
farm: farmId,
account,
depositCoin,
})

tx.transferObjects([account], tx.pure.address(address))
const freshAccounts = await farmsSdk.getAccounts(address)
const existingAccount = freshAccounts.find((acc) => acc.farm === farmId)

await executeTransaction(stakeTx)
} else {
if (existingAccount) {
const { tx } = await farmsSdk.stake({
farm: farmId,
account: account.objectId,
account: existingAccount.objectId,
depositCoin,
})

await executeTransaction(tx)

const amountInTokens = Number(amountBigInt) / Number(POW_9)
toast.success(`Staked ${formatNumberWithSuffix(amountInTokens)} ${tokenSymbol}`)
onSuccess?.()
return
}

toast("Creating new farm account...", { icon: "🔨", duration: 3000 })

const { tx, account: newAccount } = await farmsSdk.newAccount({
farm: farmId,
})

const { tx: stakeTx } = await farmsSdk.stakeUnchecked({
tx,
farm: farmId,
account: newAccount,
depositCoin,
})

stakeTx.transferObjects([newAccount], stakeTx.pure.address(address))

await executeTransaction(stakeTx)

const amountInTokens = Number(amountBigInt) / Number(POW_9)
toast.success(`Staked ${formatNumberWithSuffix(amountInTokens)} ${tokenSymbol}`)
toast.success(`Created account and staked ${formatNumberWithSuffix(amountInTokens)} ${tokenSymbol}`)
onSuccess?.()

} catch (error) {
console.error("Staking error:", error)
toast.error(`Failed to stake ${tokenSymbol}`)

if (error instanceof Error) {
if (error.message.includes("Rejected") || error.message.includes("User rejected")) {
toast.error("Transaction rejected by user")
} else if (error.message.includes("Insufficient")) {
toast.error("Insufficient balance")
} else {
toast.error(`Failed to stake: ${error.message}`)
}
} else {
toast.error(`Failed to stake ${tokenSymbol}`)
}
} finally {
setIsStaking(false)
}
Expand Down