-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #198 from paltalabs/feat/testTokens
added test tokens
- Loading branch information
Showing
6 changed files
with
168 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters