1+ using System ;
2+ using System . Threading . Tasks ;
3+
4+ namespace Thirdweb
5+ {
6+ /// <summary>
7+ /// Interact with any <c>ERC1155</c> compatible contract.
8+ /// </summary>
9+ public class ERC1155
10+ {
11+ public string chain ;
12+ public string address ;
13+ public ERC1155Signature signature ;
14+
15+ public ERC1155 ( string chain , string address )
16+ {
17+ this . chain = chain ;
18+ this . address = address ;
19+ this . signature = new ERC1155Signature ( chain , address ) ;
20+ }
21+
22+ /// READ FUNCTIONS
23+
24+ public async Task < NFT > Get ( string tokenId )
25+ {
26+ return await Bridge . InvokeRoute < NFT > ( getRoute ( "get" ) , Utils . ToJsonStringArray ( tokenId ) ) ;
27+ }
28+
29+ public async Task < NFT [ ] > GetAll ( )
30+ {
31+ return await Bridge . InvokeRoute < NFT [ ] > ( getRoute ( "getAll" ) , new string [ ] { } ) ;
32+ }
33+
34+ public async Task < NFT [ ] > GetOwned ( )
35+ {
36+ return await Bridge . InvokeRoute < NFT [ ] > ( getRoute ( "getOwned" ) , new string [ ] { } ) ;
37+ }
38+
39+ public async Task < NFT [ ] > GetOwned ( string address )
40+ {
41+ return await Bridge . InvokeRoute < NFT [ ] > ( getRoute ( "getOwned" ) , Utils . ToJsonStringArray ( address ) ) ;
42+ }
43+
44+ public async Task < string > Balance ( string tokenId )
45+ {
46+ return await Bridge . InvokeRoute < string > ( getRoute ( "balance" ) , new string [ ] { } ) ;
47+ }
48+
49+ public async Task < string > BalanceOf ( string address , string tokenId )
50+ {
51+ return await Bridge . InvokeRoute < string > ( getRoute ( "balanceOf" ) , Utils . ToJsonStringArray ( address , tokenId ) ) ;
52+ }
53+
54+ public async Task < string > IsApprovedForAll ( string address , string approvedContract )
55+ {
56+ return await Bridge . InvokeRoute < string > ( getRoute ( "isApproved" ) , Utils . ToJsonStringArray ( address , approvedContract ) ) ;
57+ }
58+
59+ public async Task < int > TotalCount ( )
60+ {
61+ return await Bridge . InvokeRoute < int > ( getRoute ( "totalCount" ) , new string [ ] { } ) ;
62+ }
63+
64+ public async Task < int > TotalSupply ( string tokenId )
65+ {
66+ return await Bridge . InvokeRoute < int > ( getRoute ( "totalUnclaimedSupply" ) , Utils . ToJsonStringArray ( tokenId ) ) ;
67+ }
68+
69+ /// WRITE FUNCTIONS
70+
71+ public async Task < TransactionResult > SetApprovalForAll ( string contractToApprove , bool approved )
72+ {
73+ return await Bridge . InvokeRoute < TransactionResult > ( getRoute ( "isApproved" ) , Utils . ToJsonStringArray ( contractToApprove , approved ) ) ;
74+ }
75+
76+ public async Task < TransactionResult > Transfer ( string to , string tokenId , int amount )
77+ {
78+ return await Bridge . InvokeRoute < TransactionResult > ( getRoute ( "transfer" ) , Utils . ToJsonStringArray ( to , tokenId , amount ) ) ;
79+ }
80+
81+ public async Task < TransactionResult > Burn ( string tokenId , int amount )
82+ {
83+ return await Bridge . InvokeRoute < TransactionResult > ( getRoute ( "burn" ) , Utils . ToJsonStringArray ( tokenId , amount ) ) ;
84+ }
85+
86+ public async Task < TransactionResult [ ] > Claim ( string tokenId , int amount )
87+ {
88+ return await Bridge . InvokeRoute < TransactionResult [ ] > ( getRoute ( "claim" ) , Utils . ToJsonStringArray ( tokenId , amount ) ) ;
89+ }
90+
91+ public async Task < TransactionResult [ ] > ClaimTo ( string address , string tokenId , int amount )
92+ {
93+ return await Bridge . InvokeRoute < TransactionResult [ ] > ( getRoute ( "claimTo" ) , Utils . ToJsonStringArray ( address , tokenId , amount ) ) ;
94+ }
95+
96+ public async Task < TransactionResult > Mint ( NFTMetadataWithSupply nft )
97+ {
98+ return await Bridge . InvokeRoute < TransactionResult > ( getRoute ( "mint" ) , Utils . ToJsonStringArray ( nft ) ) ;
99+ }
100+
101+ public async Task < TransactionResult > MintTo ( string address , NFTMetadataWithSupply nft )
102+ {
103+ return await Bridge . InvokeRoute < TransactionResult > ( getRoute ( "mintTo" ) , Utils . ToJsonStringArray ( address , nft ) ) ;
104+ }
105+
106+ public async Task < TransactionResult > MintAdditionalSupply ( string tokenId , int additionalSupply )
107+ {
108+ return await Bridge . InvokeRoute < TransactionResult > ( getRoute ( "mintAdditionalSupply" ) , Utils . ToJsonStringArray ( tokenId , additionalSupply , additionalSupply ) ) ;
109+ }
110+
111+ public async Task < TransactionResult > MintAdditionalSupplyTo ( string address , string tokenId , int additionalSupply )
112+ {
113+ return await Bridge . InvokeRoute < TransactionResult > ( getRoute ( "mintAdditionalSupplyTo" ) , Utils . ToJsonStringArray ( address , tokenId , additionalSupply ) ) ;
114+ }
115+
116+ /// PRIVATE
117+
118+ private string getRoute ( string functionPath ) {
119+ return this . address + ".erc1155." + functionPath ;
120+ }
121+ }
122+
123+ [ System . Serializable ]
124+ #nullable enable
125+ public class ERC1155MintPayload
126+ {
127+ public string tokenId ;
128+ public string to ;
129+ public string price ;
130+ public string currencyAddress ;
131+ public string primarySaleRecipient ;
132+ public string royaltyRecipient ;
133+ public int royaltyBps ;
134+ public int quantity ;
135+ public NFTMetadata ? metadata ;
136+ public string uid ;
137+ // TODO implement these, needs JS bridging support
138+ public long mintStartTime ;
139+ public long mintEndTime ;
140+
141+ public ERC1155MintPayload ( ) {
142+ this . tokenId = "" ; // TODO max uint256 by default
143+ this . to = Utils . AddressZero ;
144+ this . price = "0" ;
145+ this . currencyAddress = Utils . AddressZero ;
146+ this . primarySaleRecipient = Utils . AddressZero ;
147+ this . royaltyRecipient = Utils . AddressZero ;
148+ this . royaltyBps = 0 ;
149+ this . quantity = 1 ;
150+ this . metadata = null ;
151+ this . uid = Utils . ToBytes32HexString ( Guid . NewGuid ( ) . ToByteArray ( ) ) ;
152+ // TODO temporary solution
153+ this . mintStartTime = Utils . UnixTimeNowMs ( ) * 1000L ;
154+ this . mintEndTime = this . mintStartTime + 1000L * 60L * 60L * 24L * 365L ;
155+ }
156+ }
157+
158+ [ System . Serializable ]
159+ public struct ERC1155SignedPayload
160+ {
161+ public string signature ;
162+ public ERC1155MintPayload payload ;
163+ }
164+
165+ public class ERC1155Signature
166+ {
167+ public string chain ;
168+ public string address ;
169+
170+ public ERC1155Signature ( string chain , string address )
171+ {
172+ this . chain = chain ;
173+ this . address = address ;
174+ }
175+
176+ public async Task < ERC1155SignedPayload > Generate ( ERC1155MintPayload payloadToSign )
177+ {
178+ return await Bridge . InvokeRoute < ERC1155SignedPayload > ( getRoute ( "generate" ) , Utils . ToJsonStringArray ( payloadToSign ) ) ;
179+ }
180+
181+ public async Task < bool > Verify ( ERC1155SignedPayload signedPayload )
182+ {
183+ return await Bridge . InvokeRoute < bool > ( getRoute ( "verify" ) , Utils . ToJsonStringArray ( signedPayload ) ) ;
184+ }
185+
186+ public async Task < TransactionResult > Mint ( ERC1155SignedPayload signedPayload )
187+ {
188+ return await Bridge . InvokeRoute < TransactionResult > ( getRoute ( "mint" ) , Utils . ToJsonStringArray ( signedPayload ) ) ;
189+ }
190+
191+ private string getRoute ( string functionPath ) {
192+ return this . address + ".erc1155.signature." + functionPath ;
193+ }
194+ }
195+ }
0 commit comments