-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
113 additions
and
47 deletions.
There are no files selected for viewing
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,99 @@ | ||
import { Abi, AbiItem, Address, Hex } from "viem"; | ||
|
||
type DeterministicContract = { | ||
readonly prepareDeploy: ( | ||
deployer: Address, | ||
libraries: readonly Library[], | ||
) => { | ||
readonly address: Address; | ||
readonly bytecode: Hex; | ||
}; | ||
readonly deployedBytecodeSize: number; | ||
readonly abi: Abi; | ||
}; | ||
|
||
type Library = DeterministicContract & { | ||
/** | ||
* Path to library source file, e.g. `src/libraries/SomeLib.sol` | ||
*/ | ||
path: string; | ||
/** | ||
* Library name, e.g. `SomeLib` | ||
*/ | ||
name: string; | ||
}; | ||
|
||
// map over each and decode signature | ||
type WorldFunction = { | ||
readonly signature: string; | ||
readonly selector: Hex; | ||
readonly systemId: Hex; | ||
readonly systemFunctionSignature: string; | ||
readonly systemFunctionSelector: Hex; | ||
}; | ||
|
||
type System = DeterministicContract & { | ||
readonly namespace: string; | ||
readonly name: string; | ||
readonly systemId: Hex; | ||
readonly allowAll: boolean; | ||
readonly allowedAddresses: readonly Hex[]; | ||
readonly allowedSystemIds: readonly Hex[]; | ||
readonly functions: readonly WorldFunction[]; | ||
}; | ||
|
||
type DeployedSystem = Omit<System, "abi" | "prepareDeploy" | "deployedBytecodeSize" | "allowedSystemIds"> & { | ||
address: Address; | ||
}; | ||
|
||
export function abigenInner(system: DeployedSystem): Abi { | ||
const abi = system.functions.map((func) => { | ||
const match = func.systemFunctionSignature.match(/^([a-zA-Z_]\w*)\((.*)\)$/); | ||
if (!match) { | ||
throw new Error("Invalid signature"); | ||
} | ||
|
||
const name = match[1]; | ||
const argsString = match[2]; | ||
|
||
const args = argsString.split(","); | ||
|
||
const item: AbiItem = { | ||
type: "function", | ||
name, | ||
inputs: args.map((type, i) => { | ||
const isTuple = type[0] === "("; | ||
if (!isTuple) { | ||
return { | ||
name: "", | ||
type, | ||
internalType: type, | ||
}; | ||
} else { | ||
return { | ||
name: "", | ||
type: "tuple[]", | ||
internalType: `${name}${i}Struct[]`, | ||
components: type | ||
.replace("(", "") | ||
.replace(")", "") | ||
.split(",") | ||
.map((member, j) => ({ | ||
name: `value${j}`, | ||
type: member, | ||
internalType: member, | ||
})), | ||
}; | ||
} | ||
}), | ||
outputs: [], | ||
stateMutability: "nonpayable", | ||
}; | ||
|
||
console.log(argsString, item); | ||
|
||
return item; | ||
}); | ||
|
||
return 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