55namespace Farbcode \LaravelEvm \Codec ;
66
77use Farbcode \LaravelEvm \Contracts \AbiCodec ;
8- use Web3 \ Contract ;
9- use kornrunner \ Keccak ; // keccak256 helper
8+ use kornrunner \ Keccak ;
9+ use Web3 \ Contract ; // keccak256 helper
1010
1111class AbiCodecWeb3p implements AbiCodec
1212{
@@ -15,23 +15,24 @@ public function encodeFunction(array|string $abi, string $fn, array $args): stri
1515 // web3p Contract::getData() returns void and fills internal state. To avoid dynamic property reliance
1616 // implement a lightweight encoder for common static call patterns.
1717 $ abiArray = is_string ($ abi ) ? json_decode ($ abi , true ) : $ abi ;
18- if (!is_array ($ abiArray )) {
18+ if (! is_array ($ abiArray )) {
1919 throw new \InvalidArgumentException ('ABI must decode to array ' );
2020 }
2121 $ item = null ;
2222 foreach ($ abiArray as $ entry ) {
2323 if (($ entry ['type ' ] ?? '' ) === 'function ' && ($ entry ['name ' ] ?? '' ) === $ fn ) {
24- $ item = $ entry ; break ;
24+ $ item = $ entry ;
25+ break ;
2526 }
2627 }
27- if (!$ item ) {
28+ if (! $ item ) {
2829 throw new \RuntimeException ('Function ' .$ fn .' not found in ABI ' );
2930 }
3031 $ inputs = $ item ['inputs ' ] ?? [];
3132 // Build function selector
32- $ typesSig = implode (', ' , array_map (fn ($ i ) => $ i ['type ' ], $ inputs ));
33+ $ typesSig = implode (', ' , array_map (fn ($ i ) => $ i ['type ' ], $ inputs ));
3334 $ signature = $ fn .'( ' .$ typesSig .') ' ;
34- $ hash = Keccak::hash ($ signature , 256 );
35+ $ hash = Keccak::hash ($ signature , 256 );
3536 $ selector = '0x ' .substr ($ hash , 0 , 8 );
3637 // Encode arguments (very simplified: handles address, uint256, bytes32, bool, string)
3738 $ encodedArgs = '' ;
@@ -40,6 +41,7 @@ public function encodeFunction(array|string $abi, string $fn, array $args): stri
4041 $ val = $ args [$ idx ] ?? null ;
4142 $ encodedArgs .= $ this ->encodeValue ($ type , $ val );
4243 }
44+
4345 return $ selector .$ encodedArgs ;
4446 }
4547
@@ -51,10 +53,12 @@ private function encodeValue(string $type, mixed $val): string
5153 }
5254 if ($ type === 'address ' ) {
5355 $ clean = strtolower (preg_replace ('/^0x/ ' , '' , (string ) $ val ));
56+
5457 return str_pad ($ clean , 64 , '0 ' , STR_PAD_LEFT );
5558 }
5659 if ($ type === 'bytes32 ' ) {
5760 $ clean = strtolower (preg_replace ('/^0x/ ' , '' , (string ) $ val ));
61+
5862 return str_pad (substr ($ clean , 0 , 64 ), 64 , '0 ' , STR_PAD_RIGHT );
5963 }
6064 if ($ type === 'bool ' ) {
@@ -63,6 +67,7 @@ private function encodeValue(string $type, mixed $val): string
6367 if ($ type === 'string ' ) {
6468 // naive: hex of string truncated to 32 bytes
6569 $ hex = bin2hex ((string ) $ val );
70+
6671 return str_pad (substr ($ hex , 0 , 64 ), 64 , '0 ' , STR_PAD_RIGHT );
6772 }
6873 throw new \RuntimeException ('Unsupported ABI type ' .$ type );
@@ -71,6 +76,7 @@ private function encodeValue(string $type, mixed $val): string
7176 public function callStatic (array |string $ abi , string $ fn , array $ args , callable $ ethCall ): mixed
7277 {
7378 $ data = $ this ->encodeFunction ($ abi , $ fn , $ args );
79+
7480 return $ ethCall ($ data );
7581 }
7682}
0 commit comments