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

How to encode data for appending constructor to bytecode for deploying contract? #86

Closed
skywinder opened this issue Nov 8, 2018 · 8 comments

Comments

@skywinder
Copy link
Collaborator

From @Mahdimm on August 5, 2018 8:46

Hi thanks for @shamatar you library,
is there any func for encoding the parameter of constructor for adding to bytecode string?
or how we should do this for adding our parameter to constructor (end of bytecode)?

Copied from original issue: BANKEX/web3swift#207

@skywinder
Copy link
Collaborator Author

From @shamatar on August 6, 2018 15:13

Hello @Mahdimm

I've you've created the contract out from ABI you can specify constructor parameters when you call a .deploy function the same way as you specify parameters for .method call of the contracts function.

Sincerely, Alex

@skywinder
Copy link
Collaborator Author

From @Mahdimm on August 8, 2018 8:52

Hi @shamatar
thanks for reply, I need to encode the parameter that I appending to my bytecode is there any encryption func for that?

DispatchQueue.global(qos: .background).async {
           
           let web3 = Web3.new(URL(string: "URL_HERE")!)
           var options = Web3Options.defaultOptions()
           options.from = From.EthereumAddress
           
           let contract = web3?.contract(niluTokenABI)
           let data = Data.init(hex: (niluWalletData + name + symbol + "\(decimals)" + "\(initialSupply)"))
           
           let intermadiate = contract?.deploy(bytecode: data, options: options)
           
           let result = intermadiate?.call(options: options)
          

such as this encoding on java:

String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.asList(new Utf8String(name),
                new Utf8String(symbol),
                new Uint8(new BigInteger(decimals)),
                new Uint256(new BigDecimal(totalSupply).multiply(BigDecimal.TEN.pow(Integer.parseInt(decimals))).toBigInteger())));

should I do this manually or there is function for this in your library?

@skywinder
Copy link
Collaborator Author

From @shamatar on August 8, 2018 8:58

Hello @Mahdimm

There is an example in tests

        let web3 = Web3.new....
        let abiString =  "[{\"constant\":true,\"inputs\":[],\"name\":\"getFlagData\",\"outputs\":[{\"name\":\"data\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"data\",\"type\":\"string\"}],\"name\":\"setFlagData\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"
        guard let bytecode = Data.fromHex("6060604052341561000f57600080fd5b6103358061001e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063a16e94bf14610051578063a46b5b6b146100df575b600080fd5b341561005c57600080fd5b61006461013c565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100a4578082015181840152602081019050610089565b50505050905090810190601f1680156100d15780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156100ea57600080fd5b61013a600480803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509190505061020d565b005b610144610250565b6000808073ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102035780601f106101d857610100808354040283529160200191610203565b820191906000526020600020905b8154815290600101906020018083116101e657829003601f168201915b5050505050905090565b806000808073ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600001908051906020019061024c929190610264565b5050565b602060405190810160405280600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102a557805160ff19168380011785556102d3565b828001600101855582156102d3579182015b828111156102d25782518255916020019190600101906102b7565b5b5090506102e091906102e4565b5090565b61030691905b808211156103025760008160009055506001016102ea565b5090565b905600a165627a7a7230582017359d063cd7fdf56f19ca186a54863ce855c8f070acece905d8538fbbc4d1bf0029") else {return XCTFail()}
        let contract = web3.contract(abiString, at: nil, abiVersion: 2)
        var options = Web3Options.defaultOptions()
        options.from = EthereumAddress(...)
        options.gasLimit = BigUInt(3000000)
        let intermediate = contract?.deploy(bytecode: bytecode, parameters: [AnyObject](), options: options)
        guard let result = intermediate?.send(password: "") else {return XCTFail()}

First of all you need to get an ABI and compiled bytecode of the contract you want to deploy, then construct a transaction using the contract.deploy(...) method. parameters takes an array of variables, in your case those will be [String, String, BigUInt, BigUInt].

@skywinder
Copy link
Collaborator Author

From @MrShafiee on August 13, 2018 14:23

Hello @shamatar
thanks for your response, we used your sample code, and our request was such as below:

