1+ using System ;
2+ using System . Threading . Tasks ;
3+
4+ namespace Thirdweb
5+ {
6+ /// <summary>
7+ /// Interact with any <c>ERC20</c> compatible contract.
8+ /// </summary>
9+ public class ERC20
10+ {
11+ public string chain ;
12+ public string address ;
13+ public ERC20Signature signature ;
14+
15+ public ERC20 ( string chain , string address )
16+ {
17+ this . chain = chain ;
18+ this . address = address ;
19+ this . signature = new ERC20Signature ( chain , address ) ;
20+ }
21+
22+ /// READ FUNCTIONS
23+
24+ public async Task < Currency > Get ( )
25+ {
26+ return await Bridge . InvokeRoute < Currency > ( getRoute ( "get" ) , new string [ ] { } ) ;
27+ }
28+
29+ public async Task < CurrencyValue > Balance ( )
30+ {
31+ return await Bridge . InvokeRoute < CurrencyValue > ( getRoute ( "balance" ) , new string [ ] { } ) ;
32+ }
33+
34+ public async Task < CurrencyValue > BalanceOf ( string address )
35+ {
36+ return await Bridge . InvokeRoute < CurrencyValue > ( getRoute ( "balanceOf" ) , Utils . ToJsonStringArray ( address ) ) ;
37+ }
38+
39+ public async Task < string > Allowance ( string spender )
40+ {
41+ return await Bridge . InvokeRoute < string > ( getRoute ( "allowance" ) , Utils . ToJsonStringArray ( spender ) ) ;
42+ }
43+
44+ public async Task < string > AllowanceOf ( string owner , string spender )
45+ {
46+ return await Bridge . InvokeRoute < string > ( getRoute ( "allowanceOf" ) , Utils . ToJsonStringArray ( owner , spender ) ) ;
47+ }
48+
49+ public async Task < CurrencyValue > TotalSupply ( )
50+ {
51+ return await Bridge . InvokeRoute < CurrencyValue > ( getRoute ( "totalSupply" ) , new string [ ] { } ) ;
52+ }
53+
54+ /// WRITE FUNCTIONS
55+
56+ public async Task < TransactionResult > SetAllowance ( string spender , bool amount )
57+ {
58+ return await Bridge . InvokeRoute < TransactionResult > ( getRoute ( "setAllowance" ) , Utils . ToJsonStringArray ( spender , amount ) ) ;
59+ }
60+
61+ public async Task < TransactionResult > Transfer ( string to , string amount )
62+ {
63+ return await Bridge . InvokeRoute < TransactionResult > ( getRoute ( "transfer" ) , Utils . ToJsonStringArray ( to , amount ) ) ;
64+ }
65+
66+ public async Task < TransactionResult > Burn ( string amount )
67+ {
68+ return await Bridge . InvokeRoute < TransactionResult > ( getRoute ( "burn" ) , Utils . ToJsonStringArray ( amount ) ) ;
69+ }
70+
71+ public async Task < TransactionResult [ ] > Claim ( string amount )
72+ {
73+ return await Bridge . InvokeRoute < TransactionResult [ ] > ( getRoute ( "claim" ) , Utils . ToJsonStringArray ( amount ) ) ;
74+ }
75+
76+ public async Task < TransactionResult [ ] > ClaimTo ( string address , int amount )
77+ {
78+ return await Bridge . InvokeRoute < TransactionResult [ ] > ( getRoute ( "claimTo" ) , Utils . ToJsonStringArray ( address , amount ) ) ;
79+ }
80+
81+ public async Task < TransactionResult > Mint ( string amount )
82+ {
83+ return await Bridge . InvokeRoute < TransactionResult > ( getRoute ( "mint" ) , Utils . ToJsonStringArray ( amount ) ) ;
84+ }
85+
86+ public async Task < TransactionResult > MintTo ( string address , string amount )
87+ {
88+ return await Bridge . InvokeRoute < TransactionResult > ( getRoute ( "mintTo" ) , Utils . ToJsonStringArray ( address , amount ) ) ;
89+ }
90+
91+ /// PRIVATE
92+
93+ private string getRoute ( string functionPath ) {
94+ return this . address + ".erc20." + functionPath ;
95+ }
96+ }
97+
98+ [ System . Serializable ]
99+ #nullable enable
100+ public class ERC20MintPayload
101+ {
102+ public string to ;
103+ public string price ;
104+ public string currencyAddress ;
105+ public string primarySaleRecipient ;
106+ public string quantity ;
107+ public string uid ;
108+ // TODO implement these, needs JS bridging support
109+ // public long mintStartTime;
110+ // public long mintEndTime;
111+
112+ public ERC20MintPayload ( string receiverAddress , string quantity ) {
113+ this . to = receiverAddress ;
114+ this . quantity = quantity ;
115+ this . price = "0" ;
116+ this . currencyAddress = Utils . AddressZero ;
117+ this . primarySaleRecipient = Utils . AddressZero ;
118+ this . uid = Utils . ToBytes32HexString ( Guid . NewGuid ( ) . ToByteArray ( ) ) ;
119+ // TODO temporary solution
120+ // this.mintStartTime = Utils.UnixTimeNowMs() * 1000L;
121+ // this.mintEndTime = this.mintStartTime + 1000L * 60L * 60L * 24L * 365L;
122+ }
123+ }
124+
125+ [ System . Serializable ]
126+ public struct ERC20SignedPayloadOutput
127+ {
128+ public string to ;
129+ public string price ;
130+ public string currencyAddress ;
131+ public string primarySaleRecipient ;
132+ public string quantity ;
133+ public string uid ;
134+ public long mintStartTime ;
135+ public long mintEndTime ;
136+ }
137+
138+ [ System . Serializable ]
139+ public struct ERC20SignedPayload
140+ {
141+ public string signature ;
142+ public ERC20SignedPayloadOutput payload ;
143+ }
144+
145+ public class ERC20Signature
146+ {
147+ public string chain ;
148+ public string address ;
149+
150+ public ERC20Signature ( string chain , string address )
151+ {
152+ this . chain = chain ;
153+ this . address = address ;
154+ }
155+
156+ public async Task < ERC20SignedPayload > Generate ( ERC20MintPayload payloadToSign )
157+ {
158+ return await Bridge . InvokeRoute < ERC20SignedPayload > ( getRoute ( "generate" ) , Utils . ToJsonStringArray ( payloadToSign ) ) ;
159+ }
160+
161+ public async Task < bool > Verify ( ERC20SignedPayload signedPayload )
162+ {
163+ return await Bridge . InvokeRoute < bool > ( getRoute ( "verify" ) , Utils . ToJsonStringArray ( signedPayload ) ) ;
164+ }
165+
166+ public async Task < TransactionResult > Mint ( ERC20SignedPayload signedPayload )
167+ {
168+ return await Bridge . InvokeRoute < TransactionResult > ( getRoute ( "mint" ) , Utils . ToJsonStringArray ( signedPayload ) ) ;
169+ }
170+
171+ private string getRoute ( string functionPath ) {
172+ return this . address + ".erc20.signature." + functionPath ;
173+ }
174+ }
175+ }
0 commit comments