3
3
4
4
import { useNavigate } from 'react-router' ;
5
5
import 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' ;
21
7
22
8
export function useNewContract ( ) {
23
9
const { db } = useDatabase ( ) ;
24
10
const navigate = useNavigate ( ) ;
25
11
const instantiate = useInstantiate ( ) ;
26
- const { api } = useApi ( ) ;
27
- const { version } = useVersion ( ) ;
28
12
29
13
const {
30
14
data : { accountId, name } ,
31
15
} = instantiate ;
32
16
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 > {
47
18
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
- }
67
19
const codeHash = contract . abi . info . source . wasmHash . toHex ( ) ;
68
20
const document = {
69
21
abi : contract . abi . json ,
70
- address : calculatedAddress ! ,
22
+ address : contract . address . toString ( ) ,
71
23
codeHash,
72
24
date : new Date ( ) . toISOString ( ) ,
73
25
name,
@@ -82,7 +34,7 @@ export function useNewContract() {
82
34
} ) ,
83
35
] ) ;
84
36
85
- navigate ( `/contract/${ document . address } ` ) ;
37
+ navigate ( `/contract/${ contract . address } ` ) ;
86
38
}
87
39
} ;
88
40
}
0 commit comments