-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppCalls.tsx
100 lines (87 loc) · 3.52 KB
/
AppCalls.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import * as algokit from '@algorandfoundation/algokit-utils'
import { TransactionSignerAccount } from '@algorandfoundation/algokit-utils/types/account'
import { AppDetails } from '@algorandfoundation/algokit-utils/types/app-client'
import { useWallet } from '@txnlab/use-wallet'
import { useSnackbar } from 'notistack'
import { useState } from 'react'
import { SmartContractClient } from '../contracts/smart_contract'
import { getAlgodConfigFromViteEnvironment, getIndexerConfigFromViteEnvironment } from '../utils/network/getAlgoClientConfigs'
interface AppCallsInterface {
openModal: boolean
setModalState: (value: boolean) => void
}
const AppCalls = ({ openModal, setModalState }: AppCallsInterface) => {
const [loading, setLoading] = useState<boolean>(false)
const [contractInput, setContractInput] = useState<string>('')
const algodConfig = getAlgodConfigFromViteEnvironment()
const algodClient = algokit.getAlgoClient({
server: algodConfig.server,
port: algodConfig.port,
token: algodConfig.token,
})
const indexerConfig = getIndexerConfigFromViteEnvironment()
const indexer = algokit.getAlgoIndexerClient({
server: indexerConfig.server,
port: indexerConfig.port,
token: indexerConfig.token,
})
const { enqueueSnackbar } = useSnackbar()
const { signer, activeAddress } = useWallet()
const sendAppCall = async () => {
setLoading(true)
const appDetails = {
resolveBy: 'creatorAndName',
sender: { signer, addr: activeAddress } as TransactionSignerAccount,
creatorAddress: activeAddress,
findExistingUsing: indexer,
} as AppDetails
const appClient = new SmartContractClient(appDetails, algodClient)
// Please note, in typical production scenarios,
// you wouldn't want to use deploy directly from your frontend.
// Instead, you would deploy your contract on your backend and reference it by id.
// Given the simplicity of the starter contract, we are deploying it on the frontend
// for demonstration purposes.
const deployParams = {
onSchemaBreak: 'append',
onUpdate: 'append',
}
await appClient.deploy(deployParams).catch((e: Error) => {
enqueueSnackbar(`Error deploying the contract: ${e.message}`, { variant: 'error' })
setLoading(false)
return
})
const response = await appClient.hello({ name: contractInput }).catch((e: Error) => {
enqueueSnackbar(`Error calling the contract: ${e.message}`, { variant: 'error' })
setLoading(false)
return
})
enqueueSnackbar(`Response from the contract: ${response?.return}`, { variant: 'success' })
setLoading(false)
}
return (
<dialog id="appcalls_modal" className={`modal ${openModal ? 'modal-open' : ''} bg-slate-200`}>
<form method="dialog" className="modal-box">
<h3 className="font-bold text-lg">Say hello to your Algorand smart contract</h3>
<br />
<input
type="text"
placeholder="Provide input to hello function"
className="input input-bordered w-full"
value={contractInput}
onChange={(e) => {
setContractInput(e.target.value)
}}
/>
<div className="modal-action ">
<button className="btn" onClick={() => setModalState(!openModal)}>
Close
</button>
<button className={`btn`} onClick={sendAppCall}>
{loading ? <span className="loading loading-spinner" /> : 'Send application call'}
</button>
</div>
</form>
</dialog>
)
}
export default AppCalls