Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Implement support for loading abi from truffle json artifacts #2

Merged
merged 5 commits into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ sudo: required
dist: trusty
language: clojure
script: lein doo chrome "tests" once
notifications:
email: false
slack:
on_success: always
on_failure: always
rooms:
- district0x:53vhalUfg0MitsQwzjP5PWdd#travis-ci
addons:
chrome: stable
before_script:
Expand All @@ -11,3 +18,13 @@ before_script:
- npm install -g ganache-cli
- "echo 'Running Ganache'"
- ganache-cli -p 8549 -b 1 --noVMErrorsOnRPCResponse > /dev/null &
env:
global:
- secure: MEeuQy4kb1Lor3iDwpzI2rFpdi02jd+1LBp2PuhtVPUYiZ/UYqWelUTKzVWRMsfDIVjcGZPqTBOeTbJwEmqaIklNrLTE9GxvYYGqrEBMPgPTMLyD3siNvMuu32kLRmIgU9eRljKEA2c1san80aOH5tt1x6gChPSYOuAMjlLPDQSXo8pETFsx1EY2Hg6SK6Qm1J/m+m/i4O8cPFN1EPTyjUMOCgJk1YT41o+AhhbiM3braCqPjrSNTppCsL0Gxj/HbQzZ5fcZJOBixrFLqEW9uB0sbaNWFcOcHoXqYzSfD2fV8xzJvVYisaRSgiHkbOKhcEYmI+GjTkyapFzWesOV9N6HZoFrr/Cd753A3i0CLHsclWL/0W1Oa8XEfI1fPKxc0YVt2q61HPMlatlUnQzAswmhgAI4xoEzHSXSxbg2HSUV9wqDmlA51wuDrfQU5kUemOANeuQw1tV+U69EF2tJMFvX5YnCp0Qkt3RXFPgBkWzzAKi6V2/hIUJEtIptI1tX/t20MUZcVIeXBNDBjZWphFA65iPamLEUyYRW/7T4aA4PEFVb1UEXxXvqCM2Q7BjucKQKsUgR2ERhCoWTu8vRDT4oW5mjqRD7qC8Z0Y3PswrXOH4WVhqtzxZZfVRUEc9Z3Pcx5fZlEFoGAmiaBYI8iJk37fLAhCaiJ+YFXvjivGY=
- secure: i3S4L76biLpNHnsIix5xpDM2BN4dagDBWV6v3shCLZiKcFy/4JHZqBFr7F0zAwud/U0qH3yNTqAgXUAEjOBfATGKiv85KOMDqzBhMq0KPqmLVGLsfhwX13SuGdeFsJOaBzgp6MviUDcRUjUdbGH26LpAmPu/yIdWMEPt/KM3YLLgaXbD9k0EKx/ddqA9I0Z8560cDCBe18x8Q+Gk6jFyFI3aXU0arfrMHBlC9c2x4qmHx6ozLIHpubSG21omZWHP55oC9YwY58pCSbkXiPGtY8zhTRxs2K6sRz41pfsREMrYCRlOl2biwPXwp4MwNEZb1gj10pGvE+JrMPS/OQK+cu9G40i7LIB9N7N6AD+x4SL2CVkmimjhxxBcFA3mJ1wkW6QsYiiZPS0vCN5ozs0jgQpueanP2m0x4hSOFdLvWLzW4kvH/VG1SQVkEnoCs1DmcvVACMqoCYV63LXsw08ki+KGarMRBzL/2aWVr1X1KsUh4cGkrsNEx0NoAPHTQbLNisAlNN4qY4GWnUXEVVLTC0xlBDvteoD3GM9YHESLXTfKOF1O2drbKavoPu/C4hV0BRUELHL2BWHsXWhOy78hSZFelWOnK1Q+5Y6Xepzbv6w+HIkz+GHoJ47ntSPBbxdP5jFO18fY1rPEL2rKzyulrZ+3ChLw7MUf51510AHqRsY=
deploy:
- provider: script
skip_cleanup: true
script: lein release :patch
on:
branch: master
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
Clojurescript [re-mount](https://github.com/district0x/d0x-INFRA/blob/master/re-mount.md) module, that takes care of loading Ethereum smart-contract files.

## Installation
Add `[district0x/district-ui-smart-contracts "1.0.5"]` into your project.clj
This module is available as a Maven artifact from Clojars. The latest released version is: <br>
[![Clojars Project](https://img.shields.io/clojars/v/district0x/district-ui-smart-contracts.svg)](https://clojars.org/district0x/district-ui-smart-contracts)

Include `[district.ui.smart-contracts]` in your CLJS file, where you use `mount/start`

## API Overview
Expand Down Expand Up @@ -44,15 +46,16 @@ Include `[district.ui.smart-contracts]` in your CLJS file, where you use `mount/
- [assoc-contract-bin](#assoc-contract-bin)

## district.ui.smart-contracts
This namespace contains smart-contracts [mount](https://github.com/tolitius/mount) module. Once you start mount it'll take care
This namespace contains smart-contracts [mount](https://github.com/tolitius/mount) module. Once you start mount it'll take care
of loading smart contract files.

You can pass following args to initiate this module:
You can pass following args to initiate this module:
* `:disable-loading-at-start?` Pass true if you don't want load ABIs or BINs at start
* `:contracts` A map of smart-contracts to load
* `:load-bin?` Pass true if you want to load BIN files as well
* `:format` The compiled contracts output format, can be one of :solc-abi-bin :truffle-json
* `:contracts-path` Path where contracts should be loaded from. Default: `"./contracts/build/"`
* `:contracts-version` Pass some version for bypassing browser's cache after deploying new contracts to production.
* `:contracts-version` Pass some version for bypassing browser's cache after deploying new contracts to production.
Pass `:no-cache` if you want to invalidate browser cache on every request (useful for development)
* `:request-timeout` Request timeout for loading files. Default: 10000 (10s)

Expand All @@ -61,9 +64,9 @@ Passed `:contracts` should have following format:
(ns my-district.smart-contracts)

(def smart-contracts
{:my-contract {:name "MyContract" ;; ABI and BIN is loaded by this name
{:my-contract {:name "MyContract" ;; ABI and BIN is loaded by this name
:address "0xfbb1b73c4f0bda4f67dca266ce6ef42f520fbb98"
;; optional, if not provided, will try to load
;; optional, if not provided, will try to load
:abi nil
;; optional, if not provided, will try to load
:bin nil
Expand Down Expand Up @@ -130,7 +133,7 @@ re-frame events provided by this module:
Loads smart contracts. Pass same args as to mount start.

#### <a name="contract-loaded`">`::contract-loaded`
Event fired when a single file was loaded. Either ABI or BIN.
Event fired when a single file was loaded. Either ABI or BIN.

#### <a name="contracts-loaded`">`::contracts-loaded`
Event fired when all smart contract files have been loaded. Use this event to hook into event flow from your modules.
Expand All @@ -141,7 +144,7 @@ One example using [re-frame-forward-events-fx](https://github.com/Day8/re-frame-
(:require [district.ui.smart-contracts.events :as contracts-events]
[re-frame.core :refer [reg-event-fx]]
[day8.re-frame.forward-events-fx]))

(reg-event-fx
::my-event
(fn []
Expand Down Expand Up @@ -172,13 +175,13 @@ Deploys a smart-contract of key `contract-key` and saves new address into re-fra

(-> (mount/with-args
{:web3 {:url "http://localhost:8549"}
:smart-contracts
:smart-contracts
{:disable-loading-at-start? true
:contracts {:deploy-test-contract {:name "DeployTestContract"
:abi (js/JSON.parse "[{\"inputs\":[{\"name\":\"someNumber\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]")
:bin "0x60606040523415600e57600080fd5b604051602080607183398101604052808051915050801515602e57600080fd5b50603580603c6000396000f3006060604052600080fd00a165627a7a72305820f6c231e485f5b65831c99412cbcad5b4e41a4b69d40f3d4db8de3a38137701fb0029"}}}})
(mount/start))

(dispatch [::deploy-events/deploy-contract :deploy-test-contract {:gas 4500000
:arguments [1]
:from "0xb2930b35844a230f00e51431acae96fe543a0347"
Expand All @@ -193,11 +196,11 @@ When successfully deployed, you'll be able to access contract instance and addre
```

#### <a name="contract-deploy-failed`">`::contract-deploy-failed`
Event fired when deploying a contract failed.
Event fired when deploying a contract failed.

## district.ui.smart-contracts.queries
DB queries provided by this module:
*You should use them in your events, instead of trying to get this module's
DB queries provided by this module:
*You should use them in your events, instead of trying to get this module's
data directly with `get-in` into re-frame db.*

#### <a name="contracts">`contracts [db]`
Expand Down Expand Up @@ -243,4 +246,4 @@ lein deps
ganache-cli -p 8549 -b 1 --noVMErrorsOnRPCResponse
# To run tests and rerun on changes
lein doo chrome tests
```
```
15 changes: 14 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject district0x/district-ui-smart-contracts "1.0.5"
(defproject district0x/district-ui-smart-contracts "1.0.6-SNAPSHOT"
:description "district UI module for loading smart contract files"
:url "https://github.com/district0x/district-ui-smart-contracts"
:license {:name "Eclipse Public License"
Expand Down Expand Up @@ -32,6 +32,19 @@
[lein-doo "0.1.8"]
[lein-npm "0.6.2"]]}}

:deploy-repositories [["snapshots" {:url "https://clojars.org/repo"
:username :env/clojars_username
:password :env/clojars_password
:sign-releases false}]
["releases" {:url "https://clojars.org/repo"
:username :env/clojars_username
:password :env/clojars_password
:sign-releases false}]]

:release-tasks [
["change" "version" "leiningen.release/bump-version" "release"]
["deploy"]]

:cljsbuild {:builds [{:id "tests"
:source-paths ["src" "test"]
:compiler {:output-to "tests-output/tests.js"
Expand Down
29 changes: 19 additions & 10 deletions src/district/ui/smart_contracts/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,24 @@
(get-version-param version contracts-version)))


(defn- contracts-to-load [{:keys [:contracts :load-bin?] :as opts}]
(defn- files-to-load [{:keys [:contracts :load-bin? :format]
:or {format :solc-abi-bin}
:as opts}]
(reduce
(fn [acc [key {:keys [:abi :bin] :as contract}]]
(cond-> acc
(not abi) (conj {:file-path (get-file-path :abi contract opts)
(not abi) (conj {:file-path (get-file-path ({:solc-abi-bin :abi
:truffle-json :json} format)
contract opts)
:contract-key key
:file-type :abi})
:file-type :abi
:format format})

(and load-bin?
(not bin)) (conj {:file-path (get-file-path :bin contract opts)
:contract-key key
:file-type :bin})))
(not bin)
(= format :solc-abi-bin)) (conj {:file-path (get-file-path :bin contract opts)
:contract-key key
:file-type :bin})))
[]
contracts))

Expand All @@ -67,7 +73,7 @@
[interceptors (validate-first-arg :district.ui.smart-contracts/opts)]
(fn [{:keys [:db]} [{:keys [:request-timeout :contracts] :as opts
:or {request-timeout 10000}}]]
(let [to-load (contracts-to-load opts)
(let [to-load (files-to-load opts)
*load-batch* (atom (zipmap to-load (repeat false)))]
{:db (queries/merge-contracts db contracts)
:http-xhrio
Expand All @@ -85,11 +91,15 @@
(reg-event-fx
::contract-loaded
interceptors
(fn [{:keys [:db]} [{:keys [:contract-key :file-type] :as contract} success? *load-batch* response]]
(fn [{:keys [:db]} [{:keys [:contract-key :file-type :format] :as contract} success? *load-batch* response]]
(swap! *load-batch* assoc contract true)
(let [new-db (if success?
(condp = file-type
:abi (queries/assoc-contract-abi db contract-key response)
:abi (case format
:solc-abi-bin (queries/assoc-contract-abi db contract-key response)
:truffle-json (-> db
(queries/assoc-contract-abi contract-key (get response "abi"))
(queries/assoc-contract-bin contract-key (get response "bytecode"))))
:bin (queries/assoc-contract-bin db contract-key response))
db)]
(merge
Expand Down Expand Up @@ -130,4 +140,3 @@
interceptors
(fn [{:keys [:db]}]
{:db (queries/dissoc-smart-contracts db)}))