Skip to content

Commit

Permalink
feat: feat
Browse files Browse the repository at this point in the history
  • Loading branch information
Another-DevX committed Dec 3, 2023
1 parent f83eef6 commit f6942f2
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 74 deletions.
4 changes: 2 additions & 2 deletions kiwi/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
NEXT_PUBLIC_CONTRACT_ADDRESS="0x5a7027Ba1cC866c7966DaC2D59a49C6645F37762"
="0xF0B11c888CbC5F72BD25A935E9762397ed41eF67"
NEXT_PUBLIC_CONTRACT_ADDRESS="0xAa52a486B960330A222b2Ee3786d85A2C6c643D9"
NEXT_PUBLIC_ERC20="0x9Be0Fe71657D36843779289A1027EDccd5c74421"
3 changes: 2 additions & 1 deletion kiwi/src/components/AnimatedPrice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ function AnimatedPrice ({ price }: { price: number }) {
const count = useMotionValue(0)
const rounded = useTransform(count, latest => Math.round(latest))


useEffect(() => {
const controls = animate(count, price)
return controls.stop
}, [])
}, [price])
return (
<div className='text-2xl flex flex-row gap-1 text-green-500 font-bold'>
$<motion.div>{rounded}</motion.div>
Expand Down
106 changes: 56 additions & 50 deletions kiwi/src/components/FundLoan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from '@/components/ui/card'
import { useForm } from 'react-hook-form'
import { Input } from './ui/input'
import { useErc20, useFund } from '@/hooks'
import { useErc20, useErc20Approve, useFund } from '@/hooks'
import {
Dialog,
DialogContent,
Expand All @@ -28,24 +28,32 @@ import {
DialogTitle,
DialogTrigger
} from './ui/dialog'
import { lendingManagerAddress } from '@/constants'
import { CeloCopAddress, lendingManagerAddress } from '@/constants'
import { parseEther } from 'viem'

function FundLoan () {
const fundForm = useForm()
const form = useForm()
const loanForm = useForm()

const { writeAsync: approve } = useErc20()
const { writeAsync: approve } = useErc20Approve()

async function onSubmit (values) {
console.debug(lendingManagerAddress, values.value)
console.debug(CeloCopAddress)

await approve({
args: [lendingManagerAddress, values.value]
args: [lendingManagerAddress, parseEther(values.value)]
})
}

const { writeAsync: fund, write: isFundAvailable } = useFund()
const { writeAsync: fund, write: isFundAvailable } = useFund({
value: fundForm.watch('value')
})

function onFundSubmit (values) {}
const loanForm = useForm()
async function onFundSubmit () {
if (fund) await fund()
}

function onLoanSubmit (values) {
console.log(values)
Expand Down Expand Up @@ -112,13 +120,51 @@ function FundLoan () {
</FormItem>
)}
/>

</form>
</Form>
</CardContent>
</Card>
</TabsContent>
<TabsContent value='fondear'>
<Card>
<CardHeader>
<CardTitle>Fondear</CardTitle>
<CardDescription>
Fondea la piscina de liquidez y recibe un rendimiento por ello.
</CardDescription>
</CardHeader>
<CardContent className='space-y-2'>
<Form {...fundForm}>
<form
onSubmit={fundForm.handleSubmit(onFundSubmit)}
className='space-y-8'
>
<FormField
control={fundForm.control}
name='value'
rules={{
required: 'Este campo es requerido',
min: {
value: 0,
message: 'El monto debe ser mayor a 0'
}
}}
render={({ field }) => (
<FormItem>
<FormLabel>Valor</FormLabel>
<FormControl>
<Input type='number' placeholder='0' {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
{isFundAvailable ? (
<Button type='submit'>Solicitar prestamo</Button>
) : (
<Dialog>
<DialogTrigger className='cursor-pointer' asChild>
<Button type='button'>Solicitar prestamo</Button>
<Button type='button'>Fondear</Button>
</DialogTrigger>
<DialogContent className='sm:max-w-[425px]'>
<DialogHeader>
Expand Down Expand Up @@ -157,7 +203,7 @@ function FundLoan () {
</FormItem>
)}
/>
<Button type='submit'>Aprobar monto</Button>
<Button type='submit'>Fondear</Button>
</form>
</Form>
</DialogContent>
Expand All @@ -168,46 +214,6 @@ function FundLoan () {
</CardContent>
</Card>
</TabsContent>
<TabsContent value='fondear'>
<Card>
<CardHeader>
<CardTitle>Fondear</CardTitle>
<CardDescription>
Fondea la piscina de liquidez y recibe un rendimiento por ello.
</CardDescription>
</CardHeader>
<CardContent className='space-y-2'>
<Form {...fundForm}>
<form
onSubmit={fundForm.handleSubmit(onFundSubmit)}
className='space-y-8'
>
<FormField
control={fundForm.control}
name='value'
rules={{
required: 'Este campo es requerido',
min: {
value: 0,
message: 'El monto debe ser mayor a 0'
}
}}
render={({ field }) => (
<FormItem>
<FormLabel>Valor</FormLabel>
<FormControl>
<Input type='number' placeholder='0' {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button type='submit'>Fondear</Button>
</form>
</Form>
</CardContent>
</Card>
</TabsContent>
</Tabs>
)
}
Expand Down
22 changes: 17 additions & 5 deletions kiwi/src/components/TotalFunds.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
'use client'

import React from 'react'
import React, { useEffect } from 'react'
import { Card, CardContent, CardFooter, CardHeader, CardTitle } from './ui/card'
import { Button } from './ui/button'
import AnimatedPrice from './AnimatedPrice'
import { useFundValues } from '@/hooks'
import { Skeleton } from './ui/skeleton'
import { formatEther } from 'viem'

function TotalFunds () {
// const { data, isLoading } = useFundValues()
const isLoading = true
const { interest, initialValue, totalValue, isLoading } = useFundValues()
console.debug(interest, initialValue, totalValue, isLoading)
return (
<>
{isLoading ? (
Expand Down Expand Up @@ -60,8 +61,19 @@ function TotalFunds () {
</svg>
</CardHeader>
<CardContent>
<AnimatedPrice price={data | 0} />
<p className='text-xs text-muted-foreground'>+20.1% en total</p>
<AnimatedPrice
price={Number(
((initialValue as bigint) + (interest as bigint)) / BigInt(1e18)
)}
/>
<p className='text-xs text-muted-foreground'>
+
{BigInt(totalValue as bigint) == BigInt(0) ||
BigInt(interest as bigint) == BigInt(0)
? 0
: (Number(initialValue) / Number(totalValue)) * 100}
% en total
</p>
</CardContent>
<CardFooter>
<Button>Retirar fondos</Button>
Expand Down
93 changes: 93 additions & 0 deletions kiwi/src/constants/lendingManager.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
[
{
"inputs": [
{
"internalType": "address",
"name": "initialAdmin",
"type": "address"
},
{
"internalType": "address",
"name": "_cUSD",
"type": "address"
},
{
"internalType": "string",
"name": "goldUri",
"type": "string"
},
{
"internalType": "string",
"name": "silverUri",
"type": "string"
},
{
"internalType": "string",
"name": "bronzeUri",
"type": "string"
}
],
"stateMutability": "nonpayable",
Expand Down Expand Up @@ -571,6 +591,66 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "enum RewardAdmin.RewardType",
"name": "",
"type": "uint8"
}
],
"name": "rewards",
"outputs": [
{
"internalType": "string",
"name": "uri",
"type": "string"
},
{
"internalType": "uint256",
"name": "threshold",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "enum RewardAdmin.RewardType",
"name": "rewardType",
"type": "uint8"
},
{
"internalType": "uint256",
"name": "threshold",
"type": "uint256"
}
],
"name": "setThreshold",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "enum RewardAdmin.RewardType",
"name": "rewardType",
"type": "uint8"
},
{
"internalType": "string",
"name": "uri",
"type": "string"
}
],
"name": "setUri",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -603,6 +683,19 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "stakerReward",
"outputs": [
{
"internalType": "contract StakerReward",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand Down
2 changes: 1 addition & 1 deletion kiwi/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { useUserValidated } from './useUserValidated'
export { useFundValues } from './useFundValues'
export { useFund } from './useFund'
export { useErc20 } from './useErc20'
export { useErc20Approve } from './useErc20Approve'
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import {
import { CeloCopAddress, celoCopAbi } from '@/constants'
import { toast } from '@/components/ui/use-toast'

function useErc20 () {
function useErc20Approve () {
const CeloCop = useContractWrite({
address: CeloCopAddress,
abi: celoCopAbi,

functionName: 'approve',
onSuccess (tx: Address) {
onSuccess (tx: any) {
toast({
title: 'Monto pre-aprobado con exito',
description: `Tx: ${tx.transactionHash}`
description: `Tx: ${tx.hash}`
})
},
onError (error: Error) {
Expand All @@ -29,4 +30,4 @@ function useErc20 () {
return CeloCop
}

export { useErc20 }
export { useErc20Approve }
Loading

0 comments on commit f6942f2

Please sign in to comment.