abi-wan-kanabi
Advanced tools
Comparing version 2.0.0 to 2.1.0-rc.0
{ | ||
"editor.defaultFormatter": "rome.rome", | ||
"editor.formatOnSave": true, | ||
"editor.formatOnSave": false, | ||
"typescript.enablePromptUseWorkspaceTsdk": true, | ||
@@ -8,3 +8,3 @@ "typescript.preferences.importModuleSpecifier": "shortest", | ||
"editor.codeActionsOnSave": { | ||
"source.organizeImports.rome": true | ||
"source.organizeImports.rome": "explicit" | ||
}, | ||
@@ -11,0 +11,0 @@ "[json]": { |
@@ -11,2 +11,4 @@ import { | ||
export { type Config } from './config' | ||
export function call< | ||
@@ -13,0 +15,0 @@ TAbi extends Abi, |
165
kanabi.ts
@@ -1,9 +0,2 @@ | ||
import { | ||
BigNumberish, | ||
CallOptions, | ||
Calldata, | ||
InvokeFunctionResponse, | ||
InvokeOptions, | ||
Uint256, | ||
} from './starknet' | ||
import { ResolvedConfig } from './config' | ||
@@ -52,9 +45,19 @@ export type CairoFelt = 'core::felt252' | ||
type CairoArrayGeneric<T extends string> = `core::array::Array::<${T}>` | ||
type CairoGeneric<T extends string> = | ||
type CairoResultGeneric< | ||
T extends string, | ||
E extends string, | ||
> = `core::result::Result::<${T}, ${E}>` | ||
type CairoGeneric<T extends string, E extends string> = | ||
| CairoOptionGeneric<T> | ||
| CairoArrayGeneric<T> | ||
| CairoResultGeneric<T, E> | ||
// Note that Option<Option<number>> = T | undefined | undefined which is the same | ||
// as Option<Option<number>, is this what we want for Option ? | ||
export type Option<T> = T | undefined | ||
export type Option<T> = ResolvedConfig<T>['Option'] | ||
export type Tuple = ResolvedConfig['Tuple'] | ||
export type Result<T, E> = ResolvedConfig<any, T, E>['Result'] | ||
export type Enum = ResolvedConfig['Enum'] | ||
export type Calldata = ResolvedConfig['Calldata'] | ||
export type InvokeOptions = ResolvedConfig['InvokeOptions'] | ||
export type CallOptions = ResolvedConfig['CallOptions'] | ||
export type InvokeFunctionResponse = ResolvedConfig['InvokeFunctionResponse'] | ||
@@ -98,2 +101,10 @@ type AbiParameter = { | ||
type AbiEventKind = 'nested' | 'data' | 'key' | ||
export type AbiEventMember = { | ||
name: string | ||
type: string | ||
kind: AbiEventKind | ||
} | ||
type AbiEventStruct = { | ||
@@ -103,3 +114,3 @@ type: 'event' | ||
kind: 'struct' | ||
members: readonly AbiParameter[] | ||
members: readonly AbiEventMember[] | ||
} | ||
@@ -111,3 +122,3 @@ | ||
kind: 'enum' | ||
variants: readonly AbiParameter[] | ||
variants: readonly AbiEventMember[] | ||
} | ||
@@ -228,17 +239,30 @@ | ||
export type ExtractAbiEvents<TAbi extends Abi> = Extract< | ||
TAbi[number], | ||
{ type: 'event' } | ||
> | ||
export type ExtractAbiEventNames<TAbi extends Abi> = | ||
ExtractAbiEvents<TAbi>['name'] | ||
export type ExtractAbiEvent< | ||
TAbi extends Abi, | ||
TEventName extends ExtractAbiEventNames<TAbi>, | ||
> = Extract<ExtractAbiEvents<TAbi>, { name: TEventName }> | ||
// Question: why do we need TAbi extends Abi here, it's not used ? | ||
type PrimitiveTypeLookup<TAbi extends Abi> = { | ||
[_ in CairoFelt]: BigNumberish | ||
[_ in CairoFelt]: ResolvedConfig['FeltType'] | ||
} & { | ||
[_ in CairoFunction]: number | ||
} & { | ||
[_ in CairoInt]: number | bigint | ||
[_ in CairoInt]: ResolvedConfig['IntType'] | ||
} & { | ||
[_ in CairoU256]: number | bigint | Uint256 | ||
[_ in CairoU256]: ResolvedConfig['U256Type'] | ||
} & { | ||
[_ in CairoBigInt]: number | bigint | ||
[_ in CairoBigInt]: ResolvedConfig['BigIntType'] | ||
} & { | ||
[_ in CairoAddress]: string | ||
[_ in CairoAddress]: ResolvedConfig['AddressType'] | ||
} & { | ||
[_ in CairoClassHash]: string | ||
[_ in CairoClassHash]: ResolvedConfig['ClassHashType'] | ||
} & { | ||
@@ -266,2 +290,8 @@ [_ in CairoVoid]: void | ||
: StringToPrimitiveType<TAbi, T>[] | ||
: G extends CairoResultGeneric<infer T, infer E> | ||
? T extends AbiType | ||
? E extends AbiType | ||
? Result<AbiTypeToPrimitiveType<TAbi, T>, AbiTypeToPrimitiveType<TAbi, E>> | ||
: Result<AbiTypeToPrimitiveType<TAbi, T>, StringToPrimitiveType<TAbi, E>> | ||
: Result<StringToPrimitiveType<TAbi, T>, StringToPrimitiveType<TAbi, E>> | ||
: unknown | ||
@@ -287,2 +317,34 @@ | ||
export type EventToPrimitiveType< | ||
TAbi extends Abi, | ||
TEventName extends ExtractAbiEventNames<TAbi>, | ||
> = ExtractAbiEvent<TAbi, TEventName> extends { | ||
type: 'event' | ||
kind: 'struct' | ||
members: infer TMembers extends readonly AbiEventMember[] | ||
} | ||
? { | ||
[Member in TMembers[number] as Member['name']]: StringToPrimitiveType< | ||
TAbi, | ||
Member['type'] | ||
> | ||
} | ||
: ExtractAbiEvent<TAbi, TEventName> extends { | ||
type: 'event' | ||
kind: 'enum' | ||
variants: infer TVariants extends readonly AbiEventMember[] | ||
} | ||
? ObjectToUnion<{ | ||
[Variant in TVariants[number] as Variant['name']]: StringToPrimitiveType< | ||
TAbi, | ||
Variant['type'] | ||
> | ||
}> | ||
: never | ||
export type StringToPrimitiveTypeS< | ||
TAbi extends Abi, | ||
T extends string, | ||
> = ExtractAbiEnum<TAbi, T> | ||
export type StringToPrimitiveType< | ||
@@ -293,23 +355,27 @@ TAbi extends Abi, | ||
? AbiTypeToPrimitiveType<TAbi, T> | ||
: T extends CairoGeneric<infer _> | ||
: T extends CairoGeneric<infer _, infer _> | ||
? GenericTypeToPrimitiveType<TAbi, T> | ||
: T extends CairoTuple | ||
? CairoTupleToPrimitive<TAbi, T> | ||
? Tuple extends never | ||
? CairoTupleToPrimitive<TAbi, T> | ||
: Tuple | ||
: ExtractAbiStruct<TAbi, T> extends never | ||
? ExtractAbiEnum<TAbi, T> extends never | ||
? unknown | ||
: ExtractAbiEnum<TAbi, T> extends { | ||
: Enum extends never | ||
? ExtractAbiEnum<TAbi, T> extends { | ||
type: 'enum' | ||
variants: infer TVariants extends readonly AbiParameter[] | ||
} | ||
? ObjectToUnion<{ | ||
[Variant in | ||
TVariants[number] as Variant['name']]: StringToPrimitiveType< | ||
TAbi, | ||
Variant['type'] | ||
> | ||
}> | ||
: // We should never have a type T where ExtractAbiEnum<TAbi, T> | ||
// return something different than an enum | ||
never | ||
? ObjectToUnion<{ | ||
[Variant in | ||
TVariants[number] as Variant['name']]: StringToPrimitiveType< | ||
TAbi, | ||
Variant['type'] | ||
> | ||
}> | ||
: // We should never have a type T where ExtractAbiEnum<TAbi, T> | ||
// return something different than an enum | ||
never | ||
: Enum | ||
: ExtractAbiStruct<TAbi, T> extends { | ||
@@ -372,23 +438,20 @@ type: 'struct' | ||
? ( | ||
options?: CallOptions, | ||
...args: ExtractArgs<TAbi, TAbiFunction> | ||
...args: [...ExtractArgs<TAbi, TAbiFunction>, CallOptions] | ||
) => Promise<FunctionRet<TAbi, TAbiFunction['name']>> | ||
: ( | ||
options?: InvokeOptions, | ||
...args: ExtractArgs<TAbi, TAbiFunction> | ||
...args: [...ExtractArgs<TAbi, TAbiFunction>, InvokeOptions] | ||
) => InvokeFunctionResponse | ||
export type ContractFunctions<TAbi extends Abi> = UnionToIntersection< | ||
{ | ||
[K in keyof TAbi]: TAbi[K] extends infer TAbiFunction extends AbiFunction | ||
? { | ||
[K2 in TAbiFunction['name']]: FunctionCallWithArgs< | ||
TAbi, | ||
TAbiFunction | ||
> & | ||
FunctionCallWithCallData<TAbi, TAbiFunction> & | ||
FunctionCallWithOptions<TAbi, TAbiFunction> | ||
} | ||
: never | ||
}[number] | ||
> | ||
export type FunctionCall< | ||
TAbi extends Abi, | ||
TAbiFunction extends AbiFunction, | ||
> = FunctionCallWithArgs<TAbi, TAbiFunction> & | ||
FunctionCallWithCallData<TAbi, TAbiFunction> & | ||
FunctionCallWithOptions<TAbi, TAbiFunction> | ||
export type ContractFunctions<TAbi extends Abi> = { | ||
[K in ExtractAbiFunctionNames<TAbi>]: FunctionCall< | ||
TAbi, | ||
ExtractAbiFunction<TAbi, K> | ||
> | ||
} |
{ | ||
"name": "abi-wan-kanabi", | ||
"version": "2.0.0", | ||
"version": "2.1.0-rc.0", | ||
"description": "Abi parser for Cairo smart contracts, based on wagmi abitype", | ||
@@ -20,3 +20,6 @@ "main": "index.js", | ||
}, | ||
"keywords": ["abi", "cairo"], | ||
"keywords": [ | ||
"abi", | ||
"cairo" | ||
], | ||
"author": "", | ||
@@ -23,0 +26,0 @@ "license": "ISC", |
@@ -58,3 +58,3 @@ # ABI-WAN-KANABI | ||
To use abiwan, you must first generate types from your contracts' ABI json files, you can use the the helper script: | ||
To use abiwan, you must first generate types from your contracts' ABI json files, you can use the helper script: | ||
@@ -61,0 +61,0 @@ ```bash |
export const ABI = [ | ||
{ | ||
"type": "impl", | ||
"name": "ExampleContract", | ||
"interface_name": "example::IExampleContract" | ||
type: 'impl', | ||
name: 'ExampleContract', | ||
interface_name: 'example::IExampleContract', | ||
}, | ||
{ | ||
"type": "enum", | ||
"name": "core::bool", | ||
"variants": [ | ||
type: 'enum', | ||
name: 'core::bool', | ||
variants: [ | ||
{ | ||
"name": "False", | ||
"type": "()" | ||
name: 'False', | ||
type: '()', | ||
}, | ||
{ | ||
"name": "True", | ||
"type": "()" | ||
} | ||
] | ||
name: 'True', | ||
type: '()', | ||
}, | ||
], | ||
}, | ||
{ | ||
"type": "enum", | ||
"name": "core::option::Option::<core::integer::u8>", | ||
"variants": [ | ||
type: 'enum', | ||
name: 'core::option::Option::<core::integer::u8>', | ||
variants: [ | ||
{ | ||
"name": "Some", | ||
"type": "core::integer::u8" | ||
name: 'Some', | ||
type: 'core::integer::u8', | ||
}, | ||
{ | ||
"name": "None", | ||
"type": "()" | ||
} | ||
] | ||
name: 'None', | ||
type: '()', | ||
}, | ||
], | ||
}, | ||
{ | ||
"type": "enum", | ||
"name": "core::option::Option::<core::option::Option::<core::integer::u8>>", | ||
"variants": [ | ||
type: 'enum', | ||
name: 'core::option::Option::<core::option::Option::<core::integer::u8>>', | ||
variants: [ | ||
{ | ||
"name": "Some", | ||
"type": "core::option::Option::<core::integer::u8>" | ||
name: 'Some', | ||
type: 'core::option::Option::<core::integer::u8>', | ||
}, | ||
{ | ||
"name": "None", | ||
"type": "()" | ||
} | ||
] | ||
name: 'None', | ||
type: '()', | ||
}, | ||
], | ||
}, | ||
{ | ||
"type": "struct", | ||
"name": "example::TestStruct", | ||
"members": [ | ||
type: 'struct', | ||
name: 'example::TestStruct', | ||
members: [ | ||
{ | ||
"name": "int128", | ||
"type": "core::integer::u128" | ||
name: 'int128', | ||
type: 'core::integer::u128', | ||
}, | ||
{ | ||
"name": "felt", | ||
"type": "core::felt252" | ||
name: 'felt', | ||
type: 'core::felt252', | ||
}, | ||
{ | ||
"name": "tuple", | ||
"type": "(core::integer::u32, core::integer::u32)" | ||
} | ||
] | ||
name: 'tuple', | ||
type: '(core::integer::u32, core::integer::u32)', | ||
}, | ||
], | ||
}, | ||
{ | ||
"type": "enum", | ||
"name": "example::TestEnum", | ||
"variants": [ | ||
type: 'enum', | ||
name: 'example::TestEnum', | ||
variants: [ | ||
{ | ||
"name": "int128", | ||
"type": "core::integer::u128" | ||
name: 'int128', | ||
type: 'core::integer::u128', | ||
}, | ||
{ | ||
"name": "felt", | ||
"type": "core::felt252" | ||
name: 'felt', | ||
type: 'core::felt252', | ||
}, | ||
{ | ||
"name": "tuple", | ||
"type": "(core::integer::u32, core::integer::u32)" | ||
} | ||
] | ||
name: 'tuple', | ||
type: '(core::integer::u32, core::integer::u32)', | ||
}, | ||
{ | ||
name: 'novalue', | ||
type: '()', | ||
}, | ||
], | ||
}, | ||
{ | ||
"type": "interface", | ||
"name": "example::IExampleContract", | ||
"items": [ | ||
type: 'enum', | ||
name: 'core::result::Result::<core::integer::u8, core::integer::u8>', | ||
variants: [ | ||
{ | ||
"type": "function", | ||
"name": "fn_felt", | ||
"inputs": [ | ||
name: 'Ok', | ||
type: 'core::integer::u8', | ||
}, | ||
{ | ||
name: 'Err', | ||
type: 'core::integer::u8', | ||
}, | ||
], | ||
}, | ||
{ | ||
type: 'enum', | ||
name: 'core::result::Result::<core::option::Option::<core::integer::u8>, core::integer::u8>', | ||
variants: [ | ||
{ | ||
name: 'Ok', | ||
type: 'core::option::Option::<core::integer::u8>', | ||
}, | ||
{ | ||
name: 'Err', | ||
type: 'core::integer::u8', | ||
}, | ||
], | ||
}, | ||
{ | ||
type: 'interface', | ||
name: 'example::IExampleContract', | ||
items: [ | ||
{ | ||
type: 'function', | ||
name: 'fn_felt', | ||
inputs: [ | ||
{ | ||
"name": "felt", | ||
"type": "core::felt252" | ||
} | ||
name: 'felt', | ||
type: 'core::felt252', | ||
}, | ||
], | ||
"outputs": [], | ||
"state_mutability": "view" | ||
outputs: [], | ||
state_mutability: 'view', | ||
}, | ||
{ | ||
"type": "function", | ||
"name": "fn_felt_u8_bool", | ||
"inputs": [ | ||
type: 'function', | ||
name: 'fn_felt_u8_bool', | ||
inputs: [ | ||
{ | ||
"name": "felt", | ||
"type": "core::felt252" | ||
name: 'felt', | ||
type: 'core::felt252', | ||
}, | ||
{ | ||
"name": "int8", | ||
"type": "core::integer::u8" | ||
name: 'int8', | ||
type: 'core::integer::u8', | ||
}, | ||
{ | ||
"name": "b", | ||
"type": "core::bool" | ||
} | ||
name: 'b', | ||
type: 'core::bool', | ||
}, | ||
], | ||
"outputs": [], | ||
"state_mutability": "view" | ||
outputs: [], | ||
state_mutability: 'view', | ||
}, | ||
{ | ||
"type": "function", | ||
"name": "fn_felt_u8_bool_out_address_felt_u8_bool", | ||
"inputs": [ | ||
type: 'function', | ||
name: 'fn_felt_u8_bool_out_address_felt_u8_bool', | ||
inputs: [ | ||
{ | ||
"name": "felt", | ||
"type": "core::felt252" | ||
name: 'felt', | ||
type: 'core::felt252', | ||
}, | ||
{ | ||
"name": "int8", | ||
"type": "core::integer::u8" | ||
name: 'int8', | ||
type: 'core::integer::u8', | ||
}, | ||
{ | ||
"name": "boolean", | ||
"type": "core::bool" | ||
} | ||
name: 'boolean', | ||
type: 'core::bool', | ||
}, | ||
], | ||
"outputs": [ | ||
outputs: [ | ||
{ | ||
"type": "(core::starknet::contract_address::ContractAddress, core::felt252, core::integer::u8, core::bool)" | ||
} | ||
type: '(core::starknet::contract_address::ContractAddress, core::felt252, core::integer::u8, core::bool)', | ||
}, | ||
], | ||
"state_mutability": "view" | ||
state_mutability: 'view', | ||
}, | ||
{ | ||
"type": "function", | ||
"name": "fn_felt_out_felt", | ||
"inputs": [ | ||
type: 'function', | ||
name: 'fn_felt_out_felt', | ||
inputs: [ | ||
{ | ||
"name": "felt", | ||
"type": "core::felt252" | ||
} | ||
name: 'felt', | ||
type: 'core::felt252', | ||
}, | ||
], | ||
"outputs": [ | ||
outputs: [ | ||
{ | ||
"type": "core::felt252" | ||
} | ||
type: 'core::felt252', | ||
}, | ||
], | ||
"state_mutability": "view" | ||
state_mutability: 'view', | ||
}, | ||
{ | ||
"type": "function", | ||
"name": "fn_out_simple_option", | ||
"inputs": [], | ||
"outputs": [ | ||
type: 'function', | ||
name: 'fn_out_simple_option', | ||
inputs: [], | ||
outputs: [ | ||
{ | ||
"type": "core::option::Option::<core::integer::u8>" | ||
} | ||
type: 'core::option::Option::<core::integer::u8>', | ||
}, | ||
], | ||
"state_mutability": "view" | ||
state_mutability: 'view', | ||
}, | ||
{ | ||
"type": "function", | ||
"name": "fn_out_nested_option", | ||
"inputs": [], | ||
"outputs": [ | ||
type: 'function', | ||
name: 'fn_out_nested_option', | ||
inputs: [], | ||
outputs: [ | ||
{ | ||
"type": "core::option::Option::<core::option::Option::<core::integer::u8>>" | ||
} | ||
type: 'core::option::Option::<core::option::Option::<core::integer::u8>>', | ||
}, | ||
], | ||
"state_mutability": "view" | ||
state_mutability: 'view', | ||
}, | ||
{ | ||
"type": "function", | ||
"name": "fn_simple_array", | ||
"inputs": [ | ||
type: 'function', | ||
name: 'fn_simple_array', | ||
inputs: [ | ||
{ | ||
"name": "arg", | ||
"type": "core::array::Array::<core::integer::u8>" | ||
} | ||
name: 'arg', | ||
type: 'core::array::Array::<core::integer::u8>', | ||
}, | ||
], | ||
"outputs": [], | ||
"state_mutability": "view" | ||
outputs: [], | ||
state_mutability: 'view', | ||
}, | ||
{ | ||
"type": "function", | ||
"name": "fn_out_simple_array", | ||
"inputs": [], | ||
"outputs": [ | ||
type: 'function', | ||
name: 'fn_out_simple_array', | ||
inputs: [], | ||
outputs: [ | ||
{ | ||
"type": "core::array::Array::<core::integer::u8>" | ||
} | ||
type: 'core::array::Array::<core::integer::u8>', | ||
}, | ||
], | ||
"state_mutability": "view" | ||
state_mutability: 'view', | ||
}, | ||
{ | ||
"type": "function", | ||
"name": "fn_struct", | ||
"inputs": [ | ||
type: 'function', | ||
name: 'fn_struct', | ||
inputs: [ | ||
{ | ||
"name": "arg", | ||
"type": "example::TestStruct" | ||
} | ||
name: 'arg', | ||
type: 'example::TestStruct', | ||
}, | ||
], | ||
"outputs": [], | ||
"state_mutability": "view" | ||
outputs: [], | ||
state_mutability: 'view', | ||
}, | ||
{ | ||
"type": "function", | ||
"name": "fn_struct_array", | ||
"inputs": [ | ||
type: 'function', | ||
name: 'fn_struct_array', | ||
inputs: [ | ||
{ | ||
"name": "arg", | ||
"type": "core::array::Array::<example::TestStruct>" | ||
} | ||
name: 'arg', | ||
type: 'core::array::Array::<example::TestStruct>', | ||
}, | ||
], | ||
"outputs": [], | ||
"state_mutability": "view" | ||
outputs: [], | ||
state_mutability: 'view', | ||
}, | ||
{ | ||
"type": "function", | ||
"name": "fn_enum", | ||
"inputs": [ | ||
type: 'function', | ||
name: 'fn_enum', | ||
inputs: [ | ||
{ | ||
"name": "arg", | ||
"type": "example::TestEnum" | ||
} | ||
name: 'arg', | ||
type: 'example::TestEnum', | ||
}, | ||
], | ||
"outputs": [], | ||
"state_mutability": "view" | ||
outputs: [], | ||
state_mutability: 'view', | ||
}, | ||
{ | ||
"type": "function", | ||
"name": "fn_enum_array", | ||
"inputs": [ | ||
type: 'function', | ||
name: 'fn_enum_array', | ||
inputs: [ | ||
{ | ||
"name": "arg", | ||
"type": "core::array::Array::<example::TestEnum>" | ||
} | ||
name: 'arg', | ||
type: 'core::array::Array::<example::TestEnum>', | ||
}, | ||
], | ||
"outputs": [], | ||
"state_mutability": "view" | ||
outputs: [], | ||
state_mutability: 'view', | ||
}, | ||
{ | ||
"type": "function", | ||
"name": "fn_out_enum_array", | ||
"inputs": [], | ||
"outputs": [ | ||
type: 'function', | ||
name: 'fn_out_enum_array', | ||
inputs: [], | ||
outputs: [ | ||
{ | ||
"type": "core::array::Array::<example::TestEnum>" | ||
} | ||
type: 'core::array::Array::<example::TestEnum>', | ||
}, | ||
], | ||
"state_mutability": "view" | ||
} | ||
] | ||
state_mutability: 'view', | ||
}, | ||
{ | ||
type: 'function', | ||
name: 'fn_result', | ||
inputs: [ | ||
{ | ||
name: 'arg', | ||
type: 'core::result::Result::<core::integer::u8, core::integer::u8>', | ||
}, | ||
], | ||
outputs: [], | ||
state_mutability: 'view', | ||
}, | ||
{ | ||
type: 'function', | ||
name: 'fn_out_result', | ||
inputs: [], | ||
outputs: [ | ||
{ | ||
type: 'core::result::Result::<core::integer::u8, core::integer::u8>', | ||
}, | ||
], | ||
state_mutability: 'view', | ||
}, | ||
{ | ||
type: 'function', | ||
name: 'fn_nested_result', | ||
inputs: [ | ||
{ | ||
name: 'arg', | ||
type: 'core::result::Result::<core::option::Option::<core::integer::u8>, core::integer::u8>', | ||
}, | ||
], | ||
outputs: [], | ||
state_mutability: 'view', | ||
}, | ||
], | ||
}, | ||
{ | ||
"type": "event", | ||
"name": "example::example_contract::Event", | ||
"kind": "enum", | ||
"variants": [] | ||
} | ||
] as const; | ||
type: 'event', | ||
name: 'example::example_contract::CounterIncreased', | ||
kind: 'struct', | ||
members: [ | ||
{ | ||
name: 'amount', | ||
type: 'core::integer::u128', | ||
kind: 'data', | ||
}, | ||
], | ||
}, | ||
{ | ||
type: 'event', | ||
name: 'example::example_contract::CounterDecreased', | ||
kind: 'struct', | ||
members: [ | ||
{ | ||
name: 'amount', | ||
type: 'core::integer::u128', | ||
kind: 'data', | ||
}, | ||
], | ||
}, | ||
{ | ||
type: 'event', | ||
name: 'example::example_contract::MyStruct', | ||
kind: 'struct', | ||
members: [ | ||
{ | ||
name: 'member', | ||
type: 'core::integer::u128', | ||
kind: 'data', | ||
}, | ||
], | ||
}, | ||
{ | ||
type: 'event', | ||
name: 'example::example_contract::MyEnum', | ||
kind: 'enum', | ||
variants: [ | ||
{ | ||
name: 'Var1', | ||
type: 'example::example_contract::MyStruct', | ||
kind: 'nested', | ||
}, | ||
], | ||
}, | ||
{ | ||
type: 'event', | ||
name: 'example::example_contract::Event', | ||
kind: 'enum', | ||
variants: [ | ||
{ | ||
name: 'TestCounterIncreased', | ||
type: 'example::example_contract::CounterIncreased', | ||
kind: 'nested', | ||
}, | ||
{ | ||
name: 'TestCounterDecreased', | ||
type: 'example::example_contract::CounterDecreased', | ||
kind: 'nested', | ||
}, | ||
{ | ||
name: 'TestEnum', | ||
type: 'example::example_contract::MyEnum', | ||
kind: 'nested', | ||
}, | ||
], | ||
}, | ||
] as const |
@@ -0,1 +1,2 @@ | ||
import { TypedContract } from '..' | ||
import { | ||
@@ -11,2 +12,3 @@ AbiTypeToPrimitiveType, | ||
CairoVoid, | ||
EventToPrimitiveType, | ||
ExtractAbiFunction, | ||
@@ -110,2 +112,9 @@ ExtractAbiFunctionNames, | ||
]) | ||
assertType<FunctionArgs<TAbi, 'fn_result'>>({Ok: bigIntValue}) | ||
assertType<FunctionArgs<TAbi, 'fn_result'>>({Err: intValue}) | ||
assertType<FunctionArgs<TAbi, 'fn_nested_result'>>({Ok: intValue}) | ||
assertType<FunctionArgs<TAbi, 'fn_nested_result'>>({Ok: undefined}) | ||
assertType<FunctionArgs<TAbi, 'fn_nested_result'>>({Err: bigIntValue}) | ||
}) | ||
@@ -144,2 +153,10 @@ | ||
assertType<FunctionArgs<TAbi, 'fn_out_enum_array'>>([intValue]) | ||
// @ts-expect-error | ||
assertType<FunctionArgs<TAbi, 'fn_result'>>({Ok: voidValue}) | ||
// @ts-expect-error | ||
assertType<FunctionArgs<TAbi, 'fn_result'>>({Err: boolValue}) | ||
// @ts-expect-error | ||
assertType<FunctionArgs<TAbi, 'fn_nested_result'>>({Ok: emptyArray}) | ||
// @ts-expect-error | ||
assertType<FunctionArgs<TAbi, 'fn_nested_result'>>({Err: stringValue}) | ||
}) | ||
@@ -170,2 +187,5 @@ | ||
]) | ||
assertType<FunctionRet<TAbi, 'fn_out_result'>>({Ok: intValue}) | ||
assertType<FunctionRet<TAbi, 'fn_out_result'>>({Err: bigIntValue}) | ||
}) | ||
@@ -197,2 +217,6 @@ | ||
assertType<FunctionRet<TAbi, 'fn_out_simple_option'>>(voidValue) | ||
// @ts-expect-error | ||
assertType<FunctionRet<TAbi, 'fn_out_result'>>({Ok: stringValue}) | ||
// @ts-expect-error | ||
assertType<FunctionRet<TAbi, 'fn_out_result'>>({Err: emptyArray}) | ||
}) | ||
@@ -235,2 +259,5 @@ | ||
| 'fn_out_enum_array' | ||
| 'fn_result' | ||
| 'fn_out_result' | ||
| 'fn_nested_result' | ||
@@ -299,1 +326,50 @@ expectTypeOf<ExtractAbiFunctionNames<TAbi>>().toEqualTypeOf<Expected>() | ||
}) | ||
test('ContractFunctions', () => { | ||
// @ts-expect-error | ||
const contract: TypedContract<TAbi> = never | ||
contract.fn_felt(1) // Call with args | ||
contract.fn_felt(1, { parseRequest: true }) // Call withe invokeOptions | ||
contract.fn_felt(['0x0', '0x1']) // call with CallData | ||
// @ts-expect-error fn_felt argument should be string | number | bigint | ||
contract.fn_felt(true) | ||
contract.fn_out_simple_option() | ||
contract.fn_out_simple_option({ parseResponse: true }) | ||
contract.fn_out_simple_option(['0x0', '0x1']) | ||
}) | ||
test('StringToPrimitiveTypeEvent', () => { | ||
// TODO: add tests for struct, enum and tuple | ||
// Accept everything (unknown) for wrong types, this is done intentionally to | ||
// avoid rejecting types when abiwan make a mistake, we'll probably alter this | ||
// behavior when it gets mature enough | ||
assertType<StringToPrimitiveType<TAbi, 'wrong_type_name'>>({ | ||
TestCounterIncreased: { | ||
amount: 1, | ||
}, | ||
}) | ||
assertType<StringToPrimitiveType<TAbi, 'example::example_contract::Event'>>({ | ||
TestCounterIncreased: { | ||
amount: 1, | ||
}, | ||
}) | ||
assertType<StringToPrimitiveType<TAbi, 'example::example_contract::Event'>>({ | ||
TestCounterDecreased: { | ||
amount: 1, | ||
}, | ||
}) | ||
assertType<StringToPrimitiveType<TAbi, 'example::example_contract::Event'>>({ | ||
TestEnum: { | ||
Var1: { | ||
member: 1, | ||
}, | ||
}, | ||
}) | ||
// @ts-expect-error | ||
assertType<EventToPrimitiveType<TAbi, 'wrong_event'>>() | ||
}) |
{ | ||
"compilerOptions": { | ||
/* Visit https://aka.ms/tsconfig to read more about this file */ | ||
/* Projects */ | ||
@@ -12,6 +11,8 @@ // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ | ||
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ | ||
/* Language and Environment */ | ||
"target": "ES2020", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ | ||
"lib": ["ES2020", "ESNext"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ | ||
"target": "ES2020", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ | ||
"lib": [ | ||
"ES2020", | ||
"ESNext" | ||
], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ | ||
// "jsx": "preserve", /* Specify what JSX code is generated. */ | ||
@@ -27,5 +28,4 @@ // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ | ||
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ | ||
/* Modules */ | ||
"module": "commonjs", /* Specify what module code is generated. */ | ||
"module": "commonjs", /* Specify what module code is generated. */ | ||
// "rootDir": "./", /* Specify the root folder within your source files. */ | ||
@@ -42,3 +42,2 @@ // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ | ||
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */ | ||
/* JavaScript Support */ | ||
@@ -48,3 +47,2 @@ // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ | ||
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ | ||
/* Emit */ | ||
@@ -74,12 +72,10 @@ // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ | ||
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ | ||
/* Interop Constraints */ | ||
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ | ||
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ | ||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ | ||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ | ||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ | ||
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ | ||
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ | ||
/* Type Checking */ | ||
"strict": true, /* Enable all strict type-checking options. */ | ||
"strict": true, /* Enable all strict type-checking options. */ | ||
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ | ||
@@ -103,10 +99,9 @@ // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ | ||
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ | ||
/* Completeness */ | ||
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ | ||
"skipLibCheck": true, /* Skip type checking all .d.ts files. */ | ||
"skipLibCheck": true, /* Skip type checking all .d.ts files. */ | ||
"resolveJsonModule": true, | ||
"esModuleInterop": true, | ||
"noErrorTruncation": true, | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
116989
40
2265
0
1