[{
	"jsonrpc": "2.0",
	"method": "eth_sendTransaction",
	"id": 18,
	"params": [{
		"data": "0x6080604052....00000000",
		"value": "0x0",
		"gasPrice": "0x6fc23ac00",
		"from": "0x0d13ab5ce239....b9bccb84af"
	}]
}]

But the response of the request was:

[{
	"jsonrpc": "2.0",
	"id": 18,
	"error": {
		"code": -32000,
		"message": "unknown account"
	}
}]

what is the problem?
I get this error before in normal transaction, when I used sendTransaction function because sendRawTransaction was not public, I reported it and you set it public and when I used sendRawtransaction this error be disappeared, for this reason I think that you send transaction without any sign in this function that lead to get this error.
what is your idea about this?

@skywinder
Copy link
Collaborator Author

From @shamatar on August 13, 2018 14:55

Did you attach a keystore? My example in tests is about deploying a contract with a “remote” signing - when the key is stored on the Ethereum node. If you want to sign locally (on device), just attach a keystore and specify the right account in “options”


From: MrShafiee [email protected]
Sent: Monday, August 13, 2018 5:23:04 PM
To: BANKEX/web3swift
Cc: Alexander; Mention
Subject: Re: [BANKEX/web3swift] How to encode data for appending constructor to bytecode for deploying contract? (#207)

Hello @shamatarhttps://github.com/shamatar
thanks for your response, we used your sample code, and our request was such as below:

[{
"jsonrpc": "2.0",
"method": "eth_sendTransaction",
"id": 18,
"params": [{
"data": "0x6080604052....00000000",
"value": "0x0",
"gasPrice": "0x6fc23ac00",
"from": "0x0d13ab5ce239....b9bccb84af"
}]
}]

But the response of the request was:

[{
"jsonrpc": "2.0",
"id": 18,
"error": {
"code": -32000,
"message": "unknown account"
}
}]

what is the problem?
I get this error before in normal transaction, when I used sendTransaction function because sendRawTransaction was not public, I reported it and you set it public and when I used sendRawtransaction this error be disappeared, for this reason I think that you send transaction without any sign in this function that lead to get this error.
what is your idea about this?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubBANKEX/web3swift#207 (comment), or mute the threadhttps://github.com/notifications/unsubscribe-auth/AGKv97UW4qm5y2O-w3i7lr6WgNOxT-ivks5uQYvIgaJpZM4VvS3R.

@skywinder
Copy link
Collaborator Author

From @MrShafiee on August 14, 2018 13:12

Thanks @shamatar , you are right 👍
I have another question, when we call contract?.deploy(...) function; library call these functions on backend, eth_getTransactionCount, eth_estimateGas, eth_gasPrice; such as below:

[{
  "jsonrpc": "2.0",
  "method": "eth_getTransactionCount",
  "id": 6,
  "params": ["0x0b5cbf8f0818f4.....320953808", "pending"]
}, {
  "jsonrpc": "2.0",
  "method": "eth_estimateGas",
  "id": 7,
  "params": [{
    "data": "0x608060405234801080fd5b5.............00000000000000000",
    "value": "0x0",
    "gasPrice": "0x0",
    "from": "0x0b5cbf8f0818f4.....0953808"
  }]
}, {
  "jsonrpc": "2.0",
  "method": "eth_gasPrice",
  "id": 8,
  "params": []
}]

and after that, call eth_sendRawTransaction automatically, to deploys new contract.
Now I want put the step before this and show fee of token deployment to my users, for this I should call eth_getTransactionCount, eth_estimateGas, eth_gasPrice; manually to get it and it needs transactionObject, in transactionObject we should set To property of ethereum transaction and we do not have any To in this type of transaction and we should use data binary (byte code).
In fact how could we call gasEstimat without To property of ethereum transaction?
What is your idea about this problem?

@skywinder
Copy link
Collaborator Author

Hello. Me and Alex (@shamatar) as main maintainers are moving our development to the new repo.
➡️https://github.com/matterinc/web3swift ⬅️
Feel free continue discussion in the new repository. 👍
Cheers.

@TonioMacaronio
Copy link
Collaborator

Answer:
BANKEX/web3swift#207 (comment)
Closed

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

No branches or pull requests

2 participants