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

Handling inputs to a smart contract constructor #33

Open
oskarth opened this issue Dec 19, 2020 · 5 comments
Open

Handling inputs to a smart contract constructor #33

oskarth opened this issue Dec 19, 2020 · 5 comments

Comments

@oskarth
Copy link

oskarth commented Dec 19, 2020

Duplicate of waku-org/nwaku#323

Problem

The problem is about how to deploy a smart contract with its constructor's inputs using nim-web3.

Some thoughts:
The EthSend has a data field which embodies the compiled code of the contract, probably the constructor's parameters should be passed/encoded as part of the data field, like <smartcontract code><parameters of constructor> but couldn't find the encoding procedure in this library.

@oskarth
Copy link
Author

oskarth commented Dec 19, 2020

Original issue by @staheri14. This is also an issue for SWAP accounting that also requires a constructor: waku-org/nwaku#323

@zah or someone else do you have any ideas for how to do this within nim-web3 library? Would be very useful. Also see previous discussion in Nimbus discord.

@staheri14
Copy link

A solution for this issue is implemented in this PR waku-org/nwaku#359 (in nim-waku repo), please see https://github.com/status-im/nim-waku/blob/master/tests/v2/test_utils.nim

@jangko
Copy link
Contributor

jangko commented Jan 9, 2024

@yglukhov , is #112 also addressing this issue?

@yglukhov
Copy link
Contributor

yglukhov commented Jan 9, 2024

Yes, contract dsl now supports constructors, but web3-send api is still missing for that. Here's an example of how one could implement it:

import web3, chronos

proc deployContractAux(web3: Web3, data: seq[byte], gasPrice = 0): Future[ReceiptObject] {.async.} =
  var tr: EthSend
  tr.`from` = web3.defaultAccount
  tr.data = data
  tr.gas = Quantity(30000000).some
  if gasPrice != 0:
    tr.gasPrice = some(gasPrice.Quantity)

  let r = await web3.send(tr)
  return await web3.getMinedTransactionReceipt(r)

proc send*[T](d: ContractDeployment[T, Web3]): Future[Sender[T]] {.async.} =
  let receipt = await d.sender.deployContractAux(d.data)
  result.sender = typeof(result.sender)(web3: d.sender, contractAddress: receipt.contractAddress.get)

let contractCode: seq[byte] = ... # Load contract code
contract TestContract:
  proc constructorNameDoesNotMatter(arg1, arg2: string) {.constructor.}

let web3 = waitFor newWeb3("ws://127.0.0.1:8545/")

let testContract: Sender[TestContract] = waitFor web3.deployContract(TestContract, contractCode, "arg1 value", "arg2 value").send()

@yglukhov
Copy link
Contributor

Update:
With #118 merged, the deployment is even easier:

import web3, chronos
let contractCode: seq[byte] = ... # Load contract code
contract TestContract:
  proc constructorNameDoesNotMatter(arg1, arg2: string) {.constructor.}
let web3 = waitFor newWeb3("ws://127.0.0.1:8545/")
let testContract: Sender[TestContract] = waitFor web3.deployContract(TestContract, contractCode, "arg1 value", "arg2 value")

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

4 participants