@@ -18,6 +18,7 @@ export abstract class EvmNftProvider extends Nft<BaseProvider, Signer> {
1818
1919 protected schemas : Record < string , NftContract > ;
2020 protected cache : Record < string , NftInfo > ;
21+ protected walletProvider : EvmBaseWalletProvider < BaseProvider > ;
2122
2223 constructor ( walletProvider : EvmBaseWalletProvider < BaseProvider > ) {
2324 super ( walletProvider ) ;
@@ -36,37 +37,7 @@ export abstract class EvmNftProvider extends Nft<BaseProvider, Signer> {
3637 data = '0x' ,
3738 fee ?: FeeType
3839 ) : Promise < Transaction < EthersTransactionResponse > > {
39- const { schema, contract } = await this . _cacheGet ( contractAddress ) ;
40- const owner = ( await this . walletProvider . getAddress ( ) ) . toString ( ) ;
41- const to = receiver . toString ( ) ;
42-
43- let tx : EthersPopulatedTransaction ;
44-
45- switch ( schema ) {
46- case NftTypes . ERC721 : {
47- if ( tokenIDs . length !== 1 ) {
48- throw new Error ( `nft.transfer supports exactly 1 tokenID transfer for ERC721. received ${ tokenIDs . join ( ', ' ) } ` ) ;
49- }
50- const _contract : ERC721 = contract as ERC721 ;
51- tx = await _contract . populateTransaction [ 'safeTransferFrom(address,address,uint256,bytes)' ] ( owner , to , tokenIDs [ 0 ] , data ) ;
52- break ;
53- }
54-
55- case NftTypes . ERC1155 : {
56- const _contract : ERC1155 = contract as ERC1155 ;
57- if ( tokenIDs . length > 1 ) {
58- tx = await _contract . populateTransaction . safeBatchTransferFrom ( owner , to , tokenIDs , amounts , data ) ;
59- } else {
60- tx = await _contract . populateTransaction . safeTransferFrom ( owner , to , tokenIDs [ 0 ] , amounts [ 0 ] , data ) ;
61- }
62- break ;
63- }
64-
65- default : {
66- throw new UnsupportedMethodError ( `Unsupported NFT type: ${ schema } ` ) ;
67- }
68- }
69-
40+ const tx = await this . populateTrasnfer ( contractAddress , receiver , tokenIDs , amounts , data ) ;
7041 return await this . walletProvider . sendTransaction ( toEthereumTxRequest ( tx , fee ) ) ;
7142 }
7243
@@ -103,29 +74,17 @@ export abstract class EvmNftProvider extends Nft<BaseProvider, Signer> {
10374 tokenID : number ,
10475 fee ?: FeeType
10576 ) : Promise < Transaction < EthersTransactionResponse > > {
106- const { schema, contract } = await this . _cacheGet ( contractAddress ) ;
107- const _operator = operator . toString ( ) ;
108-
109- let tx : EthersPopulatedTransaction ;
110-
111- switch ( schema ) {
112- case NftTypes . ERC721 : {
113- const _contract : ERC721 = contract as ERC721 ;
114- tx = await _contract . populateTransaction . approve ( _operator , tokenID ) ;
115- break ;
116- }
117-
118- case NftTypes . ERC1155 : {
119- const _contract : ERC1155 = contract as ERC1155 ;
120- tx = await _contract . populateTransaction . setApprovalForAll ( _operator , true ) ;
121- break ;
122- }
123-
124- default : {
125- throw new UnsupportedMethodError ( `Unsupported NFT type: ${ schema } ` ) ;
126- }
127- }
77+ const tx = await this . populateApprove ( contractAddress , operator , tokenID ) ;
78+ return this . walletProvider . sendTransaction ( toEthereumTxRequest ( tx , fee ) ) ;
79+ }
12880
81+ public async approveAll (
82+ contractAddress : AddressType ,
83+ operator : AddressType ,
84+ state : boolean ,
85+ fee ?: FeeType
86+ ) : Promise < Transaction < EthersTransactionResponse > > {
87+ const tx = await this . populateApproveAll ( contractAddress , operator , state ) ;
12988 return this . walletProvider . sendTransaction ( toEthereumTxRequest ( tx , fee ) ) ;
13089 }
13190
@@ -135,15 +94,28 @@ export abstract class EvmNftProvider extends Nft<BaseProvider, Signer> {
13594 return await contract . isApprovedForAll ( owner . toString ( ) , operator . toString ( ) ) ;
13695 }
13796
138- public async approveAll (
97+ public async estimateTransfer (
13998 contractAddress : AddressType ,
140- operator : AddressType ,
141- state : boolean ,
142- fee ?: FeeType
143- ) : Promise < Transaction < EthersTransactionResponse > > {
144- const { contract } = await this . _cacheGet ( contractAddress ) ;
145- const tx = await contract . populateTransaction . setApprovalForAll ( operator . toString ( ) , state ) ;
146- return this . walletProvider . sendTransaction ( toEthereumTxRequest ( tx , fee ) ) ;
99+ receiver : AddressType ,
100+ tokenIDs : string [ ] ,
101+ amounts ?: number [ ] ,
102+ data = '0x'
103+ ) : Promise < BigNumber > {
104+ const tx = await this . populateTrasnfer ( contractAddress , receiver , tokenIDs , amounts , data ) ;
105+ const estimation = await this . walletProvider . estimateGas ( tx ) ;
106+ return new BigNumber ( estimation . toString ( ) ) ;
107+ }
108+
109+ public async estimateApprove ( contractAddress : AddressType , operator : AddressType , tokenID : number ) : Promise < BigNumber > {
110+ const tx = await this . populateApprove ( contractAddress , operator , tokenID ) ;
111+ const estimation = await this . walletProvider . estimateGas ( tx ) ;
112+ return new BigNumber ( estimation . toString ( ) ) ;
113+ }
114+
115+ public async estimateApproveAll ( contractAddress : AddressType , operator : AddressType , state : boolean ) : Promise < BigNumber > {
116+ const tx = await this . populateApproveAll ( contractAddress , operator , state ) ;
117+ const estimation = await this . walletProvider . estimateGas ( tx ) ;
118+ return new BigNumber ( estimation . toString ( ) ) ;
147119 }
148120
149121 async fetch ( ) : Promise < NFTAsset [ ] > {
@@ -181,4 +153,87 @@ export abstract class EvmNftProvider extends Nft<BaseProvider, Signer> {
181153
182154 throw new UnsupportedMethodError ( `Cannot find the data for ${ _contractAddress } ` ) ;
183155 }
156+
157+ private async populateApprove (
158+ contractAddress : AddressType ,
159+ operator : AddressType ,
160+ tokenID : number
161+ ) : Promise < EthersPopulatedTransaction > {
162+ const { schema, contract } = await this . _cacheGet ( contractAddress ) ;
163+ const _operator = operator . toString ( ) ;
164+
165+ let tx : EthersPopulatedTransaction ;
166+
167+ switch ( schema ) {
168+ case NftTypes . ERC721 : {
169+ const _contract : ERC721 = contract as ERC721 ;
170+ tx = await _contract . populateTransaction . approve ( _operator , tokenID ) ;
171+ break ;
172+ }
173+
174+ case NftTypes . ERC1155 : {
175+ const _contract : ERC1155 = contract as ERC1155 ;
176+ tx = await _contract . populateTransaction . setApprovalForAll ( _operator , true ) ;
177+ break ;
178+ }
179+
180+ default : {
181+ throw new UnsupportedMethodError ( `Unsupported NFT type: ${ schema } ` ) ;
182+ }
183+ }
184+
185+ return tx ;
186+ }
187+
188+ private async populateTrasnfer (
189+ contractAddress : AddressType ,
190+ receiver : AddressType ,
191+ tokenIDs : string [ ] ,
192+ amounts ?: number [ ] ,
193+ data = '0x'
194+ ) : Promise < EthersPopulatedTransaction > {
195+ const { schema, contract } = await this . _cacheGet ( contractAddress ) ;
196+ const owner = ( await this . walletProvider . getAddress ( ) ) . toString ( ) ;
197+ const to = receiver . toString ( ) ;
198+
199+ let tx : EthersPopulatedTransaction ;
200+
201+ switch ( schema ) {
202+ case NftTypes . ERC721 : {
203+ if ( tokenIDs . length !== 1 ) {
204+ throw new Error ( `nft.transfer supports exactly 1 tokenID transfer for ERC721. received ${ tokenIDs . join ( ', ' ) } ` ) ;
205+ }
206+ const _contract : ERC721 = contract as ERC721 ;
207+ tx = await _contract . populateTransaction [ 'safeTransferFrom(address,address,uint256,bytes)' ] ( owner , to , tokenIDs [ 0 ] , data ) ;
208+ break ;
209+ }
210+
211+ case NftTypes . ERC1155 : {
212+ const _contract : ERC1155 = contract as ERC1155 ;
213+ if ( tokenIDs . length > 1 ) {
214+ tx = await _contract . populateTransaction . safeBatchTransferFrom ( owner , to , tokenIDs , amounts , data ) ;
215+ } else {
216+ tx = await _contract . populateTransaction . safeTransferFrom ( owner , to , tokenIDs [ 0 ] , amounts [ 0 ] , data ) ;
217+ }
218+ break ;
219+ }
220+
221+ default : {
222+ throw new UnsupportedMethodError ( `Unsupported NFT type: ${ schema } ` ) ;
223+ }
224+ }
225+
226+ return tx ;
227+ }
228+
229+ private async populateApproveAll (
230+ contractAddress : AddressType ,
231+ operator : AddressType ,
232+ state : boolean
233+ ) : Promise < EthersPopulatedTransaction > {
234+ const { contract } = await this . _cacheGet ( contractAddress ) ;
235+ const tx = await contract . populateTransaction . setApprovalForAll ( operator . toString ( ) , state ) ;
236+
237+ return tx ;
238+ }
184239}
0 commit comments