-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #665 from 0xs34n/0.12.0/abi-parser
abi parser Cario1 Version2
- Loading branch information
Showing
16 changed files
with
9,016 additions
and
25 deletions.
There are no files selected for viewing
File renamed without changes.
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Abi } from '../../../types'; | ||
import { isCairo1Abi } from '../cairo'; | ||
import { AbiParserInterface } from './interface'; | ||
import { AbiParser1 } from './parser-0-1.1.0'; | ||
import { AbiParser2 } from './parser-2.0.0'; | ||
|
||
export function createAbiParser(abi: Abi): AbiParserInterface { | ||
const version = getAbiVersion(abi); | ||
if (version === 0 || version === 1) { | ||
return new AbiParser1(abi); | ||
} | ||
if (version === 2) { | ||
return new AbiParser2(abi); | ||
} | ||
throw Error(`Unsupported ABI version ${version}`); | ||
} | ||
|
||
export function getAbiVersion(abi: Abi) { | ||
if (abi.find((it) => it.type === 'interface')) return 2; | ||
if (isCairo1Abi(abi)) return 1; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Abi, FunctionAbi } from '../../../types'; | ||
|
||
export abstract class AbiParserInterface { | ||
/** | ||
* Helper to calculate inputs length from abi | ||
* @param abiMethod FunctionAbi | ||
* @return number | ||
*/ | ||
public abstract methodInputsLength(abiMethod: FunctionAbi): number; | ||
|
||
/** | ||
* | ||
* @param name string | ||
* @return FunctionAbi | undefined | ||
*/ | ||
public abstract getMethod(name: string): FunctionAbi | undefined; | ||
|
||
/** | ||
* Return Abi in legacy format | ||
* @return Abi | ||
*/ | ||
public abstract getLegacyFormat(): Abi; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Abi, FunctionAbi } from '../../../types'; | ||
import { isLen } from '../cairo'; | ||
import { AbiParserInterface } from './interface'; | ||
|
||
export class AbiParser1 implements AbiParserInterface { | ||
abi: Abi; | ||
|
||
constructor(abi: Abi) { | ||
this.abi = abi; | ||
} | ||
|
||
/** | ||
* abi method inputs length without '_len' inputs | ||
* cairo 0 reducer | ||
* @param abiMethod FunctionAbi | ||
* @returns number | ||
*/ | ||
public methodInputsLength(abiMethod: FunctionAbi) { | ||
return abiMethod.inputs.reduce((acc, input) => (!isLen(input.name) ? acc + 1 : acc), 0); | ||
} | ||
|
||
/** | ||
* get method definition from abi | ||
* @param name string | ||
* @returns FunctionAbi | undefined | ||
*/ | ||
public getMethod(name: string): FunctionAbi | undefined { | ||
return this.abi.find((it) => it.name === name); | ||
} | ||
|
||
/** | ||
* Get Abi in legacy format | ||
* @returns Abi | ||
*/ | ||
public getLegacyFormat() { | ||
return this.abi; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Abi, FunctionAbi } from '../../../types'; | ||
import { AbiParserInterface } from './interface'; | ||
|
||
export class AbiParser2 implements AbiParserInterface { | ||
abi: Abi; | ||
|
||
constructor(abi: Abi) { | ||
this.abi = abi; | ||
} | ||
|
||
/** | ||
* abi method inputs length | ||
* @param abiMethod FunctionAbi | ||
* @returns number | ||
*/ | ||
public methodInputsLength(abiMethod: FunctionAbi) { | ||
return abiMethod.inputs.length; | ||
} | ||
|
||
/** | ||
* get method definition from abi | ||
* @param name string | ||
* @returns FunctionAbi | undefined | ||
*/ | ||
public getMethod(name: string): FunctionAbi | undefined { | ||
const intf = this.abi.find((it) => it.type === 'interface'); | ||
return intf.items.find((it: any) => it.name === name); | ||
} | ||
|
||
/** | ||
* Get Abi in legacy format | ||
* @returns Abi | ||
*/ | ||
public getLegacyFormat(): Abi { | ||
return this.abi.flatMap((e) => { | ||
if (e.type === 'interface') { | ||
return e.items; | ||
} | ||
return e; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters