Skip to content

Commit

Permalink
Add an example for deploy contract from bytecode
Browse files Browse the repository at this point in the history
  • Loading branch information
kubo39 committed Nov 4, 2024
1 parent c06e10d commit bb35c62
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ subConfiguration "keccak-tiny" "library"
targetType "library"
subPackage "examples/devtest"
subPackage "examples/transfer"
subPackage "examples/deploybytecode"
4 changes: 4 additions & 0 deletions examples/deploybytecode/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

.PHONY: build-sol
build-sol:
solc --via-ir --abi --optimize --overwrite --bin -o views/artifacts/ contracts/Counter.sol
14 changes: 14 additions & 0 deletions examples/deploybytecode/contracts/Counter.sol
Original file line number Diff line number Diff line change
@@ -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++;
}
}
6 changes: 6 additions & 0 deletions examples/deploybytecode/dub.sdl
Original file line number Diff line number Diff line change
@@ -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"
25 changes: 25 additions & 0 deletions examples/deploybytecode/dub.selections.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
47 changes: 47 additions & 0 deletions examples/deploybytecode/source/app.d
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions examples/deploybytecode/views/artifacts/Counter.abi
Original file line number Diff line number Diff line change
@@ -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"}]
1 change: 1 addition & 0 deletions examples/deploybytecode/views/artifacts/Counter.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6080806040523460135760d2908160188239f35b5f80fdfe60808060405260043610156011575f80fd5b5f3560e01c9081633fb5c1cb1460865781638381f58a14606f575063d09de08a146039575f80fd5b34606b575f366003190112606b575f545f1981146057576001015f55005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b34606b575f366003190112606b576020905f548152f35b34606b576020366003190112606b576004355f5500fea26469706673582212203885422f3af19eed951e9db4fc540579cd6291d85ab325dcf13de3572705d2fa64736f6c634300081c0033

0 comments on commit bb35c62

Please sign in to comment.