/
asa-gold-smartcontract.algo.ts
391 lines (365 loc) · 13.4 KB
/
asa-gold-smartcontract.algo.ts
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import { Contract } from '@algorandfoundation/tealscript'
// state
// 1 : In reserve [changeQuotation callable]
// 2 : Not for sale (nft can be withdrawn from reserve)
// 3 : Secondary trading [changeQuotation callable]
// 4 : Requested parcel delivery - Activate parcel delivery process by owner of the NFT
// 5 : Shipped to customer or in the parcel delivery process
// seller : who receives the tokens. In case of public audit reserves this MUST be the reserve account of the token issued
// owner : who can update this record..
// vaultOwnerAddress : the original owner address. This address can change the status of the coin 4->5 (user requested parcel delivery -> in parcel delivery)
// weight : weight of the gold in gold coin in grams
// QUOTES - available options to purchase this asset
// quoteAsset1: quote in uint64.. this amount of asset1 (gold token) must be paid in order to allow transfer of this token
// asset1 : gold token asset id
// quoteAsset2 : quote in uint64.. this amount of asset2 (fe usdc token) must be paid in order to allow transfer of this token
// asset2 : fe usdc asset id
type AsaData = {
state: uint64
seller: Address
owner: Address
vaultOwnerAddress: Address
weight: uint64
quoteAsset1: uint64
asset1: Asset
quoteAsset2: uint64
asset2: Asset
quoteAsset3: uint64
asset3: Asset
quoteAsset4: uint64
asset4: Asset
quoteAsset5: uint64
asset5: Asset
}
const SCALE = 10_000
const BOX_SIZE = 200
// eslint-disable-next-line no-unused-vars
class AsaGoldSmartcontract extends Contract {
data = BoxMap<Asset, AsaData>({ prefix: 'd' })
reserves = BoxMap<Asset, uint64>({ prefix: 'r' })
governor = GlobalStateKey<Address>({ key: 'g' }) // account which receives royalty payments
fee = GlobalStateKey<uint64>({ key: 'f' })
createApplication(fee: uint64, governor: Address): void {
this.governor.value = governor
this.fee.value = fee
}
/**
* Creator is allowed to update application
*/
updateApplication(): void {
assert(this.txn.sender === this.app.creator)
}
/**
* Deposit Gold coin NFT, set seller owner and price for sale
*
* @param nftDepositTx NFT deposit tx
* @param vaultOwnerAddress Address of the account which can set state Shipped to customer
* @param weight The weight of the gold contained in the gold coin
* @param price The price in gold token for which the asset can be sold to clients
* @returns The sum of a and b
*/
public sellAssetWithDeposit(
nftDepositTx: AssetTransferTxn,
vaultOwnerAddress: Address,
nftAsset: Asset,
tokenAsset: Asset,
weight: uint64,
price: uint64
): void {
assert(!this.data(nftDepositTx.xferAsset).exists) // the asset must not be defined
assert(weight > 0)
assert(price > 0)
assert(tokenAsset.reserve.hasAsset(tokenAsset)) // check if the reserve address is active
const newItem: AsaData = {
state: 1,
seller: tokenAsset.reserve,
owner: this.txn.sender, //person who made deposit of this nft is owner
vaultOwnerAddress: vaultOwnerAddress, //when the sale is done, the received assets will go back to reserve account of the gold token
weight: weight,
quoteAsset1: price,
asset1: tokenAsset,
quoteAsset2: 0, //initial deposit can be sold only for the gold token asset
asset2: Asset.zeroIndex,
quoteAsset3: 0,
asset3: Asset.zeroIndex,
quoteAsset4: 0,
asset4: Asset.zeroIndex,
quoteAsset5: 0,
asset5: Asset.zeroIndex
}
verifyTxn(nftDepositTx, {
assetAmount: { greaterThan: 0 },
assetReceiver: this.app.address,
sender: this.txn.sender
})
assert(nftDepositTx.xferAsset.total == 1) // there is always only one nft representation for the gold coin
// check if nft is present when put on sale
assert(this.app.address.assetBalance(nftDepositTx.xferAsset) == 1)
assert(nftDepositTx.xferAsset == nftAsset)
// if item exists - rewrite
//this.data(nftDepositTx.xferAsset).create(BOX_SIZE)
this.data(nftAsset).value = newItem
// RESERVES MANAGEMENT
var newReserves = newItem.weight
if (this.reserves(tokenAsset).exists) {
newReserves = newReserves + this.reserves(tokenAsset).value
}
this.reserves(tokenAsset).value = newReserves
}
/**
* Client can buy NFT which is for sale on the reserve sale or secondary sale
*
* @param purchaseAssetDepositTx Transfer to the smart contract with the purchase price
* @param nftAsset Identifies NFT which client wants to buy
*/
public buyNFT(purchaseAssetDepositTx: AssetTransferTxn, nftAsset: Asset): void {
const old = this.data(nftAsset).value
const newItem: AsaData = {
state: 2, // not for sale .. user can set it for sale later, request nft to be withdrawn or request parcel delivery
seller: this.txn.sender,
owner: this.txn.sender,
vaultOwnerAddress: old.vaultOwnerAddress,
weight: old.weight,
quoteAsset1: 0,
asset1: Asset.zeroIndex,
quoteAsset2: 0,
asset2: Asset.zeroIndex,
quoteAsset3: 0,
asset3: Asset.zeroIndex,
quoteAsset4: 0,
asset4: Asset.zeroIndex,
quoteAsset5: 0,
asset5: Asset.zeroIndex
}
verifyTxn(purchaseAssetDepositTx, {
assetAmount: { greaterThan: 0 },
assetReceiver: this.app.address,
sender: this.txn.sender
})
var verifiedQuotation = false
if (old.asset1 == purchaseAssetDepositTx.xferAsset && old.quoteAsset1 > 0) {
assert(old.quoteAsset1 == purchaseAssetDepositTx.assetAmount)
verifiedQuotation = true
}
if (old.asset2 == purchaseAssetDepositTx.xferAsset && old.quoteAsset2 > 0) {
assert(old.quoteAsset2 == purchaseAssetDepositTx.assetAmount)
verifiedQuotation = true
}
if (old.asset3 == purchaseAssetDepositTx.xferAsset && old.quoteAsset3 > 0) {
assert(old.quoteAsset3 == purchaseAssetDepositTx.assetAmount)
verifiedQuotation = true
}
if (old.asset4 == purchaseAssetDepositTx.xferAsset && old.quoteAsset4 > 0) {
assert(old.quoteAsset4 == purchaseAssetDepositTx.assetAmount)
verifiedQuotation = true
}
if (old.asset5 == purchaseAssetDepositTx.xferAsset && old.quoteAsset5 > 0) {
assert(old.quoteAsset5 == purchaseAssetDepositTx.assetAmount)
verifiedQuotation = true
}
assert(verifiedQuotation) // check that user paid requested price
assert(old.state == 1 || old.state == 3) // on sale of nft we check that the old state is either new nft or secondary sale
assert(newItem.state == 2) // new state must be always not for sale
// check if nft is present when put on sale
assert(this.app.address.assetBalance(nftAsset) == 1)
var fee = (purchaseAssetDepositTx.assetAmount * this.fee.value) / SCALE
// send fee from the purchase price to the fee collector account
sendAssetTransfer({
assetAmount: fee,
xferAsset: purchaseAssetDepositTx.xferAsset,
assetReceiver: this.governor.value,
fee: 0
})
// send purchase price minus fees to seller
sendAssetTransfer({
assetAmount: purchaseAssetDepositTx.assetAmount - fee,
xferAsset: purchaseAssetDepositTx.xferAsset,
assetReceiver: old.seller,
fee: 0
})
// rewrite item
this.data(nftAsset).value = newItem
// RESERVES MANAGEMENT
if (old.state == 1) {
// we just sold the NFT from the reserves
this.reserves(old.asset1).value = this.reserves(old.asset1).value - old.weight
}
}
/**
* Change price quotes for NFT on sale
*
* @param nftAsset The gold coin NFT asset
* @param numbers The bytes containing quoteAsset1, asset1, quoteAsset2 .. asset5
*/
public changeQuotation(nftAsset: Asset, numbers: uint64[]): void {
const old = this.data(nftAsset).value
var newState = old.state
if (newState == 2) {
newState = 3 // if we move from not for sale to secondary trading
}
const newItem: AsaData = {
state: newState,
seller: old.seller,
owner: old.owner,
vaultOwnerAddress: old.vaultOwnerAddress,
weight: old.weight,
quoteAsset1: numbers[0],
asset1: Asset.fromID(numbers[1]),
quoteAsset2: numbers[2],
asset2: Asset.fromID(numbers[3]),
quoteAsset3: numbers[4],
asset3: Asset.fromID(numbers[5]),
quoteAsset4: numbers[6],
asset4: Asset.fromID(numbers[7]),
quoteAsset5: numbers[8],
asset5: Asset.fromID(numbers[9])
}
assert(
(numbers[0] > 0 && numbers[1] > 0) ||
(numbers[2] > 0 && numbers[3] > 0) ||
(numbers[4] > 0 && numbers[5] > 0) ||
(numbers[6] > 0 && numbers[7] > 0) ||
(numbers[8] > 0 && numbers[9] > 0)
)
assert(this.txn.sender == old.owner) // only owner can change quotation
// check if nft is present when put on sale
assert(this.app.address.assetBalance(nftAsset) == 1)
assert(newItem.state == 1 || newItem.state == 3) // current state must be in reserve or in secondary trading
// rewrite
this.data(nftAsset).value = newItem
}
public requestParcelDelivery(nftAsset: Asset): void {
const old = this.data(nftAsset).value
const newItem: AsaData = {
state: 4, // 3 - request parcel delivery by owner
seller: old.seller,
owner: old.owner,
vaultOwnerAddress: old.vaultOwnerAddress,
weight: old.weight,
quoteAsset1: 0,
asset1: Asset.zeroIndex,
quoteAsset2: 0,
asset2: Asset.zeroIndex,
quoteAsset3: 0,
asset3: Asset.zeroIndex,
quoteAsset4: 0,
asset4: Asset.zeroIndex,
quoteAsset5: 0,
asset5: Asset.zeroIndex
}
assert(old.state == 2 || old.state == 3) // only sold nfts can be requested for parcel delivery
assert(this.txn.sender == old.owner) // only owner can request parcel delivery
// rewrite asset
this.data(nftAsset).value = newItem
}
public setParcelDelivery(nftAsset: Asset): void {
const old = this.data(nftAsset).value
const newItem: AsaData = {
state: 5, // 5 - parcel confirmed by vault owner
seller: old.seller,
owner: old.owner,
vaultOwnerAddress: old.vaultOwnerAddress,
weight: old.weight,
quoteAsset1: 0,
asset1: Asset.zeroIndex,
quoteAsset2: 0,
asset2: Asset.zeroIndex,
quoteAsset3: 0,
asset3: Asset.zeroIndex,
quoteAsset4: 0,
asset4: Asset.zeroIndex,
quoteAsset5: 0,
asset5: Asset.zeroIndex
}
assert(old.state == 4) // only sold nfts can be requested for parcel delivery
assert(this.txn.sender == old.vaultOwnerAddress) // only initial owner can mark the asset as it is in parcel delivery
// rewrite asset
this.data(nftAsset).value = newItem
}
public setNotForSale(nftAsset: Asset): void {
const old = this.data(nftAsset).value
const newItem: AsaData = {
state: 2, // 2 - not for sale
seller: old.seller,
owner: old.owner,
vaultOwnerAddress: old.vaultOwnerAddress,
weight: old.weight,
quoteAsset1: 0,
asset1: Asset.zeroIndex,
quoteAsset2: 0,
asset2: Asset.zeroIndex,
quoteAsset3: 0,
asset3: Asset.zeroIndex,
quoteAsset4: 0,
asset4: Asset.zeroIndex,
quoteAsset5: 0,
asset5: Asset.zeroIndex
}
assert(old.state == 3) // only sold nfts can be requested for parcel delivery
assert(this.txn.sender == old.owner) // owner of NFT can set NFT not to be onsale
// rewrite asset
this.data(nftAsset).value = newItem
}
public withdrawNFT(nftAsset: Asset): void {
const old = this.data(nftAsset).value
assert(this.txn.sender == old.owner) // only owner can withdraw
assert(old.state == 2 || old.state == 4 || old.state == 5) // allow withdraw NFT only if not on sale or in parcel
// send purchase price minus fees to seller
sendAssetTransfer({
assetAmount: 1,
xferAsset: nftAsset,
assetReceiver: old.owner,
fee: 0
})
}
public optinAsset(nftAsset: Asset): void {
// anybody can opt in any asset
sendAssetTransfer({
assetAmount: 0,
xferAsset: nftAsset,
assetReceiver: this.app.address,
fee: 0
})
}
public depositNFT(nftDepositTx: AssetTransferTxn, seller: Address, numbers: uint64[]): void {
assert(this.data(nftDepositTx.xferAsset).exists) // the asset must not be defined
const old = this.data(nftDepositTx.xferAsset).value
let newState = 3 // alow deposit to sale state
if (
numbers[0] == 0 &&
numbers[2] == 0 &&
numbers[4] == 0 &&
numbers[6] == 0 &&
numbers[8] == 0
) {
newState = 2 // alow deposit to not for sale state
}
const newItem: AsaData = {
state: 3,
seller: seller,
owner: this.txn.sender,
vaultOwnerAddress: old.vaultOwnerAddress,
weight: old.weight,
quoteAsset1: numbers[0],
asset1: Asset.fromID(numbers[1]),
quoteAsset2: numbers[2],
asset2: Asset.fromID(numbers[3]),
quoteAsset3: numbers[4],
asset3: Asset.fromID(numbers[5]),
quoteAsset4: numbers[6],
asset4: Asset.fromID(numbers[7]),
quoteAsset5: numbers[8],
asset5: Asset.fromID(numbers[9])
}
verifyTxn(nftDepositTx, {
assetAmount: { greaterThan: 0 },
assetReceiver: this.app.address,
sender: this.txn.sender
})
// check if nft is present when put on sale
assert(this.app.address.assetBalance(nftDepositTx.xferAsset) == 1)
assert(old.state == 2 || old.state == 3) // allow deposit NFT only if in secondary market. if parcel requested do not allow
// rewrite
this.data(nftDepositTx.xferAsset).value = newItem
}
}