Skip to content

Commit

Permalink
✨ enter market and borrow
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Nov 27, 2023
1 parent ea8abd6 commit d183a23
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 12 deletions.
79 changes: 70 additions & 9 deletions app/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import Auditor from "@exactly/protocol/deployments/goerli/Auditor.json";
import MarketUSDC from "@exactly/protocol/deployments/goerli/MarketUSDC.json";
import MarketWETH from "@exactly/protocol/deployments/goerli/MarketWETH.json";
import { ApiKeyStamper } from "@turnkey/api-key-stamper";
import { TurnkeyClient, createActivityPoller, getWebAuthnAttestation } from "@turnkey/http";
import { deviceName } from "expo-device";
import React, { useCallback } from "react";
import { Button, Spinner, Text, XStack, YStack } from "tamagui";
import { UAParser } from "ua-parser-js";
import { useAccount, useConnect, useDisconnect, useSendTransaction } from "wagmi";
import { formatEther, getAddress, parseUnits, zeroAddress } from "viem";
import {
useAccount,
useConnect,
useDisconnect,
useReadContract,
useSimulateContract,
useWaitForTransactionReceipt,
useWriteContract,
} from "wagmi";

import base64URLEncode from "../utils/base64URLEncode";
import { rpId, turnkeyAPIPrivateKey, turnkeyAPIPublicKey, turnkeyOrganizationId } from "../utils/constants";
Expand All @@ -19,7 +31,44 @@ export default function Home() {
} = useConnect();
const { address } = useAccount();
const { disconnect } = useDisconnect();
const { sendTransaction, data: txHash, isPending: isSending } = useSendTransaction();
const { data: enterWETHSimulation } = useSimulateContract({
abi: Auditor.abi,
address: getAddress(Auditor.address),
functionName: "enterMarket",
args: [MarketWETH.address],
query: { enabled: !!address },
});
const { data: borrowUSDCSimulation } = useSimulateContract({
abi: MarketUSDC.abi,
address: getAddress(MarketUSDC.address),
functionName: "borrow",
args: [parseUnits("1", 6), address, address],
query: { enabled: !!address },
});
const { data: accountLiquidity } = useReadContract({
abi: [
{
name: "accountLiquidity",
type: "function",
stateMutability: "view",
inputs: [
{ internalType: "address", name: "account", type: "address" },
{ internalType: "contract Market", name: "marketToSimulate", type: "address" },
{ internalType: "uint256", name: "withdrawAmount", type: "uint256" },
],
outputs: [
{ internalType: "uint256", name: "sumCollateral", type: "uint256" },
{ internalType: "uint256", name: "sumDebtPlusEffects", type: "uint256" },
],
},
],
address: getAddress(Auditor.address),
functionName: "accountLiquidity",
args: [address ?? zeroAddress, zeroAddress, 0n],
query: { enabled: !!address },
});
const { writeContract, data: txHash, isPending: isSending } = useWriteContract();
const { isSuccess, isLoading: isWaiting } = useWaitForTransactionReceipt({ hash: txHash });

const createAccount = useCallback(() => {
const name = `exactly, ${new Date().toISOString()}`;
Expand Down Expand Up @@ -88,20 +137,32 @@ export default function Home() {
disconnect();
}, [disconnect]);

const send = useCallback(() => {
sendTransaction({ to: "0xE72185a9f4Ce3500d6dC7CCDCfC64cf66D823bE8" });
}, [sendTransaction]);
const enterWETH = useCallback(() => {
if (!enterWETHSimulation) throw new Error("no simulation");
writeContract(enterWETHSimulation.request);
}, [enterWETHSimulation, writeContract]);

const borrowUSDC = useCallback(() => {
if (!borrowUSDCSimulation) throw new Error("no simulation");
writeContract(borrowUSDCSimulation.request);
}, [borrowUSDCSimulation, writeContract]);

return (
<XStack flex={1} alignItems="center" space>
<YStack flex={1} alignItems="center" space>
<Text textAlign="center">{txHash}</Text>
<Button onPress={createAccount}>create</Button>
<Text textAlign="center">{txHash && `${txHash} ${isSuccess ? "✅" : ""}`}</Text>
<Text textAlign="center">
{accountLiquidity && `${accountLiquidity.map((v) => formatEther(v)).join(", ")}`}
</Text>
<Button onPress={createAccount}>create account</Button>
<Button disabled={!connector || isConnecting} onPress={address ? disconnectAccount : connectAccount}>
{isConnecting ? <Spinner size="small" /> : address ?? "connect"}
</Button>
<Button disabled={!address || isSending} onPress={send}>
{isSending ? <Spinner size="small" /> : "send"}
<Button disabled={!enterWETHSimulation || isSending || isWaiting} onPress={enterWETH}>
enter WETH market
</Button>
<Button disabled={!borrowUSDCSimulation || isSending || isWaiting} onPress={borrowUSDC}>
borrow 1 USDC
</Button>
</YStack>
</XStack>
Expand Down
Binary file modified bun.lockb
Binary file not shown.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@alchemy/aa-accounts": "^1.2.0",
"@alchemy/aa-alchemy": "^1.2.0",
"@alchemy/aa-core": "^1.2.0",
"@exactly/protocol": "^0.2.19",
"@expo/vector-icons": "^13.0.0",
"@react-native-async-storage/async-storage": "1.18.2",
"@sentry/react-native": "^5.14.1",
Expand Down Expand Up @@ -66,11 +67,11 @@
"@tamagui/babel-plugin": "^1.79.1",
"@types/babel__core": "^7.20.5",
"@types/eslint": "^8.44.7",
"@types/react": "^18.2.38",
"@types/react": "^18.2.39",
"@types/text-encoding": "^0.0.39",
"@types/ua-parser-js": "^0.7.39",
"@typescript-eslint/eslint-plugin": "^6.12.0",
"@typescript-eslint/parser": "^6.12.0",
"@typescript-eslint/eslint-plugin": "^6.13.0",
"@typescript-eslint/parser": "^6.13.0",
"babel-plugin-transform-inline-environment-variables": "^0.4.4",
"concurrently": "^8.2.2",
"eas-cli": "^5.9.1",
Expand Down

0 comments on commit d183a23

Please sign in to comment.