Skip to content

Commit

Permalink
added test tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquinsoza committed Nov 15, 2024
1 parent 9c6724b commit ce6762d
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 26 deletions.
7 changes: 5 additions & 2 deletions apps/contracts/src/deploy_hodl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, nativeToScVal, xdr, Asset, Networks } from "@stellar/stellar-sdk";
import { Address, Asset, Networks, xdr } from "@stellar/stellar-sdk";
import { AddressBook } from "./utils/address_book.js";
import {
airdropAccount,
Expand Down Expand Up @@ -45,14 +45,17 @@ export async function deployContracts(addressBook: AddressBook) {
const xlmAddress = new Address(xlmContractId);
const xlmScVal = xlmAddress.toScVal();

const soroswapUSDC = new Address("CAAFIHB4I7WQMJMKC22CZVQNNX7EONWSOMT6SUXK6I3G3F6J4XFRWNDI");
const usdcScVal = soroswapUSDC.toScVal();

const emptyVecScVal = xdr.ScVal.scvVec([]);

console.log("Initializing DeFindex HODL Strategy");
await invokeContract(
"hodl_strategy",
addressBook,
"initialize",
[xlmScVal, emptyVecScVal],
[usdcScVal, emptyVecScVal],
loadedConfig.admin
);
}
Expand Down
8 changes: 5 additions & 3 deletions apps/dapp/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"use client";
import { Container } from '@chakra-ui/react'
import { useSorobanReact } from '@soroban-react/core'
import ManageVaults from '@/components/ManageVaults/ManageVaults';
import { TestTokens } from '@/components/TestTokens';
import { Container } from '@chakra-ui/react';
import { useSorobanReact } from '@soroban-react/core';
import {
Chart as ChartJS,
ArcElement,
Chart as ChartJS,
} from 'chart.js';

ChartJS.register(ArcElement);
Expand All @@ -15,6 +16,7 @@ export default function Home() {
return (
<Container mt={16} mx={0} px={0} minW={'100vw'}>
<Container centerContent textAlign={'center'} minW={'100vw'}>
{address && (<TestTokens />)}
<ManageVaults />
</Container>
</Container>
Expand Down
37 changes: 18 additions & 19 deletions apps/dapp/src/components/DeployVault/AddNewStrategyButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
'use client'
import React from 'react'
import { useEffect, useState } from 'react'
import {
DialogBackdrop,
DialogBody,
DialogContent,
DialogFooter,
DialogRoot,
DialogTrigger,
} from '@/components/ui/dialog'
import { getTokenSymbol } from '@/helpers/getTokenInfo'
import { StrategyMethod, useStrategyCallback } from '@/hooks/useStrategy'
import { getDefaultStrategies, pushAmount, pushAsset } from '@/store/lib/features/vaultStore'
import { useAppDispatch } from '@/store/lib/storeHooks'
import { Asset, Strategy } from '@/store/lib/types'
import {
Button,
For,
Expand All @@ -12,25 +23,13 @@ import {
Stack,
Text,
} from '@chakra-ui/react'
import { MdAdd } from 'react-icons/md'
import {
DialogBackdrop,
DialogBody,
DialogContent,
DialogFooter,
DialogRoot,
DialogTrigger,
} from '@/components/ui/dialog'
import { useAppDispatch, useAppSelector } from '@/store/lib/storeHooks'
import { getDefaultStrategies, pushAmount, pushAsset } from '@/store/lib/features/vaultStore'
import { useSorobanReact } from '@soroban-react/core'
import { Asset, Strategy } from '@/store/lib/types'
import { CheckboxCard } from '../ui/checkbox-card'
import { getTokenSymbol } from '@/helpers/getTokenInfo'
import { StrategyMethod, useStrategyCallback } from '@/hooks/useStrategy'
import { scValToNative, xdr } from '@stellar/stellar-sdk'
import { InputGroup } from '../ui/input-group'
import { useEffect, useState } from 'react'
import { MdAdd } from 'react-icons/md'
import { Checkbox } from '../ui/checkbox'
import { CheckboxCard } from '../ui/checkbox-card'
import { InputGroup } from '../ui/input-group'

interface AmountInputProps {
amount: number
Expand Down Expand Up @@ -151,7 +150,7 @@ function AddNewStrategyButton() {
size="md"
textAlign={'end'}
disabled={defaultStrategies.length === 0}>
Add new assets
Add Strategy
</Button>
</DialogTrigger>
<DialogContent>
Expand Down
98 changes: 98 additions & 0 deletions apps/dapp/src/components/TestTokens.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { useSorobanReact } from "@soroban-react/core";

import { HStack, Text } from "@chakra-ui/react";
import { contractInvoke } from "@soroban-react/contracts";
import { Address, Keypair, nativeToScVal, scValToNative, xdr } from "@stellar/stellar-sdk";
import { useEffect, useState } from "react";
import { Button } from "./ui/button";

export const TestTokens = () => {
const sorobanContext = useSorobanReact();
const { address, activeChain, server } = sorobanContext;
const networkPassphrase = activeChain?.networkPassphrase ?? '';
const [isSubmitting, setSubmitting] = useState(false);
const [balance, setBalance] = useState<string>("0");

const testnetUSDC = "CAAFIHB4I7WQMJMKC22CZVQNNX7EONWSOMT6SUXK6I3G3F6J4XFRWNDI";
const admin_account = Keypair.fromSecret(
process.env.NEXT_PUBLIC_TEST_TOKENS_ADMIN as string,
);

const fetchBalance = async () => {
if (!address) return;
contractInvoke({
contractAddress: testnetUSDC,
method: 'balance',
args: [new Address(address).toScVal()],
sorobanContext,
signAndSend: false,
}).then((result) => {
let balance = scValToNative(result as xdr.ScVal);
balance = BigInt(BigInt(balance) / BigInt(1e7)).toString();
setBalance(balance);

}).catch ((error) => {
console.log('🚀 « error:', error);
})
}

useEffect(() => {
fetchBalance();
}, [address])

const handleMint = async () => {
console.log("Minting");
setSubmitting(true);

const amount = 1000000000000

let adminSource;

try {
adminSource = await server?.getAccount(admin_account.publicKey());
} catch (error) {
alert('Your wallet or the token admin wallet might not be funded');
setSubmitting(false);
return;
}

if (!address) {
return;
}
if (!adminSource) {
return;
}

try {
let result = await contractInvoke({
contractAddress: testnetUSDC,
method: 'mint',
args: [new Address(address).toScVal(), nativeToScVal(amount, {type: 'i128'})],
sorobanContext,
signAndSend: true,
secretKey: admin_account.secret(),
});
if (result) {
fetchBalance();
}
} catch (error) {
console.log('🚀 « error:', error);
}

setSubmitting(false);
}

return (
<HStack>
<Text>Current Balance: {balance} USDC</Text>
<Button
rounded={18}
onClick={handleMint}
loadingText="Minting..."
loading={isSubmitting}
>
Mint test USDC
</Button>
</HStack>
);
};
40 changes: 40 additions & 0 deletions apps/dapp/src/components/ui/button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { ButtonProps as ChakraButtonProps } from "@chakra-ui/react"
import {
AbsoluteCenter,
Button as ChakraButton,
Span,
Spinner,
} from "@chakra-ui/react"
import * as React from "react"

interface ButtonLoadingProps {
loading?: boolean
loadingText?: React.ReactNode
}

export interface ButtonProps extends ChakraButtonProps, ButtonLoadingProps {}

export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
function Button(props, ref) {
const { loading, disabled, loadingText, children, ...rest } = props
return (
<ChakraButton disabled={loading || disabled} ref={ref} {...rest}>
{loading && !loadingText ? (
<>
<AbsoluteCenter display="inline-flex">
<Spinner size="inherit" color="inherit" />
</AbsoluteCenter>
<Span opacity={0}>{children}</Span>
</>
) : loading && loadingText ? (
<>
<Spinner size="inherit" color="inherit" />
{loadingText}
</>
) : (
children
)}
</ChakraButton>
)
},
)
4 changes: 2 additions & 2 deletions public/testnet.contracts.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"ids": {
"hodl_strategy": "CDY2ZG5A4253BXSVI4MRY43AIDLUII4XUGZ35MR36D65BLL7EKQQ7U7V",
"defindex_factory": "CBZSKTHG7Y3JUDB2LK5MMWYEV25UYRFK7JHRESW6X5UVIP5VYCTI6MNA"
"hodl_strategy": "CAEYFO6TY5MMPHSX6CMUKSDHPFVLJKV4TFPHE4ZXAEER2NEIB5GSVBVG",
"defindex_factory": "CBHW3ETUDAZ4FKKEEASONLALSZAYR6IMBA2THXKRPGRAVRG7UQLZFEKP"
},
"hashes": {
"hodl_strategy": "fcdb4a3c11525a1f32611951741bca5bc4196f58fd1633af37d5b35d30fdf5b0",
Expand Down

0 comments on commit ce6762d

Please sign in to comment.