11import {
22 Contract ,
3+ ContractMethodsBase ,
34 Encoded ,
45 Tag ,
56} from '@aeternity/aepp-sdk' ;
@@ -11,25 +12,27 @@ import { useAccounts } from '@/composables/accounts';
1112import { encrypt , decrypt , importAesKeyFromSecret } from '@/utils/crypto' ;
1213import type { IAccount } from '@/types' ;
1314
14- export type SuperheroIdsContract = Contract < {
15- set_id : ( id : string ) => Promise < void > ;
16- get_id : ( ) => Promise < { decodedResult : string | undefined } > ;
17- has_id : ( ) => Promise < { decodedResult : boolean } > ;
18- } > ;
15+ interface SuperheroIdsContractMethods extends ContractMethodsBase {
16+ set_id : ( id : string ) => void ;
17+ get_id : ( ) => string | undefined ;
18+ has_id : ( ) => boolean ;
19+ }
20+
21+ export type SuperheroIdsContract = Contract < SuperheroIdsContractMethods > ;
1922
2023export class SuperheroIDService {
21- private contractId : Encoded . ContractAddress = 'ct_kaUS1K6qFXn2wP2phR26WLWXEKu5YsgA2gq4RWvkB69KY2gYF' ;
24+ private contractId = 'ct_kaUS1K6qFXn2wP2phR26WLWXEKu5YsgA2gq4RWvkB69KY2gYF' as const ;
2225
2326 private async getContractInstance ( options ?: Partial < ContractInitializeOptions > ) :
2427 Promise < SuperheroIdsContract > {
2528 const { getAeSdk } = useAeSdk ( ) ;
2629 const aeSdk = await getAeSdk ( ) ;
27- return Contract . initialize ( {
30+ return Contract . initialize < SuperheroIdsContractMethods > ( {
2831 ...aeSdk . getContext ( ) ,
2932 ...options ,
30- aci : SuperheroIdsACI as any ,
33+ aci : SuperheroIdsACI ,
3134 address : this . contractId ,
32- } ) as unknown as SuperheroIdsContract ;
35+ } ) ;
3336 }
3437
3538 private static getCallerAccount ( ) : IAccount {
@@ -45,8 +48,7 @@ export class SuperheroIDService {
4548 if ( ! account ?. secretKey ) throw new Error ( 'Missing secret key for encryption' ) ;
4649 const key = await importAesKeyFromSecret ( account . secretKey ) ;
4750 const ciphertext = await encrypt ( key , id ) ;
48- await ( contract as any )
49- . set_id ( ciphertext , { fromAccount : account . address as Encoded . AccountAddress } ) ;
51+ await contract . set_id ( ciphertext ) ;
5052 }
5153
5254 async getId ( ) : Promise < string | undefined > {
@@ -77,47 +79,46 @@ export class SuperheroIDService {
7779 if ( ! account ?. secretKey ) throw new Error ( 'Missing secret key for encryption' ) ;
7880 const key = await importAesKeyFromSecret ( account . secretKey ) ;
7981 const ciphertext = await encrypt ( key , id ) ;
80- const gasLimit = await ( contract as any ) . _estimateGas ( 'set_id' , [ ciphertext ] , {
82+ const gasLimit = await contract . _estimateGas ( 'set_id' , [ ciphertext ] , {
8183 senderId : account . address as Encoded . AccountAddress ,
8284 } ) ;
83- const callData = ( contract as any ) . _calldata . encode ( ( contract as any ) . _name , 'set_id' , [ ciphertext ] ) ;
84- const built = await aeSdk . buildTx ( {
85+ const callData = contract . _calldata . encode ( contract . _name , 'set_id' , [ ciphertext ] ) ;
86+ return aeSdk . buildTx ( {
8587 tag : Tag . ContractCallTx ,
8688 callerId : account . address as Encoded . AccountAddress ,
87- contractId : this . contractId ! ,
89+ contractId : this . contractId ,
8890 abiVersion : 3 ,
8991 amount : 0 ,
9092 callData,
9193 gasLimit,
92- } as any ) ;
93- return built ;
94+ } ) ;
9495 }
9596
9697 async deployContract ( bytecode : Encoded . ContractBytearray ) : Promise < Encoded . ContractAddress > {
9798 const { getAeSdk } = useAeSdk ( ) ;
9899 const aeSdk = await getAeSdk ( ) ;
99- const instance = await Contract . initialize ( {
100+ const instance = await Contract . initialize < SuperheroIdsContract > ( {
100101 ...aeSdk . getContext ( ) ,
101- aci : SuperheroIdsACI as any ,
102+ aci : SuperheroIdsACI ,
102103 bytecode,
103- } ) as any ;
104+ } ) ;
104105
105106 await instance . init ( ) ;
106- const address = instance . $options . address as Encoded . ContractAddress ;
107+ const { address } = instance . $options ;
107108 this . contractId = address ;
108109 return address ;
109110 }
110111
111112 async deployFromSource ( sourceCode : string ) : Promise < Encoded . ContractAddress > {
112113 const { getAeSdk } = useAeSdk ( ) ;
113114 const aeSdk = await getAeSdk ( ) ;
114- const instance = await Contract . initialize ( {
115+ const instance = await Contract . initialize < SuperheroIdsContract > ( {
115116 ...aeSdk . getContext ( ) ,
116- aci : SuperheroIdsACI as any ,
117+ aci : SuperheroIdsACI ,
117118 sourceCode,
118- } ) as any ;
119+ } ) ;
119120 await instance . init ( ) ;
120- const address = instance . $options . address as Encoded . ContractAddress ;
121+ const { address } = instance . $options ;
121122 this . contractId = address ;
122123 return address ;
123124 }
0 commit comments