33
44import { useNavigate } from 'react-router' ;
55import type { BlueprintSubmittableResult } from 'types' ;
6- import { useApi , useDatabase , useInstantiate , useVersion } from 'ui/contexts' ;
7- import { ApiTypes } from '@polkadot/api/types' ;
8- import { hexToU8a } from '@polkadot/util' ;
9- import { decodeAddress } from '@polkadot/keyring' ;
10- import { create1 , create2 , toEthAddress } from 'lib/address' ;
11-
12- interface ExtendedBlueprintSubmittableResult < T extends ApiTypes >
13- extends BlueprintSubmittableResult < T > {
14- contractData ?: {
15- salt : string ;
16- data : Uint8Array ;
17- code : string ;
18- originIsCaller ?: boolean ;
19- } ;
20- }
6+ import { useDatabase , useInstantiate } from 'ui/contexts' ;
217
228export function useNewContract ( ) {
239 const { db } = useDatabase ( ) ;
2410 const navigate = useNavigate ( ) ;
2511 const instantiate = useInstantiate ( ) ;
26- const { api } = useApi ( ) ;
27- const { version } = useVersion ( ) ;
2812
2913 const {
3014 data : { accountId, name } ,
3115 } = instantiate ;
3216
33- async function getNonce ( ) {
34- try {
35- const nonce = await api . call . accountNonceApi . accountNonce ( accountId ) ;
36- return nonce . toNumber ( ) ;
37- } catch ( error ) {
38- console . error ( 'Error fetching nonce:' , error ) ;
39- return null ;
40- }
41- }
42-
43- return async function ( {
44- contract,
45- contractData,
46- } : ExtendedBlueprintSubmittableResult < 'promise' > ) : Promise < void > {
17+ return async function ( { contract } : BlueprintSubmittableResult < 'promise' > ) : Promise < void > {
4718 if ( accountId && contract ?. abi . json ) {
48- let calculatedAddress = contract . address . toString ( ) ;
49- // Calculate the expected contract address based on the Rust logic
50- if ( version === 'v6' && contractData ) {
51- const { salt, code, data, originIsCaller = false } = contractData ;
52- const mappedAccount = toEthAddress ( decodeAddress ( accountId ) ) ;
53-
54- if ( salt ) {
55- // Use CREATE2 if salt is provided
56- calculatedAddress = create2 ( mappedAccount , hexToU8a ( code ) , data , salt ) ;
57- } else {
58- // Use CREATE1 if no salt is provided
59- const nonce = await getNonce ( ) ;
60-
61- if ( nonce !== null ) {
62- const adjustedNonce = originIsCaller ? Math . max ( 0 , nonce - 1 ) : nonce ;
63- calculatedAddress = create1 ( mappedAccount , adjustedNonce - 2 ) ;
64- }
65- }
66- }
6719 const codeHash = contract . abi . info . source . wasmHash . toHex ( ) ;
6820 const document = {
6921 abi : contract . abi . json ,
70- address : calculatedAddress ! ,
22+ address : contract . address . toString ( ) ,
7123 codeHash,
7224 date : new Date ( ) . toISOString ( ) ,
7325 name,
@@ -82,7 +34,7 @@ export function useNewContract() {
8234 } ) ,
8335 ] ) ;
8436
85- navigate ( `/contract/${ document . address } ` ) ;
37+ navigate ( `/contract/${ contract . address } ` ) ;
8638 }
8739 } ;
8840}
0 commit comments