Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support encoding constructor arguments #112

Open
kamikazechaser opened this issue Feb 14, 2024 · 2 comments
Open

Support encoding constructor arguments #112

kamikazechaser opened this issue Feb 14, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@kamikazechaser
Copy link

kamikazechaser commented Feb 14, 2024

The ABI parser expects any function to be named. In some cases it would be useful to encode constructor args. (address) for example doesn't work. One way would be to reserve the constructor keyword in the lexer.

constructorFunc, err := w3.NewFunc("constructor(address)", "")
if err != nil {
	return nil, err
}

constructorArgs := []any{w3.A(0x765DE816845861e75A25fCA122bb6898B8B1282a)}

constructorBytecode, err := constructorFunc.EncodeArgs(constructorArgs...)
if err != nil {
	return nil, err
}

// Should return bytearray representing 000000000000000000000000765de816845861e75a25fca122bb6898b8b1282a

Not sure if this is within scope of w3 or should the abi package (abi.Pack) from geth be used instead.

@lmittmann
Copy link
Owner

lmittmann commented Feb 14, 2024

There is one hacky way to do this rn. w3.Func exposes Func.Args of the "raw" geth type ab.Arguments. So you could use the constructorFunc from your example to call constructorFunc.Args.Pack.

This is definitely within the scope of w3. The problem is I don't really have a good idea how to handle constructors. Not sure if it is safe to reserve the "constructor" name. Does it even make sense to encode arguments without the deploy bytecode? How would you handle this in your code-example?

@lmittmann lmittmann added the enhancement New feature or request label Feb 14, 2024
@kamikazechaser
Copy link
Author

kamikazechaser commented Feb 15, 2024

Does it even make sense to encode arguments without the deploy bytecode? How would you handle this in your code-example?

I would append the constructor args because the contract bytecode is fixed.

func PrepareContractBytecodeData(contractBin string, contractABIJSON string, constructorArgs []any) ([]byte, error) {
	constructorBytecode, err := encodeConstructorArgs(contractABIJSON, constructorArgs)
	if err != nil {
		return nil, err
	}

	return append(common.Hex2Bytes(contractBin), constructorBytecode...), nil
}

func encodeConstructorArgs(contractABI string, constructorArgs []any) ([]byte, error) {
	abi, err := abi.JSON(strings.NewReader(contractABI))
	if err != nil {
		return nil, err
	}

	constructorBytecode, err := abi.Pack("", constructorArgs...)
	if err != nil {
		return nil, err
	}

	return constructorBytecode, nil
}

Not sure if it is safe to reserve the "constructor" name.

Yeah, it might not be the best way as the library should be as flexible as possible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants