Smart Contract
AFLBurnExchange
A.8f9231920da9af6d.AFLBurnExchange
1import NonFungibleToken from 0x1d7e57aa55817448
2import AFLNFT from 0x8f9231920da9af6d
3import Burner from 0xf233dcee88fe0abe
4
5access(all) contract AFLBurnExchange {
6 access(self) let tokens: @{UInt64: {NonFungibleToken.NFT}} // nftId of token to be swapped -> new token
7
8 access(all) event TokenAddedForExchange(oldTokenId: UInt64, newTokenId: UInt64)
9 access(all) event TokenExchanged(oldTokenId: UInt64, newTokenId: UInt64)
10
11 access(all) view fun getTokenId(id: UInt64): UInt64 {
12 let tokenRef: &{NonFungibleToken.NFT}? = &self.tokens[id]
13 return tokenRef?.id ?? 0
14 }
15
16 access(all) fun swap(token: @{NonFungibleToken.NFT}): @{NonFungibleToken.NFT} {
17 pre {
18 token.getType() == Type<@AFLNFT.NFT>() : "Wrong token type."
19 self.tokens[token.id] != nil: "No token found available in exchange for the provided token id: ".concat(token.id.toString())
20 }
21 let oldTokenId: UInt64 = token.id
22
23 // get the token details
24 let data: AFLNFT.NFTData = AFLNFT.getNFTData(nftId: token.id)
25 let templateId: UInt64 = data.templateId
26 let serial: UInt64 = data.mintNumber
27
28 // withdraw new token
29 let newToken: @{NonFungibleToken.NFT} <- self.tokens.remove(key: token.id) ?? panic("No token found for exchange")
30
31 // burn old token
32 Burner.burn(<- token)
33
34 emit TokenExchanged(oldTokenId: oldTokenId, newTokenId: newToken.id)
35
36 // return new token to user
37 return <- newToken
38 }
39
40 access(all) view fun getTokenIds(): [UInt64] {
41 return self.tokens.keys
42 }
43
44 // nftId = duplicateTokenId
45 // @token = new token
46 access(account) fun addTokenForExchange(nftId: UInt64, token: @{NonFungibleToken.NFT}) {
47 pre {
48 self.tokens[nftId] == nil: "Token already exists: ".concat(nftId.toString())
49 }
50 let newTokenId: UInt64 = token.id
51 self.tokens[nftId] <-! token
52 emit TokenAddedForExchange(oldTokenId: nftId, newTokenId: newTokenId)
53 }
54
55 access(account) fun withdrawToken(nftId: UInt64): @{NonFungibleToken.NFT} {
56 pre {
57 self.tokens[nftId] != nil: "No token found available in exchange for the provided token id: ".concat(nftId.toString())
58 }
59 let token: @{NonFungibleToken.NFT} <- self.tokens.remove(key: nftId)!
60 return <- token
61 }
62
63 init() {
64 self.tokens <- {}
65 }
66}
67
68