Skip to content

Commit

Permalink
modified the rest api call code
Browse files Browse the repository at this point in the history
  • Loading branch information
gconnect committed Jun 8, 2024
1 parent dbad1fd commit 8184c76
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
16 changes: 15 additions & 1 deletion apps/cartesify/frontend/next-app/app/cartesi/RestExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
import { useEffect, useState } from "react"
import { Button } from "@chakra-ui/react"
import { useEthersSigner } from "../utils/useEtherSigner"
import { createOrUpdateRequest, getRequest } from "./services/RestApiCalls"
import { createOrUpdateRequest, fetchWallet, getRequest } from "./services/RestApiCalls"
import { callDAppAddressRelay } from "./services/Portal"
import { useAccount } from "wagmi"

export function RestExample() {
const [backendResponse, setResponse] = useState('')
const signer = useEthersSigner()
const { chain } = useAccount()

return (
<div className="flex justify-center text-white">
Expand All @@ -23,6 +26,17 @@ export function RestExample() {
<Button colorScheme="red" className=" p-2 rounded m-2" onClick={async () => {
await createOrUpdateRequest(signer, "delete?some=body", "DELETE")
}}>DELETE</Button>

<div>
<Button colorScheme="purple" className=" p-2 rounded m-2" onClick={async () => {
await callDAppAddressRelay(signer, chain!)
}}>Dapp Relay Address</Button>

<Button colorScheme="blue" className=" p-2 rounded m-2" onClick={async () => {
await fetchWallet(signer, setResponse)
}}>Get Wallet</Button>
</div>


<div className="text-lg my-4 text-slate-400">
Backend response: <pre>{backendResponse}</pre>
Expand Down
12 changes: 6 additions & 6 deletions apps/cartesify/frontend/next-app/app/cartesi/services/Portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const depositERC20 = async (dappAddress: string, erc20address: string,
const portal = ERC20Portal__factory.connect(portalAddress, signer)
const tx = await portal.depositERC20Tokens(erc20address, dappAddress, parseUnits(erc20value, 6), '0x')
const receipt = await (tx as any).wait()
successAlert(`Transaction Hash: <a href=${explorer/+"tx"/receipt.hash}> </a>`)
successAlert(`${explorer/+"tx"/receipt.hash}`)
} catch (error) {
console.log(error)
errorAlert(error)
Expand All @@ -119,7 +119,7 @@ export const depositERC721 = async (dappAddress: string, erc721address: string,
const portal = ERC721Portal__factory.connect(portalAddress, signer)
const tx = await portal.depositERC721Token(erc721address, dappAddress, erc721id, '0x', '0x')
const receipt = await (tx as any).wait()
successAlert(`Transaction Hash: <a href=${explorer/+"tx"/receipt.hash}> </a>`)
successAlert(`${explorer/+"tx"/receipt.hash}`)
} catch (error) {
errorAlert(error)
}
Expand All @@ -138,7 +138,7 @@ export const depositSingleERC1155 = async (dappAddress: string, erc1155address:
const portal = ERC1155SinglePortal__factory.connect(portalAddress, signer)
const tx = await portal.depositSingleERC1155Token(erc1155address, dappAddress, erc1155id, amount, '0x', data)
const receipt = await (tx as any).wait()
successAlert(`Transaction Hash: <a href=${explorer/+"tx"/receipt.hash}> </a>`)
successAlert(`${explorer/+"tx"/receipt.hash}`)
} catch (error) {
errorAlert(error)
}
Expand All @@ -156,7 +156,7 @@ export const depositBatchERC1155 = async (dappAddress: string, erc1155address: s
const portal = ERC1155BatchPortal__factory.connect(portalAddress, signer)
const tx = await portal.depositBatchERC1155Token(erc1155address, dappAddress, erc1155ids, amounts, '0x', data)
const receipt = await (tx as any).wait()
successAlert(`Transaction Hash: <a href=${explorer/+"tx"/receipt.hash}> </a>`)
successAlert(`${explorer/+"tx"/receipt.hash}`)
} catch (error) {
errorAlert(error)
}
Expand All @@ -176,7 +176,7 @@ try {
console.log(tx)
const receipt = await (tx as any).wait()
console.log('Executed!', receipt)
successAlert(`Transaction Hash: <a href=${explorer/+"tx"/receipt.hash}> </a>`)
successAlert(`${explorer/+"tx"/receipt.hash}`)

}
} catch (error) {
Expand All @@ -192,7 +192,7 @@ export const callDAppAddressRelay = async (signer: JsonRpcSigner | undefined, ch
const tx = await relay.relayDAppAddress(DAPP_ADDRESS)
const receipt = await (tx as any).wait()
console.log('Executed!', receipt)
successAlert(`Transaction Hash: <a href=${explorer/+"tx"/receipt.hash}> </a>`)
successAlert(`${explorer/+"tx"/receipt.hash}`)
} catch (error) {
errorAlert(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,9 @@ export const fetchWallet = async (signer: JsonRpcSigner | undefined,
const res = await fetch(`${BASE_URL}/${signer?.address}`)
const json = await res.json()
setResponse(JSON.stringify(json, null, 4))
successAlert("Successful!")
} catch (error) {
console.log(error)
errorAlert(error)
}
}

Expand Down

0 comments on commit 8184c76

Please sign in to comment.