From bb35c62d8b1d2d3fd6e138f60f64bc99221491dd Mon Sep 17 00:00:00 2001 From: Hiroki Noda Date: Mon, 4 Nov 2024 22:36:43 +0900 Subject: [PATCH] Add an example for deploy contract from bytecode --- .gitignore | 4 +- dub.sdl | 1 + examples/deploybytecode/Makefile | 4 ++ examples/deploybytecode/contracts/Counter.sol | 14 ++++++ examples/deploybytecode/dub.sdl | 6 +++ examples/deploybytecode/dub.selections.json | 25 ++++++++++ examples/deploybytecode/source/app.d | 47 +++++++++++++++++++ .../views/artifacts/Counter.abi | 1 + .../views/artifacts/Counter.bin | 1 + 9 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 examples/deploybytecode/Makefile create mode 100644 examples/deploybytecode/contracts/Counter.sol create mode 100644 examples/deploybytecode/dub.sdl create mode 100644 examples/deploybytecode/dub.selections.json create mode 100644 examples/deploybytecode/source/app.d create mode 100644 examples/deploybytecode/views/artifacts/Counter.abi create mode 100644 examples/deploybytecode/views/artifacts/Counter.bin diff --git a/.gitignore b/.gitignore index 794b936..2d56740 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,6 @@ libdeth.a *.log ./deth-test-library -./examples/devtest/deth_devtest +examples/devtest/deth_devtest +examples/transfer/deth_transfer +examples/deploybytecode/deth_deploybytecode diff --git a/dub.sdl b/dub.sdl index 8fd538b..8d76b05 100644 --- a/dub.sdl +++ b/dub.sdl @@ -12,3 +12,4 @@ subConfiguration "keccak-tiny" "library" targetType "library" subPackage "examples/devtest" subPackage "examples/transfer" +subPackage "examples/deploybytecode" diff --git a/examples/deploybytecode/Makefile b/examples/deploybytecode/Makefile new file mode 100644 index 0000000..67ecc6d --- /dev/null +++ b/examples/deploybytecode/Makefile @@ -0,0 +1,4 @@ + +.PHONY: build-sol +build-sol: + solc --via-ir --abi --optimize --overwrite --bin -o views/artifacts/ contracts/Counter.sol diff --git a/examples/deploybytecode/contracts/Counter.sol b/examples/deploybytecode/contracts/Counter.sol new file mode 100644 index 0000000..21871bf --- /dev/null +++ b/examples/deploybytecode/contracts/Counter.sol @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-3.0 +pragma solidity >=0.8.0 <0.9.0; + +contract Counter { + uint256 public number; + + function setNumber(uint256 newNumber) public { + number = newNumber; + } + + function increment() public { + number++; + } +} diff --git a/examples/deploybytecode/dub.sdl b/examples/deploybytecode/dub.sdl new file mode 100644 index 0000000..ae6bf9e --- /dev/null +++ b/examples/deploybytecode/dub.sdl @@ -0,0 +1,6 @@ +name "deploybytecode" +description "deploy a contract from bytecode" +authors "Hiroki Noda" +copyright "Copyright © 2024, Hiroki Noda" +dependency "deth" version="*" path="../../" +license "MIT" diff --git a/examples/deploybytecode/dub.selections.json b/examples/deploybytecode/dub.selections.json new file mode 100644 index 0000000..f9dd30a --- /dev/null +++ b/examples/deploybytecode/dub.selections.json @@ -0,0 +1,25 @@ +{ + "fileVersion": 1, + "versions": { + "autointf": "1.2.0", + "botan": "1.12.19", + "botan-math": "1.0.3", + "deth": {"path":"../../"}, + "diet-ng": "1.8.1", + "eventcore": "0.9.20", + "keccak-tiny": "1.2.1", + "libasync": "0.8.6", + "memutils": "1.0.4", + "mir-linux-kernel": "1.0.1", + "openssl": "3.2.2", + "secp256k1": "2.5.1", + "silly": "1.1.1", + "stdx-allocator": "2.77.5", + "structjson": "2.100.1", + "taggedalgebraic": "0.11.22", + "tynukrpc": "1.2.0", + "unit-threaded": "2.0.5", + "vibe-core": "1.22.4", + "vibe-d": "0.9.5" + } +} diff --git a/examples/deploybytecode/source/app.d b/examples/deploybytecode/source/app.d new file mode 100644 index 0000000..8f36db4 --- /dev/null +++ b/examples/deploybytecode/source/app.d @@ -0,0 +1,47 @@ +// ported from https://github.com/alloy-rs/examples/blob/main/examples/contracts/examples/deploy_from_contract.rs + +import std.bigint; +import std.conv : to; +import std.stdio; + +import deth; + +enum abiPath = "artifacts/Counter.abi"; +enum binPath = "artifacts/Counter.bin"; + +immutable CounterABI = ContractABI.load!abiPath("Counter"); +alias CounterContract = Contract!CounterABI; + +void main() +{ + auto conn = new RPCConnector("http://127.0.0.1:8545"); + + const accounts = conn.remoteAccounts(); + + auto bytecode = import(binPath).convTo!bytes; + const chainid = conn.net_version.to!ulong; + Transaction tx = { + from: accounts[0], + data: bytecode, + chainid: chainid, + }; + + const txHash = conn.sendTransaction(tx); + const receipt = conn.getTransactionReceipt(txHash); + const contractAddress = receipt.get.contractAddress; + assert(!contractAddress.isNull); + writeln("Deployed contract at address: ", contractAddress.get.convTo!string.ox); + + auto contract = new CounterContract(conn, contractAddress.get); + + const txHash2 = contract.setNumber(42.BigInt).send(); + const receipt2 = conn.getTransactionReceipt(txHash2); + writeln("Set number to 42: ", txHash2.convTo!string.ox); + + const txHash3 = contract.increment().send(); + conn.getTransactionReceipt(txHash3); + writeln("Incremented number: ", txHash3.convTo!string.ox); + + const number = contract.number(); + writeln("Retrieved number: ", number); +} diff --git a/examples/deploybytecode/views/artifacts/Counter.abi b/examples/deploybytecode/views/artifacts/Counter.abi new file mode 100644 index 0000000..4cf6385 --- /dev/null +++ b/examples/deploybytecode/views/artifacts/Counter.abi @@ -0,0 +1 @@ +[{"inputs":[],"name":"increment","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"number","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"newNumber","type":"uint256"}],"name":"setNumber","outputs":[],"stateMutability":"nonpayable","type":"function"}] \ No newline at end of file diff --git a/examples/deploybytecode/views/artifacts/Counter.bin b/examples/deploybytecode/views/artifacts/Counter.bin new file mode 100644 index 0000000..543dbdf --- /dev/null +++ b/examples/deploybytecode/views/artifacts/Counter.bin @@ -0,0 +1 @@ +6080806040523460135760d2908160188239f35b5f80fdfe60808060405260043610156011575f80fd5b5f3560e01c9081633fb5c1cb1460865781638381f58a14606f575063d09de08a146039575f80fd5b34606b575f366003190112606b575f545f1981146057576001015f55005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b34606b575f366003190112606b576020905f548152f35b34606b576020366003190112606b576004355f5500fea26469706673582212203885422f3af19eed951e9db4fc540579cd6291d85ab325dcf13de3572705d2fa64736f6c634300081c0033 \ No newline at end of file