This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
AppCalls.tsx
259 lines (223 loc) · 7.62 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable no-console */
import * as algokit from '@algorandfoundation/algokit-utils'
import { useWallet } from '@txnlab/use-wallet'
import algosdk from 'algosdk'
import { useSnackbar } from 'notistack'
import { useState } from 'react'
import { AuctionState } from '../App'
import { AuctionClient } from '../contracts/auction'
import { getAlgodConfigFromViteEnvironment } from '../utils/network/getAlgoClientConfigs'
type Methods = 'create' | 'start' | 'bid' | 'reclaim_bids'
const AppCalls = (props: {
method: Methods
setAuctionState: React.Dispatch<React.SetStateAction<AuctionState>>
appID: number
setAppID?: React.Dispatch<React.SetStateAction<number>>
}) => {
const [loading, setLoading] = useState<boolean>(false)
const algodConfig = getAlgodConfigFromViteEnvironment()
const algodClient = algokit.getAlgoClient({
server: algodConfig.server,
port: algodConfig.port,
token: algodConfig.token,
})
const { enqueueSnackbar } = useSnackbar()
const { signer, activeAddress } = useWallet()
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const sender = { signer, addr: activeAddress! }
const appClient = new AuctionClient(
{
resolveBy: 'id',
id: props.appID,
sender,
},
algodClient,
)
const callMethods = {
create: async () => {
if (props.setAppID === undefined) throw Error('setAppID is undefined')
setLoading(true)
await appClient.create.bare().catch((e: Error) => {
enqueueSnackbar(`Error deploying the contract: ${e.message}`, { variant: 'error' })
setLoading(false)
return
})
const { appId } = await appClient.appClient.getAppReference()
setLoading(false)
props.setAppID(Number(appId))
},
start: async () => {
// opt_into_asset and start_auction logic here
setLoading(true)
const assetIndex = (document.getElementById('asa') as HTMLInputElement).valueAsNumber
const appAddress = algosdk.getApplicationAddress(props.appID)
const suggestedParams = await algodClient.getTransactionParams().do()
const atc = new algosdk.AtomicTransactionComposer()
// opt_into_asset: Pay the application 0.2 ALGO
// MBR === Minimum Balance Requirement
// 0.1 ALGO for account MBR
// 0.1 ALGO for ASA MBR
const payment = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: activeAddress!,
to: appAddress,
amount: 0.2e-6,
suggestedParams,
})
atc.addTransaction({ txn: payment, signer })
// opt_into_asset: call opt_into_asset on the application
atc.addMethodCall({
method: appClient.appClient.getABIMethod('opt_into_asset')!,
methodArgs: [assetIndex],
suggestedParams: { ...suggestedParams, fee: 2_000, flatFee: true },
sender: sender.addr,
signer,
appID: props.appID,
})
// start auction
const startPrice = (document.getElementById('start') as HTMLInputElement).valueAsNumber
const length = (document.getElementById('length') as HTMLInputElement).valueAsNumber
const assetAmount = (document.getElementById('asa-amount') as HTMLInputElement).valueAsNumber
const axfer = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({
from: activeAddress!,
to: appAddress,
assetIndex,
suggestedParams,
amount: assetAmount,
})
atc.addMethodCall({
method: appClient.appClient.getABIMethod('start_auction')!,
methodArgs: [startPrice, length, { txn: axfer, signer }],
suggestedParams,
sender: sender.addr,
signer,
appID: props.appID,
})
try {
await atc.execute(algodClient, 3)
} catch (e) {
console.warn(e)
enqueueSnackbar(`Error deploying the contract: ${(e as Error).message}`, { variant: 'error' })
setLoading(false)
return
}
setLoading(false)
props.setAuctionState(AuctionState.Started)
},
bid: async () => {
setLoading(true)
const appAddress = algosdk.getApplicationAddress(props.appID)
const suggestedParams = await algodClient.getTransactionParams().do()
const bidPayment = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: activeAddress!,
to: appAddress,
amount: (document.getElementById('bid') as HTMLInputElement).valueAsNumber,
suggestedParams,
})
const atc = new algosdk.AtomicTransactionComposer()
let optedIn = true
// If the account is not opted in, opt it in
// Otherwise, just bid
try {
await appClient.getLocalState(activeAddress!)
} catch (e) {
optedIn = false
}
if (!optedIn) {
const optInTxn = algosdk.makeApplicationOptInTxnFromObject({
from: activeAddress!,
appIndex: props.appID,
suggestedParams,
})
atc.addTransaction({ txn: optInTxn, signer })
}
atc.addMethodCall({
method: appClient.appClient.getABIMethod('bid')!,
methodArgs: [{ txn: bidPayment, signer }],
sender: sender.addr,
signer,
appID: props.appID,
suggestedParams,
})
try {
await atc.execute(algodClient, 3)
} catch (e) {
console.warn(e)
enqueueSnackbar(`Error deploying the contract: ${(e as Error).message}`, { variant: 'error' })
setLoading(false)
return
}
setLoading(false)
},
reclaim_bids: async () => {
// logic for reclaiming bids
setLoading(true)
await appClient.reclaimBids([], { sendParams: { fee: algokit.microAlgos(2000) } }).catch((e: Error) => {
enqueueSnackbar(`Error deploying the contract: ${e.message}`, { variant: 'error' })
setLoading(false)
return
})
setLoading(false)
},
}
let text: string
switch (props.method) {
case 'create':
text = 'Create App'
break
case 'start':
text = 'Start Auction'
break
case 'bid':
text = 'Bid'
break
case 'reclaim_bids':
text = 'Reclaim Bids'
}
const callButton = (
<button className={`btn m-2`} onClick={callMethods[props.method]}>
{loading ? <span className="loading loading-spinner" /> : text}
</button>
)
switch (props.method) {
case 'create':
return callButton
case 'start':
return (
<div>
<label htmlFor="asa" className="label m-2">
Asset ID
</label>
<input type="number" id="asa" defaultValue="0" className="input input-bordered" />
<label htmlFor="asa-amount" className="label m-2">
Asset Amount
</label>
<input type="number" id="asa-amount" defaultValue="0" className="input input-bordered" />
<label htmlFor="start" className="label m-2">
Starting Price
</label>
<input type="number" id="start" defaultValue="0" className="input input-bordered" />
<label htmlFor="length" className="label m-2">
Length (Seconds)
</label>
<input type="number" id="length" defaultValue="0" className="input input-bordered" />
<br />
{callButton}
</div>
)
case 'bid':
return (
<div>
<label htmlFor="bid" className="label m-2">
Bid Amount
</label>
<input type="number" id="bid" defaultValue="0" className="input input-bordered" />
<br />
{callButton}
</div>
)
case 'reclaim_bids':
return callButton
}
}
export default AppCalls