diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..98ba82d3 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +poa/data diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..98ba82d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +poa/data diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..3b6803de --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "MP-SPDZ"] + path = MP-SPDZ + url = https://github.com/initc3/MP-SPDZ.git + branch = random-shamir-prep diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..40082171 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,79 @@ +# Go (server) dependencies +FROM golang:buster as go-deps + +# ethereum +COPY --from=sbellem/go-ethereum:cfbb969da-buster \ + /go/src/golang.org/x /go/src/golang.org/x +COPY --from=sbellem/go-ethereum:cfbb969da-buster \ + /go/src/github.com/ethereum /go/src/github.com/ethereum + +COPY src /go/src/github.com/initc3/HoneyBadgerSwap/src +WORKDIR /go/src/github.com/initc3/HoneyBadgerSwap/src +RUN go get -d -v ./... + + +# main image +FROM python:3.8 + +ENV PYTHONUNBUFFERED 1 + +# MP-SPDZ +# TODO: review dependencies as some of them may not be needed. +RUN apt-get update && apt-get install -y --no-install-recommends \ + build-essential \ + libboost-dev \ + libboost-thread-dev \ + libsodium-dev \ + libssl-dev \ + libtool \ + m4 \ + texinfo \ + yasm \ + && rm -rf /var/lib/apt/lists/* +# mpir +ENV LD_LIBRARY_PATH /usr/local/lib +RUN mkdir -p /usr/local/share/info +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/lib/libmpir*.*a /usr/local/lib/ +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/lib/libmpir.so.23.0.3 /usr/local/lib/ +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/lib/libmpirxx.so.8.4.3 /usr/local/lib/ +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/include/mpir*.h /usr/local/include/ +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/share/info/* /usr/local/share/info/ +RUN set -ex \ + && cd /usr/local/lib \ + && ln -s libmpir.so.23.0.3 libmpir.so \ + && ln -s libmpir.so.23.0.3 libmpir.so.23 \ + && ln -s libmpirxx.so.8.4.3 libmpirxx.so \ + && ln -s libmpirxx.so.8.4.3 libmpirxx.so.8 + +ENV HBSWAP_HOME /usr/src/hbswap +WORKDIR $HBSWAP_HOME +ENV INPUTMASK_SHARES "/opt/hbswap/inputmask-shares" +ENV PREP_DIR "/opt/hbswap/preprocessing-data" +COPY --from=sbellem/mpspdz:2cf97686-randomshamirprep\ + /usr/src/MP-SPDZ/random-shamir.x /usr/src/hbswap/ +COPY --from=sbellem/mpspdz:2cf97686-maliciousshamirparty \ + /usr/src/MP-SPDZ/malicious-shamir-party.x /usr/src/hbswap/ +RUN mkdir -p $INPUTMASK_SHARES $PREP_DIR +############################################################################# + +# GO (server) dependencies +ENV PATH /usr/local/go/bin:$PATH +COPY --from=golang:1.15.8-buster /usr/local/go /usr/local/go +ENV GOPATH /go +ENV PATH $GOPATH/bin:$PATH +RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH" +COPY --from=go-deps /go/src /go/src +############################################################################# + +ENV DB_PATH /opt/hbswap/db + +# Python (HTTP server) dependencies for HTTP server +RUN apt-get update && apt-get install -y --no-install-recommends \ + lsof \ + libmpfr-dev \ + libmpc-dev \ + && rm -rf /var/lib/apt/lists/* + +COPY src/python /usr/src/honeybadgerswap-python +WORKDIR /usr/src/honeybadgerswap-python +RUN pip install --editable . diff --git a/MP-SPDZ b/MP-SPDZ new file mode 160000 index 00000000..2cf97686 --- /dev/null +++ b/MP-SPDZ @@ -0,0 +1 @@ +Subproject commit 2cf97686a4050183f558fe7e4fee3a7c06ab899d diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..a556c639 --- /dev/null +++ b/Makefile @@ -0,0 +1,154 @@ +.PHONY: help + +.DEFAULT_GOAL := help + +define PRINT_HELP_PYSCRIPT +import re, sys + +for line in sys.stdin: + match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line) + if match: + target, help = match.groups() + print("%-20s %s" % (target, help)) +endef + +export PRINT_HELP_PYSCRIPT + +help: + @python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) + +clean: clean-mpc-progs + docker-compose run --rm ethnode rm -rf /opt/hbswap/poa/data + #docker-compose run --rm mpc.nodes rm -rf Scripts/hbswap/log/* + +clean-mpc-progs: + docker-compose run --rm mpc.compile rm -rf Programs/Bytecode Programs/Schedules Programs/Public-Input + +clean-player-data: + docker-compose run --rm mpc.trusted.setup rm -rf Player-Data/* + +#down: down-init +down: + docker-compose -f docker-compose.yml -f liquidity-provider.yml -f trader.yml down --remove-orphans + +stop: + docker-compose stop ethnode mpc.nodes + +rm: clean + #docker-compose rm --stop --force ethnode contract.deploy contract.deposit + docker-compose rm --stop --force -v + docker volume rm \ + hbswap_public-keys \ + hbswap_secrets-p0 \ + hbswap_secrets-p1 \ + hbswap_secrets-p2 \ + hbswap_secrets-p3 + +init-eth: + docker-compose up -d ethnode + docker-compose up contract.deploy + docker-compose up pool.init + docker-compose up client.deposit + +simulation: down mpc-keys mpc-compile + docker-compose up -d ethnode + docker-compose up deploy.contract + docker-compose up -d \ + mpcnode0 \ + mpcnode1 \ + mpcnode2 \ + mpcnode3 + docker-compose -f docker-compose.yml -f liquidity-provider.yml up init.pool + docker-compose -f docker-compose.yml -f trader.yml up public.deposit + docker-compose -f docker-compose.yml -f trader.yml up secret.deposit + docker-compose -f docker-compose.yml -f trader.yml up trade-1 + docker-compose -f docker-compose.yml -f trader.yml up trade-2 + docker-compose -f docker-compose.yml -f trader.yml up trade-1 + docker-compose -f docker-compose.yml -f trader.yml up trade-2 + docker-compose -f docker-compose.yml -f trader.yml up trade-1 + docker-compose -f docker-compose.yml -f trader.yml up trade-2 + docker-compose -f docker-compose.yml -f trader.yml up trade-1 + docker-compose -f docker-compose.yml -f trader.yml up trade-2 + docker-compose -f docker-compose.yml -f trader.yml up trade-1 + docker-compose -f docker-compose.yml -f trader.yml up trade-2 + docker-compose -f docker-compose.yml -f trader.yml up trade-1 + docker-compose -f docker-compose.yml -f trader.yml up trade-2 + +start-hbswap: + docker-compose up -d ethnode + docker-compose up deploy.contract + docker-compose up -d \ + mpcnode0 \ + mpcnode1 \ + mpcnode2 \ + mpcnode3 + #sh scripts/follow-sim-logs-with-tmux.sh + +public-deposit: + docker-compose -f docker-compose.yml -f trader.yml up public.deposit + +init-secrets: + docker-compose -f docker-compose.yml -f liquidity-provider.yml up init.pool + docker-compose -f docker-compose.yml -f trader.yml up secret.deposit + +trade-1: + docker-compose -f docker-compose.yml -f trader.yml up trade-1 + +trade-2: + docker-compose -f docker-compose.yml -f trader.yml up trade-2 + +up-eth: + docker-compose up -d ethnode + +deploy-contract: up-eth + docker-compose -f docker-compose.yml -f eth.yml up contract.deploy + +lp-init-pool: + docker-compose -f docker-compose.yml -f liquidity-provider.yml up pool.init + +trader-deposit: + docker-compose -f docker-compose.yml -f trader.yml up client.deposit + +mpc-keys: + docker-compose up mpc.trusted.setup + +mpc-compile: + docker-compose up mpc.compile + +mpc-init-pool: mpc-keys mpc-compile + docker-compose up \ + mpc.init.node.0 \ + mpc.init.node.1 \ + mpc.init.node.2 \ + mpc.init.node.3 + +mpc: + docker-compose up -d \ + mpcnode0 \ + mpcnode1 \ + mpcnode2 \ + mpcnode3 + +run-client: + docker-compose up client + +#up: ## run the example +up: down rm mpc-keys mpc-compile mpc-init-pool ## run the example + docker-compose up -d ethnode + docker-compose up contract.deploy + docker-compose up contract.deposit + docker-compose up -d mpcnode0 mpcnode1 mpcnode2 mpcnode3 + docker-compose up -d client + sh scripts/follow-sim-logs-with-tmux.sh + +down-init: + docker-compose -f hbswap-init.yml down --remove-orphans + +rm-init: clean + docker-compose -f hbswap-init.yml rm --stop --force mpc.trusted.setup mpc.compile mpcnode0 mpcnode1 mpcnode2 mpcnode3 + #docker-compose -f hbswap-init.yml rm --stop --force mpc.trusted.setup mpc.compile mpc.allnodes + +up-init: down-init rm-init + docker-compose -f hbswap-init.yml up mpc.trusted.setup mpc.compile + docker-compose -f hbswap-init.yml up mpcnode0 mpcnode1 mpcnode2 mpcnode3 + #docker-compose -f hbswap-init.yml up mpc.allnodes diff --git a/OPS.md b/OPS.md new file mode 100644 index 00000000..c509434b --- /dev/null +++ b/OPS.md @@ -0,0 +1,16 @@ +# Dev Ops + +## git submodule reminders + +to update to latest submodule code: + +```shell +git submodule update --remote +``` + +to change branch: + +```shell +git config -f .gitmodules submodule.MP-SPDZ.branch new-branch-name +git submodule update --remote +``` diff --git a/conf/client.toml b/conf/client.toml new file mode 100644 index 00000000..e38945f6 --- /dev/null +++ b/conf/client.toml @@ -0,0 +1,26 @@ +# MPC network configuation + +# MPC network params +n = 4 +t = 1 + +# MPC servers +[[servers]] +id = 0 +host = "mpcnode0" +http_port = 8080 + +[[servers]] +id = 1 +host = "mpcnode1" +http_port = 8080 + +[[servers]] +id = 2 +host = "mpcnode2" +http_port = 8080 + +[[servers]] +id = 3 +host = "mpcnode3" +http_port = 8080 diff --git a/conf/config.toml b/conf/config.toml new file mode 100644 index 00000000..a69520bb --- /dev/null +++ b/conf/config.toml @@ -0,0 +1,30 @@ +# MPC network configuation + +# MPC network params +n = 4 +t = 1 + +# MPC servers +[[servers]] +id = 0 +host = "127.0.0.1" +http_host = "127.0.0.1" +http_port = 8080 + +[[servers]] +id = 1 +host = "127.0.0.1" +http_host = "127.0.0.1" +http_port = 8081 + +[[servers]] +id = 2 +host = "127.0.0.1" +http_host = "127.0.0.1" +http_port = 8082 + +[[servers]] +id = 3 +host = "127.0.0.1" +http_host = "127.0.0.1" +http_port = 8083 diff --git a/conf/server.toml b/conf/server.toml new file mode 100644 index 00000000..35c072fb --- /dev/null +++ b/conf/server.toml @@ -0,0 +1,30 @@ +# MPC network configuation + +# MPC network params +n = 4 +t = 1 + +# MPC servers +[[servers]] +id = 0 +host = "mpcnode0" +http_port = 8080 +http_host = "0.0.0.0" + +[[servers]] +id = 1 +host = "mpcnode1" +http_port = 8080 +http_host = "0.0.0.0" + +[[servers]] +id = 2 +host = "mpcnode2" +http_port = 8080 +http_host = "0.0.0.0" + +[[servers]] +id = 3 +host = "mpcnode3" +http_port = 8080 +http_host = "0.0.0.0" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..8e64b333 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,213 @@ +version: '3.8' + +networks: + hbswap: + +volumes: + mpc-bytecodes: + mpc-schedules: + public-keys: + secrets-p0: + secrets-p1: + secrets-p2: + secrets-p3: + inputmaskshares-p0: + inputmaskshares-p1: + inputmaskshares-p2: + inputmaskshares-p3: + ppdata-p0: + ppdata-p1: + ppdata-p2: + ppdata-p3: + persistence-p0: + persistence-p1: + persistence-p2: + persistence-p3: + db-p0: + db-p1: + db-p2: + db-p3: + +services: + + # TODO rename to eth.node + ethnode: + image: ethereum/client-go:latest + environment: + POADIR: /opt/poa + DATADIR: /opt/poa/data + KEYSTORE: /opt/poa/keystore/server_0 + networks: + - hbswap + volumes: + - ./scripts/chain-latest.sh:/usr/src/chain-latest.sh + - ./poa:/opt/poa + working_dir: /usr/src/ + entrypoint: "" + command: sh chain-latest.sh + + deploy.contract: + image: hbswap-go + build: + context: . + dockerfile: Dockerfile + target: go-deps + environment: + POADIR: /opt/poa + POA_KEYSTORE: /opt/poa/keystore + networks: + - hbswap + working_dir: /go/src/github.com/initc3/HoneyBadgerSwap/src/go + volumes: + - ./poa:/opt/poa + - ./scripts/wait-for-it.sh:/usr/local/bin/wait-for-it + - ./src:/go/src/github.com/initc3/HoneyBadgerSwap/src + entrypoint: "" + command: ["wait-for-it", "ethnode:8545", "--", "go", "run", "deploy/deploy.go", "ethnode"] + + mpc.trusted.setup: + image: sbellem/openssl:bullseye-slim + networks: + - hbswap + volumes: + - ./MP-SPDZ/Scripts/setup-ssl.sh:/usr/src/setup-ssl.sh + - ./scripts/generate-keys.sh:/usr/src/generate-keys.sh + - public-keys:/usr/src/Player-Data + - secrets-p0:/usr/src/Secrets-P0 + - secrets-p1:/usr/src/Secrets-P1 + - secrets-p2:/usr/src/Secrets-P2 + - secrets-p3:/usr/src/Secrets-P3 + #command: ["bash", "setup-ssl.sh", "4"] + working_dir: /usr/src + command: ["bash", "generate-keys.sh", "setup-ssl.sh", "4"] + + # compile MPC programs (hbswap_init, hbswap_trade_prep, hbswap_trade) + # see scripts/compile.sh for details + mpc.compile: + image: python:3.8 + networks: + - hbswap + volumes: + - ./MP-SPDZ/compile.py:/usr/src/compile.py + - ./MP-SPDZ/Compiler:/usr/src/Compiler + - ./src/mpc:/usr/src/Programs/Source + - ./scripts/compile.sh:/usr/src/compile.sh + - mpc-bytecodes:/usr/src/Programs/Bytecode + - mpc-schedules:/usr/src/Programs/Schedules + working_dir: /usr/src + command: ["bash", "compile.sh"] + + mpcnode0: + image: hbswap + build: + context: . + dockerfile: Dockerfile + environment: + POADIR: /opt/poa + POA_KEYSTORE: /opt/poa/keystore + PYTHONUNBUFFERED: 1 + ports: + - 58080:8080 + networks: + - hbswap + volumes: + - mpc-bytecodes:/usr/src/hbswap/Programs/Bytecode + - mpc-schedules:/usr/src/hbswap/Programs/Schedules + - ./conf/server.toml:/opt/hbswap/conf/server.toml + - ./scripts/mpc-node.sh:/usr/src/hbswap/mpc-node.sh + - ./scripts/wait-for-it.sh:/usr/local/bin/wait-for-it + - ./src:/go/src/github.com/initc3/HoneyBadgerSwap/src + - public-keys:/opt/hbswap/public-keys:ro + - secrets-p0:/opt/hbswap/secrets + - ./poa:/opt/poa + #- inputmaskshares-p0:/opt/hbswap/inputmask-shares + #- ppdata-p0:/opt/hbswap/preprocessing-data + #- persistence-p0:/usr/src/hbswap/Persistence + working_dir: /usr/src/hbswap + # NOTE IMPORTANT! + # The hostname (2nd arg) must be the one of player 0, i.e. "mpcnode0" + command: ["wait-for-it", "ethnode:8545", "--", "bash", "mpc-node.sh", "0", "ethnode", "mpcnode0"] + + mpcnode1: + image: hbswap + build: + context: . + dockerfile: Dockerfile + environment: + POADIR: /opt/poa + POA_KEYSTORE: /opt/poa/keystore + PYTHONUNBUFFERED: 1 + ports: + - 58081:8080 + networks: + - hbswap + volumes: + - mpc-bytecodes:/usr/src/hbswap/Programs/Bytecode + - mpc-schedules:/usr/src/hbswap/Programs/Schedules + - ./conf/server.toml:/opt/hbswap/conf/server.toml + - ./scripts/mpc-node.sh:/usr/src/hbswap/mpc-node.sh + - ./scripts/wait-for-it.sh:/usr/local/bin/wait-for-it + - ./src:/go/src/github.com/initc3/HoneyBadgerSwap/src + - public-keys:/opt/hbswap/public-keys:ro + - secrets-p1:/opt/hbswap/secrets + - ./poa:/opt/poa + working_dir: /usr/src/hbswap + # NOTE IMPORTANT! + # The hostname (2nd arg) must be the one of player 0, i.e. "mpcnode0" + command: ["wait-for-it", "ethnode:8545", "--", "bash", "mpc-node.sh", "1", "ethnode", "mpcnode0"] + + mpcnode2: + image: hbswap + build: + context: . + dockerfile: Dockerfile + environment: + POADIR: /opt/poa + POA_KEYSTORE: /opt/poa/keystore + PYTHONUNBUFFERED: 1 + ports: + - 58082:8080 + networks: + - hbswap + volumes: + - mpc-bytecodes:/usr/src/hbswap/Programs/Bytecode + - mpc-schedules:/usr/src/hbswap/Programs/Schedules + - ./conf/server.toml:/opt/hbswap/conf/server.toml + - ./scripts/mpc-node.sh:/usr/src/hbswap/mpc-node.sh + - ./scripts/wait-for-it.sh:/usr/local/bin/wait-for-it + - ./src:/go/src/github.com/initc3/HoneyBadgerSwap/src + - public-keys:/opt/hbswap/public-keys:ro + - secrets-p2:/opt/hbswap/secrets + - ./poa:/opt/poa + working_dir: /usr/src/hbswap + # NOTE IMPORTANT! + # The hostname (2nd arg) must be the one of player 0, i.e. "mpcnode0" + command: ["wait-for-it", "ethnode:8545", "--", "bash", "mpc-node.sh", "2", "ethnode", "mpcnode0"] + + mpcnode3: + image: hbswap + build: + context: . + dockerfile: Dockerfile + environment: + POADIR: /opt/poa + POA_KEYSTORE: /opt/poa/keystore + PYTHONUNBUFFERED: 1 + ports: + - 58083:8080 + networks: + - hbswap + volumes: + - mpc-bytecodes:/usr/src/hbswap/Programs/Bytecode + - mpc-schedules:/usr/src/hbswap/Programs/Schedules + - ./conf/server.toml:/opt/hbswap/conf/server.toml + - ./scripts/mpc-node.sh:/usr/src/hbswap/mpc-node.sh + - ./scripts/wait-for-it.sh:/usr/local/bin/wait-for-it + - ./src:/go/src/github.com/initc3/HoneyBadgerSwap/src + - public-keys:/opt/hbswap/public-keys:ro + - secrets-p3:/opt/hbswap/secrets + - ./poa:/opt/poa + working_dir: /usr/src/hbswap + # NOTE IMPORTANT! + # The hostname (2nd arg) must be the one of player 0, i.e. "mpcnode0" + command: ["wait-for-it", "ethnode:8545", "--", "bash", "mpc-node.sh", "3", "ethnode", "mpcnode0"] diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 00000000..fdda11d0 --- /dev/null +++ b/docker/README.md @@ -0,0 +1,22 @@ +# docker images + +## [goeth.Dockerfile](https://hub.docker.com/repository/docker/sbellem/go-ethereum) +`go-ethereum` image, commit `cfbb969da` by default. Pre-built at +https://hub.docker.com/repository/docker/sbellem/go-ethereum. + +## [openssl.Dockerfile](https://hub.docker.com/repository/docker/sbellem/openssl) +`openssl` on `bullseye-slim` base image. Pre-built at +https://hub.docker.com/repository/docker/sbellem/openssl. + +## [mpspdzbuilds.Dockerfile](https://hub.docker.com/repository/docker/sbellem/mpspdz) +MP-SPDZ builds used by HoneyBadgerSwap:`random-shamir.x` and +`malicious-shamir-party.x`. Pre-builts available from +https://hub.docker.com/repository/docker/sbellem/mpspdz. Different builds +are available as different tags: + +* [`sbellem/mpspdz:randomshamirprep`](https://hub.docker.com/layers/139098383/sbellem/mpspdz/randomshamirprep/images/sha256-eab1c1ab1641ced1a2e89b4c0c6552948e4e1c9ade08d66b942b625169015f09?context=explore) +* [`sbellem/mpspdz:maliciousshamirparty`](https://hub.docker.com/layers/139098336/sbellem/mpspdz/maliciousshamirparty/images/sha256-ac1f16820a16149d6991c916dd2a686139c11d28c4db0e8c861f457796537f8d?context=explore) + +Note that in order to run these pre-built binaries, `mpir` is necessary. See +the `Dockerfile` under the root of this repository for examples of how these +images can be used. diff --git a/docker/dev.Dockerfile b/docker/dev.Dockerfile new file mode 100644 index 00000000..ca170f26 --- /dev/null +++ b/docker/dev.Dockerfile @@ -0,0 +1,159 @@ +FROM python:3.8 as base-mp-spdz + +ENV PYTHONUNBUFFERED 1 + +RUN apt-get update && apt-get install -y --no-install-recommends \ + automake \ + build-essential \ + git \ + libboost-dev \ + libboost-thread-dev \ + libsodium-dev \ + libssl-dev \ + libtool \ + m4 \ + texinfo \ + yasm \ + vim \ + gdb \ + valgrind \ + && rm -rf /var/lib/apt/lists/* + +ENV MP_SPDZ_HOME /usr/src/MP-SPDZ +WORKDIR $MP_SPDZ_HOME + +# mpir +ENV LD_LIBRARY_PATH /usr/local/lib +RUN mkdir -p /usr/local/share/info +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/lib/libmpir*.*a /usr/local/lib/ +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/lib/libmpir.so.23.0.3 /usr/local/lib/ +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/lib/libmpirxx.so.8.4.3 /usr/local/lib/ +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/include/mpir*.h /usr/local/include/ +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/share/info/* /usr/local/share/info/ +RUN set -ex \ + && cd /usr/local/lib \ + && ln -s libmpir.so.23.0.3 libmpir.so \ + && ln -s libmpir.so.23.0.3 libmpir.so.23 \ + && ln -s libmpirxx.so.8.4.3 libmpirxx.so \ + && ln -s libmpirxx.so.8.4.3 libmpirxx.so.8 + +COPY MP-SPDZ/Makefile . +COPY MP-SPDZ/CONFIG . +COPY MP-SPDZ/BMR BMR +COPY MP-SPDZ/Exceptions Exceptions +COPY MP-SPDZ/GC GC +COPY MP-SPDZ/Machines Machines +COPY MP-SPDZ/Math Math +COPY MP-SPDZ/Networking Networking +COPY MP-SPDZ/OT OT +COPY MP-SPDZ/Processor Processor +COPY MP-SPDZ/Protocols Protocols +COPY MP-SPDZ/SimpleOT SimpleOT +COPY MP-SPDZ/Tools Tools +COPY MP-SPDZ/Utils Utils + +RUN make clean + +# DEBUG and configuration flags +RUN echo "MY_CFLAGS += -DDEBUG_NETWORKING" >> CONFIG.mine \ + && echo "MY_CFLAGS += -DVERBOSE" >> CONFIG.mine \ + && echo "MY_CFLAGS += -DDEBUG_MAC" >> CONFIG.mine \ + && echo "MY_CFLAGS += -DDEBUG_FILE" >> CONFIG.mine \ + && echo "MOD = -DGFP_MOD_SZ=4" >> CONFIG.mine + +ENV PRIME 52435875175126190479447740508185965837690552500527637822603658699938581184513 +ENV N_PARTIES 4 +ENV THRESHOLD 1 + +# Compile random-shamir +FROM base-mp-spdz as inputmask-preprocessing +ENV INPUTMASK_SHARES "/opt/hbswap/inputmask-shares" +RUN mkdir -p $INPUTMASK_SHARES \ + && echo "PREP_DIR = '-DPREP_DIR=\"/opt/hbswap/inputmask-shares/\"'" >> CONFIG.mine +RUN make random-shamir.x + +# Compile malicious-shamir-party +FROM base-mp-spdz as malicious-shamir +ENV PREP_DIR "/opt/hbswap/preprocessing-data" +RUN mkdir -p $PREP_DIR \ + && echo "PREP_DIR = '-DPREP_DIR=\"/opt/hbswap/preprocessing-data/\"'" >> CONFIG.mine +RUN make malicious-shamir-party.x + +########################## end of mp-spdz builds ####################################### + +#FROM base-mp-spdz as hbswap +FROM python:3.8 as hbswap + +ENV PYTHONUNBUFFERED 1 + +############################################################################# +# GO (server) dependencies +ENV PATH /usr/local/go/bin:$PATH +COPY --from=golang:1.15.6-buster /usr/local/go /usr/local/go +ENV GOPATH /go +ENV PATH $GOPATH/bin:$PATH +RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH" + +RUN go get -d -v github.com/ethereum/go-ethereum + +WORKDIR $GOPATH/src/github.com/ethereum/go-ethereum +RUN git checkout cfbb969da + +COPY src /go/src/github.com/initc3/HoneyBadgerSwap/src + +WORKDIR /go/src/github.com/initc3/HoneyBadgerSwap/src + +RUN go get -d -v ./... +############################################################################# + + +############################################################################# +# MP-SPDZ +RUN apt-get update && apt-get install -y --no-install-recommends \ + build-essential \ + libboost-dev \ + libboost-thread-dev \ + libsodium-dev \ + libssl-dev \ + libtool \ + m4 \ + texinfo \ + yasm \ + && rm -rf /var/lib/apt/lists/* + +ENV HBSWAP_HOME /usr/src/hbswap +WORKDIR $HBSWAP_HOME +ENV INPUTMASK_SHARES "/opt/hbswap/inputmask-shares" +ENV PREP_DIR "/opt/hbswap/preprocessing-data" +COPY --from=inputmask-preprocessing /usr/src/MP-SPDZ/random-shamir.x /usr/src/hbswap/ +COPY --from=malicious-shamir /usr/src/MP-SPDZ/malicious-shamir-party.x /usr/src/hbswap/ +RUN mkdir -p $INPUTMASK_SHARES +RUN mkdir -p $PREP_DIR +# mpir +ENV LD_LIBRARY_PATH /usr/local/lib +RUN mkdir -p /usr/local/share/info +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/lib/libmpir*.*a /usr/local/lib/ +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/lib/libmpir.so.23.0.3 /usr/local/lib/ +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/lib/libmpirxx.so.8.4.3 /usr/local/lib/ +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/include/mpir*.h /usr/local/include/ +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/share/info/* /usr/local/share/info/ +RUN set -ex \ + && cd /usr/local/lib \ + && ln -s libmpir.so.23.0.3 libmpir.so \ + && ln -s libmpir.so.23.0.3 libmpir.so.23 \ + && ln -s libmpirxx.so.8.4.3 libmpirxx.so \ + && ln -s libmpirxx.so.8.4.3 libmpirxx.so.8 +############################################################################# + +ENV DB_PATH /opt/hbswap/db + +# Python (HTTP server) dependencies for HTTP server +RUN apt-get update && apt-get install -y --no-install-recommends \ + lsof \ + libmpfr-dev \ + libmpc-dev \ + && rm -rf /var/lib/apt/lists/* + +COPY src/python /usr/src/honeybadgerswap-python +WORKDIR /usr/src/honeybadgerswap-python +RUN pip install --editable . diff --git a/docker/goeth.Dockerfile b/docker/goeth.Dockerfile new file mode 100644 index 00000000..8a1b1d37 --- /dev/null +++ b/docker/goeth.Dockerfile @@ -0,0 +1,12 @@ +FROM golang:1.15.8-buster + +# TODO Work with go module mode +# SEE https://github.com/golang/go/wiki/Modules +# RUN go get -d -v github.com/ethereum/go-ethereum@cfbb969da +# RUN go get -d -v github.com/ethereum/go-ethereum +ARG commit=cfbb969da + +RUN go get -d -v github.com/ethereum/go-ethereum + +WORKDIR $GOPATH/src/github.com/ethereum/go-ethereum +RUN git checkout $commit diff --git a/docker/mpspdzbuilds.Dockerfile b/docker/mpspdzbuilds.Dockerfile new file mode 100644 index 00000000..d248fb0d --- /dev/null +++ b/docker/mpspdzbuilds.Dockerfile @@ -0,0 +1,77 @@ +FROM python:3.8 as mpspdz-base + +ENV PYTHONUNBUFFERED 1 + +RUN apt-get update && apt-get install -y --no-install-recommends \ + automake \ + build-essential \ + git \ + libboost-dev \ + libboost-thread-dev \ + libsodium-dev \ + libssl-dev \ + libtool \ + m4 \ + texinfo \ + yasm \ + && rm -rf /var/lib/apt/lists/* + +ENV MP_SPDZ_HOME /usr/src/MP-SPDZ +WORKDIR $MP_SPDZ_HOME + +# mpir +ENV LD_LIBRARY_PATH /usr/local/lib +RUN mkdir -p /usr/local/share/info +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/lib/libmpir*.*a /usr/local/lib/ +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/lib/libmpir.so.23.0.3 /usr/local/lib/ +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/lib/libmpirxx.so.8.4.3 /usr/local/lib/ +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/include/mpir*.h /usr/local/include/ +COPY --from=initc3/mpir:55fe6a9 /usr/local/mpir/share/info/* /usr/local/share/info/ +RUN set -ex \ + && cd /usr/local/lib \ + && ln -s libmpir.so.23.0.3 libmpir.so \ + && ln -s libmpir.so.23.0.3 libmpir.so.23 \ + && ln -s libmpirxx.so.8.4.3 libmpirxx.so \ + && ln -s libmpirxx.so.8.4.3 libmpirxx.so.8 + +COPY MP-SPDZ/Makefile . +COPY MP-SPDZ/CONFIG . +COPY MP-SPDZ/BMR BMR +COPY MP-SPDZ/Exceptions Exceptions +COPY MP-SPDZ/GC GC +COPY MP-SPDZ/Machines Machines +COPY MP-SPDZ/Math Math +COPY MP-SPDZ/Networking Networking +COPY MP-SPDZ/OT OT +COPY MP-SPDZ/Processor Processor +COPY MP-SPDZ/Protocols Protocols +COPY MP-SPDZ/SimpleOT SimpleOT +COPY MP-SPDZ/Tools Tools +COPY MP-SPDZ/Utils Utils + +RUN make clean + +# DEBUG and configuration flags +RUN echo "MY_CFLAGS += -DDEBUG_NETWORKING" >> CONFIG.mine \ + && echo "MY_CFLAGS += -DVERBOSE" >> CONFIG.mine \ + && echo "MY_CFLAGS += -DDEBUG_MAC" >> CONFIG.mine \ + && echo "MY_CFLAGS += -DDEBUG_FILE" >> CONFIG.mine \ + && echo "MOD = -DGFP_MOD_SZ=4" >> CONFIG.mine + +ENV PRIME 52435875175126190479447740508185965837690552500527637822603658699938581184513 +ENV N_PARTIES 4 +ENV THRESHOLD 1 + +# Compile random-shamir +FROM mpspdz-base as random-shamir-prep +ENV INPUTMASK_SHARES "/opt/hbswap/inputmask-shares" +RUN mkdir -p $INPUTMASK_SHARES \ + && echo "PREP_DIR = '-DPREP_DIR=\"/opt/hbswap/inputmask-shares/\"'" >> CONFIG.mine +RUN make random-shamir.x + +# Compile malicious-shamir-party +FROM mpspdz-base as malicious-shamir-party +ENV PREP_DIR "/opt/hbswap/preprocessing-data" +RUN mkdir -p $PREP_DIR \ + && echo "PREP_DIR = '-DPREP_DIR=\"/opt/hbswap/preprocessing-data/\"'" >> CONFIG.mine +RUN make malicious-shamir-party.x diff --git a/docker/openssl.Dockerfile b/docker/openssl.Dockerfile new file mode 100644 index 00000000..1adf21cf --- /dev/null +++ b/docker/openssl.Dockerfile @@ -0,0 +1,4 @@ +FROM debian:bullseye-slim + +RUN apt-get update && apt-get install -y --no-install-recommends openssl \ + && rm -rf /var/lib/apt/lists/* diff --git a/liquidity-provider.yml b/liquidity-provider.yml new file mode 100644 index 00000000..37512520 --- /dev/null +++ b/liquidity-provider.yml @@ -0,0 +1,23 @@ +version: '3.8' + +services: + init.pool: + image: hbswap-go + build: + context: . + dockerfile: Dockerfile + target: go-deps + environment: + POADIR: /opt/poa + POA_KEYSTORE: /opt/poa/keystore + networks: + - hbswap + working_dir: /usr/src/scripts + volumes: + - ./poa:/opt/poa + - ./scripts/wait-for-it.sh:/usr/local/bin/wait-for-it + - ./src:/go/src/github.com/initc3/HoneyBadgerSwap/src + - ./scripts:/usr/src/scripts + entrypoint: "" + # TODO wait for contract to be deployed/available + command: ["bash", "init-pool.sh", "ethnode"] diff --git a/poa/empty_password.txt b/poa/empty_password.txt new file mode 100644 index 00000000..e69de29b diff --git a/poa/genesis.json b/poa/genesis.json new file mode 100644 index 00000000..68572643 --- /dev/null +++ b/poa/genesis.json @@ -0,0 +1,28 @@ +{ + "config": { + "chainId": 123, + "homesteadBlock": 0, + "eip150Block": 0, + "eip155Block": 0, + "eip158Block": 0, + "byzantiumBlock": 0, + "constantinopleBlock": 0, + "clique": { + "period": 1, + "epoch": 30000 + } + }, + "alloc": { + "096f48173c8eb3849bb415f62d3113cb3b90c640": { "balance": "0x200000000000000000000000000000000000000000000000000000000000000" } + }, + "coinbase": "0x0000000000000000000000000000000000000000", + "difficulty": "1", + "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000096f48173c8eb3849bb415f62d3113cb3b90c6400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "gasLimit": "20000000", + "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "nonce": "0", + "timestamp": "0x5b613c8f" +} + + +//https://ethereum.stackexchange.com/questions/51091/clique-genesis-file \ No newline at end of file diff --git a/poa/keystore/account_0/UTC--2020-06-30T20-29-27.694714000Z--ef860fb0634474ae0e6cec1a8e0dbe7c70a280a5 b/poa/keystore/account_0/UTC--2020-06-30T20-29-27.694714000Z--ef860fb0634474ae0e6cec1a8e0dbe7c70a280a5 new file mode 100644 index 00000000..e09ce260 --- /dev/null +++ b/poa/keystore/account_0/UTC--2020-06-30T20-29-27.694714000Z--ef860fb0634474ae0e6cec1a8e0dbe7c70a280a5 @@ -0,0 +1 @@ +{"address":"ef860fb0634474ae0e6cec1a8e0dbe7c70a280a5","crypto":{"cipher":"aes-128-ctr","ciphertext":"ef78844ca575e03eab239f3a97cf5f2393326b3190a103e8ecfa4c8f0fa087a8","cipherparams":{"iv":"a56aa4c42ffc07a13708b2fe61a98e3d"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"6bb662badf3affd44acf50aec280074ee487ad86f30967ef69e64582f2d9074f"},"mac":"1e684483d72e49e4dfa5b9b4caba7a04ba9091f6512bacbd30a0e6976424f807"},"id":"f47efed8-2874-4801-85af-4a90af7f9c2b","version":3} \ No newline at end of file diff --git a/poa/keystore/account_1/UTC--2020-09-01T19-49-18.516678000Z--c8206540e1553206597fe74ae993e4f94ac79b81 b/poa/keystore/account_1/UTC--2020-09-01T19-49-18.516678000Z--c8206540e1553206597fe74ae993e4f94ac79b81 new file mode 100644 index 00000000..2289f3a2 --- /dev/null +++ b/poa/keystore/account_1/UTC--2020-09-01T19-49-18.516678000Z--c8206540e1553206597fe74ae993e4f94ac79b81 @@ -0,0 +1 @@ +{"address":"c8206540e1553206597fe74ae993e4f94ac79b81","crypto":{"cipher":"aes-128-ctr","ciphertext":"105e356826267ddde0e9f5f49764754490aa576d23cced736adfa390296522b7","cipherparams":{"iv":"75514a7c5aec1a67dd99b4efbaf02f07"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"d27665e4520970c9c7684eaf6ae23abff266b3aa241e71ec51a2ab0f59d6a744"},"mac":"bac71312a50ec89c7b1cfd51e5ea3969d2824c1ba8dc66671729bc4153c61a3d"},"id":"edf39d75-1e1c-48ba-8582-45258f680d8b","version":3} \ No newline at end of file diff --git a/poa/keystore/account_2/UTC--2020-09-02T02-33-39.871505000Z--2ec92501d03647b8a0e953cbf582fcf950101592 b/poa/keystore/account_2/UTC--2020-09-02T02-33-39.871505000Z--2ec92501d03647b8a0e953cbf582fcf950101592 new file mode 100644 index 00000000..1310ce99 --- /dev/null +++ b/poa/keystore/account_2/UTC--2020-09-02T02-33-39.871505000Z--2ec92501d03647b8a0e953cbf582fcf950101592 @@ -0,0 +1 @@ +{"address":"2ec92501d03647b8a0e953cbf582fcf950101592","crypto":{"cipher":"aes-128-ctr","ciphertext":"afa4edec99f171416b79bb192a2d23cd3d736676cf2cf5c148d6e6932c1664de","cipherparams":{"iv":"6b8fe9d65ae8d6a5bbec0ea9eec0f848"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"e0b4e683d735ab44faacbffd0f179ac307fc26f2f1659ecd90fdf62d2c7dca06"},"mac":"80f0af63171474dd1afd7b98b8e16d22c41f7334428cbc2f7162582aa9b59e3b"},"id":"22a4ee68-5b5f-4677-8a45-b2bb3e1b246a","version":3} \ No newline at end of file diff --git a/poa/keystore/account_3/UTC--2020-09-02T02-33-43.101223000Z--2b8a1bca2b97e1f25620ca245f95738fa8ce77c5 b/poa/keystore/account_3/UTC--2020-09-02T02-33-43.101223000Z--2b8a1bca2b97e1f25620ca245f95738fa8ce77c5 new file mode 100644 index 00000000..887b4057 --- /dev/null +++ b/poa/keystore/account_3/UTC--2020-09-02T02-33-43.101223000Z--2b8a1bca2b97e1f25620ca245f95738fa8ce77c5 @@ -0,0 +1 @@ +{"address":"2b8a1bca2b97e1f25620ca245f95738fa8ce77c5","crypto":{"cipher":"aes-128-ctr","ciphertext":"25be2ad91f53c9e5030e5ab75ee76c7195bee3641053bf2e186423e1d505c02b","cipherparams":{"iv":"2dae71148d7ae7d6997d4e29afe4bdfa"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"f47ea5b60c8293905e312c708416ab02bcb88954380ec163ceb7e6bd215ad0b9"},"mac":"8438dd11b7c769b57af0cfc99557a0d6a8079c60ab3884f10fa02b3b7c7b852a"},"id":"ef78cf08-1a67-45da-8e55-bf25b6a0cdfe","version":3} \ No newline at end of file diff --git a/poa/keystore/server_0/UTC--2019-09-18T04-48-01.635775000Z--096f48173c8eb3849bb415f62d3113cb3b90c640 b/poa/keystore/server_0/UTC--2019-09-18T04-48-01.635775000Z--096f48173c8eb3849bb415f62d3113cb3b90c640 new file mode 100644 index 00000000..a3475d31 --- /dev/null +++ b/poa/keystore/server_0/UTC--2019-09-18T04-48-01.635775000Z--096f48173c8eb3849bb415f62d3113cb3b90c640 @@ -0,0 +1 @@ +{"address":"096f48173c8eb3849bb415f62d3113cb3b90c640","crypto":{"cipher":"aes-128-ctr","ciphertext":"8046cde74e9b011244d23e5fe2fb15eddb8ded7a35a62deb049fa91ce5556a74","cipherparams":{"iv":"e49ee2a471084b5ab35ea9eedf9d935a"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"5f90e83b9307e10a1609f6ff40893c8a5151377ea654ed194e0e6ce41f237213"},"mac":"282821ce65d3dcec6d149159a9f4a9249e415e9f329638c751f5b328e5215a14"},"id":"ea3770d2-6dda-417a-a2c9-1bea0f5cac98","version":3} \ No newline at end of file diff --git a/poa/keystore/server_1/UTC--2019-11-14T21-08-52.001723000Z--4878a9004dd90d2c2c9e87079f12a938cfad7a64 b/poa/keystore/server_1/UTC--2019-11-14T21-08-52.001723000Z--4878a9004dd90d2c2c9e87079f12a938cfad7a64 new file mode 100644 index 00000000..9564fdda --- /dev/null +++ b/poa/keystore/server_1/UTC--2019-11-14T21-08-52.001723000Z--4878a9004dd90d2c2c9e87079f12a938cfad7a64 @@ -0,0 +1 @@ +{"address":"4878a9004dd90d2c2c9e87079f12a938cfad7a64","crypto":{"cipher":"aes-128-ctr","ciphertext":"26991ebc18b9f17ea5104b4d715f8c3652a557e090793ae07d113f26238e855d","cipherparams":{"iv":"373dc5dd0db16b878dfffa496cd2544f"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"f3a6f080e103dae4a97a8bed69b4d76109f710080d739f48a2aeeb4d81ad42a5"},"mac":"f1922624f927bdcd2e3c819e4b43dcec249d6867294b7edd56b59f9760197e2d"},"id":"6017e5c8-9f4e-4d28-a97d-6c8709d9db18","version":3} \ No newline at end of file diff --git a/poa/keystore/server_2/UTC--2019-10-15T05-21-25.578144000Z--5d623446e5af093073393e1cb1f6630e142144a2 b/poa/keystore/server_2/UTC--2019-10-15T05-21-25.578144000Z--5d623446e5af093073393e1cb1f6630e142144a2 new file mode 100644 index 00000000..7351e5d7 --- /dev/null +++ b/poa/keystore/server_2/UTC--2019-10-15T05-21-25.578144000Z--5d623446e5af093073393e1cb1f6630e142144a2 @@ -0,0 +1 @@ +{"address":"5d623446e5af093073393e1cb1f6630e142144a2","crypto":{"cipher":"aes-128-ctr","ciphertext":"1b611e7f4c1a9dab6fdae0de9a913bf51d8ec44b74329ad4facbaf43e1053a31","cipherparams":{"iv":"581990974c1b3811d015e85cea29f2f4"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"908558fdd60962bac54dc97e4e185f8b23af80ba9f5f02d6bd13560df1837897"},"mac":"bf4de266abca30b07f70a84b2bc9a3f70d3f33dd61223ae52a6037ba296d66d6"},"id":"2164c26a-91c0-4122-9523-0957502c511b","version":3} \ No newline at end of file diff --git a/poa/keystore/server_3/UTC--2020-01-01T03-25-22.329767000Z--8ae054390170797fd871eec47fc77f5666c64d96 b/poa/keystore/server_3/UTC--2020-01-01T03-25-22.329767000Z--8ae054390170797fd871eec47fc77f5666c64d96 new file mode 100644 index 00000000..6f0f704a --- /dev/null +++ b/poa/keystore/server_3/UTC--2020-01-01T03-25-22.329767000Z--8ae054390170797fd871eec47fc77f5666c64d96 @@ -0,0 +1 @@ +{"address":"8ae054390170797fd871eec47fc77f5666c64d96","crypto":{"cipher":"aes-128-ctr","ciphertext":"2749d5579902135775a500608f561a6234be1a565ea1a7be63450310d8def732","cipherparams":{"iv":"1be1bcdb231bb44210bfe99188cf462d"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"978bcdab11d7a97ca4357d838d14693a76fc2771595d5eb143ad2a9eb107cb78"},"mac":"dfcff9b97b6c5880d2522e43f87fd507d55e9760dc36ec9b9f0ecc4d375a744e"},"id":"8505f5a5-345f-4610-8e7b-51b4a1d88ca9","version":3} \ No newline at end of file diff --git a/scripts/chain-latest.sh b/scripts/chain-latest.sh new file mode 100644 index 00000000..96662e0e --- /dev/null +++ b/scripts/chain-latest.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +POADIR=${POADIR:-/opt/poa} +DATADIR=${POA_DATADIR:-/opt/poa/data} +KEYSTORE=${POA_KEYSTORE:-/opt/poa/keystore/server_0} + +rm -rf $DATADIR +mkdir $DATADIR + +geth --datadir $DATADIR init $POADIR/genesis.json + +geth \ + --datadir $DATADIR \ + --keystore $KEYSTORE \ + --mine --allow-insecure-unlock --unlock 0 \ + --password $POADIR/empty_password.txt \ + --http \ + --http.addr 0.0.0.0 \ + --http.corsdomain '*' \ + --http.api admin,debug,eth,miner,net,personal,shh,txpool,web3 \ + --ws \ + --ws.addr 0.0.0.0 \ + --ws.origins '*' \ + --ws.api admin,debug,eth,miner,net,personal,shh,txpool,web3 \ + --syncmode full \ + --ipcpath "$DATADIR/geth.ipc" diff --git a/scripts/chain.sh b/scripts/chain.sh new file mode 100644 index 00000000..2a9c7dac --- /dev/null +++ b/scripts/chain.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +POADIR=${POADIR:-/opt/poa} +DATADIR=${DATADIR:-/opt/poa/data} +KEYSTORE=${KEYSTORE:-/opt/poa/keystore/server_0} + +geth --datadir $DATADIR init $POADIR/genesis.json + +geth \ + --datadir $DATADIR \ + --keystore $KEYSTORE \ + --mine --allow-insecure-unlock --unlock 0 \ + --password $POADIR/empty_password.txt \ + --rpc \ + --rpcaddr 0.0.0.0 \ + --rpccorsdomain '*' \ + --rpcapi admin,debug,eth,miner,net,personal,shh,txpool,web3 \ + --ws \ + --wsaddr 0.0.0.0 \ + --wsorigins '*' \ + --wsapi admin,debug,eth,miner,net,personal,shh,txpool,web3 \ + --syncmode full \ + --ipcpath "$DATADIR/geth.ipc" \ + 2>> $DATADIR/geth.log & diff --git a/scripts/client-public-deposit.sh b/scripts/client-public-deposit.sh new file mode 100644 index 00000000..73b88830 --- /dev/null +++ b/scripts/client-public-deposit.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +set -e + +eth_host=${1:-localhost} +go_code_path=/go/src/github.com/initc3/HoneyBadgerSwap/src/go + +. eth-data.sh + +deposit() { + go run $go_code_path/client/deposit.go $1 $2 $3 $4 $5 $eth_host +} + +deposit 0 $eth $token_1 10 10 diff --git a/scripts/client-secret-deposit.sh b/scripts/client-secret-deposit.sh new file mode 100644 index 00000000..36f2071f --- /dev/null +++ b/scripts/client-secret-deposit.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +set -e + +eth_host=${1:-localhost} +go_code_path=/go/src/github.com/initc3/HoneyBadgerSwap/src/go + +. eth-data.sh + +deposit() { + go run $go_code_path/client/secret_deposit.go $1 $2 $3 $4 $5 $eth_host +} + +deposit 0 $eth $token_1 10 10 diff --git a/scripts/client.sh b/scripts/client.sh new file mode 100644 index 00000000..73158b62 --- /dev/null +++ b/scripts/client.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +set -e + +eth_host=${1:-localhost} + +deposit() { + go run Scripts/hbswap/go/client/deposit.go $1 $2 $3 $eth_host +} + +withdraw() { + go run Scripts/hbswap/go/client/withdraw.go $1 $2 $3 $eth_host +} + +secret_deposit() { + go run Scripts/hbswap/go/client/secret_deposit.go $1 $2 $3 $eth_host +} + +secret_withdraw() { + go run Scripts/hbswap/go/client/secret_withdraw.go $1 $2 $3 $eth_host +} + +trade() { + go run Scripts/hbswap/go/client/trade.go $1 $2 $3 $eth_host +} + +secret_deposit 0 10 10 + +trade 0 1.1 -2.5 +trade 0 -1.1 2 + +secret_withdraw 0 9 9 + +withdraw 0 9 9 diff --git a/scripts/compile.sh b/scripts/compile.sh new file mode 100755 index 00000000..9d57dcb2 --- /dev/null +++ b/scripts/compile.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +set -e + +compile() { + ./compile.py -v -C -F 256 $1 +} + +rm -rf Programs/Bytecode/* Programs/Public-Input Programs/Schedules/* + +compile hbswap_add_liquidity +compile hbswap_calc_price +compile hbswap_check_balance +compile hbswap_remove_liquidity +compile hbswap_trade diff --git a/scripts/eth-data.sh b/scripts/eth-data.sh new file mode 100644 index 00000000..da113a41 --- /dev/null +++ b/scripts/eth-data.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +user='0xeF860fb0634474AE0E6ceC1A8E0dBE7C70A280A5' +eth='0x0000000000000000000000000000000000000000' +token_1='0x6b5c9637e0207c72Ee1a275b6C3b686ba8D87385' +token_2='0x8C89e5D2bCc0e4C26E3295d48d052E11bd03C06A' diff --git a/scripts/follow-sim-logs-with-tmux.sh b/scripts/follow-sim-logs-with-tmux.sh new file mode 100644 index 00000000..cc32041a --- /dev/null +++ b/scripts/follow-sim-logs-with-tmux.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +if [ -z $TMUX ]; then + echo "tmux is not active, will start new session" + TMUX_CMD="new-session" +else + echo "tmux is active, will launch into new window" + TMUX_CMD="new-window" +fi + +tmux $TMUX_CMD "docker-compose logs -f eth.chain; sh" \; \ + splitw -v -p 60 "docker-compose logs -f mpcnode0; sh" \; \ + splitw -v -p 60 "docker-compose logs -f mpcnode1; sh" \; \ + splitw -v -p 60 "docker-compose logs -f mpcnode2; sh" \; \ + splitw -v -p 60 "docker-compose logs -f mpcnode3; sh" \; \ + selectp -t 0 \; \ + splitw -h -p 60 "docker-compose logs -f contract.deploy; sh" \; \ + splitw -h -p 60 "docker-compose logs -f contract.deposit; sh" diff --git a/scripts/gen.sh b/scripts/gen.sh new file mode 100644 index 00000000..40fa44f2 --- /dev/null +++ b/scripts/gen.sh @@ -0,0 +1,29 @@ +set -e + +truffle_complie() { + rm -rf build/ + truffle compile +} + +extract_abi_bin() { + jq .abi build/contracts/$1.json > genfiles/$1.abi + jq -r .bytecode build/contracts/$1.json > genfiles/$1.bin +} + +abigen_files() { + INPUT_DIR=genfiles + + OUTPUT_DIR=go_bindings + mkdir -p $OUTPUT_DIR/$2 + abigen -abi $INPUT_DIR/$1.abi -bin $INPUT_DIR/$1.bin -pkg $2 -type $1 -out $OUTPUT_DIR/$2/$2.go +} + +sync_go_binding() { +# extract_abi_bin $1 + abigen_files $1 $2 +} + +cd Scripts/hbswap +#truffle_complie +sync_go_binding HbSwap hbswap +#sync_go_binding Token token \ No newline at end of file diff --git a/scripts/generate-keys.sh b/scripts/generate-keys.sh new file mode 100755 index 00000000..fff5b92d --- /dev/null +++ b/scripts/generate-keys.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +set -e + +script=${1:-"Scripts/setup-ssl.sh"} +nparties=${2:-4} + +rm -f Player-Data/* +bash $script $nparties + +for ((i=0; i < $nparties; i++)) +do + mkdir -p Secrets-P$i + mv Player-Data/P$i.key Secrets-P$i/ +done diff --git a/scripts/init-pool.sh b/scripts/init-pool.sh new file mode 100644 index 00000000..6c1f63e2 --- /dev/null +++ b/scripts/init-pool.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +set -e + +eth_host=${1:-localhost} +go_code_path=/go/src/github.com/initc3/HoneyBadgerSwap/src/go + +. eth-data.sh + +init_pool() { + go run $go_code_path/client/deposit.go $1 $2 $3 $4 $5 $eth_host + go run $go_code_path/client/secret_deposit.go $1 $2 $3 $4 $5 $eth_host + go run $go_code_path/client/init_pool.go $1 $2 $3 $4 $5 $eth_host +} + +init_pool 0 $eth $token_1 10 20 diff --git a/scripts/mpc-node.sh b/scripts/mpc-node.sh new file mode 100644 index 00000000..d4f817bb --- /dev/null +++ b/scripts/mpc-node.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +set -e + +#host=${1:-localhost} +node_id=$1 +eth_hostname=$2 +leader_hostname=$3 +go_code_path=/go/src/github.com/initc3/HoneyBadgerSwap/src/go + +# Place the data where MP-SPDZ expects it +setup_data() { + rm -rf /opt/hbswap/db + mkdir -p Persistence Player-Data + mkdir -p /opt/hbswap/db /opt/hbswap/inputmask-shares /opt/hbswap/preprocessing-data + # Copy the private key, where MP-SPDZ expects it to be (under Player-Data/). + cp /opt/hbswap/secrets/P$node_id.key Player-Data/ + # Copy the public keys of all players + cp /opt/hbswap/public-keys/* Player-Data/ +} + + +httpserver() { + python -m honeybadgerswap.server.start_server $1 +} + +mpcserver() { + go run $go_code_path/server/server.go $1 $eth_hostname $leader_hostname +} + +setup_data +httpserver $node_id & mpcserver $node_id diff --git a/scripts/prep.sh b/scripts/prep.sh new file mode 100755 index 00000000..6b3040ab --- /dev/null +++ b/scripts/prep.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +set -e + +db_dir=${DB_PATH:-/opt/hbswap/db} + +. Scripts/hbswap/scripts/utils.sh + +prepare() { + rm -rf Player-Data + + rm -rf Persistence + mkdir Persistence + + rm -rf $db_dir + mkdir $db_dir + + Scripts/setup-ssl.sh $players +} + +compile() { + ./compile.py -v -C -F 256 $1 +} + +prepare + +compile hbswap_add_liquidity +compile hbswap_calc_price +compile hbswap_check_balance +compile hbswap_remove_liquidity +compile hbswap_trade diff --git a/scripts/run.sh b/scripts/run.sh new file mode 100644 index 00000000..5f810dc6 --- /dev/null +++ b/scripts/run.sh @@ -0,0 +1,131 @@ +#!/usr/bin/env bash +set -e + +eth_host=${1:-localhost} +leader_host=${2:-localhost} + +. Scripts/hbswap/scripts/utils.sh + +prepare_nodes() { + echo 'Preparing mpc nodes...' + bash Scripts/hbswap/scripts/prep.sh + echo 'Finished preparing mpc nodes' +} + +start_local_network() { + echo 'Staring local network...' + pkill -f geth | true + rm -rf Scripts/hbswap/poa/data + bash Scripts/hbswap/scripts/chain.sh + echo 'Finished staring local network' +} + +deploy_contract() { + echo 'Deploying contracts...' + go run Scripts/hbswap/go/deploy/deploy.go $eth_host + echo 'Finished deploying contracts' +} + +shut_down() { + echo 'Shuting down previous instances...' + pkill -f random-shamir.x || true + pkill -f $prog || true + pkill -f start_server.py || true + pkill -f server.go || true + + rm -rf Scripts/hbswap/log + mkdir Scripts/hbswap/log + + sleep 2 + echo 'Finished shuting down previous instances' +} + +start_servers() { + echo 'Starting servers...' + mpcserver 0 & + mpcserver 1 & + mpcserver 2 & + mpcserver 3 & + httpserver 0 & + httpserver 1 & + httpserver 2 & + httpserver 3 & + echo 'Finished starting servers' +} + +deposit() { + go run Scripts/hbswap/go/client/deposit.go $1 $2 $3 $4 $5 $eth_host + go run Scripts/hbswap/go/client/secret_deposit.go $1 $2 $3 $4 $5 $eth_host +} + +init_pool() { + deposit $1 $2 $3 $4 $5 + go run Scripts/hbswap/go/client/init_pool.go $1 $2 $3 $4 $5 $eth_host +} + +add_liquidity() { + deposit $1 $2 $3 $4 $5 + go run Scripts/hbswap/go/client/add_liquidity.go $1 $2 $3 $4 $5 +} + +remove_liquidity() { + go run Scripts/hbswap/go/client/remove_liquidity.go $1 $2 $3 $4 +} + +withdraw() { + go run Scripts/hbswap/go/client/secret_withdraw.go $1 $2 $3 $4 $5 $eth_host + go run Scripts/hbswap/go/client/withdraw.go $1 $2 $3 $4 $5 $eth_host +} + +trade() { + go run Scripts/hbswap/go/client/trade.go $1 $2 $3 $4 $5 $eth_host +} + +httpserver() { + python3 Scripts/hbswap/python/server/start_server.py $1 +} + +mpcserver() { + go run Scripts/hbswap/go/server/server.go $1 $eth_host $leader_host > Scripts/hbswap/log/mpc_server_$1.log 2>&1 +} + +prepare_nodes + +start_local_network + +deploy_contract + +shut_down + +start_servers + +init_pool 0 $eth $token_1 10 20 +#add_liquidity 0 $eth $token_1 10 10 +#remove_liquidity 0 $eth $token_1 10 +#init_pool 0 $eth $token_2 10 20 + +deposit 0 $eth $token_1 10 10 +#deposit 0 $eth $token_2 10 10 + +#python3 Scripts/hbswap/python/client/req_balance.py $eth $user +#python3 Scripts/hbswap/python/client/req_balance.py $token_1 $user +#python3 Scripts/hbswap/python/client/req_balance.py $token_2 $user + +trade 0 $eth $token_1 1.1 -2.5 +#python3 Scripts/hbswap/python/client/req_price.py 1 +trade 0 $eth $token_1 -1.1 2 +#python3 Scripts/hbswap/python/client/req_price.py 2 +# +#trade 0 $eth $token_2 1.1 -2.5 +#python3 Scripts/hbswap/python/client/req_price.py 3 +#trade 0 $eth $token_2 -1.1 2 +#python3 Scripts/hbswap/python/client/req_price.py 4 + +#withdraw 0 $eth $token_1 9 9 +#withdraw 0 $eth $token_2 9 9 + + + + + + diff --git a/scripts/trade-1.sh b/scripts/trade-1.sh new file mode 100644 index 00000000..85e05781 --- /dev/null +++ b/scripts/trade-1.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +set -e + +eth_host=${1:-localhost} +go_code_path=/go/src/github.com/initc3/HoneyBadgerSwap/src/go +#user=$2 +#amt_A=$3 +#amt_B=$4 + + +. utils.sh + +trade() { + go run $go_code_path/client/trade.go $1 $2 $3 $4 $5 $eth_host +} + +trade 0 $eth $token_1 1.1 -2.5 +#trade $user $eth $token_1 $amt_A $amt_B + +#trade 0 $eth $token_1 -1.1 2 diff --git a/scripts/trade-2.sh b/scripts/trade-2.sh new file mode 100755 index 00000000..bdb7b472 --- /dev/null +++ b/scripts/trade-2.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +set -e + +eth_host=${1:-localhost} +go_code_path=/go/src/github.com/initc3/HoneyBadgerSwap/src/go +#user=$2 +#amt_A=$3 +#amt_B=$4 + + +. utils.sh + +trade() { + go run $go_code_path/client/trade.go $1 $2 $3 $4 $5 $eth_host +} + +#trade 0 $eth $token_1 1.1 -2.5 +#trade $user $eth $token_1 $amt_A $amt_B + +trade 0 $eth $token_1 -1.1 2 diff --git a/scripts/trade.sh b/scripts/trade.sh new file mode 100755 index 00000000..95ab1128 --- /dev/null +++ b/scripts/trade.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set -e + +eth_host=${1:-localhost} +#user=$2 +#amt_A=$3 +#amt_B=$4 + + +. Scripts/hbswap/scripts/utils.sh + +trade() { + go run Scripts/hbswap/go/client/trade.go $1 $2 $3 $4 $5 $eth_host +} + +#trade 0 $eth $token_1 1.1 -2.5 +#trade $user $eth $token_1 $amt_A $amt_B + +trade 0 $eth $token_1 -1.1 2 diff --git a/scripts/utils.sh b/scripts/utils.sh new file mode 100755 index 00000000..dc8d4665 --- /dev/null +++ b/scripts/utils.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +players=4 +threshold=1 +port=5000 +prime=52435875175126190479447740508185965837690552500527637822603658699938581184513 + +prog="malicious-shamir-party.x" + +user='0xeF860fb0634474AE0E6ceC1A8E0dBE7C70A280A5' +eth='0x0000000000000000000000000000000000000000' +token_1='0x6b5c9637e0207c72Ee1a275b6C3b686ba8D87385' +token_2='0x8C89e5D2bCc0e4C26E3295d48d052E11bd03C06A' \ No newline at end of file diff --git a/scripts/wait-for-it.sh b/scripts/wait-for-it.sh new file mode 100755 index 00000000..c5773a44 --- /dev/null +++ b/scripts/wait-for-it.sh @@ -0,0 +1,184 @@ +#!/usr/bin/env bash +# Use this script to test if a given TCP host/port are available + +# Source: https://github.com/vishnubob/wait-for-it/blob/c096cface5fbd9f2d6b037391dfecae6fde1362e/wait-for-it.sh + +WAITFORIT_cmdname=${0##*/} + +echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } + +usage() +{ + cat << USAGE >&2 +Usage: + $WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args] + -h HOST | --host=HOST Host or IP under test + -p PORT | --port=PORT TCP port under test + Alternatively, you specify the host and port as host:port + -s | --strict Only execute subcommand if the test succeeds + -q | --quiet Don't output any status messages + -t TIMEOUT | --timeout=TIMEOUT + Timeout in seconds, zero for no timeout + -- COMMAND ARGS Execute command with args after the test finishes +USAGE + exit 1 +} + +wait_for() +{ + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + else + echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout" + fi + WAITFORIT_start_ts=$(date +%s) + while : + do + if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then + nc -z $WAITFORIT_HOST $WAITFORIT_PORT + WAITFORIT_result=$? + else + (echo > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1 + WAITFORIT_result=$? + fi + if [[ $WAITFORIT_result -eq 0 ]]; then + WAITFORIT_end_ts=$(date +%s) + echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds" + break + fi + sleep 1 + done + return $WAITFORIT_result +} + +wait_for_wrapper() +{ + # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 + if [[ $WAITFORIT_QUIET -eq 1 ]]; then + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & + else + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & + fi + WAITFORIT_PID=$! + trap "kill -INT -$WAITFORIT_PID" INT + wait $WAITFORIT_PID + WAITFORIT_RESULT=$? + if [[ $WAITFORIT_RESULT -ne 0 ]]; then + echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + fi + return $WAITFORIT_RESULT +} + +# process arguments +while [[ $# -gt 0 ]] +do + case "$1" in + *:* ) + WAITFORIT_hostport=(${1//:/ }) + WAITFORIT_HOST=${WAITFORIT_hostport[0]} + WAITFORIT_PORT=${WAITFORIT_hostport[1]} + shift 1 + ;; + --child) + WAITFORIT_CHILD=1 + shift 1 + ;; + -q | --quiet) + WAITFORIT_QUIET=1 + shift 1 + ;; + -s | --strict) + WAITFORIT_STRICT=1 + shift 1 + ;; + -h) + WAITFORIT_HOST="$2" + if [[ $WAITFORIT_HOST == "" ]]; then break; fi + shift 2 + ;; + --host=*) + WAITFORIT_HOST="${1#*=}" + shift 1 + ;; + -p) + WAITFORIT_PORT="$2" + if [[ $WAITFORIT_PORT == "" ]]; then break; fi + shift 2 + ;; + --port=*) + WAITFORIT_PORT="${1#*=}" + shift 1 + ;; + -t) + WAITFORIT_TIMEOUT="$2" + if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi + shift 2 + ;; + --timeout=*) + WAITFORIT_TIMEOUT="${1#*=}" + shift 1 + ;; + --) + shift + WAITFORIT_CLI=("$@") + break + ;; + --help) + usage + ;; + *) + echoerr "Unknown argument: $1" + usage + ;; + esac +done + +if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then + echoerr "Error: you need to provide a host and port to test." + usage +fi + +WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15} +WAITFORIT_STRICT=${WAITFORIT_STRICT:-0} +WAITFORIT_CHILD=${WAITFORIT_CHILD:-0} +WAITFORIT_QUIET=${WAITFORIT_QUIET:-0} + +# Check to see if timeout is from busybox? +WAITFORIT_TIMEOUT_PATH=$(type -p timeout) +WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH) + +WAITFORIT_BUSYTIMEFLAG="" +if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then + WAITFORIT_ISBUSY=1 + # Check if busybox timeout uses -t flag + # (recent Alpine versions don't support -t anymore) + if timeout &>/dev/stdout | grep -q -e '-t '; then + WAITFORIT_BUSYTIMEFLAG="-t" + fi +else + WAITFORIT_ISBUSY=0 +fi + +if [[ $WAITFORIT_CHILD -gt 0 ]]; then + wait_for + WAITFORIT_RESULT=$? + exit $WAITFORIT_RESULT +else + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + wait_for_wrapper + WAITFORIT_RESULT=$? + else + wait_for + WAITFORIT_RESULT=$? + fi +fi + +if [[ $WAITFORIT_CLI != "" ]]; then + if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then + echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess" + exit $WAITFORIT_RESULT + fi + exec "${WAITFORIT_CLI[@]}" +else + exit $WAITFORIT_RESULT +fi diff --git a/src/contracts/FlattenedHbSwap.sol b/src/contracts/FlattenedHbSwap.sol new file mode 100644 index 00000000..8b87e9d4 --- /dev/null +++ b/src/contracts/FlattenedHbSwap.sol @@ -0,0 +1,621 @@ +// File: @openzeppelin/contracts/math/SafeMath.sol + +pragma solidity ^0.6.0; + +/** + * @dev Wrappers over Solidity's arithmetic operations with added overflow + * checks. + * + * Arithmetic operations in Solidity wrap on overflow. This can easily result + * in bugs, because programmers usually assume that an overflow raises an + * error, which is the standard behavior in high level programming languages. + * `SafeMath` restores this intuition by reverting the transaction when an + * operation overflows. + * + * Using this library instead of the unchecked operations eliminates an entire + * class of bugs, so it's recommended to use it always. + */ +library SafeMath { + /** + * @dev Returns the addition of two unsigned integers, reverting on + * overflow. + * + * Counterpart to Solidity's `+` operator. + * + * Requirements: + * + * - Addition cannot overflow. + */ + function add(uint256 a, uint256 b) internal pure returns (uint256) { + uint256 c = a + b; + require(c >= a, "SafeMath: addition overflow"); + + return c; + } + + /** + * @dev Returns the subtraction of two unsigned integers, reverting on + * overflow (when the result is negative). + * + * Counterpart to Solidity's `-` operator. + * + * Requirements: + * + * - Subtraction cannot overflow. + */ + function sub(uint256 a, uint256 b) internal pure returns (uint256) { + return sub(a, b, "SafeMath: subtraction overflow"); + } + + /** + * @dev Returns the subtraction of two unsigned integers, reverting with custom message on + * overflow (when the result is negative). + * + * Counterpart to Solidity's `-` operator. + * + * Requirements: + * + * - Subtraction cannot overflow. + */ + function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { + require(b <= a, errorMessage); + uint256 c = a - b; + + return c; + } + + /** + * @dev Returns the multiplication of two unsigned integers, reverting on + * overflow. + * + * Counterpart to Solidity's `*` operator. + * + * Requirements: + * + * - Multiplication cannot overflow. + */ + function mul(uint256 a, uint256 b) internal pure returns (uint256) { + // Gas optimization: this is cheaper than requiring 'a' not being zero, but the + // benefit is lost if 'b' is also tested. + // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 + if (a == 0) { + return 0; + } + + uint256 c = a * b; + require(c / a == b, "SafeMath: multiplication overflow"); + + return c; + } + + /** + * @dev Returns the integer division of two unsigned integers. Reverts on + * division by zero. The result is rounded towards zero. + * + * Counterpart to Solidity's `/` operator. Note: this function uses a + * `revert` opcode (which leaves remaining gas untouched) while Solidity + * uses an invalid opcode to revert (consuming all remaining gas). + * + * Requirements: + * + * - The divisor cannot be zero. + */ + function div(uint256 a, uint256 b) internal pure returns (uint256) { + return div(a, b, "SafeMath: division by zero"); + } + + /** + * @dev Returns the integer division of two unsigned integers. Reverts with custom message on + * division by zero. The result is rounded towards zero. + * + * Counterpart to Solidity's `/` operator. Note: this function uses a + * `revert` opcode (which leaves remaining gas untouched) while Solidity + * uses an invalid opcode to revert (consuming all remaining gas). + * + * Requirements: + * + * - The divisor cannot be zero. + */ + function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { + require(b > 0, errorMessage); + uint256 c = a / b; + // assert(a == b * c + a % b); // There is no case in which this doesn't hold + + return c; + } + + /** + * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), + * Reverts when dividing by zero. + * + * Counterpart to Solidity's `%` operator. This function uses a `revert` + * opcode (which leaves remaining gas untouched) while Solidity uses an + * invalid opcode to revert (consuming all remaining gas). + * + * Requirements: + * + * - The divisor cannot be zero. + */ + function mod(uint256 a, uint256 b) internal pure returns (uint256) { + return mod(a, b, "SafeMath: modulo by zero"); + } + + /** + * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), + * Reverts with custom message when dividing by zero. + * + * Counterpart to Solidity's `%` operator. This function uses a `revert` + * opcode (which leaves remaining gas untouched) while Solidity uses an + * invalid opcode to revert (consuming all remaining gas). + * + * Requirements: + * + * - The divisor cannot be zero. + */ + function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { + require(b != 0, errorMessage); + return a % b; + } +} + +// File: @openzeppelin/contracts/token/ERC20/IERC20.sol + +pragma solidity ^0.6.0; + +/** + * @dev Interface of the ERC20 standard as defined in the EIP. + */ +interface IERC20 { + /** + * @dev Returns the amount of tokens in existence. + */ + function totalSupply() external view returns (uint256); + + /** + * @dev Returns the amount of tokens owned by `account`. + */ + function balanceOf(address account) external view returns (uint256); + + /** + * @dev Moves `amount` tokens from the caller's account to `recipient`. + * + * Returns a boolean value indicating whether the operation succeeded. + * + * Emits a {Transfer} event. + */ + function transfer(address recipient, uint256 amount) external returns (bool); + + /** + * @dev Returns the remaining number of tokens that `spender` will be + * allowed to spend on behalf of `owner` through {transferFrom}. This is + * zero by default. + * + * This value changes when {approve} or {transferFrom} are called. + */ + function allowance(address owner, address spender) external view returns (uint256); + + /** + * @dev Sets `amount` as the allowance of `spender` over the caller's tokens. + * + * Returns a boolean value indicating whether the operation succeeded. + * + * IMPORTANT: Beware that changing an allowance with this method brings the risk + * that someone may use both the old and the new allowance by unfortunate + * transaction ordering. One possible solution to mitigate this race + * condition is to first reduce the spender's allowance to 0 and set the + * desired value afterwards: + * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 + * + * Emits an {Approval} event. + */ + function approve(address spender, uint256 amount) external returns (bool); + + /** + * @dev Moves `amount` tokens from `sender` to `recipient` using the + * allowance mechanism. `amount` is then deducted from the caller's + * allowance. + * + * Returns a boolean value indicating whether the operation succeeded. + * + * Emits a {Transfer} event. + */ + function transferFrom(address sender, address recipient, uint256 amount) external returns (bool); + + /** + * @dev Emitted when `value` tokens are moved from one account (`from`) to + * another (`to`). + * + * Note that `value` may be zero. + */ + event Transfer(address indexed from, address indexed to, uint256 value); + + /** + * @dev Emitted when the allowance of a `spender` for an `owner` is set by + * a call to {approve}. `value` is the new allowance. + */ + event Approval(address indexed owner, address indexed spender, uint256 value); +} + +// File: @openzeppelin/contracts/utils/Address.sol + +pragma solidity ^0.6.2; + +/** + * @dev Collection of functions related to the address type + */ +library Address { + /** + * @dev Returns true if `account` is a contract. + * + * [IMPORTANT] + * ==== + * It is unsafe to assume that an address for which this function returns + * false is an externally-owned account (EOA) and not a contract. + * + * Among others, `isContract` will return false for the following + * types of addresses: + * + * - an externally-owned account + * - a contract in construction + * - an address where a contract will be created + * - an address where a contract lived, but was destroyed + * ==== + */ + function isContract(address account) internal view returns (bool) { + // This method relies in extcodesize, which returns 0 for contracts in + // construction, since the code is only stored at the end of the + // constructor execution. + + uint256 size; + // solhint-disable-next-line no-inline-assembly + assembly { size := extcodesize(account) } + return size > 0; + } + + /** + * @dev Replacement for Solidity's `transfer`: sends `amount` wei to + * `recipient`, forwarding all available gas and reverting on errors. + * + * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost + * of certain opcodes, possibly making contracts go over the 2300 gas limit + * imposed by `transfer`, making them unable to receive funds via + * `transfer`. {sendValue} removes this limitation. + * + * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more]. + * + * IMPORTANT: because control is transferred to `recipient`, care must be + * taken to not create reentrancy vulnerabilities. Consider using + * {ReentrancyGuard} or the + * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern]. + */ + function sendValue(address payable recipient, uint256 amount) internal { + require(address(this).balance >= amount, "Address: insufficient balance"); + + // solhint-disable-next-line avoid-low-level-calls, avoid-call-value + (bool success, ) = recipient.call{ value: amount }(""); + require(success, "Address: unable to send value, recipient may have reverted"); + } + + /** + * @dev Performs a Solidity function call using a low level `call`. A + * plain`call` is an unsafe replacement for a function call: use this + * function instead. + * + * If `target` reverts with a revert reason, it is bubbled up by this + * function (like regular Solidity function calls). + * + * Returns the raw returned data. To convert to the expected return value, + * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`]. + * + * Requirements: + * + * - `target` must be a contract. + * - calling `target` with `data` must not revert. + * + * _Available since v3.1._ + */ + function functionCall(address target, bytes memory data) internal returns (bytes memory) { + return functionCall(target, data, "Address: low-level call failed"); + } + + /** + * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with + * `errorMessage` as a fallback revert reason when `target` reverts. + * + * _Available since v3.1._ + */ + function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) { + return _functionCallWithValue(target, data, 0, errorMessage); + } + + /** + * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], + * but also transferring `value` wei to `target`. + * + * Requirements: + * + * - the calling contract must have an ETH balance of at least `value`. + * - the called Solidity function must be `payable`. + * + * _Available since v3.1._ + */ + function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) { + return functionCallWithValue(target, data, value, "Address: low-level call with value failed"); + } + + /** + * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but + * with `errorMessage` as a fallback revert reason when `target` reverts. + * + * _Available since v3.1._ + */ + function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) { + require(address(this).balance >= value, "Address: insufficient balance for call"); + return _functionCallWithValue(target, data, value, errorMessage); + } + + function _functionCallWithValue(address target, bytes memory data, uint256 weiValue, string memory errorMessage) private returns (bytes memory) { + require(isContract(target), "Address: call to non-contract"); + + // solhint-disable-next-line avoid-low-level-calls + (bool success, bytes memory returndata) = target.call{ value: weiValue }(data); + if (success) { + return returndata; + } else { + // Look for revert reason and bubble it up if present + if (returndata.length > 0) { + // The easiest way to bubble the revert reason is using memory via assembly + + // solhint-disable-next-line no-inline-assembly + assembly { + let returndata_size := mload(returndata) + revert(add(32, returndata), returndata_size) + } + } else { + revert(errorMessage); + } + } + } +} + +// File: @openzeppelin/contracts/token/ERC20/SafeERC20.sol + +pragma solidity ^0.6.0; + + + + +/** + * @title SafeERC20 + * @dev Wrappers around ERC20 operations that throw on failure (when the token + * contract returns false). Tokens that return no value (and instead revert or + * throw on failure) are also supported, non-reverting calls are assumed to be + * successful. + * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract, + * which allows you to call the safe operations as `token.safeTransfer(...)`, etc. + */ +library SafeERC20 { + using SafeMath for uint256; + using Address for address; + + function safeTransfer(IERC20 token, address to, uint256 value) internal { + _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value)); + } + + function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal { + _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value)); + } + + /** + * @dev Deprecated. This function has issues similar to the ones found in + * {IERC20-approve}, and its usage is discouraged. + * + * Whenever possible, use {safeIncreaseAllowance} and + * {safeDecreaseAllowance} instead. + */ + function safeApprove(IERC20 token, address spender, uint256 value) internal { + // safeApprove should only be called when setting an initial allowance, + // or when resetting it to zero. To increase and decrease it, use + // 'safeIncreaseAllowance' and 'safeDecreaseAllowance' + // solhint-disable-next-line max-line-length + require((value == 0) || (token.allowance(address(this), spender) == 0), + "SafeERC20: approve from non-zero to non-zero allowance" + ); + _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value)); + } + + function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal { + uint256 newAllowance = token.allowance(address(this), spender).add(value); + _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance)); + } + + function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal { + uint256 newAllowance = token.allowance(address(this), spender).sub(value, "SafeERC20: decreased allowance below zero"); + _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance)); + } + + /** + * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement + * on the return value: the return value is optional (but if data is returned, it must not be false). + * @param token The token targeted by the call. + * @param data The call data (encoded using abi.encode or one of its variants). + */ + function _callOptionalReturn(IERC20 token, bytes memory data) private { + // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since + // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that + // the target address contains contract code and also asserts for success in the low-level call. + + bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed"); + if (returndata.length > 0) { // Return data is optional + // solhint-disable-next-line max-line-length + require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed"); + } + } +} + +// File: contracts/HbSwap.sol + +pragma solidity ^0.6.0; + + + + +contract HbSwap { + using SafeMath for uint; + using SafeERC20 for IERC20; + + event InitPool(address tokenA, address tokenB, uint amtA, uint amtB); + event TradePrep(address user, uint idxA, uint idxB); + event Trade(uint tradeSeq, address user, address tokenA, address tokenB, uint idxA, uint idxB, uint maskedA, uint maskedB); + event SecretDeposit(address token, address user, uint amt); + event SecretWithdraw(uint seq, address token, address user, uint amt); + + mapping (address => mapping (address => bool)) public tradingPairs; + mapping (address => mapping (address => mapping (address => uint))) public liquidityToken; + mapping (address => mapping (address => string)) public prices; + mapping (address => mapping (address => uint)) public updateTimes; + + uint public threshold; + uint public inputmaskCnt; + mapping (address => mapping(address => uint)) public balances; + + uint public serverNum; + mapping (address => bool) public servers; + + uint public tradeCnt; + + struct SecretWithdrawIntention { + address token; + address user; + uint amt; + } + + uint public secretWithdrawCnt; + mapping (uint => SecretWithdrawIntention) public secretWithdrawMap; + mapping (uint => mapping (address => bool)) public consentRecord; + mapping (uint => uint) consentCounter; + mapping (uint => bool) expired; + + constructor(address[] memory _servers, uint _threshold) public { + for (uint i = 0; i < _servers.length; i++) { + servers[_servers[i]] = true; + } + threshold = _threshold; + } + + function initPool(address _tokenA, address _tokenB, uint _amtA, uint _amtB) payable public { + require(_tokenA < _tokenB, "invalid trading pair"); + require(!tradingPairs[_tokenA][_tokenB], "pool already initiated"); + + address user = msg.sender; + + if (_tokenA != address(0x0)) { + IERC20 tokenA = IERC20(_tokenA); + tokenA.safeTransferFrom(user, address(this), _amtA); + } + if (_tokenB != address(0x0)) { + IERC20 tokenB = IERC20(_tokenB); + tokenB.safeTransferFrom(user, address(this), _amtB); + } + + tradingPairs[_tokenA][_tokenB] = true; + liquidityToken[_tokenA][_tokenB][user] = _sqrt(_amtA * _amtB); + + emit InitPool(_tokenA, _tokenB, _amtA, _amtB); + } + + function deposit(address _token, uint _amt) payable public { + address user = msg.sender; + + if (_token == address(0x0)) { + balances[_token][user] += msg.value; + } else { + IERC20 token = IERC20(_token); + token.safeTransferFrom(user, address(this), _amt); + balances[_token][user] += _amt; + } + } + + function withdraw(address _token, uint _amt) public { + address payable user = msg.sender; + + if (_token == address(0x0)) { + balances[_token][user] -= _amt; + user.transfer(_amt); + } else { + IERC20 token = IERC20(_token); + token.safeTransfer(user, _amt); + balances[_token][user] -= _amt; + } + } + + function secretDeposit(address _token, uint _amt) public { + address user = msg.sender; + + require(balances[_token][user] >= _amt, "not enough balance"); + balances[_token][user] -= _amt; + + emit SecretDeposit(_token, user, _amt); + } + + function secretWithdraw(address _token, uint _amt) public { + address user = msg.sender; + + secretWithdrawCnt += 1; + secretWithdrawMap[secretWithdrawCnt] = SecretWithdrawIntention(_token, user, _amt); + + emit SecretWithdraw(secretWithdrawCnt, _token, user, _amt); + } + + function consent(uint _seq) public { + address server = msg.sender; + + require(servers[server], "not a valid server"); + require(!consentRecord[_seq][server], "already consent"); + + consentRecord[_seq][server] = true; + consentCounter[_seq] += 1; + if (consentCounter[_seq] > threshold && !expired[_seq]) { + SecretWithdrawIntention memory secretWithdrawIntention = secretWithdrawMap[_seq]; + balances[secretWithdrawIntention.token][secretWithdrawIntention.user] += secretWithdrawIntention.amt; + expired[_seq] = true; + } + } + + function tradePrep() public { + emit TradePrep(msg.sender, inputmaskCnt, inputmaskCnt + 1); + inputmaskCnt += 2; + } + + function trade(address _tokenA, address _tokenB, uint _idxA, uint _idxB, uint _maskedA, uint _maskedB) public { + require(_tokenA < _tokenB, "invalid trading pair"); + require(tradingPairs[_tokenA][_tokenB], "pool not exist"); + tradeCnt += 1; + emit Trade(tradeCnt, msg.sender, _tokenA, _tokenB, _idxA, _idxB, _maskedA, _maskedB); + } + + function reset() public { + inputmaskCnt = 0; + } + + function updatePrice(address _tokenA, address _tokenB, string calldata _price) public { + require(_tokenA < _tokenB, "invalid trading pair"); + require(tradingPairs[_tokenA][_tokenB], "pool not exist"); + prices[_tokenA][_tokenB] = _price; + updateTimes[_tokenA][_tokenB] = block.number; + } + + // babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method) + function _sqrt(uint y) internal pure returns (uint z) { + if (y > 3) { + z = y; + uint x = y / 2 + 1; + while (x < z) { + z = x; + x = (y / x + x) / 2; + } + } else if (y != 0) { + z = 1; + } + } +} diff --git a/src/contracts/HbSwap.sol b/src/contracts/HbSwap.sol new file mode 100644 index 00000000..8d09dcc2 --- /dev/null +++ b/src/contracts/HbSwap.sol @@ -0,0 +1,138 @@ +pragma solidity ^0.6.0; + +import "@openzeppelin/contracts/math/SafeMath.sol"; +import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; +import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol"; + +contract HbSwap { + using SafeMath for uint; + using SafeERC20 for IERC20; + + event InitPool(address user, address tokenA, address tokenB, uint amtA, uint amtB); + event AddLiquidity(address user, address tokenA, address tokenB, uint amtA, uint amtB); + event RemoveLiquidity(address user, address tokenA, address tokenB, uint amt); + event SecretDeposit(address token, address user, uint amt); + event TradePrep(address user, uint idxA, uint idxB); + event Trade(uint tradeSeq, address user, address tokenA, address tokenB, uint idxA, uint idxB, uint maskedA, uint maskedB); + event SecretWithdraw(uint seq, address token, address user, uint amt); + + struct SecretWithdrawIntention { + address token; + address user; + uint amt; + } + + uint public threshold; + mapping (address => bool) public servers; + + uint public inputmaskCnt; + uint public tradeCnt; + mapping (address => mapping(address => uint)) public balances; + + uint public secretWithdrawCnt; + mapping (uint => SecretWithdrawIntention) public secretWithdrawMap; + mapping (uint => mapping (address => bool)) public consentRecord; + mapping (uint => uint) consentCounter; + mapping (uint => bool) expired; + + mapping (address => mapping (address => string)) public prices; + mapping (address => mapping (address => uint)) public updateTimes; + + constructor(address[] memory _servers, uint _threshold) public { + for (uint i = 0; i < _servers.length; i++) { + servers[_servers[i]] = true; + } + threshold = _threshold; + } + + function initPool(address _tokenA, address _tokenB, uint _amtA, uint _amtB) public { + require(_tokenA < _tokenB, "invalid trading pair"); + emit InitPool(msg.sender, _tokenA, _tokenB, _amtA, _amtB); + } + + function addLiquidity(address _tokenA, address _tokenB, uint _amtA, uint _amtB) public { + require(_tokenA < _tokenB, "invalid trading pair"); + emit AddLiquidity(msg.sender, _tokenA, _tokenB, _amtA, _amtB); + } + + function removeLiquidity(address _tokenA, address _tokenB, uint _amt) public { + require(_tokenA < _tokenB, "invalid trading pair"); + emit RemoveLiquidity(msg.sender, _tokenA, _tokenB, _amt); + } + + function deposit(address _token, uint _amt) payable public { + address user = msg.sender; + + if (_token == address(0x0)) { + balances[_token][user] += msg.value; + } else { + IERC20 token = IERC20(_token); + token.safeTransferFrom(user, address(this), _amt); + balances[_token][user] += _amt; + } + } + + function secretDeposit(address _token, uint _amt) public { + address user = msg.sender; + + require(balances[_token][user] >= _amt, "not enough balance"); + balances[_token][user] -= _amt; + + emit SecretDeposit(_token, user, _amt); + } + + function secretWithdraw(address _token, uint _amt) public { + address user = msg.sender; + + secretWithdrawCnt += 1; + secretWithdrawMap[secretWithdrawCnt] = SecretWithdrawIntention(_token, user, _amt); + + emit SecretWithdraw(secretWithdrawCnt, _token, user, _amt); + } + + function consent(uint _seq) public { + address server = msg.sender; + + require(servers[server], "not a valid server"); + require(!consentRecord[_seq][server], "already consent"); + + consentRecord[_seq][server] = true; + consentCounter[_seq] += 1; + if (consentCounter[_seq] > threshold && !expired[_seq]) { + SecretWithdrawIntention memory secretWithdrawIntention = secretWithdrawMap[_seq]; + balances[secretWithdrawIntention.token][secretWithdrawIntention.user] += secretWithdrawIntention.amt; + expired[_seq] = true; + } + } + + function withdraw(address _token, uint _amt) public { + address payable user = msg.sender; + + if (_token == address(0x0)) { + balances[_token][user] -= _amt; + user.transfer(_amt); + } else { + IERC20 token = IERC20(_token); + token.safeTransfer(user, _amt); + balances[_token][user] -= _amt; + } + } + + function tradePrep() public { + emit TradePrep(msg.sender, inputmaskCnt, inputmaskCnt + 1); + inputmaskCnt += 2; + } + + function trade(address _tokenA, address _tokenB, uint _idxA, uint _idxB, uint _maskedA, uint _maskedB) public { + require(_tokenA < _tokenB, "invalid trading pair"); + tradeCnt += 1; + emit Trade(tradeCnt, msg.sender, _tokenA, _tokenB, _idxA, _idxB, _maskedA, _maskedB); + } + + function updatePrice(address _tokenA, address _tokenB, string calldata _price) public { + require(_tokenA < _tokenB, "invalid trading pair"); + prices[_tokenA][_tokenB] = _price; + updateTimes[_tokenA][_tokenB] = block.number; + } + +} diff --git a/src/contracts/Token.sol b/src/contracts/Token.sol new file mode 100644 index 00000000..5237df22 --- /dev/null +++ b/src/contracts/Token.sol @@ -0,0 +1,22 @@ +// Based on https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/examples/SimpleToken.sol +pragma solidity ^0.6.1; + +import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; + +/** + * @title SimpleToken + * @notice Very simple ERC20 Token example, where all tokens are pre-assigned to the creator. + * Note they can later distribute these tokens as they wish using `transfer` and other + * `ERC20` functions. + */ +contract Token is ERC20 { + uint8 public constant DECIMALS = 18; + uint256 public constant INITIAL_SUPPLY = 1e28; + + /** + * @notice Constructor that gives msg.sender all of existing tokens. + */ + constructor () public ERC20("Token", "token") { + _mint(msg.sender, INITIAL_SUPPLY); + } +} \ No newline at end of file diff --git a/src/genfiles/HbSwap.abi b/src/genfiles/HbSwap.abi new file mode 100644 index 00000000..c04bd9ce --- /dev/null +++ b/src/genfiles/HbSwap.abi @@ -0,0 +1,691 @@ +[ + { + "inputs": [ + { + "internalType": "address[]", + "name": "_servers", + "type": "address[]" + }, + { + "internalType": "uint256", + "name": "_threshold", + "type": "uint256" + } + ], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "user", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "tokenA", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "tokenB", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amtA", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amtB", + "type": "uint256" + } + ], + "name": "AddLiquidity", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "user", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "tokenA", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "tokenB", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amtA", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amtB", + "type": "uint256" + } + ], + "name": "InitPool", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "user", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "tokenA", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "tokenB", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amt", + "type": "uint256" + } + ], + "name": "RemoveLiquidity", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "token", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "user", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amt", + "type": "uint256" + } + ], + "name": "SecretDeposit", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "seq", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "address", + "name": "token", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "user", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amt", + "type": "uint256" + } + ], + "name": "SecretWithdraw", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "tradeSeq", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "address", + "name": "user", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "tokenA", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "tokenB", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "idxA", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "idxB", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "maskedA", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "maskedB", + "type": "uint256" + } + ], + "name": "Trade", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "user", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "idxA", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "idxB", + "type": "uint256" + } + ], + "name": "TradePrep", + "type": "event" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + }, + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "name": "balances", + "constant": true, + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + }, + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "name": "consentRecord", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "inputmaskCnt", + "constant": true, + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + }, + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "name": "prices", + "constant": true, + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "secretWithdrawCnt", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "name": "secretWithdrawMap", + "outputs": [ + { + "internalType": "address", + "name": "token", + "type": "address" + }, + { + "internalType": "address", + "name": "user", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amt", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "name": "servers", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "threshold", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "tradeCnt", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + }, + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "name": "updateTimes", + "constant": true, + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_tokenA", + "type": "address" + }, + { + "internalType": "address", + "name": "_tokenB", + "type": "address" + }, + { + "internalType": "uint256", + "name": "_amtA", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_amtB", + "type": "uint256" + } + ], + "name": "initPool", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_tokenA", + "type": "address" + }, + { + "internalType": "address", + "name": "_tokenB", + "type": "address" + }, + { + "internalType": "uint256", + "name": "_amtA", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_amtB", + "type": "uint256" + } + ], + "name": "addLiquidity", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_tokenA", + "type": "address" + }, + { + "internalType": "address", + "name": "_tokenB", + "type": "address" + }, + { + "internalType": "uint256", + "name": "_amt", + "type": "uint256" + } + ], + "name": "removeLiquidity", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_token", + "type": "address" + }, + { + "internalType": "uint256", + "name": "_amt", + "type": "uint256" + } + ], + "name": "deposit", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_token", + "type": "address" + }, + { + "internalType": "uint256", + "name": "_amt", + "type": "uint256" + } + ], + "name": "secretDeposit", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_token", + "type": "address" + }, + { + "internalType": "uint256", + "name": "_amt", + "type": "uint256" + } + ], + "name": "secretWithdraw", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_seq", + "type": "uint256" + } + ], + "name": "consent", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_token", + "type": "address" + }, + { + "internalType": "uint256", + "name": "_amt", + "type": "uint256" + } + ], + "name": "withdraw", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "tradePrep", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_tokenA", + "type": "address" + }, + { + "internalType": "address", + "name": "_tokenB", + "type": "address" + }, + { + "internalType": "uint256", + "name": "_idxA", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_idxB", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_maskedA", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_maskedB", + "type": "uint256" + } + ], + "name": "trade", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_tokenA", + "type": "address" + }, + { + "internalType": "address", + "name": "_tokenB", + "type": "address" + }, + { + "internalType": "string", + "name": "_price", + "type": "string" + } + ], + "name": "updatePrice", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + } +] diff --git a/src/genfiles/HbSwap.bin b/src/genfiles/HbSwap.bin new file mode 100644 index 00000000..3d46a41c --- /dev/null +++ b/src/genfiles/HbSwap.bin @@ -0,0 +1 @@ +0x60806040523480156200001157600080fd5b506040516200271d3803806200271d833981810160405260408110156200003757600080fd5b81019080805160405193929190846401000000008211156200005857600080fd5b838201915060208201858111156200006f57600080fd5b82518660208202830111640100000000821117156200008d57600080fd5b8083526020830192505050908051906020019060200280838360005b83811015620000c6578082015181840152602081019050620000a9565b505050509050016040526020018051906020019092919050505060008090505b8251811015620001695760018060008584815181106200010257fe5b602002602001015173ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff0219169083151502179055508080600101915050620000e6565b5080600081905550505061259a80620001836000396000f3fe60806040526004361061012a5760003560e01c80639eef6ce8116100ab578063c23f001f1161006f578063c23f001f146106f4578063cf6c62ea14610779578063d3fd8987146107fe578063d752fab2146108c4578063dee405951461093f578063f3fef3a31461099a5761012a565b80639eef6ce814610515578063ade28aad14610550578063af4170c4146105ab578063b72a2139146105d6578063bca8a7c11461066f5761012a565b80633394dc6f116100f25780633394dc6f1461039157806342cde4e8146103bc57806347e7ef24146103e75780637aa6fd651461043557806393910e66146104ea5761012a565b8063064d48101461012f57806312ada8de146101465780631768af96146101af5780631f312404146102225780632f8a68c51461030c575b600080fd5b34801561013b57600080fd5b506101446109f5565b005b34801561015257600080fd5b506101956004803603602081101561016957600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610a81565b604051808215151515815260200191505060405180910390f35b3480156101bb57600080fd5b50610208600480360360408110156101d257600080fd5b8101908080359060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610aa1565b604051808215151515815260200191505060405180910390f35b34801561022e57600080fd5b506102916004803603604081101561024557600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610ad0565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156102d15780820151818401526020810190506102b6565b50505050905090810190601f1680156102fe5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561031857600080fd5b5061038f6004803603608081101561032f57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919080359060200190929190505050610b8d565b005b34801561039d57600080fd5b506103a6610d0f565b6040518082815260200191505060405180910390f35b3480156103c857600080fd5b506103d1610d15565b6040518082815260200191505060405180910390f35b610433600480360360408110156103fd57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610d1b565b005b34801561044157600080fd5b5061046e6004803603602081101561045857600080fd5b8101908080359060200190929190505050610ea7565b604051808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001828152602001935050505060405180910390f35b3480156104f657600080fd5b506104ff610f11565b6040518082815260200191505060405180910390f35b34801561052157600080fd5b5061054e6004803603602081101561053857600080fd5b8101908080359060200190929190505050610f17565b005b34801561055c57600080fd5b506105a96004803603604081101561057357600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050611324565b005b3480156105b757600080fd5b506105c0611549565b6040518082815260200191505060405180910390f35b3480156105e257600080fd5b5061066d600480360360c08110156105f957600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919080359060200190929190803590602001909291908035906020019092919050505061154f565b005b34801561067b57600080fd5b506106de6004803603604081101561069257600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506116fe565b6040518082815260200191505060405180910390f35b34801561070057600080fd5b506107636004803603604081101561071757600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050611723565b6040518082815260200191505060405180910390f35b34801561078557600080fd5b506107fc6004803603608081101561079c57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919080359060200190929190505050611748565b005b34801561080a57600080fd5b506108c26004803603606081101561082157600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019064010000000081111561087e57600080fd5b82018360208201111561089057600080fd5b803590602001918460018302840111640100000000831117156108b257600080fd5b90919293919293905050506118ca565b005b3480156108d057600080fd5b5061093d600480360360608110156108e757600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050611a7e565b005b34801561094b57600080fd5b506109986004803603604081101561096257600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050611bf7565b005b3480156109a657600080fd5b506109f3600480360360408110156109bd57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050611db2565b005b7fb2fd402d6b838b10cf190139b9d4495eefcfea7543bc1056544d13732d82e6ac33600254600160025401604051808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001838152602001828152602001935050505060405180910390a160028060008282540192505081905550565b60016020528060005260406000206000915054906101000a900460ff1681565b60076020528160005260406000206020528060005260406000206000915091509054906101000a900460ff1681565b600a602052816000526040600020602052806000526040600020600091509150508054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610b855780601f10610b5a57610100808354040283529160200191610b85565b820191906000526020600020905b815481529060010190602001808311610b6857829003601f168201915b505050505081565b8273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff1610610c2e576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f696e76616c69642074726164696e67207061697200000000000000000000000081525060200191505060405180910390fd5b7ffaaebcb30b1b421f4f2ca7f2620e5add6a64532c087ee0646fd665a33d36fdf53385858585604051808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018381526020018281526020019550505050505060405180910390a150505050565b60055481565b60005481565b6000339050600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff161415610de45734600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540192505081905550610ea2565b6000839050610e168230858473ffffffffffffffffffffffffffffffffffffffff16611f83909392919063ffffffff16565b82600460008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540192505081905550505b505050565b60066020528060005260406000206000915090508060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16908060010160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16908060020154905083565b60025481565b6000339050600160008273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff16610fdb576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260128152602001807f6e6f7420612076616c696420736572766572000000000000000000000000000081525060200191505060405180910390fd5b6007600083815260200190815260200160002060008273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff16156110ac576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252600f8152602001807f616c726561647920636f6e73656e74000000000000000000000000000000000081525060200191505060405180910390fd5b60016007600084815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff02191690831515021790555060016008600084815260200190815260200160002060008282540192505081905550600054600860008481526020019081526020016000205411801561117957506009600083815260200190815260200160002060009054906101000a900460ff16155b1561132057611186612448565b600660008481526020019081526020016000206040518060600160405290816000820160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020016001820160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020016002820154815250509050806040015160046000836000015173ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000836020015173ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555060016009600085815260200190815260200160002060006101000a81548160ff021916908315150217905550505b5050565b600033905081600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561141b576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260128152602001807f6e6f7420656e6f7567682062616c616e6365000000000000000000000000000081525060200191505060405180910390fd5b81600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055507f07c06144435b7d2bdccf9ee7e5a7022c63382ac7c3a0e14ed08b5969dedf0ecf838284604051808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001828152602001935050505060405180910390a1505050565b60035481565b8473ffffffffffffffffffffffffffffffffffffffff168673ffffffffffffffffffffffffffffffffffffffff16106115f0576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f696e76616c69642074726164696e67207061697200000000000000000000000081525060200191505060405180910390fd5b60016003600082825401925050819055507f2b4d91cd20cc8800407e3614b8466a6f0729ac3b1fa43d4e2b059ff5593cbae660035433888888888888604051808981526020018873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018581526020018481526020018381526020018281526020019850505050505050505060405180910390a1505050505050565b600b602052816000526040600020602052806000526040600020600091509150505481565b6004602052816000526040600020602052806000526040600020600091509150505481565b8273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff16106117e9576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f696e76616c69642074726164696e67207061697200000000000000000000000081525060200191505060405180910390fd5b7fc33fbc9654f9c0dcfcbd829113bdb10afe95619bc0824bc5959ad82fd6952bd93385858585604051808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018381526020018281526020019550505050505060405180910390a150505050565b8273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff161061196b576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f696e76616c69642074726164696e67207061697200000000000000000000000081525060200191505060405180910390fd5b8181600a60008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002091906119f6929190612495565b5043600b60008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555050505050565b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1610611b1f576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f696e76616c69642074726164696e67207061697200000000000000000000000081525060200191505060405180910390fd5b7f2a1a4e62bda5b4987b9aa2f23ddbb29e434808f7a717452a3226bc15243c927733848484604051808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200182815260200194505050505060405180910390a1505050565b6000339050600160056000828254019250508190555060405180606001604052808473ffffffffffffffffffffffffffffffffffffffff1681526020018273ffffffffffffffffffffffffffffffffffffffff1681526020018381525060066000600554815260200190815260200160002060008201518160000160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060208201518160010160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550604082015181600201559050507f4ef3cc4825a92c3b6922acc8a45152cc96ef48463e8ed500dacd5df9e29a67f3600554848385604051808581526020018473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200182815260200194505050505060405180910390a1505050565b6000339050600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff161415611ec25781600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055508073ffffffffffffffffffffffffffffffffffffffff166108fc839081150290604051600060405180830381858888f19350505050158015611ebc573d6000803e3d6000fd5b50611f7e565b6000839050611ef282848373ffffffffffffffffffffffffffffffffffffffff166120709092919063ffffffff16565b82600460008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550505b505050565b61206a846323b872dd60e01b858585604051602401808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018281526020019350505050604051602081830303815290604052907bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff8381831617835250505050612128565b50505050565b6121238363a9059cbb60e01b8484604051602401808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200182815260200192505050604051602081830303815290604052907bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff8381831617835250505050612128565b505050565b606061218a826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c65648152508573ffffffffffffffffffffffffffffffffffffffff166122179092919063ffffffff16565b9050600081511115612212578080602001905160208110156121ab57600080fd5b8101908080519060200190929190505050612211576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602a81526020018061253b602a913960400191505060405180910390fd5b5b505050565b6060612226848460008561222f565b90509392505050565b606061223a85612435565b6122ac576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601d8152602001807f416464726573733a2063616c6c20746f206e6f6e2d636f6e747261637400000081525060200191505060405180910390fd5b600060608673ffffffffffffffffffffffffffffffffffffffff1685876040518082805190602001908083835b602083106122fc57805182526020820191506020810190506020830392506122d9565b6001836020036101000a03801982511681845116808217855250505050505090500191505060006040518083038185875af1925050503d806000811461235e576040519150601f19603f3d011682016040523d82523d6000602084013e612363565b606091505b5091509150811561237857809250505061242d565b60008151111561238b5780518082602001fd5b836040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b838110156123f25780820151818401526020810190506123d7565b50505050905090810190601f16801561241f5780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b949350505050565b600080823b905060008111915050919050565b6040518060600160405280600073ffffffffffffffffffffffffffffffffffffffff168152602001600073ffffffffffffffffffffffffffffffffffffffff168152602001600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106124d657803560ff1916838001178555612504565b82800160010185558215612504579182015b828111156125035782358255916020019190600101906124e8565b5b5090506125119190612515565b5090565b61253791905b8082111561253357600081600090555060010161251b565b5090565b9056fe5361666545524332303a204552433230206f7065726174696f6e20646964206e6f742073756363656564a264697066735822122079ae607cba125fe17e83abc35ceb75533db541525cc262a7a6e2f205bdda783964736f6c634300060a0033 diff --git a/src/genfiles/Token.abi b/src/genfiles/Token.abi new file mode 100644 index 00000000..30c7b85e --- /dev/null +++ b/src/genfiles/Token.abi @@ -0,0 +1,304 @@ +[ + { + "inputs": [], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "value", + "type": "uint256" + } + ], + "name": "Approval", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "value", + "type": "uint256" + } + ], + "name": "Transfer", + "type": "event" + }, + { + "inputs": [], + "name": "DECIMALS", + "outputs": [ + { + "internalType": "uint8", + "name": "", + "type": "uint8" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "INITIAL_SUPPLY", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "internalType": "address", + "name": "spender", + "type": "address" + } + ], + "name": "allowance", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "approve", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "account", + "type": "address" + } + ], + "name": "balanceOf", + "constant": true, + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "decimals", + "outputs": [ + { + "internalType": "uint8", + "name": "", + "type": "uint8" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "internalType": "uint256", + "name": "subtractedValue", + "type": "uint256" + } + ], + "name": "decreaseAllowance", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "internalType": "uint256", + "name": "addedValue", + "type": "uint256" + } + ], + "name": "increaseAllowance", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "name", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "symbol", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "totalSupply", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "recipient", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "transfer", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "sender", + "type": "address" + }, + { + "internalType": "address", + "name": "recipient", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "transferFrom", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + } +] diff --git a/src/genfiles/Token.bin b/src/genfiles/Token.bin new file mode 100644 index 00000000..f792ffc2 --- /dev/null +++ b/src/genfiles/Token.bin @@ -0,0 +1 @@ +0x60806040523480156200001157600080fd5b506040518060400160405280600581526020017f546f6b656e0000000000000000000000000000000000000000000000000000008152506040518060400160405280600581526020017f746f6b656e0000000000000000000000000000000000000000000000000000008152508160039080519060200190620000969291906200035e565b508060049080519060200190620000af9291906200035e565b506012600560006101000a81548160ff021916908360ff1602179055505050620000ec336b204fce5e3e25026110000000620000f260201b60201c565b6200040d565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16141562000196576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601f8152602001807f45524332303a206d696e7420746f20746865207a65726f20616464726573730081525060200191505060405180910390fd5b620001aa60008383620002d060201b60201c565b620001c681600254620002d560201b62000a2a1790919060201c565b60028190555062000224816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054620002d560201b62000a2a1790919060201c565b6000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a35050565b505050565b60008082840190508381101562000354576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601b8152602001807f536166654d6174683a206164646974696f6e206f766572666c6f77000000000081525060200191505060405180910390fd5b8091505092915050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10620003a157805160ff1916838001178555620003d2565b82800160010185558215620003d2579182015b82811115620003d1578251825591602001919060010190620003b4565b5b509050620003e19190620003e5565b5090565b6200040a91905b8082111562000406576000816000905550600101620003ec565b5090565b90565b61116e806200041d6000396000f3fe608060405234801561001057600080fd5b50600436106100cf5760003560e01c8063313ce5671161008c57806395d89b411161006657806395d89b4114610385578063a457c2d714610408578063a9059cbb1461046e578063dd62ed3e146104d4576100cf565b8063313ce567146102a357806339509351146102c757806370a082311461032d576100cf565b806306fdde03146100d4578063095ea7b31461015757806318160ddd146101bd57806323b872dd146101db5780632e0f2625146102615780632ff2e9dc14610285575b600080fd5b6100dc61054c565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561011c578082015181840152602081019050610101565b50505050905090810190601f1680156101495780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6101a36004803603604081101561016d57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506105ee565b604051808215151515815260200191505060405180910390f35b6101c561060c565b6040518082815260200191505060405180910390f35b610247600480360360608110156101f157600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610616565b604051808215151515815260200191505060405180910390f35b6102696106ef565b604051808260ff1660ff16815260200191505060405180910390f35b61028d6106f4565b6040518082815260200191505060405180910390f35b6102ab610704565b604051808260ff1660ff16815260200191505060405180910390f35b610313600480360360408110156102dd57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919050505061071b565b604051808215151515815260200191505060405180910390f35b61036f6004803603602081101561034357600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506107ce565b6040518082815260200191505060405180910390f35b61038d610816565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156103cd5780820151818401526020810190506103b2565b50505050905090810190601f1680156103fa5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6104546004803603604081101561041e57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506108b8565b604051808215151515815260200191505060405180910390f35b6104ba6004803603604081101561048457600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610985565b604051808215151515815260200191505060405180910390f35b610536600480360360408110156104ea57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506109a3565b6040518082815260200191505060405180910390f35b606060038054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156105e45780601f106105b9576101008083540402835291602001916105e4565b820191906000526020600020905b8154815290600101906020018083116105c757829003601f168201915b5050505050905090565b60006106026105fb610ab2565b8484610aba565b6001905092915050565b6000600254905090565b6000610623848484610cb1565b6106e48461062f610ab2565b6106df856040518060600160405280602881526020016110a360289139600160008b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000610695610ab2565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610f729092919063ffffffff16565b610aba565b600190509392505050565b601281565b6b204fce5e3e2502611000000081565b6000600560009054906101000a900460ff16905090565b60006107c4610728610ab2565b846107bf8560016000610739610ab2565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008973ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610a2a90919063ffffffff16565b610aba565b6001905092915050565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b606060048054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156108ae5780601f10610883576101008083540402835291602001916108ae565b820191906000526020600020905b81548152906001019060200180831161089157829003601f168201915b5050505050905090565b600061097b6108c5610ab2565b846109768560405180606001604052806025815260200161111460259139600160006108ef610ab2565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008a73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610f729092919063ffffffff16565b610aba565b6001905092915050565b6000610999610992610ab2565b8484610cb1565b6001905092915050565b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905092915050565b600080828401905083811015610aa8576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601b8152602001807f536166654d6174683a206164646974696f6e206f766572666c6f77000000000081525060200191505060405180910390fd5b8091505092915050565b600033905090565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff161415610b40576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260248152602001806110f06024913960400191505060405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415610bc6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602281526020018061105b6022913960400191505060405180910390fd5b80600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925836040518082815260200191505060405180910390a3505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff161415610d37576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260258152602001806110cb6025913960400191505060405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415610dbd576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260238152602001806110386023913960400191505060405180910390fd5b610dc8838383611032565b610e338160405180606001604052806026815260200161107d602691396000808773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610f729092919063ffffffff16565b6000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550610ec6816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610a2a90919063ffffffff16565b6000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a3505050565b600083831115829061101f576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b83811015610fe4578082015181840152602081019050610fc9565b50505050905090810190601f1680156110115780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b5060008385039050809150509392505050565b50505056fe45524332303a207472616e7366657220746f20746865207a65726f206164647265737345524332303a20617070726f766520746f20746865207a65726f206164647265737345524332303a207472616e7366657220616d6f756e7420657863656564732062616c616e636545524332303a207472616e7366657220616d6f756e74206578636565647320616c6c6f77616e636545524332303a207472616e736665722066726f6d20746865207a65726f206164647265737345524332303a20617070726f76652066726f6d20746865207a65726f206164647265737345524332303a2064656372656173656420616c6c6f77616e63652062656c6f77207a65726fa264697066735822122018d0d720c83eb9ed4e669c6771bd4e101684751f4c48cc984730e5a85726aaf564736f6c634300060a0033 diff --git a/src/go/client/add_liquidity.go b/src/go/client/add_liquidity.go new file mode 100644 index 00000000..5a454484 --- /dev/null +++ b/src/go/client/add_liquidity.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "github.com/ethereum/go-ethereum/common" + "github.com/initc3/HoneyBadgerSwap/src/go/utils" + "os" +) + +func main() { + user := utils.GetAccount(fmt.Sprintf("account_%s", os.Args[1])) + tokenA, tokenB := common.HexToAddress(os.Args[2]), common.HexToAddress(os.Args[3]) + amtA, amtB := os.Args[4], os.Args[5] + + conn := utils.GetEthClient(utils.HttpEndpoint) + + utils.AddLiquidity(conn, user, tokenA, tokenB, utils.StrToBig(amtA), utils.StrToBig(amtB)) +} diff --git a/src/go/client/deposit.go b/src/go/client/deposit.go new file mode 100644 index 00000000..2916924a --- /dev/null +++ b/src/go/client/deposit.go @@ -0,0 +1,62 @@ +package main + +import ( + "bytes" + "fmt" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/ethclient" + "github.com/initc3/HoneyBadgerSwap/src/go/utils" + "math/big" + "os" +) + +func depositETH(conn *ethclient.Client, auth *bind.TransactOpts, _amt string) { + amt := utils.StrToBig(_amt) + if amt.Cmp(big.NewInt(0)) == 0 { + return + } + + utils.FundETH(conn, auth.From, amt) + auth.Value = amt + utils.Deposit(conn, auth, utils.EthAddr, amt) + + utils.GetBalance(conn, utils.EthAddr, auth.From) +} + +func depositTOK(conn *ethclient.Client, auth *bind.TransactOpts, tokenAddr common.Address, _amt string) { + amt := utils.StrToBig(_amt) + if amt.Cmp(big.NewInt(0)) == 0 { + return + } + + utils.FundToken(conn, tokenAddr, auth.From, amt) + + auth.Value = big.NewInt(0) + utils.Approve(conn, auth, tokenAddr, utils.HbswapAddr, amt) + utils.Deposit(conn, auth, tokenAddr, amt) + + utils.GetBalance(conn, tokenAddr, auth.From) +} + +func main() { + user := utils.GetAccount(fmt.Sprintf("account_%s", os.Args[1])) + tokenA, tokenB := common.HexToAddress(os.Args[2]), common.HexToAddress(os.Args[3]) + amtA, amtB := os.Args[4], os.Args[5] + + ethHostname := os.Args[6] + ethUrl := utils.GetEthURL(ethHostname) + conn := utils.GetEthClient(ethUrl) + + if bytes.Equal(tokenA.Bytes(), utils.EthAddr.Bytes()) { + depositETH(conn, user, amtA) + } else { + depositTOK(conn, user, tokenA, amtA) + } + + if bytes.Equal(tokenB.Bytes(), utils.EthAddr.Bytes()) { + depositETH(conn, user, amtB) + } else { + depositTOK(conn, user, tokenB, amtB) + } +} diff --git a/src/go/client/init_pool.go b/src/go/client/init_pool.go new file mode 100644 index 00000000..707166a6 --- /dev/null +++ b/src/go/client/init_pool.go @@ -0,0 +1,57 @@ +package main + +import ( + "fmt" + "github.com/ethereum/go-ethereum/common" + "github.com/initc3/HoneyBadgerSwap/src/go/utils" + "os" +) + +//func prepareETH(conn *ethclient.Client, auth *bind.TransactOpts, _amt string) *big.Int { +// amt := utils.StrToBig(_amt) +// if amt.Cmp(big.NewInt(0)) == 1 { +// utils.FundETH(conn, auth.From, amt) +// } +// return amt +//} +// +//func prepareTOK(conn *ethclient.Client, auth *bind.TransactOpts, tokenAddr common.Address, _amt string) { +// amt := utils.StrToBig(_amt) +// if amt.Cmp(big.NewInt(0)) == 0 { +// return +// } +// +// utils.FundToken(conn, tokenAddr, auth.From, amt) +// +// utils.Approve(conn, auth, tokenAddr, utils.HbswapAddr, amt) +//} + +func main() { + user := utils.GetAccount(fmt.Sprintf("account_%s", os.Args[1])) + tokenA, tokenB := common.HexToAddress(os.Args[2]), common.HexToAddress(os.Args[3]) + amtA, amtB := os.Args[4], os.Args[5] + + ethHostname := os.Args[6] + ethUrl := utils.GetEthURL(ethHostname) + conn := utils.GetEthClient(ethUrl) + + //ethA := bytes.Equal(tokenA.Bytes(), utils.EthAddr.Bytes()) + //ethB := bytes.Equal(tokenB.Bytes(), utils.EthAddr.Bytes()) + // + //if !ethA { + // prepareTOK(conn, user, tokenA, amtA) + //} + //if !ethB { + // prepareTOK(conn, user, tokenB, amtB) + //} + // + //value := big.NewInt(0) + //if ethA { + // value = prepareETH(conn, user, amtA) + //} + //if ethB { + // value = prepareETH(conn, user, amtB) + //} + + utils.InitPool(conn, user, tokenA, tokenB, utils.StrToBig(amtA), utils.StrToBig(amtB)) +} diff --git a/src/go/client/remove_liquidity.go b/src/go/client/remove_liquidity.go new file mode 100644 index 00000000..0aeb0090 --- /dev/null +++ b/src/go/client/remove_liquidity.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "github.com/ethereum/go-ethereum/common" + "github.com/initc3/HoneyBadgerSwap/src/go/utils" + "os" +) + +func main() { + user := utils.GetAccount(fmt.Sprintf("account_%s", os.Args[1])) + tokenA, tokenB := common.HexToAddress(os.Args[2]), common.HexToAddress(os.Args[3]) + amt := os.Args[4] + + conn := utils.GetEthClient(utils.HttpEndpoint) + + utils.RemoveLiquidity(conn, user, tokenA, tokenB, utils.StrToBig(amt)) +} diff --git a/src/go/client/secret_deposit.go b/src/go/client/secret_deposit.go new file mode 100644 index 00000000..873c255b --- /dev/null +++ b/src/go/client/secret_deposit.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/ethclient" + "github.com/initc3/HoneyBadgerSwap/src/go/utils" + "math/big" + "os" +) + +func secretDeposit(conn *ethclient.Client, auth *bind.TransactOpts, token common.Address, _amt string) { + amt := utils.StrToBig(_amt) + if amt.Cmp(big.NewInt(0)) == 0 { + return + } + + utils.SecretDeposit(conn, auth, token, amt) + + utils.GetBalance(conn, token, auth.From) +} + +func main() { + user := os.Args[1] + tokenA, tokenB := common.HexToAddress(os.Args[2]), common.HexToAddress(os.Args[3]) + amtA, amtB := os.Args[4], os.Args[5] + + ethHostname := os.Args[6] + ethUrl := utils.GetEthURL(ethHostname) + conn := utils.GetEthClient(ethUrl) + + owner := utils.GetAccount(fmt.Sprintf("account_%s", user)) + + secretDeposit(conn, owner, tokenA, amtA) + secretDeposit(conn, owner, tokenB, amtB) +} diff --git a/src/go/client/secret_withdraw.go b/src/go/client/secret_withdraw.go new file mode 100644 index 00000000..7a8337f4 --- /dev/null +++ b/src/go/client/secret_withdraw.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/ethclient" + "github.com/initc3/HoneyBadgerSwap/src/go/utils" + "math/big" + "os" +) + +func secretWithdraw(conn *ethclient.Client, auth *bind.TransactOpts, token common.Address, _amt string) { + amt := utils.StrToBig(_amt) + if amt.Cmp(big.NewInt(0)) == 0 { + return + } + + utils.SecretWithdraw(conn, auth, token, amt) + + utils.GetBalance(conn, token, auth.From) +} + +func main() { + user := os.Args[1] + tokenA, tokenB := common.HexToAddress(os.Args[2]), common.HexToAddress(os.Args[3]) + amtA, amtB := os.Args[4], os.Args[5] + + ethHostname := os.Args[6] + ethUrl := utils.GetEthURL(ethHostname) + conn := utils.GetEthClient(ethUrl) + + owner := utils.GetAccount(fmt.Sprintf("account_%s", user)) + + secretWithdraw(conn, owner, tokenA, amtA) + secretWithdraw(conn, owner, tokenB, amtB) +} diff --git a/src/go/client/testnet.go b/src/go/client/testnet.go new file mode 100644 index 00000000..d5cc104b --- /dev/null +++ b/src/go/client/testnet.go @@ -0,0 +1,22 @@ +package main + +import ( + "context" + "fmt" + "github.com/initc3/HoneyBadgerSwap/src/go/utils" + "math/big" +) + +func main() { + conn := utils.GetEthClient("https://kovan.infura.io/v3/6a82d2519efb4d748c02552e02e369c1") + + admin := utils.GetAccount(fmt.Sprintf("server_0")) + + balance, _ := conn.BalanceAt(context.Background(), admin.From, nil) + fmt.Printf("balance %v\n", balance) + + peer := utils.GetAccount(fmt.Sprintf("server_1")) + utils.FundETH(conn, peer.From, big.NewInt(1)) + balance, _ = conn.BalanceAt(context.Background(), peer.From, nil) + fmt.Printf("balance %v\n", balance) +} diff --git a/src/go/client/trade.go b/src/go/client/trade.go new file mode 100644 index 00000000..082edef2 --- /dev/null +++ b/src/go/client/trade.go @@ -0,0 +1,36 @@ +package main + +import ( + "fmt" + "github.com/ethereum/go-ethereum/common" + "github.com/initc3/HoneyBadgerSwap/src/go/utils" + "math/big" + "os" + "os/exec" + "strconv" + "strings" +) + +func main() { + user := os.Args[1] + tokenA, tokenB := common.HexToAddress(os.Args[2]), common.HexToAddress(os.Args[3]) + amtA, amtB := os.Args[4], os.Args[5] + + ethHostname := os.Args[6] + ethUrl := utils.GetEthURL(ethHostname) + conn := utils.GetEthClient(ethUrl) + + owner := utils.GetAccount(fmt.Sprintf("account_%s", user)) + + idxA, idxB := utils.TradePrep(conn, owner) + + cmd := exec.Command("python3", "-m", "honeybadgerswap.client.req_inputmasks", strconv.Itoa(int(idxA)), amtA, strconv.Itoa(int(idxB)), amtB) + stdout := utils.ExecCmd(cmd) + maskedInputs := strings.Split(stdout[:len(stdout)-1], " ") + + maskedA := utils.StrToBig(maskedInputs[0]) + maskedB := utils.StrToBig(maskedInputs[1]) + + fmt.Printf("maskedInputs: %v\n", maskedInputs) + utils.Trade(conn, owner, tokenA, tokenB, big.NewInt(idxA), big.NewInt(idxB), maskedA, maskedB) +} diff --git a/src/go/client/withdraw.go b/src/go/client/withdraw.go new file mode 100644 index 00000000..05875d53 --- /dev/null +++ b/src/go/client/withdraw.go @@ -0,0 +1,62 @@ +package main + +import ( + "bytes" + "fmt" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/ethclient" + "github.com/initc3/HoneyBadgerSwap/src/go/utils" + "math/big" + "os" +) + +func withdrawETH(conn *ethclient.Client, auth *bind.TransactOpts, _amt string) { + amt := utils.StrToBig(_amt) + if amt.Cmp(big.NewInt(0)) == 0 { + return + } + + utils.GetBalance(conn, utils.EthAddr, auth.From) + + utils.Withdraw(conn, auth, utils.EthAddr, amt) + + utils.GetBalance(conn, utils.EthAddr, auth.From) +} + +func withdrawTOK(conn *ethclient.Client, auth *bind.TransactOpts, tokenAddr common.Address, _amt string) { + amt := utils.StrToBig(_amt) + if amt.Cmp(big.NewInt(0)) == 0 { + return + } + + utils.GetBalance(conn, tokenAddr, auth.From) + + utils.Withdraw(conn, auth, tokenAddr, amt) + + utils.GetBalance(conn, tokenAddr, auth.From) +} + +func main() { + user := os.Args[1] + tokenA, tokenB := common.HexToAddress(os.Args[2]), common.HexToAddress(os.Args[3]) + amtA, amtB := os.Args[4], os.Args[5] + + ethHostname := os.Args[6] + ethUrl := utils.GetEthURL(ethHostname) + conn := utils.GetEthClient(ethUrl) + + owner := utils.GetAccount(fmt.Sprintf("account_%s", user)) + + if bytes.Equal(tokenA.Bytes(), utils.EthAddr.Bytes()) { + withdrawETH(conn, owner, amtA) + } else { + withdrawTOK(conn, owner, tokenA, amtA) + } + + if bytes.Equal(tokenB.Bytes(), utils.EthAddr.Bytes()) { + withdrawETH(conn, owner, amtB) + } else { + withdrawTOK(conn, owner, tokenB, amtB) + } +} diff --git a/src/go/deploy/deploy.go b/src/go/deploy/deploy.go new file mode 100644 index 00000000..5554bbeb --- /dev/null +++ b/src/go/deploy/deploy.go @@ -0,0 +1,80 @@ +package main + +import ( + "context" + "fmt" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/ethclient" + "github.com/initc3/HoneyBadgerSwap/src/go/utils" + "github.com/initc3/HoneyBadgerSwap/src/go_bindings/hbswap" + "github.com/initc3/HoneyBadgerSwap/src/go_bindings/token" + "log" + "math/big" + "os" +) + +const ( + n = 4 + t = 1 +) + +func DeployHbSwap(conn *ethclient.Client, auth *bind.TransactOpts) common.Address { + fmt.Println("Deploying HbSwap contract...") + + var servers []common.Address + for i := 0; i < n; i++ { + transactOpt := utils.GetAccount(fmt.Sprintf("server_%v", i)) + servers = append(servers, transactOpt.From) + } + + hbswapAddr, tx, _, err := hbswap.DeployHbSwap(auth, conn, servers, big.NewInt(t)) + if err != nil { + log.Fatalf("Failed to deploy HbSwap: %v", err) + } + + receipt, err := utils.WaitMined(context.Background(), conn, tx, 0) + if err != nil { + log.Fatalf("Failed to WaitMined HbSwap: %v", err) + } + if receipt.Status == 0 { + log.Fatalf("Transaction status: %x", receipt.Status) + } + + fmt.Println("Deployed HbSwap contract at", hbswapAddr.Hex()) + + return hbswapAddr +} + +func DeployToken(conn *ethclient.Client, auth *bind.TransactOpts) common.Address { + fmt.Println("Deploying Token contract...") + + tokenAddr, tx, _, err := token.DeployToken(auth, conn) + if err != nil { + log.Fatalf("Failed to deploy Token: %v", err) + } + + receipt, err := utils.WaitMined(context.Background(), conn, tx, 0) + if err != nil { + log.Fatalf("Failed to WaitMined Token: %v", err) + } + if receipt.Status == 0 { + log.Fatalf("Transaction status: %x", receipt.Status) + } + + fmt.Println("Deployed Token contract at", tokenAddr.Hex()) + + return tokenAddr +} + +func main() { + ethHostname := os.Args[1] + ethUrl := utils.GetEthURL(ethHostname) + conn := utils.GetEthClient(ethUrl) + + owner := utils.GetAccount("server_0") + + DeployHbSwap(conn, owner) + DeployToken(conn, owner) + DeployToken(conn, owner) +} diff --git a/src/go/server/server.go b/src/go/server/server.go new file mode 100644 index 00000000..022c9f6c --- /dev/null +++ b/src/go/server/server.go @@ -0,0 +1,309 @@ +package main + +import ( + "fmt" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/ethclient" + "github.com/initc3/HoneyBadgerSwap/src/go/utils" + "github.com/initc3/HoneyBadgerSwap/src/go_bindings/hbswap" + "log" + "math" + "os" + "os/exec" + "strconv" + "strings" + "sync" + "time" +) + +const ( + prog = "./malicious-shamir-party.x" + players = "4" + threshold = "1" + mpcPort = "5000" + blsPrime = "52435875175126190479447740508185965837690552500527637822603658699938581184513" + nshares = 1000 + checkInterval = 10 + prep_dir = "/opt/hbswap/preprocessing-data" +) + +var ( + serverID string + conn *ethclient.Client + server *bind.TransactOpts + prevTime = int64(0) +) + +func checkBalance(token string, user string, amt string, leader_hostname string) int { + cmd := exec.Command("python3", "-m", "honeybadgerswap.server.check_balance_set_data", serverID, token, user, amt) + utils.ExecCmd(cmd) + + cmd = exec.Command(prog, "-N", players, "-T", threshold, "-p", serverID, "-pn", mpcPort, "-P", blsPrime, "--hostname", leader_hostname, "hbswap_check_balance") + stdout := utils.ExecCmd(cmd) + + cmd = exec.Command("python3", "-m", "honeybadgerswap.server.check_balance_org_data", serverID) + stdout = utils.ExecCmd(cmd) + enoughBalance, _ := strconv.Atoi(stdout[:1]) + fmt.Printf("enoughBalance %v\n", enoughBalance) + + return enoughBalance +} + +func updateBalance(token string, user string, amt string, flag string) { + cmd := exec.Command("python3", "-m", "honeybadgerswap.server.update_balance", serverID, token, user, amt, flag) + utils.ExecCmd(cmd) +} + +func genInputmask(leader_hostname string) { + tot := utils.GetInputmaskCnt(conn) + for true { + cnt := utils.GetInputmaskCnt(conn) + + if cnt+100 > tot { + fmt.Printf("Generating new inputmasks...\n") + + cmd := exec.Command("./random-shamir.x", "-i", serverID, "-N", players, "-T", threshold, "--nshares", strconv.Itoa(nshares), "--host", leader_hostname) + utils.ExecCmd(cmd) + + cmd = exec.Command("python3", "-m", "honeybadgerswap.server.proc_inputmask", serverID, strconv.Itoa(int(tot))) + utils.ExecCmd(cmd) + + tot += nshares + fmt.Printf("Total inputmask number: %v\n", tot) + } + + time.Sleep(30 * time.Second) + } +} + +func watch(leader_hostname string) { + hbswapInstance, err := hbswap.NewHbSwap(utils.HbswapAddr, conn) + + initPoolChannel := make(chan *hbswap.HbSwapInitPool) + initPoolSub, err := hbswapInstance.WatchInitPool(nil, initPoolChannel) + if err != nil { + log.Fatal("watch InitPool err:", err) + } + + AddLiquidityChannel := make(chan *hbswap.HbSwapAddLiquidity) + AddLiquiditySub, err := hbswapInstance.WatchAddLiquidity(nil, AddLiquidityChannel) + if err != nil { + log.Fatal("watch AddLiquidity err:", err) + } + + RemoveLiquidityChannel := make(chan *hbswap.HbSwapRemoveLiquidity) + RemoveLiquiditySub, err := hbswapInstance.WatchRemoveLiquidity(nil, RemoveLiquidityChannel) + if err != nil { + log.Fatal("watch RemoveLiquidity err:", err) + } + + tradeChannel := make(chan *hbswap.HbSwapTrade) + tradeSub, err := hbswapInstance.WatchTrade(nil, tradeChannel) + if err != nil { + log.Fatal("watch Trade err:", err) + } + + secretDepositPrepChannel := make(chan *hbswap.HbSwapSecretDeposit) + secretDepositPrepSub, err := hbswapInstance.WatchSecretDeposit(nil, secretDepositPrepChannel) + if err != nil { + log.Fatal("watch LocalDepositPrep err:", err) + } + + secretWithdrawChannel := make(chan *hbswap.HbSwapSecretWithdraw) + secretWithdrawSub, err := hbswapInstance.WatchSecretWithdraw(nil, secretWithdrawChannel) + if err != nil { + log.Fatal("watch secretWithdraw err:", err) + } + + for { + select { + case err := <-initPoolSub.Err(): + log.Fatal(err) + case oce := <-initPoolChannel: + go func() { + fmt.Printf("**** InitPool ****\n") + + user := oce.User.String() + tokenA := oce.TokenA.String() + tokenB := oce.TokenB.String() + amtA := oce.AmtA.String() + amtB := oce.AmtB.String() + + if checkBalance(tokenA, user, amtA, leader_hostname) == 1 && checkBalance(tokenB, user, amtB, leader_hostname) == 1 { + amtLiquidity := fmt.Sprintf("%f", math.Sqrt(float64(oce.AmtA.Int64()*oce.AmtB.Int64()))) + cmd := exec.Command("python3", "-m", "honeybadgerswap.server.init_pool", serverID, tokenA, tokenB, amtA, amtB, amtLiquidity) + utils.ExecCmd(cmd) + + updateBalance(tokenA, user, fmt.Sprintf("-%s", amtA), "1") + updateBalance(tokenB, user, fmt.Sprintf("-%s", amtB), "1") + updateBalance(fmt.Sprintf("%s+%s", tokenA, tokenB), user, amtLiquidity, "1") + } + }() + + case err := <-AddLiquiditySub.Err(): + log.Fatal(err) + case oce := <-AddLiquidityChannel: + go func() { + fmt.Printf("**** AddLiquidity ****\n") + + user := oce.User.String() + tokenA := oce.TokenA.String() + tokenB := oce.TokenB.String() + amtA := oce.AmtA.String() + amtB := oce.AmtB.String() + + if checkBalance(tokenA, user, amtA, leader_hostname) == 1 && checkBalance(tokenB, user, amtB, leader_hostname) == 1 { + cmd := exec.Command("python3", "-m", "honeybadgerswap.server.add_liquidity_set_data", serverID, user, tokenA, tokenB, amtA, amtB) + utils.ExecCmd(cmd) + + cmd = exec.Command(prog, "-N", players, "-T", threshold, "-p", serverID, "-pn", mpcPort, "-P", blsPrime, "--hostname", leader_hostname, "hbswap_add_liquidity") + utils.ExecCmd(cmd) + + cmd = exec.Command("python3", "-m", "honeybadgerswap.server.add_liquidity_org_data", serverID, tokenA, tokenB) + stdout := utils.ExecCmd(cmd) + amts := strings.Split(strings.Split(stdout, "\n")[0], " ") + amtA = amts[0] + amtB = amts[1] + amtLiquidity := amts[2] + fmt.Printf("amt_A %s amt_B %s amt %s\n", amtA, amtB, amtLiquidity) + + updateBalance(tokenA, user, fmt.Sprintf("-%s", amtA), "0") + updateBalance(tokenB, user, fmt.Sprintf("-%s", amtB), "0") + updateBalance(fmt.Sprintf("%s+%s", tokenA, tokenB), user, amtLiquidity, "0") + } + }() + + case err := <-RemoveLiquiditySub.Err(): + log.Fatal(err) + case oce := <-RemoveLiquidityChannel: + go func() { + fmt.Printf("**** RemoveLiquidity ****\n") + + user := oce.User.String() + tokenA := oce.TokenA.String() + tokenB := oce.TokenB.String() + amtLiquidity := oce.Amt.String() + + if checkBalance(fmt.Sprintf("%s+%s", tokenA, tokenB), user, amtLiquidity, leader_hostname) == 1 { + cmd := exec.Command("python3", "-m", "honeybadgerswap.server.remove_liquidity_set_data", serverID, user, tokenA, tokenB, amtLiquidity) + utils.ExecCmd(cmd) + + //cmd = exec.Command(prog, "-N", players, "-T", threshold, "-p", serverID, "-pn", mpcPort, "-P", blsPrime, "--hostname", leader_hostname, "hbswap_remove_liquidity") + cmd = exec.Command(prog, "-N", players, "-T", threshold, "-p", serverID, "-pn", mpcPort, "-P", blsPrime, "--hostname", leader_hostname, "hbswap_remove_liquidity") + utils.ExecCmd(cmd) + + cmd = exec.Command("python3", "-m", "honeybadgerswap.server.remove_liquidity_org_data", serverID, tokenA, tokenB, amtLiquidity) + stdout := utils.ExecCmd(cmd) + amts := strings.Split(strings.Split(stdout, "\n")[0], " ") + amtA := amts[0] + amtB := amts[1] + fmt.Printf("amt_A %s amt_B %s\n", amtA, amtB) + + updateBalance(tokenA, user, amtA, "0") + updateBalance(tokenB, user, amtB, "0") + updateBalance(fmt.Sprintf("%s+%s", tokenA, tokenB), user, fmt.Sprintf("-%s", amtLiquidity), "1") + } + }() + + case err := <-tradeSub.Err(): + log.Fatal(err) + case oce := <-tradeChannel: + go func() { + fmt.Printf("**** Trade ****\n") + + //if serverID != "0" { + // time.Sleep(1 * time.Second) + //} + + user := oce.User.Hex() + tokenA := oce.TokenA.String() + tokenB := oce.TokenB.String() + + cmd := exec.Command("python3", "-m", "honeybadgerswap.server.trade_set_data", serverID, user, tokenA, tokenB, oce.IdxA.String(), oce.IdxB.String(), oce.MaskedA.String(), oce.MaskedB.String()) + utils.ExecCmd(cmd) + os.RemoveAll(fmt.Sprintf(prep_dir)) + os.Mkdir(fmt.Sprintf(prep_dir), 0777) + cmd = exec.Command(prog, "-N", players, "-T", threshold, "-p", serverID, "-pn", mpcPort, "-P", blsPrime, "--hostname", leader_hostname, "hbswap_trade") + utils.ExecCmd(cmd) + + cmd = exec.Command("python3", "-m", "honeybadgerswap.server.trade_org_data", serverID, tokenA, tokenB, oce.TradeSeq.String()) + stdout := utils.ExecCmd(cmd) + changes := strings.Split(strings.Split(stdout, "\n")[0], " ") + changeA := changes[0] + changeB := changes[1] + fmt.Printf("changeA %s changeB %s\n", changeA, changeB) + + updateBalance(tokenA, user, changeA, "0") + updateBalance(tokenB, user, changeB, "0") + + if time.Now().Unix()-prevTime > checkInterval { + cmd = exec.Command("python3", "-m", "honeybadgerswap.server.calc_price_set_data", serverID, tokenA, tokenB) + utils.ExecCmd(cmd) + + os.RemoveAll(fmt.Sprintf(prep_dir)) + os.Mkdir(fmt.Sprintf(prep_dir), 0777) + cmd = exec.Command(prog, "-N", players, "-T", threshold, "-p", serverID, "-pn", mpcPort, "-P", blsPrime, "--hostname", leader_hostname, "hbswap_calc_price") + stdout := utils.ExecCmd(cmd) + price := strings.Split(stdout, "\n")[0] + fmt.Printf("avg_price %s\n", price) + + if serverID == "0" { + utils.UpdatePrice(conn, server, oce.TokenA, oce.TokenB, price) + } else { + prevBlockNum := utils.GetUpdateTime(conn, oce.TokenA, oce.TokenB) + for true { + time.Sleep(time.Second) + curBlockNum := utils.GetUpdateTime(conn, oce.TokenA, oce.TokenB) + if curBlockNum > prevBlockNum { + break + } + } + } + + prevTime = time.Now().Unix() + } + }() + + case err := <-secretDepositPrepSub.Err(): + log.Fatal(err) + case oce := <-secretDepositPrepChannel: + go func() { + fmt.Printf("**** SecretDeposit ****\n") + + updateBalance(oce.Token.Hex(), oce.User.Hex(), oce.Amt.String(), "1") + }() + + case err := <-secretWithdrawSub.Err(): + log.Fatal(err) + case oce := <-secretWithdrawChannel: + go func() { + fmt.Printf("**** SecretWithdraw ****\n") + + if checkBalance(oce.Token.String(), oce.User.String(), oce.Amt.String(), leader_hostname) == 1 { + utils.Consent(conn, server, oce.Seq) + updateBalance(oce.Token.Hex(), oce.User.Hex(), fmt.Sprintf("-%s", oce.Amt.String()), "1") + } + }() + + } + } +} + +func main() { + serverID = os.Args[1] + fmt.Printf("Starting mpc server %v\n", serverID) + + ethHostname := os.Args[2] + wsUrl := utils.GetEthWsURL(ethHostname) + conn = utils.GetEthClient(wsUrl) + + leader_hostname := os.Args[3] + + server = utils.GetAccount(fmt.Sprintf("server_%s", serverID)) + + var wg sync.WaitGroup + wg.Add(1) + go genInputmask(leader_hostname) + go watch(leader_hostname) + wg.Wait() +} diff --git a/src/go/utils/utils.go b/src/go/utils/utils.go new file mode 100644 index 00000000..3544b94d --- /dev/null +++ b/src/go/utils/utils.go @@ -0,0 +1,201 @@ +package utils + +import ( + "bytes" + "context" + "errors" + "fmt" + "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethclient" + "io/ioutil" + "log" + "math/big" + "net" + "net/url" + "os" + "os/exec" + "path/filepath" + "strings" + "time" +) + +var ( + GOPATH = os.Getenv("GOPATH") + KEYSTORE = os.Getenv("POA_KEYSTORE") + minBalance = big.NewInt(300000000000000000) + + // parameter for private net + chainID = "123" + HttpEndpoint = "http://127.0.0.1:8545" + EthPort = 8545 + WsEndpoint = "ws://127.0.0.1:8546" + EthWsPort = 8546 + EthAddr = common.HexToAddress("0x0000000000000000000000000000000000000000") + HbswapAddr = common.HexToAddress("0xF74Eb25Ab1785D24306CA6b3CBFf0D0b0817C5E2") + TokenAddrs = []common.Address{ + common.HexToAddress("0x6b5c9637e0207c72Ee1a275b6C3b686ba8D87385"), + common.HexToAddress("0x8C89e5D2bCc0e4C26E3295d48d052E11bd03C06A"), + } + + //// parameter for kovan test net + //chainID = "42" + //HttpEndpoint = "https://kovan.infura.io/v3/6a82d2519efb4d748c02552e02e369c1" + //WsEndpoint = "wss://kovan.infura.io/ws/v3/6a82d2519efb4d748c02552e02e369c1" + //EthAddr = common.HexToAddress("0x0000000000000000000000000000000000000000") + //HbswapAddr = common.HexToAddress("0x7230873b02394AfA05bdDfa303298EF28bb2f0E8") + //TokenAddrs = []common.Address{ + // common.HexToAddress("0x63e7F20503256DdCFEC64872aAdb785d5A290CBb"), + // common.HexToAddress("0x403B0F962566Ffb960d0dE98875dc09603Aa67e9"), + // } +) + +func ExecCmd(cmd *exec.Cmd) string { + fmt.Printf("Cmd:\n====================\n%v\n====================\n", cmd) + var stdout, stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + err := cmd.Run() + if err != nil { + fmt.Printf("err:\n%s\n", stderr.String()) + log.Fatalf("cmd.Run() failed with %s\n", err) + } + fmt.Printf("Output:\n====================\n%s====================\n", stdout.String()) + return stdout.String() +} + +func StrToBig(st string) *big.Int { + v, _ := new(big.Int).SetString(st, 10) + return v +} + +func GetEthClient(ethInstance string) *ethclient.Client { + conn, err := ethclient.Dial(ethInstance) + if err != nil { + log.Fatal(err) + } + + return conn +} + +func GetAccount(account string) *bind.TransactOpts { + dir := filepath.Join(KEYSTORE, account) + + list, err := ioutil.ReadDir(dir) + if err != nil { + log.Printf("Error reading directory %s", dir) + log.Fatal(err) + } + + var name string + for _, info := range list { + name = info.Name() + if err != nil { + log.Fatal(err) + } + } + + //bytes, err := ioutil.ReadFile(dir + name) + bytes, err := ioutil.ReadFile(filepath.Join(dir, name)) + if err != nil { + log.Fatal(err) + } + + password := "" + auth, err := bind.NewTransactor(strings.NewReader(string(bytes)), password) + if err != nil { + log.Fatal(err) + } + + auth.GasLimit = 8000000 + + return auth +} + +func WaitMined(ctx context.Context, ec *ethclient.Client, + tx *types.Transaction, blockDelay uint64) (*types.Receipt, error) { + const missingFieldErr = "missing required field 'transactionHash' for Log" + + if ec == nil { + return nil, errors.New("nil ethclient") + } + queryTicker := time.NewTicker(time.Second) + defer queryTicker.Stop() + txHashBytes := common.HexToHash(tx.Hash().Hex()) + for { + receipt, rerr := ec.TransactionReceipt(ctx, txHashBytes) + if rerr == nil { + if blockDelay == 0 { + return receipt, rerr + } + break + } else if rerr == ethereum.NotFound || rerr.Error() == missingFieldErr { + select { + case <-ctx.Done(): + return nil, ctx.Err() + case <-queryTicker.C: + } + } else { + return receipt, rerr + } + } + ddl := big.NewInt(0) + latestBlockHeader, err := ec.HeaderByNumber(ctx, nil) + if err == nil { + ddl.Add(new(big.Int).SetUint64(blockDelay), latestBlockHeader.Number) + } + for { + latestBlockHeader, err := ec.HeaderByNumber(ctx, nil) + if err == nil && ddl.Cmp(latestBlockHeader.Number) < 0 { + receipt, rerr := ec.TransactionReceipt(ctx, txHashBytes) + if rerr == nil { + //fmt.Println("tx confirmed!") + return receipt, rerr + } else if rerr == ethereum.NotFound || rerr.Error() == missingFieldErr { + return nil, errors.New("tx is dropped due to chain re-org") + } else { + return receipt, rerr + } + } + select { + case <-ctx.Done(): + return nil, ctx.Err() + case <-queryTicker.C: + } + } +} + +func stringToBigInt(v string) *big.Int { + value := big.NewInt(0) + value.SetString(v, 10) + return value +} + +func GetIPAddr(hostname string) net.IP { + addrs, err := net.LookupIP(hostname) + if err != nil { + fmt.Sprintf("Error looking up hostname %s", hostname) + log.Fatal(err) + } + return addrs[0] +} + +func GetURL(hostname string, port int, scheme string) string { + addr := GetIPAddr(hostname) + host := fmt.Sprintf("%s:%d", addr, port) + u := &url.URL{ + Scheme: scheme, + Host: host, + } + return u.String() +} + +func GetEthURL(hostname string) string { + return GetURL(hostname, EthPort, "http") +} + +func GetEthWsURL(hostname string) string { + return GetURL(hostname, EthWsPort, "ws") +} diff --git a/src/go/utils/utils_eth.go b/src/go/utils/utils_eth.go new file mode 100644 index 00000000..21ed3801 --- /dev/null +++ b/src/go/utils/utils_eth.go @@ -0,0 +1,89 @@ +package utils + +import ( + "context" + "fmt" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethclient" + "log" + "math/big" +) + +/********** external functions **********/ + +func FundETH(conn *ethclient.Client, toAddr common.Address, amount *big.Int) { + adminAuth := GetAccount("server_0") + transferETH(conn, chainID, adminAuth, toAddr, amount) + + //balance := GetBalanceETH(conn, toAddr) + //fmt.Printf("Funded account %s to %v ETH\n", toAddr.Hex(), balance) +} + +func GetBalanceETH(conn *ethclient.Client, addr common.Address) (*big.Int) { + balance, err := conn.BalanceAt(context.Background(), addr, nil) + if err != nil { + log.Fatal(err) + } + + return balance +} + +/********** internal functions **********/ +func fundGas(conn *ethclient.Client, toAddr common.Address) { + balance := GetBalanceETH(conn, toAddr) + amount := minBalance + if balance.Cmp(amount) != -1 { + //fmt.Printf("Funded %s to %v ETH on mainchain\n", toAddr.Hex(), balance) + return + } + amount.Sub(amount, balance) + + adminAuth := GetAccount("server_0") + transferETH(conn, chainID, adminAuth, toAddr, amount) + + balance = GetBalanceETH(conn, toAddr) + //fmt.Printf("Funded account %s to %v ETH\n", toAddr.Hex(), balance) +} + +func transferETH(conn *ethclient.Client, chainId string, fromAuth *bind.TransactOpts, toAddr common.Address, amount *big.Int) { + ctx := context.Background() + + fromAddr := fromAuth.From + + nonce, err := conn.PendingNonceAt(ctx, fromAddr) + if err != nil { + log.Fatal(err) + } + + gasLimit := uint64(21000) // in units + gasPrice, err := conn.SuggestGasPrice(ctx) + if err != nil { + log.Fatal(err) + } + + tx := types.NewTransaction(nonce, toAddr, amount, gasLimit, gasPrice, nil) + signedTx, err := fromAuth.Signer(types.NewEIP155Signer(stringToBigInt(chainId)), fromAddr, tx) + if err != nil { + log.Fatal(err) + } + + //fmt.Printf("Sending %v wei from %s to %s\n", amount, fromAddr.Hex(), toAddr.Hex()) + err = conn.SendTransaction(ctx, signedTx) + if err != nil { + log.Fatal(err) + transferETH(conn, chainId, fromAuth, toAddr, amount) + } + + //fmt.Printf("Waiting for transaction to be mined...\n") + receipt, err := WaitMined(ctx, conn, signedTx, 0) + if err != nil { + log.Fatal(err) + transferETH(conn, chainId, fromAuth, toAddr, amount) + } + if receipt.Status == 0 { + fmt.Printf("Transaction status: %x", receipt.Status) + transferETH(conn, chainId, fromAuth, toAddr, amount) + } +} \ No newline at end of file diff --git a/src/go/utils/utils_hbswap.go b/src/go/utils/utils_hbswap.go new file mode 100644 index 00000000..9f889ff4 --- /dev/null +++ b/src/go/utils/utils_hbswap.go @@ -0,0 +1,352 @@ +package utils + +import ( + "context" + "fmt" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/ethclient" + "github.com/initc3/HoneyBadgerSwap/src/go_bindings/hbswap" + "log" + "math/big" + "strconv" + "time" +) + +func InitPool(conn *ethclient.Client, auth *bind.TransactOpts, tokenA common.Address, tokenB common.Address, amtA *big.Int, amtB *big.Int) { + fmt.Printf("InitPool tokenA %s tokenB %s amtA %v amtB %v\n", tokenA.Hex(), tokenB.Hex(), amtA, amtB) + hbswapInstance, err := hbswap.NewHbSwap(HbswapAddr, conn) + if err != nil { + log.Fatal(err) + } + + fundGas(conn, auth.From) + tx, err := hbswapInstance.InitPool(auth, tokenA, tokenB, amtA, amtB) + if err != nil { + log.Fatal(err) + } + + receipt, err := WaitMined(context.Background(), conn, tx, 0) + if err != nil { + log.Fatal(err) + } + + if receipt.Status == 0 { + log.Fatalf("Transaction status: %v", receipt.Status) + } +} + +func AddLiquidity(conn *ethclient.Client, auth *bind.TransactOpts, tokenA common.Address, tokenB common.Address, amtA *big.Int, amtB *big.Int) { + fmt.Printf("AddLiquidity tokenA %s tokenB %s amtA %v amtB %v\n", tokenA.Hex(), tokenB.Hex(), amtA, amtB) + hbswapInstance, err := hbswap.NewHbSwap(HbswapAddr, conn) + if err != nil { + log.Fatal(err) + } + + fundGas(conn, auth.From) + tx, err := hbswapInstance.AddLiquidity(auth, tokenA, tokenB, amtA, amtB) + if err != nil { + log.Fatal(err) + } + + receipt, err := WaitMined(context.Background(), conn, tx, 0) + if err != nil { + log.Fatal(err) + } + + if receipt.Status == 0 { + log.Fatalf("Transaction status: %v", receipt.Status) + } +} + +func RemoveLiquidity(conn *ethclient.Client, auth *bind.TransactOpts, tokenA common.Address, tokenB common.Address, amt *big.Int) { + fmt.Printf("RemoveLiquidity tokenA %s tokenB %s amt %v\n", tokenA.Hex(), tokenB.Hex(), amt) + hbswapInstance, err := hbswap.NewHbSwap(HbswapAddr, conn) + if err != nil { + log.Fatal(err) + } + + fundGas(conn, auth.From) + tx, err := hbswapInstance.RemoveLiquidity(auth, tokenA, tokenB, amt) + if err != nil { + log.Fatal(err) + } + + receipt, err := WaitMined(context.Background(), conn, tx, 0) + if err != nil { + log.Fatal(err) + } + + if receipt.Status == 0 { + log.Fatalf("Transaction status: %v", receipt.Status) + } +} + +func Deposit(conn *ethclient.Client, auth *bind.TransactOpts, tokenAddr common.Address, amt *big.Int) { + fmt.Printf("Deposit user %s %v token %s\n", auth.From.Hex(), amt, tokenAddr.Hex()) + hbswapInstance, err := hbswap.NewHbSwap(HbswapAddr, conn) + if err != nil { + log.Fatal(err) + } + + fundGas(conn, auth.From) + tx, err := hbswapInstance.Deposit(auth, tokenAddr, amt) + if err != nil { + log.Fatal(err) + } + + receipt, err := WaitMined(context.Background(), conn, tx, 0) + if err != nil { + log.Fatal(err) + } + + if receipt.Status == 0 { + log.Fatalf("Transaction status: %v", receipt.Status) + } +} + +func Withdraw(conn *ethclient.Client, auth *bind.TransactOpts, tokenAddr common.Address, amt *big.Int) { + hbswapInstance, err := hbswap.NewHbSwap(HbswapAddr, conn) + if err != nil { + log.Fatal(err) + } + + fundGas(conn, auth.From) + tx, err := hbswapInstance.Withdraw(auth, tokenAddr, amt) + if err != nil { + log.Fatal(err) + } + + receipt, err := WaitMined(context.Background(), conn, tx, 0) + if err != nil { + log.Fatal(err) + } + + if receipt.Status == 0 { + log.Fatalf("Transaction status: %v", receipt.Status) + } +} + +func SecretDeposit(conn *ethclient.Client, auth *bind.TransactOpts, tokenAddr common.Address, amt *big.Int) { + hbswapInstance, err := hbswap.NewHbSwap(HbswapAddr, conn) + if err != nil { + log.Fatal(err) + } + + fundGas(conn, auth.From) + fmt.Printf("SecretDeposit %s %v\n", tokenAddr.String(), amt) + tx, err := hbswapInstance.SecretDeposit(auth, tokenAddr, amt) + if err != nil { + log.Fatal(err) + } + + receipt, err := WaitMined(context.Background(), conn, tx, 0) + if err != nil { + log.Fatal(err) + } + + if receipt.Status == 0 { + log.Fatalf("Transaction status: %v", receipt.Status) + } +} + +func SecretWithdraw(conn *ethclient.Client, auth *bind.TransactOpts, tokenAddr common.Address, amt *big.Int) { + fmt.Printf("secret withdraw %s\n", tokenAddr.Hex()) + hbswapInstance, err := hbswap.NewHbSwap(HbswapAddr, conn) + if err != nil { + log.Fatal(err) + } + + prevBalance := GetBalance(conn, tokenAddr, auth.From).Int64() + + fundGas(conn, auth.From) + tx, err := hbswapInstance.SecretWithdraw(auth, tokenAddr, amt) + if err != nil { + log.Fatal(err) + } + + receipt, err := WaitMined(context.Background(), conn, tx, 0) + if err != nil { + log.Fatal(err) + } + + if receipt.Status == 0 { + log.Fatalf("Transaction status: %v", receipt.Status) + } + + //fmt.Printf("current balance %v\n", prevBalance) + for true { + time.Sleep(10 * time.Second) + balance := GetBalance(conn, tokenAddr, auth.From).Int64() + //fmt.Printf("current balance %v\n", balance) + if prevBalance+amt.Int64() == balance { + break + } + } +} + +func Consent(conn *ethclient.Client, auth *bind.TransactOpts, seq *big.Int) { + fmt.Printf("Consent seq %v\n", seq) + hbswapInstance, err := hbswap.NewHbSwap(HbswapAddr, conn) + if err != nil { + log.Fatal(err) + } + + fundGas(conn, auth.From) + tx, err := hbswapInstance.Consent(auth, seq) + if err != nil { + log.Fatal(err) + } + + receipt, err := WaitMined(context.Background(), conn, tx, 0) + if err != nil { + log.Fatal(err) + } + + if receipt.Status == 0 { + log.Fatalf("Transaction status: %v", receipt.Status) + } + + fmt.Printf("Consent seq %v done\n", seq) +} + +func TradePrep(conn *ethclient.Client, auth *bind.TransactOpts) (int64, int64) { + hbswapInstance, err := hbswap.NewHbSwap(HbswapAddr, conn) + if err != nil { + log.Fatal(err) + } + + fundGas(conn, auth.From) + tx, err := hbswapInstance.TradePrep(auth) + if err != nil { + log.Fatal(err) + } + + receipt, err := WaitMined(context.Background(), conn, tx, 0) + if err != nil { + log.Fatal(err) + } + + if receipt.Status == 0 { + log.Fatalf("Transaction status: %v", receipt.Status) + } + + data := receipt.Logs[0].Data + idxSell, err := strconv.ParseInt(common.Bytes2Hex(data[1*32:2*32]), 16, 64) + if err != nil { + log.Fatal(err) + } + idxBuy, err := strconv.ParseInt(common.Bytes2Hex(data[2*32:3*32]), 16, 64) + if err != nil { + log.Fatal(err) + } + + fmt.Printf("idxSell:%v idxBuy:%v\n", idxSell, idxBuy) + return idxSell, idxBuy +} + +func Trade(conn *ethclient.Client, auth *bind.TransactOpts, tokenA common.Address, tokenB common.Address, idxA *big.Int, idxB *big.Int, maskedA *big.Int, maskedB *big.Int) { + hbswapInstance, err := hbswap.NewHbSwap(HbswapAddr, conn) + if err != nil { + log.Fatal(err) + } + + fundGas(conn, auth.From) + tx, err := hbswapInstance.Trade(auth, tokenA, tokenB, idxA, idxB, maskedA, maskedB) + if err != nil { + log.Fatal(err) + } + + receipt, err := WaitMined(context.Background(), conn, tx, 0) + if err != nil { + log.Fatal(err) + } + + if receipt.Status == 0 { + log.Fatalf("Transaction status: %v", receipt.Status) + } + + data := receipt.Logs[0].Data + tradeSeq, err := strconv.ParseInt(common.Bytes2Hex(data[0*32:1*32]), 16, 64) + if err != nil { + log.Fatal(err) + } + fmt.Printf("tradeSeq %v txHash %s\n", tradeSeq, tx.Hash().Hex()) +} + +func UpdatePrice(conn *ethclient.Client, auth *bind.TransactOpts, tokenA common.Address, tokenB common.Address, price string) { + hbswapInstance, err := hbswap.NewHbSwap(HbswapAddr, conn) + if err != nil { + log.Fatal(err) + } + + GetPrice(conn, tokenA, tokenB) + + fundGas(conn, auth.From) + tx, err := hbswapInstance.UpdatePrice(auth, tokenA, tokenB, price) + if err != nil { + log.Fatal(err) + } + + receipt, err := WaitMined(context.Background(), conn, tx, 0) + if err != nil { + log.Fatal(err) + } + + if receipt.Status == 0 { + log.Fatalf("Transaction status: %v", receipt.Status) + } + + GetPrice(conn, tokenA, tokenB) +} + +/******** read-only ********/ + +func GetBalance(conn *ethclient.Client, tokenAddr common.Address, user common.Address) *big.Int { + hbswapInstance, err := hbswap.NewHbSwap(HbswapAddr, conn) + if err != nil { + fmt.Printf("here") + log.Fatal(err) + } + + balance, _ := hbswapInstance.Balances(nil, tokenAddr, user) + fmt.Printf("On-chain balance user %s token %s: %v\n", user.Hex(), tokenAddr.Hex(), balance) + + return balance +} + +func GetInputmaskCnt(conn *ethclient.Client) int64 { + hbswapInstance, err := hbswap.NewHbSwap(HbswapAddr, conn) + if err != nil { + log.Fatal(err) + } + + cnt, _ := hbswapInstance.InputmaskCnt(nil) + fmt.Printf("Inputmaks shares used: %v\n", cnt) + + return cnt.Int64() +} + +func GetPrice(conn *ethclient.Client, tokenA common.Address, tokenB common.Address) string { + hbswapInstance, err := hbswap.NewHbSwap(HbswapAddr, conn) + if err != nil { + log.Fatal(err) + } + + price, _ := hbswapInstance.Prices(nil, tokenA, tokenB) + fmt.Printf("price: %v\n", price) + + return price +} + +func GetUpdateTime(conn *ethclient.Client, tokenA common.Address, tokenB common.Address) int64 { + hbswapInstance, err := hbswap.NewHbSwap(HbswapAddr, conn) + if err != nil { + log.Fatal(err) + } + + t, _ := hbswapInstance.UpdateTimes(nil, tokenA, tokenB) + fmt.Printf("updateTime: %v\n", t) + + return t.Int64() +} diff --git a/src/go/utils/utils_token.go b/src/go/utils/utils_token.go new file mode 100644 index 00000000..8f2fcaca --- /dev/null +++ b/src/go/utils/utils_token.go @@ -0,0 +1,83 @@ +package utils + +import ( + "context" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/ethclient" + "github.com/initc3/HoneyBadgerSwap/src/go_bindings/token" + "log" + "math/big" +) + +func FundToken(conn *ethclient.Client, tokenAddr common.Address, toAddr common.Address, amount *big.Int) { + balance := GetBalanceToken(conn, toAddr, tokenAddr) + if balance.Cmp(amount) != -1 { + //fmt.Printf("Funded account %s to %v token\n", toAddr.Hex(), balance) + return + } + amount.Sub(amount, balance) + + adminAuth := GetAccount("server_0") + transferToken(conn, tokenAddr, adminAuth, toAddr, amount) + + balance = GetBalanceToken(conn, toAddr, tokenAddr) + //fmt.Printf("Funded account %s to %v token\n", toAddr.Hex(), balance) +} + +func Approve(conn *ethclient.Client, auth *bind.TransactOpts, tokenAddr common.Address, receiver common.Address, amt *big.Int) { + tokenInstance, err := token.NewToken(tokenAddr, conn) + if err != nil { + log.Fatal(err) + } + + fundGas(conn, auth.From) + tx, err := tokenInstance.Approve(auth, receiver, amt) + if err != nil { + log.Fatal(err) + } + + receipt, err := WaitMined(context.Background(), conn, tx, 0) + if err != nil { + log.Fatal(err) + } + + if receipt.Status == 0 { + log.Fatalf("Transaction status: %v", receipt.Status) + } +} + +func GetBalanceToken(conn *ethclient.Client, addr common.Address, tokenAddr common.Address) *big.Int { + tokenInstance, err := token.NewToken(tokenAddr, conn) + if err != nil { + log.Fatal(err) + } + + balance, err := tokenInstance.BalanceOf(nil, addr) + if err != nil { + log.Fatal(err) + } + return balance +} + +func transferToken(conn *ethclient.Client, tokenAddr common.Address, fromAuth *bind.TransactOpts, toAddr common.Address, value *big.Int) { + //fmt.Printf("Trasfering %v token from %s to %s\n", value, fromAuth.From.Hex(), toAddr.Hex()) + + tokenInstance, err := token.NewToken(tokenAddr, conn) + if err != nil { + log.Fatal(err) + } + + tx, err := tokenInstance.Transfer(fromAuth, toAddr, value) + if err != nil { + log.Fatal(err) + } + + receipt, err := WaitMined(context.Background(), conn, tx, 0) + if err != nil { + log.Fatal(err) + } + if receipt.Status == 0 { + log.Fatalf("Transaction status: %x", receipt.Status) + } +} diff --git a/src/go_bindings/hbswap/hbswap.go b/src/go_bindings/hbswap/hbswap.go new file mode 100644 index 00000000..68a6249c --- /dev/null +++ b/src/go_bindings/hbswap/hbswap.go @@ -0,0 +1,1607 @@ +// Code generated - DO NOT EDIT. +// This file is a generated binding and any manual changes will be lost. + +package hbswap + +import ( + "math/big" + "strings" + + ethereum "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/event" +) + +// Reference imports to suppress errors if they are not otherwise used. +var ( + _ = big.NewInt + _ = strings.NewReader + _ = ethereum.NotFound + _ = abi.U256 + _ = bind.Bind + _ = common.Big1 + _ = types.BloomLookup + _ = event.NewSubscription +) + +// HbSwapABI is the input ABI used to generate the binding from. +const HbSwapABI = "[{\"inputs\":[{\"internalType\":\"address[]\",\"name\":\"_servers\",\"type\":\"address[]\"},{\"internalType\":\"uint256\",\"name\":\"_threshold\",\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"address\",\"name\":\"user\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"tokenA\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"tokenB\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"amtA\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"amtB\",\"type\":\"uint256\"}],\"name\":\"AddLiquidity\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"address\",\"name\":\"user\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"tokenA\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"tokenB\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"amtA\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"amtB\",\"type\":\"uint256\"}],\"name\":\"InitPool\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"address\",\"name\":\"user\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"tokenA\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"tokenB\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"amt\",\"type\":\"uint256\"}],\"name\":\"RemoveLiquidity\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"address\",\"name\":\"token\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"user\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"amt\",\"type\":\"uint256\"}],\"name\":\"SecretDeposit\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"seq\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"token\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"user\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"amt\",\"type\":\"uint256\"}],\"name\":\"SecretWithdraw\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"tradeSeq\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"user\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"tokenA\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"tokenB\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"idxA\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"idxB\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"maskedA\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"maskedB\",\"type\":\"uint256\"}],\"name\":\"Trade\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"address\",\"name\":\"user\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"idxA\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"idxB\",\"type\":\"uint256\"}],\"name\":\"TradePrep\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"name\":\"balances\",\"constant\":true,\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"},{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"name\":\"consentRecord\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"inputmaskCnt\",\"constant\":true,\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"name\":\"prices\",\"constant\":true,\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"secretWithdrawCnt\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"secretWithdrawMap\",\"outputs\":[{\"internalType\":\"address\",\"name\":\"token\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"user\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amt\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"name\":\"servers\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"threshold\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"tradeCnt\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"name\":\"updateTimes\",\"constant\":true,\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"_tokenA\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"_tokenB\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"_amtA\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"_amtB\",\"type\":\"uint256\"}],\"name\":\"initPool\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"_tokenA\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"_tokenB\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"_amtA\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"_amtB\",\"type\":\"uint256\"}],\"name\":\"addLiquidity\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"_tokenA\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"_tokenB\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"_amt\",\"type\":\"uint256\"}],\"name\":\"removeLiquidity\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"_token\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"_amt\",\"type\":\"uint256\"}],\"name\":\"deposit\",\"outputs\":[],\"stateMutability\":\"payable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"_token\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"_amt\",\"type\":\"uint256\"}],\"name\":\"secretDeposit\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"_token\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"_amt\",\"type\":\"uint256\"}],\"name\":\"secretWithdraw\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"_seq\",\"type\":\"uint256\"}],\"name\":\"consent\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"_token\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"_amt\",\"type\":\"uint256\"}],\"name\":\"withdraw\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"tradePrep\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"_tokenA\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"_tokenB\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"_idxA\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"_idxB\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"_maskedA\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"_maskedB\",\"type\":\"uint256\"}],\"name\":\"trade\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"_tokenA\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"_tokenB\",\"type\":\"address\"},{\"internalType\":\"string\",\"name\":\"_price\",\"type\":\"string\"}],\"name\":\"updatePrice\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]" + +// HbSwapBin is the compiled bytecode used for deploying new contracts. +var HbSwapBin = "0x60806040523480156200001157600080fd5b506040516200271d3803806200271d833981810160405260408110156200003757600080fd5b81019080805160405193929190846401000000008211156200005857600080fd5b838201915060208201858111156200006f57600080fd5b82518660208202830111640100000000821117156200008d57600080fd5b8083526020830192505050908051906020019060200280838360005b83811015620000c6578082015181840152602081019050620000a9565b505050509050016040526020018051906020019092919050505060008090505b8251811015620001695760018060008584815181106200010257fe5b602002602001015173ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff0219169083151502179055508080600101915050620000e6565b5080600081905550505061259a80620001836000396000f3fe60806040526004361061012a5760003560e01c80639eef6ce8116100ab578063c23f001f1161006f578063c23f001f146106f4578063cf6c62ea14610779578063d3fd8987146107fe578063d752fab2146108c4578063dee405951461093f578063f3fef3a31461099a5761012a565b80639eef6ce814610515578063ade28aad14610550578063af4170c4146105ab578063b72a2139146105d6578063bca8a7c11461066f5761012a565b80633394dc6f116100f25780633394dc6f1461039157806342cde4e8146103bc57806347e7ef24146103e75780637aa6fd651461043557806393910e66146104ea5761012a565b8063064d48101461012f57806312ada8de146101465780631768af96146101af5780631f312404146102225780632f8a68c51461030c575b600080fd5b34801561013b57600080fd5b506101446109f5565b005b34801561015257600080fd5b506101956004803603602081101561016957600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610a81565b604051808215151515815260200191505060405180910390f35b3480156101bb57600080fd5b50610208600480360360408110156101d257600080fd5b8101908080359060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610aa1565b604051808215151515815260200191505060405180910390f35b34801561022e57600080fd5b506102916004803603604081101561024557600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610ad0565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156102d15780820151818401526020810190506102b6565b50505050905090810190601f1680156102fe5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561031857600080fd5b5061038f6004803603608081101561032f57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919080359060200190929190505050610b8d565b005b34801561039d57600080fd5b506103a6610d0f565b6040518082815260200191505060405180910390f35b3480156103c857600080fd5b506103d1610d15565b6040518082815260200191505060405180910390f35b610433600480360360408110156103fd57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610d1b565b005b34801561044157600080fd5b5061046e6004803603602081101561045857600080fd5b8101908080359060200190929190505050610ea7565b604051808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001828152602001935050505060405180910390f35b3480156104f657600080fd5b506104ff610f11565b6040518082815260200191505060405180910390f35b34801561052157600080fd5b5061054e6004803603602081101561053857600080fd5b8101908080359060200190929190505050610f17565b005b34801561055c57600080fd5b506105a96004803603604081101561057357600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050611324565b005b3480156105b757600080fd5b506105c0611549565b6040518082815260200191505060405180910390f35b3480156105e257600080fd5b5061066d600480360360c08110156105f957600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919080359060200190929190803590602001909291908035906020019092919050505061154f565b005b34801561067b57600080fd5b506106de6004803603604081101561069257600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506116fe565b6040518082815260200191505060405180910390f35b34801561070057600080fd5b506107636004803603604081101561071757600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050611723565b6040518082815260200191505060405180910390f35b34801561078557600080fd5b506107fc6004803603608081101561079c57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919080359060200190929190505050611748565b005b34801561080a57600080fd5b506108c26004803603606081101561082157600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019064010000000081111561087e57600080fd5b82018360208201111561089057600080fd5b803590602001918460018302840111640100000000831117156108b257600080fd5b90919293919293905050506118ca565b005b3480156108d057600080fd5b5061093d600480360360608110156108e757600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050611a7e565b005b34801561094b57600080fd5b506109986004803603604081101561096257600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050611bf7565b005b3480156109a657600080fd5b506109f3600480360360408110156109bd57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050611db2565b005b7fb2fd402d6b838b10cf190139b9d4495eefcfea7543bc1056544d13732d82e6ac33600254600160025401604051808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001838152602001828152602001935050505060405180910390a160028060008282540192505081905550565b60016020528060005260406000206000915054906101000a900460ff1681565b60076020528160005260406000206020528060005260406000206000915091509054906101000a900460ff1681565b600a602052816000526040600020602052806000526040600020600091509150508054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610b855780601f10610b5a57610100808354040283529160200191610b85565b820191906000526020600020905b815481529060010190602001808311610b6857829003601f168201915b505050505081565b8273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff1610610c2e576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f696e76616c69642074726164696e67207061697200000000000000000000000081525060200191505060405180910390fd5b7ffaaebcb30b1b421f4f2ca7f2620e5add6a64532c087ee0646fd665a33d36fdf53385858585604051808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018381526020018281526020019550505050505060405180910390a150505050565b60055481565b60005481565b6000339050600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff161415610de45734600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540192505081905550610ea2565b6000839050610e168230858473ffffffffffffffffffffffffffffffffffffffff16611f83909392919063ffffffff16565b82600460008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540192505081905550505b505050565b60066020528060005260406000206000915090508060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16908060010160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16908060020154905083565b60025481565b6000339050600160008273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff16610fdb576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260128152602001807f6e6f7420612076616c696420736572766572000000000000000000000000000081525060200191505060405180910390fd5b6007600083815260200190815260200160002060008273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff16156110ac576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252600f8152602001807f616c726561647920636f6e73656e74000000000000000000000000000000000081525060200191505060405180910390fd5b60016007600084815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff02191690831515021790555060016008600084815260200190815260200160002060008282540192505081905550600054600860008481526020019081526020016000205411801561117957506009600083815260200190815260200160002060009054906101000a900460ff16155b1561132057611186612448565b600660008481526020019081526020016000206040518060600160405290816000820160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020016001820160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020016002820154815250509050806040015160046000836000015173ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000836020015173ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555060016009600085815260200190815260200160002060006101000a81548160ff021916908315150217905550505b5050565b600033905081600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561141b576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260128152602001807f6e6f7420656e6f7567682062616c616e6365000000000000000000000000000081525060200191505060405180910390fd5b81600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055507f07c06144435b7d2bdccf9ee7e5a7022c63382ac7c3a0e14ed08b5969dedf0ecf838284604051808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001828152602001935050505060405180910390a1505050565b60035481565b8473ffffffffffffffffffffffffffffffffffffffff168673ffffffffffffffffffffffffffffffffffffffff16106115f0576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f696e76616c69642074726164696e67207061697200000000000000000000000081525060200191505060405180910390fd5b60016003600082825401925050819055507f2b4d91cd20cc8800407e3614b8466a6f0729ac3b1fa43d4e2b059ff5593cbae660035433888888888888604051808981526020018873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018581526020018481526020018381526020018281526020019850505050505050505060405180910390a1505050505050565b600b602052816000526040600020602052806000526040600020600091509150505481565b6004602052816000526040600020602052806000526040600020600091509150505481565b8273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff16106117e9576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f696e76616c69642074726164696e67207061697200000000000000000000000081525060200191505060405180910390fd5b7fc33fbc9654f9c0dcfcbd829113bdb10afe95619bc0824bc5959ad82fd6952bd93385858585604051808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018381526020018281526020019550505050505060405180910390a150505050565b8273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff161061196b576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f696e76616c69642074726164696e67207061697200000000000000000000000081525060200191505060405180910390fd5b8181600a60008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002091906119f6929190612495565b5043600b60008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555050505050565b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1610611b1f576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f696e76616c69642074726164696e67207061697200000000000000000000000081525060200191505060405180910390fd5b7f2a1a4e62bda5b4987b9aa2f23ddbb29e434808f7a717452a3226bc15243c927733848484604051808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200182815260200194505050505060405180910390a1505050565b6000339050600160056000828254019250508190555060405180606001604052808473ffffffffffffffffffffffffffffffffffffffff1681526020018273ffffffffffffffffffffffffffffffffffffffff1681526020018381525060066000600554815260200190815260200160002060008201518160000160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060208201518160010160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550604082015181600201559050507f4ef3cc4825a92c3b6922acc8a45152cc96ef48463e8ed500dacd5df9e29a67f3600554848385604051808581526020018473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200182815260200194505050505060405180910390a1505050565b6000339050600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff161415611ec25781600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055508073ffffffffffffffffffffffffffffffffffffffff166108fc839081150290604051600060405180830381858888f19350505050158015611ebc573d6000803e3d6000fd5b50611f7e565b6000839050611ef282848373ffffffffffffffffffffffffffffffffffffffff166120709092919063ffffffff16565b82600460008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550505b505050565b61206a846323b872dd60e01b858585604051602401808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018281526020019350505050604051602081830303815290604052907bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff8381831617835250505050612128565b50505050565b6121238363a9059cbb60e01b8484604051602401808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200182815260200192505050604051602081830303815290604052907bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff8381831617835250505050612128565b505050565b606061218a826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c65648152508573ffffffffffffffffffffffffffffffffffffffff166122179092919063ffffffff16565b9050600081511115612212578080602001905160208110156121ab57600080fd5b8101908080519060200190929190505050612211576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602a81526020018061253b602a913960400191505060405180910390fd5b5b505050565b6060612226848460008561222f565b90509392505050565b606061223a85612435565b6122ac576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601d8152602001807f416464726573733a2063616c6c20746f206e6f6e2d636f6e747261637400000081525060200191505060405180910390fd5b600060608673ffffffffffffffffffffffffffffffffffffffff1685876040518082805190602001908083835b602083106122fc57805182526020820191506020810190506020830392506122d9565b6001836020036101000a03801982511681845116808217855250505050505090500191505060006040518083038185875af1925050503d806000811461235e576040519150601f19603f3d011682016040523d82523d6000602084013e612363565b606091505b5091509150811561237857809250505061242d565b60008151111561238b5780518082602001fd5b836040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b838110156123f25780820151818401526020810190506123d7565b50505050905090810190601f16801561241f5780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b949350505050565b600080823b905060008111915050919050565b6040518060600160405280600073ffffffffffffffffffffffffffffffffffffffff168152602001600073ffffffffffffffffffffffffffffffffffffffff168152602001600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106124d657803560ff1916838001178555612504565b82800160010185558215612504579182015b828111156125035782358255916020019190600101906124e8565b5b5090506125119190612515565b5090565b61253791905b8082111561253357600081600090555060010161251b565b5090565b9056fe5361666545524332303a204552433230206f7065726174696f6e20646964206e6f742073756363656564a264697066735822122079ae607cba125fe17e83abc35ceb75533db541525cc262a7a6e2f205bdda783964736f6c634300060a0033" + +// DeployHbSwap deploys a new Ethereum contract, binding an instance of HbSwap to it. +func DeployHbSwap(auth *bind.TransactOpts, backend bind.ContractBackend, _servers []common.Address, _threshold *big.Int) (common.Address, *types.Transaction, *HbSwap, error) { + parsed, err := abi.JSON(strings.NewReader(HbSwapABI)) + if err != nil { + return common.Address{}, nil, nil, err + } + + address, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex(HbSwapBin), backend, _servers, _threshold) + if err != nil { + return common.Address{}, nil, nil, err + } + return address, tx, &HbSwap{HbSwapCaller: HbSwapCaller{contract: contract}, HbSwapTransactor: HbSwapTransactor{contract: contract}, HbSwapFilterer: HbSwapFilterer{contract: contract}}, nil +} + +// HbSwap is an auto generated Go binding around an Ethereum contract. +type HbSwap struct { + HbSwapCaller // Read-only binding to the contract + HbSwapTransactor // Write-only binding to the contract + HbSwapFilterer // Log filterer for contract events +} + +// HbSwapCaller is an auto generated read-only Go binding around an Ethereum contract. +type HbSwapCaller struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// HbSwapTransactor is an auto generated write-only Go binding around an Ethereum contract. +type HbSwapTransactor struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// HbSwapFilterer is an auto generated log filtering Go binding around an Ethereum contract events. +type HbSwapFilterer struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// HbSwapSession is an auto generated Go binding around an Ethereum contract, +// with pre-set call and transact options. +type HbSwapSession struct { + Contract *HbSwap // Generic contract binding to set the session for + CallOpts bind.CallOpts // Call options to use throughout this session + TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session +} + +// HbSwapCallerSession is an auto generated read-only Go binding around an Ethereum contract, +// with pre-set call options. +type HbSwapCallerSession struct { + Contract *HbSwapCaller // Generic contract caller binding to set the session for + CallOpts bind.CallOpts // Call options to use throughout this session +} + +// HbSwapTransactorSession is an auto generated write-only Go binding around an Ethereum contract, +// with pre-set transact options. +type HbSwapTransactorSession struct { + Contract *HbSwapTransactor // Generic contract transactor binding to set the session for + TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session +} + +// HbSwapRaw is an auto generated low-level Go binding around an Ethereum contract. +type HbSwapRaw struct { + Contract *HbSwap // Generic contract binding to access the raw methods on +} + +// HbSwapCallerRaw is an auto generated low-level read-only Go binding around an Ethereum contract. +type HbSwapCallerRaw struct { + Contract *HbSwapCaller // Generic read-only contract binding to access the raw methods on +} + +// HbSwapTransactorRaw is an auto generated low-level write-only Go binding around an Ethereum contract. +type HbSwapTransactorRaw struct { + Contract *HbSwapTransactor // Generic write-only contract binding to access the raw methods on +} + +// NewHbSwap creates a new instance of HbSwap, bound to a specific deployed contract. +func NewHbSwap(address common.Address, backend bind.ContractBackend) (*HbSwap, error) { + contract, err := bindHbSwap(address, backend, backend, backend) + if err != nil { + return nil, err + } + return &HbSwap{HbSwapCaller: HbSwapCaller{contract: contract}, HbSwapTransactor: HbSwapTransactor{contract: contract}, HbSwapFilterer: HbSwapFilterer{contract: contract}}, nil +} + +// NewHbSwapCaller creates a new read-only instance of HbSwap, bound to a specific deployed contract. +func NewHbSwapCaller(address common.Address, caller bind.ContractCaller) (*HbSwapCaller, error) { + contract, err := bindHbSwap(address, caller, nil, nil) + if err != nil { + return nil, err + } + return &HbSwapCaller{contract: contract}, nil +} + +// NewHbSwapTransactor creates a new write-only instance of HbSwap, bound to a specific deployed contract. +func NewHbSwapTransactor(address common.Address, transactor bind.ContractTransactor) (*HbSwapTransactor, error) { + contract, err := bindHbSwap(address, nil, transactor, nil) + if err != nil { + return nil, err + } + return &HbSwapTransactor{contract: contract}, nil +} + +// NewHbSwapFilterer creates a new log filterer instance of HbSwap, bound to a specific deployed contract. +func NewHbSwapFilterer(address common.Address, filterer bind.ContractFilterer) (*HbSwapFilterer, error) { + contract, err := bindHbSwap(address, nil, nil, filterer) + if err != nil { + return nil, err + } + return &HbSwapFilterer{contract: contract}, nil +} + +// bindHbSwap binds a generic wrapper to an already deployed contract. +func bindHbSwap(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor, filterer bind.ContractFilterer) (*bind.BoundContract, error) { + parsed, err := abi.JSON(strings.NewReader(HbSwapABI)) + if err != nil { + return nil, err + } + return bind.NewBoundContract(address, parsed, caller, transactor, filterer), nil +} + +// Call invokes the (constant) contract method with params as input values and +// sets the output to result. The result type might be a single field for simple +// returns, a slice of interfaces for anonymous returns and a struct for named +// returns. +func (_HbSwap *HbSwapRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error { + return _HbSwap.Contract.HbSwapCaller.contract.Call(opts, result, method, params...) +} + +// Transfer initiates a plain transaction to move funds to the contract, calling +// its default method if one is available. +func (_HbSwap *HbSwapRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) { + return _HbSwap.Contract.HbSwapTransactor.contract.Transfer(opts) +} + +// Transact invokes the (paid) contract method with params as input values. +func (_HbSwap *HbSwapRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { + return _HbSwap.Contract.HbSwapTransactor.contract.Transact(opts, method, params...) +} + +// Call invokes the (constant) contract method with params as input values and +// sets the output to result. The result type might be a single field for simple +// returns, a slice of interfaces for anonymous returns and a struct for named +// returns. +func (_HbSwap *HbSwapCallerRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error { + return _HbSwap.Contract.contract.Call(opts, result, method, params...) +} + +// Transfer initiates a plain transaction to move funds to the contract, calling +// its default method if one is available. +func (_HbSwap *HbSwapTransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) { + return _HbSwap.Contract.contract.Transfer(opts) +} + +// Transact invokes the (paid) contract method with params as input values. +func (_HbSwap *HbSwapTransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { + return _HbSwap.Contract.contract.Transact(opts, method, params...) +} + +// Balances is a free data retrieval call binding the contract method 0xc23f001f. +// +// Solidity: function balances(address , address ) constant returns(uint256) +func (_HbSwap *HbSwapCaller) Balances(opts *bind.CallOpts, arg0 common.Address, arg1 common.Address) (*big.Int, error) { + var ( + ret0 = new(*big.Int) + ) + out := ret0 + err := _HbSwap.contract.Call(opts, out, "balances", arg0, arg1) + return *ret0, err +} + +// Balances is a free data retrieval call binding the contract method 0xc23f001f. +// +// Solidity: function balances(address , address ) constant returns(uint256) +func (_HbSwap *HbSwapSession) Balances(arg0 common.Address, arg1 common.Address) (*big.Int, error) { + return _HbSwap.Contract.Balances(&_HbSwap.CallOpts, arg0, arg1) +} + +// Balances is a free data retrieval call binding the contract method 0xc23f001f. +// +// Solidity: function balances(address , address ) constant returns(uint256) +func (_HbSwap *HbSwapCallerSession) Balances(arg0 common.Address, arg1 common.Address) (*big.Int, error) { + return _HbSwap.Contract.Balances(&_HbSwap.CallOpts, arg0, arg1) +} + +// InputmaskCnt is a free data retrieval call binding the contract method 0x93910e66. +// +// Solidity: function inputmaskCnt() constant returns(uint256) +func (_HbSwap *HbSwapCaller) InputmaskCnt(opts *bind.CallOpts) (*big.Int, error) { + var ( + ret0 = new(*big.Int) + ) + out := ret0 + err := _HbSwap.contract.Call(opts, out, "inputmaskCnt") + return *ret0, err +} + +// InputmaskCnt is a free data retrieval call binding the contract method 0x93910e66. +// +// Solidity: function inputmaskCnt() constant returns(uint256) +func (_HbSwap *HbSwapSession) InputmaskCnt() (*big.Int, error) { + return _HbSwap.Contract.InputmaskCnt(&_HbSwap.CallOpts) +} + +// InputmaskCnt is a free data retrieval call binding the contract method 0x93910e66. +// +// Solidity: function inputmaskCnt() constant returns(uint256) +func (_HbSwap *HbSwapCallerSession) InputmaskCnt() (*big.Int, error) { + return _HbSwap.Contract.InputmaskCnt(&_HbSwap.CallOpts) +} + +// Prices is a free data retrieval call binding the contract method 0x1f312404. +// +// Solidity: function prices(address , address ) constant returns(string) +func (_HbSwap *HbSwapCaller) Prices(opts *bind.CallOpts, arg0 common.Address, arg1 common.Address) (string, error) { + var ( + ret0 = new(string) + ) + out := ret0 + err := _HbSwap.contract.Call(opts, out, "prices", arg0, arg1) + return *ret0, err +} + +// Prices is a free data retrieval call binding the contract method 0x1f312404. +// +// Solidity: function prices(address , address ) constant returns(string) +func (_HbSwap *HbSwapSession) Prices(arg0 common.Address, arg1 common.Address) (string, error) { + return _HbSwap.Contract.Prices(&_HbSwap.CallOpts, arg0, arg1) +} + +// Prices is a free data retrieval call binding the contract method 0x1f312404. +// +// Solidity: function prices(address , address ) constant returns(string) +func (_HbSwap *HbSwapCallerSession) Prices(arg0 common.Address, arg1 common.Address) (string, error) { + return _HbSwap.Contract.Prices(&_HbSwap.CallOpts, arg0, arg1) +} + +// UpdateTimes is a free data retrieval call binding the contract method 0xbca8a7c1. +// +// Solidity: function updateTimes(address , address ) constant returns(uint256) +func (_HbSwap *HbSwapCaller) UpdateTimes(opts *bind.CallOpts, arg0 common.Address, arg1 common.Address) (*big.Int, error) { + var ( + ret0 = new(*big.Int) + ) + out := ret0 + err := _HbSwap.contract.Call(opts, out, "updateTimes", arg0, arg1) + return *ret0, err +} + +// UpdateTimes is a free data retrieval call binding the contract method 0xbca8a7c1. +// +// Solidity: function updateTimes(address , address ) constant returns(uint256) +func (_HbSwap *HbSwapSession) UpdateTimes(arg0 common.Address, arg1 common.Address) (*big.Int, error) { + return _HbSwap.Contract.UpdateTimes(&_HbSwap.CallOpts, arg0, arg1) +} + +// UpdateTimes is a free data retrieval call binding the contract method 0xbca8a7c1. +// +// Solidity: function updateTimes(address , address ) constant returns(uint256) +func (_HbSwap *HbSwapCallerSession) UpdateTimes(arg0 common.Address, arg1 common.Address) (*big.Int, error) { + return _HbSwap.Contract.UpdateTimes(&_HbSwap.CallOpts, arg0, arg1) +} + +// AddLiquidity is a paid mutator transaction binding the contract method 0xcf6c62ea. +// +// Solidity: function addLiquidity(address _tokenA, address _tokenB, uint256 _amtA, uint256 _amtB) returns() +func (_HbSwap *HbSwapTransactor) AddLiquidity(opts *bind.TransactOpts, _tokenA common.Address, _tokenB common.Address, _amtA *big.Int, _amtB *big.Int) (*types.Transaction, error) { + return _HbSwap.contract.Transact(opts, "addLiquidity", _tokenA, _tokenB, _amtA, _amtB) +} + +// AddLiquidity is a paid mutator transaction binding the contract method 0xcf6c62ea. +// +// Solidity: function addLiquidity(address _tokenA, address _tokenB, uint256 _amtA, uint256 _amtB) returns() +func (_HbSwap *HbSwapSession) AddLiquidity(_tokenA common.Address, _tokenB common.Address, _amtA *big.Int, _amtB *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.AddLiquidity(&_HbSwap.TransactOpts, _tokenA, _tokenB, _amtA, _amtB) +} + +// AddLiquidity is a paid mutator transaction binding the contract method 0xcf6c62ea. +// +// Solidity: function addLiquidity(address _tokenA, address _tokenB, uint256 _amtA, uint256 _amtB) returns() +func (_HbSwap *HbSwapTransactorSession) AddLiquidity(_tokenA common.Address, _tokenB common.Address, _amtA *big.Int, _amtB *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.AddLiquidity(&_HbSwap.TransactOpts, _tokenA, _tokenB, _amtA, _amtB) +} + +// Consent is a paid mutator transaction binding the contract method 0x9eef6ce8. +// +// Solidity: function consent(uint256 _seq) returns() +func (_HbSwap *HbSwapTransactor) Consent(opts *bind.TransactOpts, _seq *big.Int) (*types.Transaction, error) { + return _HbSwap.contract.Transact(opts, "consent", _seq) +} + +// Consent is a paid mutator transaction binding the contract method 0x9eef6ce8. +// +// Solidity: function consent(uint256 _seq) returns() +func (_HbSwap *HbSwapSession) Consent(_seq *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.Consent(&_HbSwap.TransactOpts, _seq) +} + +// Consent is a paid mutator transaction binding the contract method 0x9eef6ce8. +// +// Solidity: function consent(uint256 _seq) returns() +func (_HbSwap *HbSwapTransactorSession) Consent(_seq *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.Consent(&_HbSwap.TransactOpts, _seq) +} + +// ConsentRecord is a paid mutator transaction binding the contract method 0x1768af96. +// +// Solidity: function consentRecord(uint256 , address ) returns(bool) +func (_HbSwap *HbSwapTransactor) ConsentRecord(opts *bind.TransactOpts, arg0 *big.Int, arg1 common.Address) (*types.Transaction, error) { + return _HbSwap.contract.Transact(opts, "consentRecord", arg0, arg1) +} + +// ConsentRecord is a paid mutator transaction binding the contract method 0x1768af96. +// +// Solidity: function consentRecord(uint256 , address ) returns(bool) +func (_HbSwap *HbSwapSession) ConsentRecord(arg0 *big.Int, arg1 common.Address) (*types.Transaction, error) { + return _HbSwap.Contract.ConsentRecord(&_HbSwap.TransactOpts, arg0, arg1) +} + +// ConsentRecord is a paid mutator transaction binding the contract method 0x1768af96. +// +// Solidity: function consentRecord(uint256 , address ) returns(bool) +func (_HbSwap *HbSwapTransactorSession) ConsentRecord(arg0 *big.Int, arg1 common.Address) (*types.Transaction, error) { + return _HbSwap.Contract.ConsentRecord(&_HbSwap.TransactOpts, arg0, arg1) +} + +// Deposit is a paid mutator transaction binding the contract method 0x47e7ef24. +// +// Solidity: function deposit(address _token, uint256 _amt) returns() +func (_HbSwap *HbSwapTransactor) Deposit(opts *bind.TransactOpts, _token common.Address, _amt *big.Int) (*types.Transaction, error) { + return _HbSwap.contract.Transact(opts, "deposit", _token, _amt) +} + +// Deposit is a paid mutator transaction binding the contract method 0x47e7ef24. +// +// Solidity: function deposit(address _token, uint256 _amt) returns() +func (_HbSwap *HbSwapSession) Deposit(_token common.Address, _amt *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.Deposit(&_HbSwap.TransactOpts, _token, _amt) +} + +// Deposit is a paid mutator transaction binding the contract method 0x47e7ef24. +// +// Solidity: function deposit(address _token, uint256 _amt) returns() +func (_HbSwap *HbSwapTransactorSession) Deposit(_token common.Address, _amt *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.Deposit(&_HbSwap.TransactOpts, _token, _amt) +} + +// InitPool is a paid mutator transaction binding the contract method 0x2f8a68c5. +// +// Solidity: function initPool(address _tokenA, address _tokenB, uint256 _amtA, uint256 _amtB) returns() +func (_HbSwap *HbSwapTransactor) InitPool(opts *bind.TransactOpts, _tokenA common.Address, _tokenB common.Address, _amtA *big.Int, _amtB *big.Int) (*types.Transaction, error) { + return _HbSwap.contract.Transact(opts, "initPool", _tokenA, _tokenB, _amtA, _amtB) +} + +// InitPool is a paid mutator transaction binding the contract method 0x2f8a68c5. +// +// Solidity: function initPool(address _tokenA, address _tokenB, uint256 _amtA, uint256 _amtB) returns() +func (_HbSwap *HbSwapSession) InitPool(_tokenA common.Address, _tokenB common.Address, _amtA *big.Int, _amtB *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.InitPool(&_HbSwap.TransactOpts, _tokenA, _tokenB, _amtA, _amtB) +} + +// InitPool is a paid mutator transaction binding the contract method 0x2f8a68c5. +// +// Solidity: function initPool(address _tokenA, address _tokenB, uint256 _amtA, uint256 _amtB) returns() +func (_HbSwap *HbSwapTransactorSession) InitPool(_tokenA common.Address, _tokenB common.Address, _amtA *big.Int, _amtB *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.InitPool(&_HbSwap.TransactOpts, _tokenA, _tokenB, _amtA, _amtB) +} + +// RemoveLiquidity is a paid mutator transaction binding the contract method 0xd752fab2. +// +// Solidity: function removeLiquidity(address _tokenA, address _tokenB, uint256 _amt) returns() +func (_HbSwap *HbSwapTransactor) RemoveLiquidity(opts *bind.TransactOpts, _tokenA common.Address, _tokenB common.Address, _amt *big.Int) (*types.Transaction, error) { + return _HbSwap.contract.Transact(opts, "removeLiquidity", _tokenA, _tokenB, _amt) +} + +// RemoveLiquidity is a paid mutator transaction binding the contract method 0xd752fab2. +// +// Solidity: function removeLiquidity(address _tokenA, address _tokenB, uint256 _amt) returns() +func (_HbSwap *HbSwapSession) RemoveLiquidity(_tokenA common.Address, _tokenB common.Address, _amt *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.RemoveLiquidity(&_HbSwap.TransactOpts, _tokenA, _tokenB, _amt) +} + +// RemoveLiquidity is a paid mutator transaction binding the contract method 0xd752fab2. +// +// Solidity: function removeLiquidity(address _tokenA, address _tokenB, uint256 _amt) returns() +func (_HbSwap *HbSwapTransactorSession) RemoveLiquidity(_tokenA common.Address, _tokenB common.Address, _amt *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.RemoveLiquidity(&_HbSwap.TransactOpts, _tokenA, _tokenB, _amt) +} + +// SecretDeposit is a paid mutator transaction binding the contract method 0xade28aad. +// +// Solidity: function secretDeposit(address _token, uint256 _amt) returns() +func (_HbSwap *HbSwapTransactor) SecretDeposit(opts *bind.TransactOpts, _token common.Address, _amt *big.Int) (*types.Transaction, error) { + return _HbSwap.contract.Transact(opts, "secretDeposit", _token, _amt) +} + +// SecretDeposit is a paid mutator transaction binding the contract method 0xade28aad. +// +// Solidity: function secretDeposit(address _token, uint256 _amt) returns() +func (_HbSwap *HbSwapSession) SecretDeposit(_token common.Address, _amt *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.SecretDeposit(&_HbSwap.TransactOpts, _token, _amt) +} + +// SecretDeposit is a paid mutator transaction binding the contract method 0xade28aad. +// +// Solidity: function secretDeposit(address _token, uint256 _amt) returns() +func (_HbSwap *HbSwapTransactorSession) SecretDeposit(_token common.Address, _amt *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.SecretDeposit(&_HbSwap.TransactOpts, _token, _amt) +} + +// SecretWithdraw is a paid mutator transaction binding the contract method 0xdee40595. +// +// Solidity: function secretWithdraw(address _token, uint256 _amt) returns() +func (_HbSwap *HbSwapTransactor) SecretWithdraw(opts *bind.TransactOpts, _token common.Address, _amt *big.Int) (*types.Transaction, error) { + return _HbSwap.contract.Transact(opts, "secretWithdraw", _token, _amt) +} + +// SecretWithdraw is a paid mutator transaction binding the contract method 0xdee40595. +// +// Solidity: function secretWithdraw(address _token, uint256 _amt) returns() +func (_HbSwap *HbSwapSession) SecretWithdraw(_token common.Address, _amt *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.SecretWithdraw(&_HbSwap.TransactOpts, _token, _amt) +} + +// SecretWithdraw is a paid mutator transaction binding the contract method 0xdee40595. +// +// Solidity: function secretWithdraw(address _token, uint256 _amt) returns() +func (_HbSwap *HbSwapTransactorSession) SecretWithdraw(_token common.Address, _amt *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.SecretWithdraw(&_HbSwap.TransactOpts, _token, _amt) +} + +// SecretWithdrawCnt is a paid mutator transaction binding the contract method 0x3394dc6f. +// +// Solidity: function secretWithdrawCnt() returns(uint256) +func (_HbSwap *HbSwapTransactor) SecretWithdrawCnt(opts *bind.TransactOpts) (*types.Transaction, error) { + return _HbSwap.contract.Transact(opts, "secretWithdrawCnt") +} + +// SecretWithdrawCnt is a paid mutator transaction binding the contract method 0x3394dc6f. +// +// Solidity: function secretWithdrawCnt() returns(uint256) +func (_HbSwap *HbSwapSession) SecretWithdrawCnt() (*types.Transaction, error) { + return _HbSwap.Contract.SecretWithdrawCnt(&_HbSwap.TransactOpts) +} + +// SecretWithdrawCnt is a paid mutator transaction binding the contract method 0x3394dc6f. +// +// Solidity: function secretWithdrawCnt() returns(uint256) +func (_HbSwap *HbSwapTransactorSession) SecretWithdrawCnt() (*types.Transaction, error) { + return _HbSwap.Contract.SecretWithdrawCnt(&_HbSwap.TransactOpts) +} + +// SecretWithdrawMap is a paid mutator transaction binding the contract method 0x7aa6fd65. +// +// Solidity: function secretWithdrawMap(uint256 ) returns(address token, address user, uint256 amt) +func (_HbSwap *HbSwapTransactor) SecretWithdrawMap(opts *bind.TransactOpts, arg0 *big.Int) (*types.Transaction, error) { + return _HbSwap.contract.Transact(opts, "secretWithdrawMap", arg0) +} + +// SecretWithdrawMap is a paid mutator transaction binding the contract method 0x7aa6fd65. +// +// Solidity: function secretWithdrawMap(uint256 ) returns(address token, address user, uint256 amt) +func (_HbSwap *HbSwapSession) SecretWithdrawMap(arg0 *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.SecretWithdrawMap(&_HbSwap.TransactOpts, arg0) +} + +// SecretWithdrawMap is a paid mutator transaction binding the contract method 0x7aa6fd65. +// +// Solidity: function secretWithdrawMap(uint256 ) returns(address token, address user, uint256 amt) +func (_HbSwap *HbSwapTransactorSession) SecretWithdrawMap(arg0 *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.SecretWithdrawMap(&_HbSwap.TransactOpts, arg0) +} + +// Servers is a paid mutator transaction binding the contract method 0x12ada8de. +// +// Solidity: function servers(address ) returns(bool) +func (_HbSwap *HbSwapTransactor) Servers(opts *bind.TransactOpts, arg0 common.Address) (*types.Transaction, error) { + return _HbSwap.contract.Transact(opts, "servers", arg0) +} + +// Servers is a paid mutator transaction binding the contract method 0x12ada8de. +// +// Solidity: function servers(address ) returns(bool) +func (_HbSwap *HbSwapSession) Servers(arg0 common.Address) (*types.Transaction, error) { + return _HbSwap.Contract.Servers(&_HbSwap.TransactOpts, arg0) +} + +// Servers is a paid mutator transaction binding the contract method 0x12ada8de. +// +// Solidity: function servers(address ) returns(bool) +func (_HbSwap *HbSwapTransactorSession) Servers(arg0 common.Address) (*types.Transaction, error) { + return _HbSwap.Contract.Servers(&_HbSwap.TransactOpts, arg0) +} + +// Threshold is a paid mutator transaction binding the contract method 0x42cde4e8. +// +// Solidity: function threshold() returns(uint256) +func (_HbSwap *HbSwapTransactor) Threshold(opts *bind.TransactOpts) (*types.Transaction, error) { + return _HbSwap.contract.Transact(opts, "threshold") +} + +// Threshold is a paid mutator transaction binding the contract method 0x42cde4e8. +// +// Solidity: function threshold() returns(uint256) +func (_HbSwap *HbSwapSession) Threshold() (*types.Transaction, error) { + return _HbSwap.Contract.Threshold(&_HbSwap.TransactOpts) +} + +// Threshold is a paid mutator transaction binding the contract method 0x42cde4e8. +// +// Solidity: function threshold() returns(uint256) +func (_HbSwap *HbSwapTransactorSession) Threshold() (*types.Transaction, error) { + return _HbSwap.Contract.Threshold(&_HbSwap.TransactOpts) +} + +// Trade is a paid mutator transaction binding the contract method 0xb72a2139. +// +// Solidity: function trade(address _tokenA, address _tokenB, uint256 _idxA, uint256 _idxB, uint256 _maskedA, uint256 _maskedB) returns() +func (_HbSwap *HbSwapTransactor) Trade(opts *bind.TransactOpts, _tokenA common.Address, _tokenB common.Address, _idxA *big.Int, _idxB *big.Int, _maskedA *big.Int, _maskedB *big.Int) (*types.Transaction, error) { + return _HbSwap.contract.Transact(opts, "trade", _tokenA, _tokenB, _idxA, _idxB, _maskedA, _maskedB) +} + +// Trade is a paid mutator transaction binding the contract method 0xb72a2139. +// +// Solidity: function trade(address _tokenA, address _tokenB, uint256 _idxA, uint256 _idxB, uint256 _maskedA, uint256 _maskedB) returns() +func (_HbSwap *HbSwapSession) Trade(_tokenA common.Address, _tokenB common.Address, _idxA *big.Int, _idxB *big.Int, _maskedA *big.Int, _maskedB *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.Trade(&_HbSwap.TransactOpts, _tokenA, _tokenB, _idxA, _idxB, _maskedA, _maskedB) +} + +// Trade is a paid mutator transaction binding the contract method 0xb72a2139. +// +// Solidity: function trade(address _tokenA, address _tokenB, uint256 _idxA, uint256 _idxB, uint256 _maskedA, uint256 _maskedB) returns() +func (_HbSwap *HbSwapTransactorSession) Trade(_tokenA common.Address, _tokenB common.Address, _idxA *big.Int, _idxB *big.Int, _maskedA *big.Int, _maskedB *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.Trade(&_HbSwap.TransactOpts, _tokenA, _tokenB, _idxA, _idxB, _maskedA, _maskedB) +} + +// TradeCnt is a paid mutator transaction binding the contract method 0xaf4170c4. +// +// Solidity: function tradeCnt() returns(uint256) +func (_HbSwap *HbSwapTransactor) TradeCnt(opts *bind.TransactOpts) (*types.Transaction, error) { + return _HbSwap.contract.Transact(opts, "tradeCnt") +} + +// TradeCnt is a paid mutator transaction binding the contract method 0xaf4170c4. +// +// Solidity: function tradeCnt() returns(uint256) +func (_HbSwap *HbSwapSession) TradeCnt() (*types.Transaction, error) { + return _HbSwap.Contract.TradeCnt(&_HbSwap.TransactOpts) +} + +// TradeCnt is a paid mutator transaction binding the contract method 0xaf4170c4. +// +// Solidity: function tradeCnt() returns(uint256) +func (_HbSwap *HbSwapTransactorSession) TradeCnt() (*types.Transaction, error) { + return _HbSwap.Contract.TradeCnt(&_HbSwap.TransactOpts) +} + +// TradePrep is a paid mutator transaction binding the contract method 0x064d4810. +// +// Solidity: function tradePrep() returns() +func (_HbSwap *HbSwapTransactor) TradePrep(opts *bind.TransactOpts) (*types.Transaction, error) { + return _HbSwap.contract.Transact(opts, "tradePrep") +} + +// TradePrep is a paid mutator transaction binding the contract method 0x064d4810. +// +// Solidity: function tradePrep() returns() +func (_HbSwap *HbSwapSession) TradePrep() (*types.Transaction, error) { + return _HbSwap.Contract.TradePrep(&_HbSwap.TransactOpts) +} + +// TradePrep is a paid mutator transaction binding the contract method 0x064d4810. +// +// Solidity: function tradePrep() returns() +func (_HbSwap *HbSwapTransactorSession) TradePrep() (*types.Transaction, error) { + return _HbSwap.Contract.TradePrep(&_HbSwap.TransactOpts) +} + +// UpdatePrice is a paid mutator transaction binding the contract method 0xd3fd8987. +// +// Solidity: function updatePrice(address _tokenA, address _tokenB, string _price) returns() +func (_HbSwap *HbSwapTransactor) UpdatePrice(opts *bind.TransactOpts, _tokenA common.Address, _tokenB common.Address, _price string) (*types.Transaction, error) { + return _HbSwap.contract.Transact(opts, "updatePrice", _tokenA, _tokenB, _price) +} + +// UpdatePrice is a paid mutator transaction binding the contract method 0xd3fd8987. +// +// Solidity: function updatePrice(address _tokenA, address _tokenB, string _price) returns() +func (_HbSwap *HbSwapSession) UpdatePrice(_tokenA common.Address, _tokenB common.Address, _price string) (*types.Transaction, error) { + return _HbSwap.Contract.UpdatePrice(&_HbSwap.TransactOpts, _tokenA, _tokenB, _price) +} + +// UpdatePrice is a paid mutator transaction binding the contract method 0xd3fd8987. +// +// Solidity: function updatePrice(address _tokenA, address _tokenB, string _price) returns() +func (_HbSwap *HbSwapTransactorSession) UpdatePrice(_tokenA common.Address, _tokenB common.Address, _price string) (*types.Transaction, error) { + return _HbSwap.Contract.UpdatePrice(&_HbSwap.TransactOpts, _tokenA, _tokenB, _price) +} + +// Withdraw is a paid mutator transaction binding the contract method 0xf3fef3a3. +// +// Solidity: function withdraw(address _token, uint256 _amt) returns() +func (_HbSwap *HbSwapTransactor) Withdraw(opts *bind.TransactOpts, _token common.Address, _amt *big.Int) (*types.Transaction, error) { + return _HbSwap.contract.Transact(opts, "withdraw", _token, _amt) +} + +// Withdraw is a paid mutator transaction binding the contract method 0xf3fef3a3. +// +// Solidity: function withdraw(address _token, uint256 _amt) returns() +func (_HbSwap *HbSwapSession) Withdraw(_token common.Address, _amt *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.Withdraw(&_HbSwap.TransactOpts, _token, _amt) +} + +// Withdraw is a paid mutator transaction binding the contract method 0xf3fef3a3. +// +// Solidity: function withdraw(address _token, uint256 _amt) returns() +func (_HbSwap *HbSwapTransactorSession) Withdraw(_token common.Address, _amt *big.Int) (*types.Transaction, error) { + return _HbSwap.Contract.Withdraw(&_HbSwap.TransactOpts, _token, _amt) +} + +// HbSwapAddLiquidityIterator is returned from FilterAddLiquidity and is used to iterate over the raw logs and unpacked data for AddLiquidity events raised by the HbSwap contract. +type HbSwapAddLiquidityIterator struct { + Event *HbSwapAddLiquidity // Event containing the contract specifics and raw log + + contract *bind.BoundContract // Generic contract to use for unpacking event data + event string // Event name to use for unpacking event data + + logs chan types.Log // Log channel receiving the found contract events + sub ethereum.Subscription // Subscription for errors, completion and termination + done bool // Whether the subscription completed delivering logs + fail error // Occurred error to stop iteration +} + +// Next advances the iterator to the subsequent event, returning whether there +// are any more events found. In case of a retrieval or parsing error, false is +// returned and Error() can be queried for the exact failure. +func (it *HbSwapAddLiquidityIterator) Next() bool { + // If the iterator failed, stop iterating + if it.fail != nil { + return false + } + // If the iterator completed, deliver directly whatever's available + if it.done { + select { + case log := <-it.logs: + it.Event = new(HbSwapAddLiquidity) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + default: + return false + } + } + // Iterator still in progress, wait for either a data or an error event + select { + case log := <-it.logs: + it.Event = new(HbSwapAddLiquidity) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + case err := <-it.sub.Err(): + it.done = true + it.fail = err + return it.Next() + } +} + +// Error returns any retrieval or parsing error occurred during filtering. +func (it *HbSwapAddLiquidityIterator) Error() error { + return it.fail +} + +// Close terminates the iteration process, releasing any pending underlying +// resources. +func (it *HbSwapAddLiquidityIterator) Close() error { + it.sub.Unsubscribe() + return nil +} + +// HbSwapAddLiquidity represents a AddLiquidity event raised by the HbSwap contract. +type HbSwapAddLiquidity struct { + User common.Address + TokenA common.Address + TokenB common.Address + AmtA *big.Int + AmtB *big.Int + Raw types.Log // Blockchain specific contextual infos +} + +// FilterAddLiquidity is a free log retrieval operation binding the contract event 0xc33fbc9654f9c0dcfcbd829113bdb10afe95619bc0824bc5959ad82fd6952bd9. +// +// Solidity: event AddLiquidity(address user, address tokenA, address tokenB, uint256 amtA, uint256 amtB) +func (_HbSwap *HbSwapFilterer) FilterAddLiquidity(opts *bind.FilterOpts) (*HbSwapAddLiquidityIterator, error) { + + logs, sub, err := _HbSwap.contract.FilterLogs(opts, "AddLiquidity") + if err != nil { + return nil, err + } + return &HbSwapAddLiquidityIterator{contract: _HbSwap.contract, event: "AddLiquidity", logs: logs, sub: sub}, nil +} + +// WatchAddLiquidity is a free log subscription operation binding the contract event 0xc33fbc9654f9c0dcfcbd829113bdb10afe95619bc0824bc5959ad82fd6952bd9. +// +// Solidity: event AddLiquidity(address user, address tokenA, address tokenB, uint256 amtA, uint256 amtB) +func (_HbSwap *HbSwapFilterer) WatchAddLiquidity(opts *bind.WatchOpts, sink chan<- *HbSwapAddLiquidity) (event.Subscription, error) { + + logs, sub, err := _HbSwap.contract.WatchLogs(opts, "AddLiquidity") + if err != nil { + return nil, err + } + return event.NewSubscription(func(quit <-chan struct{}) error { + defer sub.Unsubscribe() + for { + select { + case log := <-logs: + // New log arrived, parse the event and forward to the user + event := new(HbSwapAddLiquidity) + if err := _HbSwap.contract.UnpackLog(event, "AddLiquidity", log); err != nil { + return err + } + event.Raw = log + + select { + case sink <- event: + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + } + }), nil +} + +// ParseAddLiquidity is a log parse operation binding the contract event 0xc33fbc9654f9c0dcfcbd829113bdb10afe95619bc0824bc5959ad82fd6952bd9. +// +// Solidity: event AddLiquidity(address user, address tokenA, address tokenB, uint256 amtA, uint256 amtB) +func (_HbSwap *HbSwapFilterer) ParseAddLiquidity(log types.Log) (*HbSwapAddLiquidity, error) { + event := new(HbSwapAddLiquidity) + if err := _HbSwap.contract.UnpackLog(event, "AddLiquidity", log); err != nil { + return nil, err + } + return event, nil +} + +// HbSwapInitPoolIterator is returned from FilterInitPool and is used to iterate over the raw logs and unpacked data for InitPool events raised by the HbSwap contract. +type HbSwapInitPoolIterator struct { + Event *HbSwapInitPool // Event containing the contract specifics and raw log + + contract *bind.BoundContract // Generic contract to use for unpacking event data + event string // Event name to use for unpacking event data + + logs chan types.Log // Log channel receiving the found contract events + sub ethereum.Subscription // Subscription for errors, completion and termination + done bool // Whether the subscription completed delivering logs + fail error // Occurred error to stop iteration +} + +// Next advances the iterator to the subsequent event, returning whether there +// are any more events found. In case of a retrieval or parsing error, false is +// returned and Error() can be queried for the exact failure. +func (it *HbSwapInitPoolIterator) Next() bool { + // If the iterator failed, stop iterating + if it.fail != nil { + return false + } + // If the iterator completed, deliver directly whatever's available + if it.done { + select { + case log := <-it.logs: + it.Event = new(HbSwapInitPool) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + default: + return false + } + } + // Iterator still in progress, wait for either a data or an error event + select { + case log := <-it.logs: + it.Event = new(HbSwapInitPool) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + case err := <-it.sub.Err(): + it.done = true + it.fail = err + return it.Next() + } +} + +// Error returns any retrieval or parsing error occurred during filtering. +func (it *HbSwapInitPoolIterator) Error() error { + return it.fail +} + +// Close terminates the iteration process, releasing any pending underlying +// resources. +func (it *HbSwapInitPoolIterator) Close() error { + it.sub.Unsubscribe() + return nil +} + +// HbSwapInitPool represents a InitPool event raised by the HbSwap contract. +type HbSwapInitPool struct { + User common.Address + TokenA common.Address + TokenB common.Address + AmtA *big.Int + AmtB *big.Int + Raw types.Log // Blockchain specific contextual infos +} + +// FilterInitPool is a free log retrieval operation binding the contract event 0xfaaebcb30b1b421f4f2ca7f2620e5add6a64532c087ee0646fd665a33d36fdf5. +// +// Solidity: event InitPool(address user, address tokenA, address tokenB, uint256 amtA, uint256 amtB) +func (_HbSwap *HbSwapFilterer) FilterInitPool(opts *bind.FilterOpts) (*HbSwapInitPoolIterator, error) { + + logs, sub, err := _HbSwap.contract.FilterLogs(opts, "InitPool") + if err != nil { + return nil, err + } + return &HbSwapInitPoolIterator{contract: _HbSwap.contract, event: "InitPool", logs: logs, sub: sub}, nil +} + +// WatchInitPool is a free log subscription operation binding the contract event 0xfaaebcb30b1b421f4f2ca7f2620e5add6a64532c087ee0646fd665a33d36fdf5. +// +// Solidity: event InitPool(address user, address tokenA, address tokenB, uint256 amtA, uint256 amtB) +func (_HbSwap *HbSwapFilterer) WatchInitPool(opts *bind.WatchOpts, sink chan<- *HbSwapInitPool) (event.Subscription, error) { + + logs, sub, err := _HbSwap.contract.WatchLogs(opts, "InitPool") + if err != nil { + return nil, err + } + return event.NewSubscription(func(quit <-chan struct{}) error { + defer sub.Unsubscribe() + for { + select { + case log := <-logs: + // New log arrived, parse the event and forward to the user + event := new(HbSwapInitPool) + if err := _HbSwap.contract.UnpackLog(event, "InitPool", log); err != nil { + return err + } + event.Raw = log + + select { + case sink <- event: + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + } + }), nil +} + +// ParseInitPool is a log parse operation binding the contract event 0xfaaebcb30b1b421f4f2ca7f2620e5add6a64532c087ee0646fd665a33d36fdf5. +// +// Solidity: event InitPool(address user, address tokenA, address tokenB, uint256 amtA, uint256 amtB) +func (_HbSwap *HbSwapFilterer) ParseInitPool(log types.Log) (*HbSwapInitPool, error) { + event := new(HbSwapInitPool) + if err := _HbSwap.contract.UnpackLog(event, "InitPool", log); err != nil { + return nil, err + } + return event, nil +} + +// HbSwapRemoveLiquidityIterator is returned from FilterRemoveLiquidity and is used to iterate over the raw logs and unpacked data for RemoveLiquidity events raised by the HbSwap contract. +type HbSwapRemoveLiquidityIterator struct { + Event *HbSwapRemoveLiquidity // Event containing the contract specifics and raw log + + contract *bind.BoundContract // Generic contract to use for unpacking event data + event string // Event name to use for unpacking event data + + logs chan types.Log // Log channel receiving the found contract events + sub ethereum.Subscription // Subscription for errors, completion and termination + done bool // Whether the subscription completed delivering logs + fail error // Occurred error to stop iteration +} + +// Next advances the iterator to the subsequent event, returning whether there +// are any more events found. In case of a retrieval or parsing error, false is +// returned and Error() can be queried for the exact failure. +func (it *HbSwapRemoveLiquidityIterator) Next() bool { + // If the iterator failed, stop iterating + if it.fail != nil { + return false + } + // If the iterator completed, deliver directly whatever's available + if it.done { + select { + case log := <-it.logs: + it.Event = new(HbSwapRemoveLiquidity) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + default: + return false + } + } + // Iterator still in progress, wait for either a data or an error event + select { + case log := <-it.logs: + it.Event = new(HbSwapRemoveLiquidity) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + case err := <-it.sub.Err(): + it.done = true + it.fail = err + return it.Next() + } +} + +// Error returns any retrieval or parsing error occurred during filtering. +func (it *HbSwapRemoveLiquidityIterator) Error() error { + return it.fail +} + +// Close terminates the iteration process, releasing any pending underlying +// resources. +func (it *HbSwapRemoveLiquidityIterator) Close() error { + it.sub.Unsubscribe() + return nil +} + +// HbSwapRemoveLiquidity represents a RemoveLiquidity event raised by the HbSwap contract. +type HbSwapRemoveLiquidity struct { + User common.Address + TokenA common.Address + TokenB common.Address + Amt *big.Int + Raw types.Log // Blockchain specific contextual infos +} + +// FilterRemoveLiquidity is a free log retrieval operation binding the contract event 0x2a1a4e62bda5b4987b9aa2f23ddbb29e434808f7a717452a3226bc15243c9277. +// +// Solidity: event RemoveLiquidity(address user, address tokenA, address tokenB, uint256 amt) +func (_HbSwap *HbSwapFilterer) FilterRemoveLiquidity(opts *bind.FilterOpts) (*HbSwapRemoveLiquidityIterator, error) { + + logs, sub, err := _HbSwap.contract.FilterLogs(opts, "RemoveLiquidity") + if err != nil { + return nil, err + } + return &HbSwapRemoveLiquidityIterator{contract: _HbSwap.contract, event: "RemoveLiquidity", logs: logs, sub: sub}, nil +} + +// WatchRemoveLiquidity is a free log subscription operation binding the contract event 0x2a1a4e62bda5b4987b9aa2f23ddbb29e434808f7a717452a3226bc15243c9277. +// +// Solidity: event RemoveLiquidity(address user, address tokenA, address tokenB, uint256 amt) +func (_HbSwap *HbSwapFilterer) WatchRemoveLiquidity(opts *bind.WatchOpts, sink chan<- *HbSwapRemoveLiquidity) (event.Subscription, error) { + + logs, sub, err := _HbSwap.contract.WatchLogs(opts, "RemoveLiquidity") + if err != nil { + return nil, err + } + return event.NewSubscription(func(quit <-chan struct{}) error { + defer sub.Unsubscribe() + for { + select { + case log := <-logs: + // New log arrived, parse the event and forward to the user + event := new(HbSwapRemoveLiquidity) + if err := _HbSwap.contract.UnpackLog(event, "RemoveLiquidity", log); err != nil { + return err + } + event.Raw = log + + select { + case sink <- event: + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + } + }), nil +} + +// ParseRemoveLiquidity is a log parse operation binding the contract event 0x2a1a4e62bda5b4987b9aa2f23ddbb29e434808f7a717452a3226bc15243c9277. +// +// Solidity: event RemoveLiquidity(address user, address tokenA, address tokenB, uint256 amt) +func (_HbSwap *HbSwapFilterer) ParseRemoveLiquidity(log types.Log) (*HbSwapRemoveLiquidity, error) { + event := new(HbSwapRemoveLiquidity) + if err := _HbSwap.contract.UnpackLog(event, "RemoveLiquidity", log); err != nil { + return nil, err + } + return event, nil +} + +// HbSwapSecretDepositIterator is returned from FilterSecretDeposit and is used to iterate over the raw logs and unpacked data for SecretDeposit events raised by the HbSwap contract. +type HbSwapSecretDepositIterator struct { + Event *HbSwapSecretDeposit // Event containing the contract specifics and raw log + + contract *bind.BoundContract // Generic contract to use for unpacking event data + event string // Event name to use for unpacking event data + + logs chan types.Log // Log channel receiving the found contract events + sub ethereum.Subscription // Subscription for errors, completion and termination + done bool // Whether the subscription completed delivering logs + fail error // Occurred error to stop iteration +} + +// Next advances the iterator to the subsequent event, returning whether there +// are any more events found. In case of a retrieval or parsing error, false is +// returned and Error() can be queried for the exact failure. +func (it *HbSwapSecretDepositIterator) Next() bool { + // If the iterator failed, stop iterating + if it.fail != nil { + return false + } + // If the iterator completed, deliver directly whatever's available + if it.done { + select { + case log := <-it.logs: + it.Event = new(HbSwapSecretDeposit) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + default: + return false + } + } + // Iterator still in progress, wait for either a data or an error event + select { + case log := <-it.logs: + it.Event = new(HbSwapSecretDeposit) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + case err := <-it.sub.Err(): + it.done = true + it.fail = err + return it.Next() + } +} + +// Error returns any retrieval or parsing error occurred during filtering. +func (it *HbSwapSecretDepositIterator) Error() error { + return it.fail +} + +// Close terminates the iteration process, releasing any pending underlying +// resources. +func (it *HbSwapSecretDepositIterator) Close() error { + it.sub.Unsubscribe() + return nil +} + +// HbSwapSecretDeposit represents a SecretDeposit event raised by the HbSwap contract. +type HbSwapSecretDeposit struct { + Token common.Address + User common.Address + Amt *big.Int + Raw types.Log // Blockchain specific contextual infos +} + +// FilterSecretDeposit is a free log retrieval operation binding the contract event 0x07c06144435b7d2bdccf9ee7e5a7022c63382ac7c3a0e14ed08b5969dedf0ecf. +// +// Solidity: event SecretDeposit(address token, address user, uint256 amt) +func (_HbSwap *HbSwapFilterer) FilterSecretDeposit(opts *bind.FilterOpts) (*HbSwapSecretDepositIterator, error) { + + logs, sub, err := _HbSwap.contract.FilterLogs(opts, "SecretDeposit") + if err != nil { + return nil, err + } + return &HbSwapSecretDepositIterator{contract: _HbSwap.contract, event: "SecretDeposit", logs: logs, sub: sub}, nil +} + +// WatchSecretDeposit is a free log subscription operation binding the contract event 0x07c06144435b7d2bdccf9ee7e5a7022c63382ac7c3a0e14ed08b5969dedf0ecf. +// +// Solidity: event SecretDeposit(address token, address user, uint256 amt) +func (_HbSwap *HbSwapFilterer) WatchSecretDeposit(opts *bind.WatchOpts, sink chan<- *HbSwapSecretDeposit) (event.Subscription, error) { + + logs, sub, err := _HbSwap.contract.WatchLogs(opts, "SecretDeposit") + if err != nil { + return nil, err + } + return event.NewSubscription(func(quit <-chan struct{}) error { + defer sub.Unsubscribe() + for { + select { + case log := <-logs: + // New log arrived, parse the event and forward to the user + event := new(HbSwapSecretDeposit) + if err := _HbSwap.contract.UnpackLog(event, "SecretDeposit", log); err != nil { + return err + } + event.Raw = log + + select { + case sink <- event: + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + } + }), nil +} + +// ParseSecretDeposit is a log parse operation binding the contract event 0x07c06144435b7d2bdccf9ee7e5a7022c63382ac7c3a0e14ed08b5969dedf0ecf. +// +// Solidity: event SecretDeposit(address token, address user, uint256 amt) +func (_HbSwap *HbSwapFilterer) ParseSecretDeposit(log types.Log) (*HbSwapSecretDeposit, error) { + event := new(HbSwapSecretDeposit) + if err := _HbSwap.contract.UnpackLog(event, "SecretDeposit", log); err != nil { + return nil, err + } + return event, nil +} + +// HbSwapSecretWithdrawIterator is returned from FilterSecretWithdraw and is used to iterate over the raw logs and unpacked data for SecretWithdraw events raised by the HbSwap contract. +type HbSwapSecretWithdrawIterator struct { + Event *HbSwapSecretWithdraw // Event containing the contract specifics and raw log + + contract *bind.BoundContract // Generic contract to use for unpacking event data + event string // Event name to use for unpacking event data + + logs chan types.Log // Log channel receiving the found contract events + sub ethereum.Subscription // Subscription for errors, completion and termination + done bool // Whether the subscription completed delivering logs + fail error // Occurred error to stop iteration +} + +// Next advances the iterator to the subsequent event, returning whether there +// are any more events found. In case of a retrieval or parsing error, false is +// returned and Error() can be queried for the exact failure. +func (it *HbSwapSecretWithdrawIterator) Next() bool { + // If the iterator failed, stop iterating + if it.fail != nil { + return false + } + // If the iterator completed, deliver directly whatever's available + if it.done { + select { + case log := <-it.logs: + it.Event = new(HbSwapSecretWithdraw) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + default: + return false + } + } + // Iterator still in progress, wait for either a data or an error event + select { + case log := <-it.logs: + it.Event = new(HbSwapSecretWithdraw) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + case err := <-it.sub.Err(): + it.done = true + it.fail = err + return it.Next() + } +} + +// Error returns any retrieval or parsing error occurred during filtering. +func (it *HbSwapSecretWithdrawIterator) Error() error { + return it.fail +} + +// Close terminates the iteration process, releasing any pending underlying +// resources. +func (it *HbSwapSecretWithdrawIterator) Close() error { + it.sub.Unsubscribe() + return nil +} + +// HbSwapSecretWithdraw represents a SecretWithdraw event raised by the HbSwap contract. +type HbSwapSecretWithdraw struct { + Seq *big.Int + Token common.Address + User common.Address + Amt *big.Int + Raw types.Log // Blockchain specific contextual infos +} + +// FilterSecretWithdraw is a free log retrieval operation binding the contract event 0x4ef3cc4825a92c3b6922acc8a45152cc96ef48463e8ed500dacd5df9e29a67f3. +// +// Solidity: event SecretWithdraw(uint256 seq, address token, address user, uint256 amt) +func (_HbSwap *HbSwapFilterer) FilterSecretWithdraw(opts *bind.FilterOpts) (*HbSwapSecretWithdrawIterator, error) { + + logs, sub, err := _HbSwap.contract.FilterLogs(opts, "SecretWithdraw") + if err != nil { + return nil, err + } + return &HbSwapSecretWithdrawIterator{contract: _HbSwap.contract, event: "SecretWithdraw", logs: logs, sub: sub}, nil +} + +// WatchSecretWithdraw is a free log subscription operation binding the contract event 0x4ef3cc4825a92c3b6922acc8a45152cc96ef48463e8ed500dacd5df9e29a67f3. +// +// Solidity: event SecretWithdraw(uint256 seq, address token, address user, uint256 amt) +func (_HbSwap *HbSwapFilterer) WatchSecretWithdraw(opts *bind.WatchOpts, sink chan<- *HbSwapSecretWithdraw) (event.Subscription, error) { + + logs, sub, err := _HbSwap.contract.WatchLogs(opts, "SecretWithdraw") + if err != nil { + return nil, err + } + return event.NewSubscription(func(quit <-chan struct{}) error { + defer sub.Unsubscribe() + for { + select { + case log := <-logs: + // New log arrived, parse the event and forward to the user + event := new(HbSwapSecretWithdraw) + if err := _HbSwap.contract.UnpackLog(event, "SecretWithdraw", log); err != nil { + return err + } + event.Raw = log + + select { + case sink <- event: + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + } + }), nil +} + +// ParseSecretWithdraw is a log parse operation binding the contract event 0x4ef3cc4825a92c3b6922acc8a45152cc96ef48463e8ed500dacd5df9e29a67f3. +// +// Solidity: event SecretWithdraw(uint256 seq, address token, address user, uint256 amt) +func (_HbSwap *HbSwapFilterer) ParseSecretWithdraw(log types.Log) (*HbSwapSecretWithdraw, error) { + event := new(HbSwapSecretWithdraw) + if err := _HbSwap.contract.UnpackLog(event, "SecretWithdraw", log); err != nil { + return nil, err + } + return event, nil +} + +// HbSwapTradeIterator is returned from FilterTrade and is used to iterate over the raw logs and unpacked data for Trade events raised by the HbSwap contract. +type HbSwapTradeIterator struct { + Event *HbSwapTrade // Event containing the contract specifics and raw log + + contract *bind.BoundContract // Generic contract to use for unpacking event data + event string // Event name to use for unpacking event data + + logs chan types.Log // Log channel receiving the found contract events + sub ethereum.Subscription // Subscription for errors, completion and termination + done bool // Whether the subscription completed delivering logs + fail error // Occurred error to stop iteration +} + +// Next advances the iterator to the subsequent event, returning whether there +// are any more events found. In case of a retrieval or parsing error, false is +// returned and Error() can be queried for the exact failure. +func (it *HbSwapTradeIterator) Next() bool { + // If the iterator failed, stop iterating + if it.fail != nil { + return false + } + // If the iterator completed, deliver directly whatever's available + if it.done { + select { + case log := <-it.logs: + it.Event = new(HbSwapTrade) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + default: + return false + } + } + // Iterator still in progress, wait for either a data or an error event + select { + case log := <-it.logs: + it.Event = new(HbSwapTrade) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + case err := <-it.sub.Err(): + it.done = true + it.fail = err + return it.Next() + } +} + +// Error returns any retrieval or parsing error occurred during filtering. +func (it *HbSwapTradeIterator) Error() error { + return it.fail +} + +// Close terminates the iteration process, releasing any pending underlying +// resources. +func (it *HbSwapTradeIterator) Close() error { + it.sub.Unsubscribe() + return nil +} + +// HbSwapTrade represents a Trade event raised by the HbSwap contract. +type HbSwapTrade struct { + TradeSeq *big.Int + User common.Address + TokenA common.Address + TokenB common.Address + IdxA *big.Int + IdxB *big.Int + MaskedA *big.Int + MaskedB *big.Int + Raw types.Log // Blockchain specific contextual infos +} + +// FilterTrade is a free log retrieval operation binding the contract event 0x2b4d91cd20cc8800407e3614b8466a6f0729ac3b1fa43d4e2b059ff5593cbae6. +// +// Solidity: event Trade(uint256 tradeSeq, address user, address tokenA, address tokenB, uint256 idxA, uint256 idxB, uint256 maskedA, uint256 maskedB) +func (_HbSwap *HbSwapFilterer) FilterTrade(opts *bind.FilterOpts) (*HbSwapTradeIterator, error) { + + logs, sub, err := _HbSwap.contract.FilterLogs(opts, "Trade") + if err != nil { + return nil, err + } + return &HbSwapTradeIterator{contract: _HbSwap.contract, event: "Trade", logs: logs, sub: sub}, nil +} + +// WatchTrade is a free log subscription operation binding the contract event 0x2b4d91cd20cc8800407e3614b8466a6f0729ac3b1fa43d4e2b059ff5593cbae6. +// +// Solidity: event Trade(uint256 tradeSeq, address user, address tokenA, address tokenB, uint256 idxA, uint256 idxB, uint256 maskedA, uint256 maskedB) +func (_HbSwap *HbSwapFilterer) WatchTrade(opts *bind.WatchOpts, sink chan<- *HbSwapTrade) (event.Subscription, error) { + + logs, sub, err := _HbSwap.contract.WatchLogs(opts, "Trade") + if err != nil { + return nil, err + } + return event.NewSubscription(func(quit <-chan struct{}) error { + defer sub.Unsubscribe() + for { + select { + case log := <-logs: + // New log arrived, parse the event and forward to the user + event := new(HbSwapTrade) + if err := _HbSwap.contract.UnpackLog(event, "Trade", log); err != nil { + return err + } + event.Raw = log + + select { + case sink <- event: + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + } + }), nil +} + +// ParseTrade is a log parse operation binding the contract event 0x2b4d91cd20cc8800407e3614b8466a6f0729ac3b1fa43d4e2b059ff5593cbae6. +// +// Solidity: event Trade(uint256 tradeSeq, address user, address tokenA, address tokenB, uint256 idxA, uint256 idxB, uint256 maskedA, uint256 maskedB) +func (_HbSwap *HbSwapFilterer) ParseTrade(log types.Log) (*HbSwapTrade, error) { + event := new(HbSwapTrade) + if err := _HbSwap.contract.UnpackLog(event, "Trade", log); err != nil { + return nil, err + } + return event, nil +} + +// HbSwapTradePrepIterator is returned from FilterTradePrep and is used to iterate over the raw logs and unpacked data for TradePrep events raised by the HbSwap contract. +type HbSwapTradePrepIterator struct { + Event *HbSwapTradePrep // Event containing the contract specifics and raw log + + contract *bind.BoundContract // Generic contract to use for unpacking event data + event string // Event name to use for unpacking event data + + logs chan types.Log // Log channel receiving the found contract events + sub ethereum.Subscription // Subscription for errors, completion and termination + done bool // Whether the subscription completed delivering logs + fail error // Occurred error to stop iteration +} + +// Next advances the iterator to the subsequent event, returning whether there +// are any more events found. In case of a retrieval or parsing error, false is +// returned and Error() can be queried for the exact failure. +func (it *HbSwapTradePrepIterator) Next() bool { + // If the iterator failed, stop iterating + if it.fail != nil { + return false + } + // If the iterator completed, deliver directly whatever's available + if it.done { + select { + case log := <-it.logs: + it.Event = new(HbSwapTradePrep) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + default: + return false + } + } + // Iterator still in progress, wait for either a data or an error event + select { + case log := <-it.logs: + it.Event = new(HbSwapTradePrep) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + case err := <-it.sub.Err(): + it.done = true + it.fail = err + return it.Next() + } +} + +// Error returns any retrieval or parsing error occurred during filtering. +func (it *HbSwapTradePrepIterator) Error() error { + return it.fail +} + +// Close terminates the iteration process, releasing any pending underlying +// resources. +func (it *HbSwapTradePrepIterator) Close() error { + it.sub.Unsubscribe() + return nil +} + +// HbSwapTradePrep represents a TradePrep event raised by the HbSwap contract. +type HbSwapTradePrep struct { + User common.Address + IdxA *big.Int + IdxB *big.Int + Raw types.Log // Blockchain specific contextual infos +} + +// FilterTradePrep is a free log retrieval operation binding the contract event 0xb2fd402d6b838b10cf190139b9d4495eefcfea7543bc1056544d13732d82e6ac. +// +// Solidity: event TradePrep(address user, uint256 idxA, uint256 idxB) +func (_HbSwap *HbSwapFilterer) FilterTradePrep(opts *bind.FilterOpts) (*HbSwapTradePrepIterator, error) { + + logs, sub, err := _HbSwap.contract.FilterLogs(opts, "TradePrep") + if err != nil { + return nil, err + } + return &HbSwapTradePrepIterator{contract: _HbSwap.contract, event: "TradePrep", logs: logs, sub: sub}, nil +} + +// WatchTradePrep is a free log subscription operation binding the contract event 0xb2fd402d6b838b10cf190139b9d4495eefcfea7543bc1056544d13732d82e6ac. +// +// Solidity: event TradePrep(address user, uint256 idxA, uint256 idxB) +func (_HbSwap *HbSwapFilterer) WatchTradePrep(opts *bind.WatchOpts, sink chan<- *HbSwapTradePrep) (event.Subscription, error) { + + logs, sub, err := _HbSwap.contract.WatchLogs(opts, "TradePrep") + if err != nil { + return nil, err + } + return event.NewSubscription(func(quit <-chan struct{}) error { + defer sub.Unsubscribe() + for { + select { + case log := <-logs: + // New log arrived, parse the event and forward to the user + event := new(HbSwapTradePrep) + if err := _HbSwap.contract.UnpackLog(event, "TradePrep", log); err != nil { + return err + } + event.Raw = log + + select { + case sink <- event: + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + } + }), nil +} + +// ParseTradePrep is a log parse operation binding the contract event 0xb2fd402d6b838b10cf190139b9d4495eefcfea7543bc1056544d13732d82e6ac. +// +// Solidity: event TradePrep(address user, uint256 idxA, uint256 idxB) +func (_HbSwap *HbSwapFilterer) ParseTradePrep(log types.Log) (*HbSwapTradePrep, error) { + event := new(HbSwapTradePrep) + if err := _HbSwap.contract.UnpackLog(event, "TradePrep", log); err != nil { + return nil, err + } + return event, nil +} diff --git a/src/go_bindings/token/token.go b/src/go_bindings/token/token.go new file mode 100644 index 00000000..0651b922 --- /dev/null +++ b/src/go_bindings/token/token.go @@ -0,0 +1,774 @@ +// Code generated - DO NOT EDIT. +// This file is a generated binding and any manual changes will be lost. + +package token + +import ( + "math/big" + "strings" + + ethereum "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/event" +) + +// Reference imports to suppress errors if they are not otherwise used. +var ( + _ = big.NewInt + _ = strings.NewReader + _ = ethereum.NotFound + _ = abi.U256 + _ = bind.Bind + _ = common.Big1 + _ = types.BloomLookup + _ = event.NewSubscription +) + +// TokenABI is the input ABI used to generate the binding from. +const TokenABI = "[{\"inputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Approval\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"},{\"inputs\":[],\"name\":\"DECIMALS\",\"outputs\":[{\"internalType\":\"uint8\",\"name\":\"\",\"type\":\"uint8\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"INITIAL_SUPPLY\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"}],\"name\":\"allowance\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"constant\":true,\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"internalType\":\"uint8\",\"name\":\"\",\"type\":\"uint8\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"subtractedValue\",\"type\":\"uint256\"}],\"name\":\"decreaseAllowance\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"addedValue\",\"type\":\"uint256\"}],\"name\":\"increaseAllowance\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"recipient\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"sender\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"recipient\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]" + +// TokenBin is the compiled bytecode used for deploying new contracts. +var TokenBin = "0x60806040523480156200001157600080fd5b506040518060400160405280600581526020017f546f6b656e0000000000000000000000000000000000000000000000000000008152506040518060400160405280600581526020017f746f6b656e0000000000000000000000000000000000000000000000000000008152508160039080519060200190620000969291906200035e565b508060049080519060200190620000af9291906200035e565b506012600560006101000a81548160ff021916908360ff1602179055505050620000ec336b204fce5e3e25026110000000620000f260201b60201c565b6200040d565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16141562000196576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601f8152602001807f45524332303a206d696e7420746f20746865207a65726f20616464726573730081525060200191505060405180910390fd5b620001aa60008383620002d060201b60201c565b620001c681600254620002d560201b62000a2a1790919060201c565b60028190555062000224816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054620002d560201b62000a2a1790919060201c565b6000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a35050565b505050565b60008082840190508381101562000354576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601b8152602001807f536166654d6174683a206164646974696f6e206f766572666c6f77000000000081525060200191505060405180910390fd5b8091505092915050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10620003a157805160ff1916838001178555620003d2565b82800160010185558215620003d2579182015b82811115620003d1578251825591602001919060010190620003b4565b5b509050620003e19190620003e5565b5090565b6200040a91905b8082111562000406576000816000905550600101620003ec565b5090565b90565b61116e806200041d6000396000f3fe608060405234801561001057600080fd5b50600436106100cf5760003560e01c8063313ce5671161008c57806395d89b411161006657806395d89b4114610385578063a457c2d714610408578063a9059cbb1461046e578063dd62ed3e146104d4576100cf565b8063313ce567146102a357806339509351146102c757806370a082311461032d576100cf565b806306fdde03146100d4578063095ea7b31461015757806318160ddd146101bd57806323b872dd146101db5780632e0f2625146102615780632ff2e9dc14610285575b600080fd5b6100dc61054c565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561011c578082015181840152602081019050610101565b50505050905090810190601f1680156101495780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6101a36004803603604081101561016d57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506105ee565b604051808215151515815260200191505060405180910390f35b6101c561060c565b6040518082815260200191505060405180910390f35b610247600480360360608110156101f157600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610616565b604051808215151515815260200191505060405180910390f35b6102696106ef565b604051808260ff1660ff16815260200191505060405180910390f35b61028d6106f4565b6040518082815260200191505060405180910390f35b6102ab610704565b604051808260ff1660ff16815260200191505060405180910390f35b610313600480360360408110156102dd57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919050505061071b565b604051808215151515815260200191505060405180910390f35b61036f6004803603602081101561034357600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506107ce565b6040518082815260200191505060405180910390f35b61038d610816565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156103cd5780820151818401526020810190506103b2565b50505050905090810190601f1680156103fa5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6104546004803603604081101561041e57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506108b8565b604051808215151515815260200191505060405180910390f35b6104ba6004803603604081101561048457600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610985565b604051808215151515815260200191505060405180910390f35b610536600480360360408110156104ea57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506109a3565b6040518082815260200191505060405180910390f35b606060038054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156105e45780601f106105b9576101008083540402835291602001916105e4565b820191906000526020600020905b8154815290600101906020018083116105c757829003601f168201915b5050505050905090565b60006106026105fb610ab2565b8484610aba565b6001905092915050565b6000600254905090565b6000610623848484610cb1565b6106e48461062f610ab2565b6106df856040518060600160405280602881526020016110a360289139600160008b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000610695610ab2565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610f729092919063ffffffff16565b610aba565b600190509392505050565b601281565b6b204fce5e3e2502611000000081565b6000600560009054906101000a900460ff16905090565b60006107c4610728610ab2565b846107bf8560016000610739610ab2565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008973ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610a2a90919063ffffffff16565b610aba565b6001905092915050565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b606060048054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156108ae5780601f10610883576101008083540402835291602001916108ae565b820191906000526020600020905b81548152906001019060200180831161089157829003601f168201915b5050505050905090565b600061097b6108c5610ab2565b846109768560405180606001604052806025815260200161111460259139600160006108ef610ab2565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008a73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610f729092919063ffffffff16565b610aba565b6001905092915050565b6000610999610992610ab2565b8484610cb1565b6001905092915050565b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905092915050565b600080828401905083811015610aa8576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601b8152602001807f536166654d6174683a206164646974696f6e206f766572666c6f77000000000081525060200191505060405180910390fd5b8091505092915050565b600033905090565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff161415610b40576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260248152602001806110f06024913960400191505060405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415610bc6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602281526020018061105b6022913960400191505060405180910390fd5b80600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925836040518082815260200191505060405180910390a3505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff161415610d37576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260258152602001806110cb6025913960400191505060405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415610dbd576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260238152602001806110386023913960400191505060405180910390fd5b610dc8838383611032565b610e338160405180606001604052806026815260200161107d602691396000808773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610f729092919063ffffffff16565b6000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550610ec6816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610a2a90919063ffffffff16565b6000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a3505050565b600083831115829061101f576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b83811015610fe4578082015181840152602081019050610fc9565b50505050905090810190601f1680156110115780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b5060008385039050809150509392505050565b50505056fe45524332303a207472616e7366657220746f20746865207a65726f206164647265737345524332303a20617070726f766520746f20746865207a65726f206164647265737345524332303a207472616e7366657220616d6f756e7420657863656564732062616c616e636545524332303a207472616e7366657220616d6f756e74206578636565647320616c6c6f77616e636545524332303a207472616e736665722066726f6d20746865207a65726f206164647265737345524332303a20617070726f76652066726f6d20746865207a65726f206164647265737345524332303a2064656372656173656420616c6c6f77616e63652062656c6f77207a65726fa264697066735822122018d0d720c83eb9ed4e669c6771bd4e101684751f4c48cc984730e5a85726aaf564736f6c634300060a0033" + +// DeployToken deploys a new Ethereum contract, binding an instance of Token to it. +func DeployToken(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *Token, error) { + parsed, err := abi.JSON(strings.NewReader(TokenABI)) + if err != nil { + return common.Address{}, nil, nil, err + } + + address, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex(TokenBin), backend) + if err != nil { + return common.Address{}, nil, nil, err + } + return address, tx, &Token{TokenCaller: TokenCaller{contract: contract}, TokenTransactor: TokenTransactor{contract: contract}, TokenFilterer: TokenFilterer{contract: contract}}, nil +} + +// Token is an auto generated Go binding around an Ethereum contract. +type Token struct { + TokenCaller // Read-only binding to the contract + TokenTransactor // Write-only binding to the contract + TokenFilterer // Log filterer for contract events +} + +// TokenCaller is an auto generated read-only Go binding around an Ethereum contract. +type TokenCaller struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// TokenTransactor is an auto generated write-only Go binding around an Ethereum contract. +type TokenTransactor struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// TokenFilterer is an auto generated log filtering Go binding around an Ethereum contract events. +type TokenFilterer struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// TokenSession is an auto generated Go binding around an Ethereum contract, +// with pre-set call and transact options. +type TokenSession struct { + Contract *Token // Generic contract binding to set the session for + CallOpts bind.CallOpts // Call options to use throughout this session + TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session +} + +// TokenCallerSession is an auto generated read-only Go binding around an Ethereum contract, +// with pre-set call options. +type TokenCallerSession struct { + Contract *TokenCaller // Generic contract caller binding to set the session for + CallOpts bind.CallOpts // Call options to use throughout this session +} + +// TokenTransactorSession is an auto generated write-only Go binding around an Ethereum contract, +// with pre-set transact options. +type TokenTransactorSession struct { + Contract *TokenTransactor // Generic contract transactor binding to set the session for + TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session +} + +// TokenRaw is an auto generated low-level Go binding around an Ethereum contract. +type TokenRaw struct { + Contract *Token // Generic contract binding to access the raw methods on +} + +// TokenCallerRaw is an auto generated low-level read-only Go binding around an Ethereum contract. +type TokenCallerRaw struct { + Contract *TokenCaller // Generic read-only contract binding to access the raw methods on +} + +// TokenTransactorRaw is an auto generated low-level write-only Go binding around an Ethereum contract. +type TokenTransactorRaw struct { + Contract *TokenTransactor // Generic write-only contract binding to access the raw methods on +} + +// NewToken creates a new instance of Token, bound to a specific deployed contract. +func NewToken(address common.Address, backend bind.ContractBackend) (*Token, error) { + contract, err := bindToken(address, backend, backend, backend) + if err != nil { + return nil, err + } + return &Token{TokenCaller: TokenCaller{contract: contract}, TokenTransactor: TokenTransactor{contract: contract}, TokenFilterer: TokenFilterer{contract: contract}}, nil +} + +// NewTokenCaller creates a new read-only instance of Token, bound to a specific deployed contract. +func NewTokenCaller(address common.Address, caller bind.ContractCaller) (*TokenCaller, error) { + contract, err := bindToken(address, caller, nil, nil) + if err != nil { + return nil, err + } + return &TokenCaller{contract: contract}, nil +} + +// NewTokenTransactor creates a new write-only instance of Token, bound to a specific deployed contract. +func NewTokenTransactor(address common.Address, transactor bind.ContractTransactor) (*TokenTransactor, error) { + contract, err := bindToken(address, nil, transactor, nil) + if err != nil { + return nil, err + } + return &TokenTransactor{contract: contract}, nil +} + +// NewTokenFilterer creates a new log filterer instance of Token, bound to a specific deployed contract. +func NewTokenFilterer(address common.Address, filterer bind.ContractFilterer) (*TokenFilterer, error) { + contract, err := bindToken(address, nil, nil, filterer) + if err != nil { + return nil, err + } + return &TokenFilterer{contract: contract}, nil +} + +// bindToken binds a generic wrapper to an already deployed contract. +func bindToken(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor, filterer bind.ContractFilterer) (*bind.BoundContract, error) { + parsed, err := abi.JSON(strings.NewReader(TokenABI)) + if err != nil { + return nil, err + } + return bind.NewBoundContract(address, parsed, caller, transactor, filterer), nil +} + +// Call invokes the (constant) contract method with params as input values and +// sets the output to result. The result type might be a single field for simple +// returns, a slice of interfaces for anonymous returns and a struct for named +// returns. +func (_Token *TokenRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error { + return _Token.Contract.TokenCaller.contract.Call(opts, result, method, params...) +} + +// Transfer initiates a plain transaction to move funds to the contract, calling +// its default method if one is available. +func (_Token *TokenRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) { + return _Token.Contract.TokenTransactor.contract.Transfer(opts) +} + +// Transact invokes the (paid) contract method with params as input values. +func (_Token *TokenRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { + return _Token.Contract.TokenTransactor.contract.Transact(opts, method, params...) +} + +// Call invokes the (constant) contract method with params as input values and +// sets the output to result. The result type might be a single field for simple +// returns, a slice of interfaces for anonymous returns and a struct for named +// returns. +func (_Token *TokenCallerRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error { + return _Token.Contract.contract.Call(opts, result, method, params...) +} + +// Transfer initiates a plain transaction to move funds to the contract, calling +// its default method if one is available. +func (_Token *TokenTransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) { + return _Token.Contract.contract.Transfer(opts) +} + +// Transact invokes the (paid) contract method with params as input values. +func (_Token *TokenTransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { + return _Token.Contract.contract.Transact(opts, method, params...) +} + +// BalanceOf is a free data retrieval call binding the contract method 0x70a08231. +// +// Solidity: function balanceOf(address account) constant returns(uint256) +func (_Token *TokenCaller) BalanceOf(opts *bind.CallOpts, account common.Address) (*big.Int, error) { + var ( + ret0 = new(*big.Int) + ) + out := ret0 + err := _Token.contract.Call(opts, out, "balanceOf", account) + return *ret0, err +} + +// BalanceOf is a free data retrieval call binding the contract method 0x70a08231. +// +// Solidity: function balanceOf(address account) constant returns(uint256) +func (_Token *TokenSession) BalanceOf(account common.Address) (*big.Int, error) { + return _Token.Contract.BalanceOf(&_Token.CallOpts, account) +} + +// BalanceOf is a free data retrieval call binding the contract method 0x70a08231. +// +// Solidity: function balanceOf(address account) constant returns(uint256) +func (_Token *TokenCallerSession) BalanceOf(account common.Address) (*big.Int, error) { + return _Token.Contract.BalanceOf(&_Token.CallOpts, account) +} + +// DECIMALS is a paid mutator transaction binding the contract method 0x2e0f2625. +// +// Solidity: function DECIMALS() returns(uint8) +func (_Token *TokenTransactor) DECIMALS(opts *bind.TransactOpts) (*types.Transaction, error) { + return _Token.contract.Transact(opts, "DECIMALS") +} + +// DECIMALS is a paid mutator transaction binding the contract method 0x2e0f2625. +// +// Solidity: function DECIMALS() returns(uint8) +func (_Token *TokenSession) DECIMALS() (*types.Transaction, error) { + return _Token.Contract.DECIMALS(&_Token.TransactOpts) +} + +// DECIMALS is a paid mutator transaction binding the contract method 0x2e0f2625. +// +// Solidity: function DECIMALS() returns(uint8) +func (_Token *TokenTransactorSession) DECIMALS() (*types.Transaction, error) { + return _Token.Contract.DECIMALS(&_Token.TransactOpts) +} + +// INITIALSUPPLY is a paid mutator transaction binding the contract method 0x2ff2e9dc. +// +// Solidity: function INITIAL_SUPPLY() returns(uint256) +func (_Token *TokenTransactor) INITIALSUPPLY(opts *bind.TransactOpts) (*types.Transaction, error) { + return _Token.contract.Transact(opts, "INITIAL_SUPPLY") +} + +// INITIALSUPPLY is a paid mutator transaction binding the contract method 0x2ff2e9dc. +// +// Solidity: function INITIAL_SUPPLY() returns(uint256) +func (_Token *TokenSession) INITIALSUPPLY() (*types.Transaction, error) { + return _Token.Contract.INITIALSUPPLY(&_Token.TransactOpts) +} + +// INITIALSUPPLY is a paid mutator transaction binding the contract method 0x2ff2e9dc. +// +// Solidity: function INITIAL_SUPPLY() returns(uint256) +func (_Token *TokenTransactorSession) INITIALSUPPLY() (*types.Transaction, error) { + return _Token.Contract.INITIALSUPPLY(&_Token.TransactOpts) +} + +// Allowance is a paid mutator transaction binding the contract method 0xdd62ed3e. +// +// Solidity: function allowance(address owner, address spender) returns(uint256) +func (_Token *TokenTransactor) Allowance(opts *bind.TransactOpts, owner common.Address, spender common.Address) (*types.Transaction, error) { + return _Token.contract.Transact(opts, "allowance", owner, spender) +} + +// Allowance is a paid mutator transaction binding the contract method 0xdd62ed3e. +// +// Solidity: function allowance(address owner, address spender) returns(uint256) +func (_Token *TokenSession) Allowance(owner common.Address, spender common.Address) (*types.Transaction, error) { + return _Token.Contract.Allowance(&_Token.TransactOpts, owner, spender) +} + +// Allowance is a paid mutator transaction binding the contract method 0xdd62ed3e. +// +// Solidity: function allowance(address owner, address spender) returns(uint256) +func (_Token *TokenTransactorSession) Allowance(owner common.Address, spender common.Address) (*types.Transaction, error) { + return _Token.Contract.Allowance(&_Token.TransactOpts, owner, spender) +} + +// Approve is a paid mutator transaction binding the contract method 0x095ea7b3. +// +// Solidity: function approve(address spender, uint256 amount) returns(bool) +func (_Token *TokenTransactor) Approve(opts *bind.TransactOpts, spender common.Address, amount *big.Int) (*types.Transaction, error) { + return _Token.contract.Transact(opts, "approve", spender, amount) +} + +// Approve is a paid mutator transaction binding the contract method 0x095ea7b3. +// +// Solidity: function approve(address spender, uint256 amount) returns(bool) +func (_Token *TokenSession) Approve(spender common.Address, amount *big.Int) (*types.Transaction, error) { + return _Token.Contract.Approve(&_Token.TransactOpts, spender, amount) +} + +// Approve is a paid mutator transaction binding the contract method 0x095ea7b3. +// +// Solidity: function approve(address spender, uint256 amount) returns(bool) +func (_Token *TokenTransactorSession) Approve(spender common.Address, amount *big.Int) (*types.Transaction, error) { + return _Token.Contract.Approve(&_Token.TransactOpts, spender, amount) +} + +// Decimals is a paid mutator transaction binding the contract method 0x313ce567. +// +// Solidity: function decimals() returns(uint8) +func (_Token *TokenTransactor) Decimals(opts *bind.TransactOpts) (*types.Transaction, error) { + return _Token.contract.Transact(opts, "decimals") +} + +// Decimals is a paid mutator transaction binding the contract method 0x313ce567. +// +// Solidity: function decimals() returns(uint8) +func (_Token *TokenSession) Decimals() (*types.Transaction, error) { + return _Token.Contract.Decimals(&_Token.TransactOpts) +} + +// Decimals is a paid mutator transaction binding the contract method 0x313ce567. +// +// Solidity: function decimals() returns(uint8) +func (_Token *TokenTransactorSession) Decimals() (*types.Transaction, error) { + return _Token.Contract.Decimals(&_Token.TransactOpts) +} + +// DecreaseAllowance is a paid mutator transaction binding the contract method 0xa457c2d7. +// +// Solidity: function decreaseAllowance(address spender, uint256 subtractedValue) returns(bool) +func (_Token *TokenTransactor) DecreaseAllowance(opts *bind.TransactOpts, spender common.Address, subtractedValue *big.Int) (*types.Transaction, error) { + return _Token.contract.Transact(opts, "decreaseAllowance", spender, subtractedValue) +} + +// DecreaseAllowance is a paid mutator transaction binding the contract method 0xa457c2d7. +// +// Solidity: function decreaseAllowance(address spender, uint256 subtractedValue) returns(bool) +func (_Token *TokenSession) DecreaseAllowance(spender common.Address, subtractedValue *big.Int) (*types.Transaction, error) { + return _Token.Contract.DecreaseAllowance(&_Token.TransactOpts, spender, subtractedValue) +} + +// DecreaseAllowance is a paid mutator transaction binding the contract method 0xa457c2d7. +// +// Solidity: function decreaseAllowance(address spender, uint256 subtractedValue) returns(bool) +func (_Token *TokenTransactorSession) DecreaseAllowance(spender common.Address, subtractedValue *big.Int) (*types.Transaction, error) { + return _Token.Contract.DecreaseAllowance(&_Token.TransactOpts, spender, subtractedValue) +} + +// IncreaseAllowance is a paid mutator transaction binding the contract method 0x39509351. +// +// Solidity: function increaseAllowance(address spender, uint256 addedValue) returns(bool) +func (_Token *TokenTransactor) IncreaseAllowance(opts *bind.TransactOpts, spender common.Address, addedValue *big.Int) (*types.Transaction, error) { + return _Token.contract.Transact(opts, "increaseAllowance", spender, addedValue) +} + +// IncreaseAllowance is a paid mutator transaction binding the contract method 0x39509351. +// +// Solidity: function increaseAllowance(address spender, uint256 addedValue) returns(bool) +func (_Token *TokenSession) IncreaseAllowance(spender common.Address, addedValue *big.Int) (*types.Transaction, error) { + return _Token.Contract.IncreaseAllowance(&_Token.TransactOpts, spender, addedValue) +} + +// IncreaseAllowance is a paid mutator transaction binding the contract method 0x39509351. +// +// Solidity: function increaseAllowance(address spender, uint256 addedValue) returns(bool) +func (_Token *TokenTransactorSession) IncreaseAllowance(spender common.Address, addedValue *big.Int) (*types.Transaction, error) { + return _Token.Contract.IncreaseAllowance(&_Token.TransactOpts, spender, addedValue) +} + +// Name is a paid mutator transaction binding the contract method 0x06fdde03. +// +// Solidity: function name() returns(string) +func (_Token *TokenTransactor) Name(opts *bind.TransactOpts) (*types.Transaction, error) { + return _Token.contract.Transact(opts, "name") +} + +// Name is a paid mutator transaction binding the contract method 0x06fdde03. +// +// Solidity: function name() returns(string) +func (_Token *TokenSession) Name() (*types.Transaction, error) { + return _Token.Contract.Name(&_Token.TransactOpts) +} + +// Name is a paid mutator transaction binding the contract method 0x06fdde03. +// +// Solidity: function name() returns(string) +func (_Token *TokenTransactorSession) Name() (*types.Transaction, error) { + return _Token.Contract.Name(&_Token.TransactOpts) +} + +// Symbol is a paid mutator transaction binding the contract method 0x95d89b41. +// +// Solidity: function symbol() returns(string) +func (_Token *TokenTransactor) Symbol(opts *bind.TransactOpts) (*types.Transaction, error) { + return _Token.contract.Transact(opts, "symbol") +} + +// Symbol is a paid mutator transaction binding the contract method 0x95d89b41. +// +// Solidity: function symbol() returns(string) +func (_Token *TokenSession) Symbol() (*types.Transaction, error) { + return _Token.Contract.Symbol(&_Token.TransactOpts) +} + +// Symbol is a paid mutator transaction binding the contract method 0x95d89b41. +// +// Solidity: function symbol() returns(string) +func (_Token *TokenTransactorSession) Symbol() (*types.Transaction, error) { + return _Token.Contract.Symbol(&_Token.TransactOpts) +} + +// TotalSupply is a paid mutator transaction binding the contract method 0x18160ddd. +// +// Solidity: function totalSupply() returns(uint256) +func (_Token *TokenTransactor) TotalSupply(opts *bind.TransactOpts) (*types.Transaction, error) { + return _Token.contract.Transact(opts, "totalSupply") +} + +// TotalSupply is a paid mutator transaction binding the contract method 0x18160ddd. +// +// Solidity: function totalSupply() returns(uint256) +func (_Token *TokenSession) TotalSupply() (*types.Transaction, error) { + return _Token.Contract.TotalSupply(&_Token.TransactOpts) +} + +// TotalSupply is a paid mutator transaction binding the contract method 0x18160ddd. +// +// Solidity: function totalSupply() returns(uint256) +func (_Token *TokenTransactorSession) TotalSupply() (*types.Transaction, error) { + return _Token.Contract.TotalSupply(&_Token.TransactOpts) +} + +// Transfer is a paid mutator transaction binding the contract method 0xa9059cbb. +// +// Solidity: function transfer(address recipient, uint256 amount) returns(bool) +func (_Token *TokenTransactor) Transfer(opts *bind.TransactOpts, recipient common.Address, amount *big.Int) (*types.Transaction, error) { + return _Token.contract.Transact(opts, "transfer", recipient, amount) +} + +// Transfer is a paid mutator transaction binding the contract method 0xa9059cbb. +// +// Solidity: function transfer(address recipient, uint256 amount) returns(bool) +func (_Token *TokenSession) Transfer(recipient common.Address, amount *big.Int) (*types.Transaction, error) { + return _Token.Contract.Transfer(&_Token.TransactOpts, recipient, amount) +} + +// Transfer is a paid mutator transaction binding the contract method 0xa9059cbb. +// +// Solidity: function transfer(address recipient, uint256 amount) returns(bool) +func (_Token *TokenTransactorSession) Transfer(recipient common.Address, amount *big.Int) (*types.Transaction, error) { + return _Token.Contract.Transfer(&_Token.TransactOpts, recipient, amount) +} + +// TransferFrom is a paid mutator transaction binding the contract method 0x23b872dd. +// +// Solidity: function transferFrom(address sender, address recipient, uint256 amount) returns(bool) +func (_Token *TokenTransactor) TransferFrom(opts *bind.TransactOpts, sender common.Address, recipient common.Address, amount *big.Int) (*types.Transaction, error) { + return _Token.contract.Transact(opts, "transferFrom", sender, recipient, amount) +} + +// TransferFrom is a paid mutator transaction binding the contract method 0x23b872dd. +// +// Solidity: function transferFrom(address sender, address recipient, uint256 amount) returns(bool) +func (_Token *TokenSession) TransferFrom(sender common.Address, recipient common.Address, amount *big.Int) (*types.Transaction, error) { + return _Token.Contract.TransferFrom(&_Token.TransactOpts, sender, recipient, amount) +} + +// TransferFrom is a paid mutator transaction binding the contract method 0x23b872dd. +// +// Solidity: function transferFrom(address sender, address recipient, uint256 amount) returns(bool) +func (_Token *TokenTransactorSession) TransferFrom(sender common.Address, recipient common.Address, amount *big.Int) (*types.Transaction, error) { + return _Token.Contract.TransferFrom(&_Token.TransactOpts, sender, recipient, amount) +} + +// TokenApprovalIterator is returned from FilterApproval and is used to iterate over the raw logs and unpacked data for Approval events raised by the Token contract. +type TokenApprovalIterator struct { + Event *TokenApproval // Event containing the contract specifics and raw log + + contract *bind.BoundContract // Generic contract to use for unpacking event data + event string // Event name to use for unpacking event data + + logs chan types.Log // Log channel receiving the found contract events + sub ethereum.Subscription // Subscription for errors, completion and termination + done bool // Whether the subscription completed delivering logs + fail error // Occurred error to stop iteration +} + +// Next advances the iterator to the subsequent event, returning whether there +// are any more events found. In case of a retrieval or parsing error, false is +// returned and Error() can be queried for the exact failure. +func (it *TokenApprovalIterator) Next() bool { + // If the iterator failed, stop iterating + if it.fail != nil { + return false + } + // If the iterator completed, deliver directly whatever's available + if it.done { + select { + case log := <-it.logs: + it.Event = new(TokenApproval) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + default: + return false + } + } + // Iterator still in progress, wait for either a data or an error event + select { + case log := <-it.logs: + it.Event = new(TokenApproval) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + case err := <-it.sub.Err(): + it.done = true + it.fail = err + return it.Next() + } +} + +// Error returns any retrieval or parsing error occurred during filtering. +func (it *TokenApprovalIterator) Error() error { + return it.fail +} + +// Close terminates the iteration process, releasing any pending underlying +// resources. +func (it *TokenApprovalIterator) Close() error { + it.sub.Unsubscribe() + return nil +} + +// TokenApproval represents a Approval event raised by the Token contract. +type TokenApproval struct { + Owner common.Address + Spender common.Address + Value *big.Int + Raw types.Log // Blockchain specific contextual infos +} + +// FilterApproval is a free log retrieval operation binding the contract event 0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925. +// +// Solidity: event Approval(address indexed owner, address indexed spender, uint256 value) +func (_Token *TokenFilterer) FilterApproval(opts *bind.FilterOpts, owner []common.Address, spender []common.Address) (*TokenApprovalIterator, error) { + + var ownerRule []interface{} + for _, ownerItem := range owner { + ownerRule = append(ownerRule, ownerItem) + } + var spenderRule []interface{} + for _, spenderItem := range spender { + spenderRule = append(spenderRule, spenderItem) + } + + logs, sub, err := _Token.contract.FilterLogs(opts, "Approval", ownerRule, spenderRule) + if err != nil { + return nil, err + } + return &TokenApprovalIterator{contract: _Token.contract, event: "Approval", logs: logs, sub: sub}, nil +} + +// WatchApproval is a free log subscription operation binding the contract event 0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925. +// +// Solidity: event Approval(address indexed owner, address indexed spender, uint256 value) +func (_Token *TokenFilterer) WatchApproval(opts *bind.WatchOpts, sink chan<- *TokenApproval, owner []common.Address, spender []common.Address) (event.Subscription, error) { + + var ownerRule []interface{} + for _, ownerItem := range owner { + ownerRule = append(ownerRule, ownerItem) + } + var spenderRule []interface{} + for _, spenderItem := range spender { + spenderRule = append(spenderRule, spenderItem) + } + + logs, sub, err := _Token.contract.WatchLogs(opts, "Approval", ownerRule, spenderRule) + if err != nil { + return nil, err + } + return event.NewSubscription(func(quit <-chan struct{}) error { + defer sub.Unsubscribe() + for { + select { + case log := <-logs: + // New log arrived, parse the event and forward to the user + event := new(TokenApproval) + if err := _Token.contract.UnpackLog(event, "Approval", log); err != nil { + return err + } + event.Raw = log + + select { + case sink <- event: + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + } + }), nil +} + +// ParseApproval is a log parse operation binding the contract event 0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925. +// +// Solidity: event Approval(address indexed owner, address indexed spender, uint256 value) +func (_Token *TokenFilterer) ParseApproval(log types.Log) (*TokenApproval, error) { + event := new(TokenApproval) + if err := _Token.contract.UnpackLog(event, "Approval", log); err != nil { + return nil, err + } + return event, nil +} + +// TokenTransferIterator is returned from FilterTransfer and is used to iterate over the raw logs and unpacked data for Transfer events raised by the Token contract. +type TokenTransferIterator struct { + Event *TokenTransfer // Event containing the contract specifics and raw log + + contract *bind.BoundContract // Generic contract to use for unpacking event data + event string // Event name to use for unpacking event data + + logs chan types.Log // Log channel receiving the found contract events + sub ethereum.Subscription // Subscription for errors, completion and termination + done bool // Whether the subscription completed delivering logs + fail error // Occurred error to stop iteration +} + +// Next advances the iterator to the subsequent event, returning whether there +// are any more events found. In case of a retrieval or parsing error, false is +// returned and Error() can be queried for the exact failure. +func (it *TokenTransferIterator) Next() bool { + // If the iterator failed, stop iterating + if it.fail != nil { + return false + } + // If the iterator completed, deliver directly whatever's available + if it.done { + select { + case log := <-it.logs: + it.Event = new(TokenTransfer) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + default: + return false + } + } + // Iterator still in progress, wait for either a data or an error event + select { + case log := <-it.logs: + it.Event = new(TokenTransfer) + if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { + it.fail = err + return false + } + it.Event.Raw = log + return true + + case err := <-it.sub.Err(): + it.done = true + it.fail = err + return it.Next() + } +} + +// Error returns any retrieval or parsing error occurred during filtering. +func (it *TokenTransferIterator) Error() error { + return it.fail +} + +// Close terminates the iteration process, releasing any pending underlying +// resources. +func (it *TokenTransferIterator) Close() error { + it.sub.Unsubscribe() + return nil +} + +// TokenTransfer represents a Transfer event raised by the Token contract. +type TokenTransfer struct { + From common.Address + To common.Address + Value *big.Int + Raw types.Log // Blockchain specific contextual infos +} + +// FilterTransfer is a free log retrieval operation binding the contract event 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef. +// +// Solidity: event Transfer(address indexed from, address indexed to, uint256 value) +func (_Token *TokenFilterer) FilterTransfer(opts *bind.FilterOpts, from []common.Address, to []common.Address) (*TokenTransferIterator, error) { + + var fromRule []interface{} + for _, fromItem := range from { + fromRule = append(fromRule, fromItem) + } + var toRule []interface{} + for _, toItem := range to { + toRule = append(toRule, toItem) + } + + logs, sub, err := _Token.contract.FilterLogs(opts, "Transfer", fromRule, toRule) + if err != nil { + return nil, err + } + return &TokenTransferIterator{contract: _Token.contract, event: "Transfer", logs: logs, sub: sub}, nil +} + +// WatchTransfer is a free log subscription operation binding the contract event 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef. +// +// Solidity: event Transfer(address indexed from, address indexed to, uint256 value) +func (_Token *TokenFilterer) WatchTransfer(opts *bind.WatchOpts, sink chan<- *TokenTransfer, from []common.Address, to []common.Address) (event.Subscription, error) { + + var fromRule []interface{} + for _, fromItem := range from { + fromRule = append(fromRule, fromItem) + } + var toRule []interface{} + for _, toItem := range to { + toRule = append(toRule, toItem) + } + + logs, sub, err := _Token.contract.WatchLogs(opts, "Transfer", fromRule, toRule) + if err != nil { + return nil, err + } + return event.NewSubscription(func(quit <-chan struct{}) error { + defer sub.Unsubscribe() + for { + select { + case log := <-logs: + // New log arrived, parse the event and forward to the user + event := new(TokenTransfer) + if err := _Token.contract.UnpackLog(event, "Transfer", log); err != nil { + return err + } + event.Raw = log + + select { + case sink <- event: + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + case err := <-sub.Err(): + return err + case <-quit: + return nil + } + } + }), nil +} + +// ParseTransfer is a log parse operation binding the contract event 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef. +// +// Solidity: event Transfer(address indexed from, address indexed to, uint256 value) +func (_Token *TokenFilterer) ParseTransfer(log types.Log) (*TokenTransfer, error) { + event := new(TokenTransfer) + if err := _Token.contract.UnpackLog(event, "Transfer", log); err != nil { + return nil, err + } + return event, nil +} diff --git a/src/mpc/hbswap_add_liquidity.mpc b/src/mpc/hbswap_add_liquidity.mpc new file mode 100644 index 00000000..703924c9 --- /dev/null +++ b/src/mpc/hbswap_add_liquidity.mpc @@ -0,0 +1,48 @@ +def read_sint(idx): + start = regint(idx * 32) + end = regint() + masked_value = sint() + readsharesfromfile(start, end, masked_value) + return masked_value + +def sint_to_sfix(x): + return sfix._new(x) + +def sfix_to_sint(x): + return x.v + +def main(): + pool_A = sint_to_sfix(read_sint(0)) + pool_B = sint_to_sfix(read_sint(1)) + print_ln('pool_A %s', pool_A.reveal()) + print_ln('pool_B %s', pool_B.reveal()) + + amt_A = sint_to_sfix(read_sint(2)) + amt_B = sint_to_sfix(read_sint(3)) + print_ln('amt_A %s', amt_A.reveal()) + print_ln('amt_B %s', amt_B.reveal()) + + total_supply = sint_to_sfix(read_sint(4)) + print_ln('total_supply %s', total_supply.reveal()) + + flag = amt_A * pool_B > amt_B * pool_A + not_flag = 1 - flag + amt_A = flag * (amt_B * pool_A / pool_B) + not_flag * amt_A + amt_B = flag * amt_B + not_flag * (amt_A * pool_B / pool_A) + print_ln('actual amtA %s amtB %s', amt_A.reveal(), amt_B.reveal()) + + amt_liquidity = amt_A * total_supply / pool_A + print_ln('amt_liquidity %s', amt_liquidity.reveal()) + + pool_A = pool_A + amt_A + pool_B = pool_B + amt_B + print_ln('pool_A %s', pool_A.reveal()) + print_ln('pool_B %s', pool_B.reveal()) + + writesharestofile(sfix_to_sint(pool_A)) + writesharestofile(sfix_to_sint(pool_B)) + writesharestofile(sfix_to_sint(amt_A)) + writesharestofile(sfix_to_sint(amt_B)) + writesharestofile(sfix_to_sint(amt_liquidity)) + +main() diff --git a/src/mpc/hbswap_calc_price.mpc b/src/mpc/hbswap_calc_price.mpc new file mode 100644 index 00000000..c623de40 --- /dev/null +++ b/src/mpc/hbswap_calc_price.mpc @@ -0,0 +1,19 @@ +def read_sint(idx): + start = regint(idx * 32) + end = regint() + masked_value = sint() + readsharesfromfile(start, end, masked_value) + return masked_value + +def sint_to_sfix(x): + return sfix._new(x) + +def main(): + total_price = sint_to_sfix(read_sint(0)) + + total_cnt = read_sint(1) + + avg_price = total_price / total_cnt + print_ln('%s', avg_price.reveal()) + +main() diff --git a/src/mpc/hbswap_check_balance.mpc b/src/mpc/hbswap_check_balance.mpc new file mode 100644 index 00000000..3198f1a8 --- /dev/null +++ b/src/mpc/hbswap_check_balance.mpc @@ -0,0 +1,22 @@ +def read_sint(idx): + start = regint(idx * 32) + end = regint() + masked_value = sint() + readsharesfromfile(start, end, masked_value) + return masked_value + +def sint_to_sfix(x): + return sfix._new(x) + +def main(): + balance = sint_to_sfix(read_sint(0)) + amt = sint_to_sfix(read_sint(1)) + print_ln('balance %s amt %s', balance.reveal(), amt.reveal()) + + enough_balance = balance >= amt + print_ln('enough_balance %s', enough_balance.reveal()) + + for i in range(4): + enough_balance.reveal_to(i) + +main() diff --git a/src/mpc/hbswap_remove_liquidity.mpc b/src/mpc/hbswap_remove_liquidity.mpc new file mode 100644 index 00000000..1e78be55 --- /dev/null +++ b/src/mpc/hbswap_remove_liquidity.mpc @@ -0,0 +1,41 @@ +def read_sint(idx): + start = regint(idx * 32) + end = regint() + masked_value = sint() + readsharesfromfile(start, end, masked_value) + return masked_value + +def sint_to_sfix(x): + return sfix._new(x) + +def sfix_to_sint(x): + return x.v + +def main(): + pool_A = sint_to_sfix(read_sint(0)) + pool_B = sint_to_sfix(read_sint(1)) + print_ln('pool_A %s', pool_A.reveal()) + print_ln('pool_B %s', pool_B.reveal()) + + amt_liquidity = sint_to_sfix(read_sint(2)) + print_ln('amt_liquidity %s', amt_liquidity.reveal()) + + total_supply = sint_to_sfix(read_sint(3)) + print_ln('total_supply %s', total_supply.reveal()) + + amt_A = amt_liquidity * pool_A / total_supply + amt_B = amt_liquidity * pool_B / total_supply + print_ln('amt_A %s', amt_A.reveal()) + print_ln('amt_B %s', amt_B.reveal()) + + pool_A = pool_A - amt_A + pool_B = pool_B - amt_B + print_ln('pool_A %s', pool_A.reveal()) + print_ln('pool_B %s', pool_B.reveal()) + + writesharestofile(sfix_to_sint(pool_A)) + writesharestofile(sfix_to_sint(pool_B)) + writesharestofile(sfix_to_sint(amt_A)) + writesharestofile(sfix_to_sint(amt_B)) + +main() diff --git a/src/mpc/hbswap_test.mpc b/src/mpc/hbswap_test.mpc new file mode 100644 index 00000000..d40b0882 --- /dev/null +++ b/src/mpc/hbswap_test.mpc @@ -0,0 +1,14 @@ +def readshare(idx): + start = regint(idx * 32) + end = regint() + masked_value = sint() + readsharesfromfile(start, end, masked_value) + return masked_value + +def main(): + #print_ln('!!!! %s', sfix._new(readshare(0)).reveal()) + #print_ln('!!!! %s', readshare(0).reveal()) + sint(0).reveal_to(0) + sint(10).reveal_to(0) + +main() diff --git a/src/mpc/hbswap_trade.mpc b/src/mpc/hbswap_trade.mpc new file mode 100644 index 00000000..2d711baf --- /dev/null +++ b/src/mpc/hbswap_trade.mpc @@ -0,0 +1,67 @@ +def read_sint(idx): + start = regint(idx * 32) + end = regint() + masked_value = sint() + readsharesfromfile(start, end, masked_value) + return masked_value + +def sint_to_sfix(x): + return sfix._new(x) + +def sfix_to_sint(x): + return x.v + +def main(): + pool_A = sint_to_sfix(read_sint(0)) + pool_B = sint_to_sfix(read_sint(1)) + print_ln('pool_A %s', pool_A.reveal()) + print_ln('pool_B %s', pool_B.reveal()) + + balance_A = sint_to_sfix(read_sint(2)) + balance_B = sint_to_sfix(read_sint(3)) + print_ln('balance_A %s', balance_A.reveal()) + print_ln('balance_B %s', balance_B.reveal()) + + + amt_A = sint_to_sfix(read_sint(6) - read_sint(4)) + amt_B = sint_to_sfix(read_sint(7) - read_sint(5)) + print_ln('amt_A %s', amt_A.reveal()) + print_ln('amt_B %s', amt_B.reveal()) + + buy_A = amt_A > 0 + enough_B = -amt_B <= balance_B + calc_A = pool_A - pool_B * pool_A / (pool_B - amt_B) + accept_A = calc_A >= amt_A + flag_buy_A = buy_A * enough_B * accept_A + print_ln('buy_A %s\nenough_B %s\ncalc_A %s\naccept_A %s\nflag_buy_A %s', buy_A.reveal(), enough_B.reveal(), calc_A.reveal(), accept_A.reveal(), flag_buy_A.reveal()) + pool_A -= flag_buy_A * calc_A + pool_B -= flag_buy_A * amt_B + print_ln('pool_A %s', pool_A.reveal()) + print_ln('pool_B %s', pool_B.reveal()) + + buy_B = 1 - buy_A + enough_A = -amt_A <= balance_A + calc_B = pool_B - pool_A * pool_B / (pool_A - amt_A) + accept_B = calc_B >= amt_B + flag_buy_B = buy_B * enough_A * accept_B + print_ln('buy_B %s\nenough_A %s\ncalc_B %s\naccept_B %s\nflag_buy_B %s', buy_B.reveal(), enough_A.reveal(), calc_B.reveal(), accept_B.reveal(), flag_buy_B.reveal()) + pool_A -= flag_buy_B * amt_A + pool_B -= flag_buy_B * calc_B + print_ln('pool_A %s', pool_A.reveal()) + print_ln('pool_B %s', pool_B.reveal()) + + change_A = flag_buy_A * calc_A + flag_buy_B * amt_A + change_B = flag_buy_A * amt_B + flag_buy_B * calc_B + print_ln('change_A %s', change_A.reveal()) + print_ln('change_B %s', change_B.reveal()) + + trade_price = -1 * change_B / change_A + print_ln('marketPrice %s', trade_price.reveal()) + + writesharestofile(sfix_to_sint(pool_A)) + writesharestofile(sfix_to_sint(pool_B)) + writesharestofile(sfix_to_sint(change_A)) + writesharestofile(sfix_to_sint(change_B)) + writesharestofile(sfix_to_sint(trade_price)) + +main() diff --git a/src/node_modules/.package-lock.json b/src/node_modules/.package-lock.json new file mode 100644 index 00000000..c5a58e8c --- /dev/null +++ b/src/node_modules/.package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "hbswap", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "node_modules/@openzeppelin/contracts": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-3.2.0.tgz", + "integrity": "sha512-bUOmkSoPkjnUyMiKo6RYnb0VHBk5D9KKDAgNLzF41aqAM3TeE0yGdFF5dVRcV60pZdJLlyFT/jjXIZCWyyEzAQ==" + } + } +} diff --git a/src/node_modules/@openzeppelin/contracts/GSN/Context.sol b/src/node_modules/@openzeppelin/contracts/GSN/Context.sol new file mode 100644 index 00000000..337acb19 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/GSN/Context.sol @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/* + * @dev Provides information about the current execution context, including the + * sender of the transaction and its data. While these are generally available + * via msg.sender and msg.data, they should not be accessed in such a direct + * manner, since when dealing with GSN meta-transactions the account sending and + * paying for execution may not be the actual sender (as far as an application + * is concerned). + * + * This contract is only required for intermediate, library-like contracts. + */ +abstract contract Context { + function _msgSender() internal view virtual returns (address payable) { + return msg.sender; + } + + function _msgData() internal view virtual returns (bytes memory) { + this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691 + return msg.data; + } +} diff --git a/src/node_modules/@openzeppelin/contracts/GSN/GSNRecipient.sol b/src/node_modules/@openzeppelin/contracts/GSN/GSNRecipient.sol new file mode 100644 index 00000000..c69d53bb --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/GSN/GSNRecipient.sol @@ -0,0 +1,230 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./IRelayRecipient.sol"; +import "./IRelayHub.sol"; +import "./Context.sol"; + +/** + * @dev Base GSN recipient contract: includes the {IRelayRecipient} interface + * and enables GSN support on all contracts in the inheritance tree. + * + * TIP: This contract is abstract. The functions {IRelayRecipient-acceptRelayedCall}, + * {_preRelayedCall}, and {_postRelayedCall} are not implemented and must be + * provided by derived contracts. See the + * xref:ROOT:gsn-strategies.adoc#gsn-strategies[GSN strategies] for more + * information on how to use the pre-built {GSNRecipientSignature} and + * {GSNRecipientERC20Fee}, or how to write your own. + */ +abstract contract GSNRecipient is IRelayRecipient, Context { + // Default RelayHub address, deployed on mainnet and all testnets at the same address + address private _relayHub = 0xD216153c06E857cD7f72665E0aF1d7D82172F494; + + uint256 constant private _RELAYED_CALL_ACCEPTED = 0; + uint256 constant private _RELAYED_CALL_REJECTED = 11; + + // How much gas is forwarded to postRelayedCall + uint256 constant internal _POST_RELAYED_CALL_MAX_GAS = 100000; + + /** + * @dev Emitted when a contract changes its {IRelayHub} contract to a new one. + */ + event RelayHubChanged(address indexed oldRelayHub, address indexed newRelayHub); + + /** + * @dev Returns the address of the {IRelayHub} contract for this recipient. + */ + function getHubAddr() public view override returns (address) { + return _relayHub; + } + + /** + * @dev Switches to a new {IRelayHub} instance. This method is added for future-proofing: there's no reason to not + * use the default instance. + * + * IMPORTANT: After upgrading, the {GSNRecipient} will no longer be able to receive relayed calls from the old + * {IRelayHub} instance. Additionally, all funds should be previously withdrawn via {_withdrawDeposits}. + */ + function _upgradeRelayHub(address newRelayHub) internal virtual { + address currentRelayHub = _relayHub; + require(newRelayHub != address(0), "GSNRecipient: new RelayHub is the zero address"); + require(newRelayHub != currentRelayHub, "GSNRecipient: new RelayHub is the current one"); + + emit RelayHubChanged(currentRelayHub, newRelayHub); + + _relayHub = newRelayHub; + } + + /** + * @dev Returns the version string of the {IRelayHub} for which this recipient implementation was built. If + * {_upgradeRelayHub} is used, the new {IRelayHub} instance should be compatible with this version. + */ + // This function is view for future-proofing, it may require reading from + // storage in the future. + function relayHubVersion() public view returns (string memory) { + this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691 + return "1.0.0"; + } + + /** + * @dev Withdraws the recipient's deposits in `RelayHub`. + * + * Derived contracts should expose this in an external interface with proper access control. + */ + function _withdrawDeposits(uint256 amount, address payable payee) internal virtual { + IRelayHub(_relayHub).withdraw(amount, payee); + } + + // Overrides for Context's functions: when called from RelayHub, sender and + // data require some pre-processing: the actual sender is stored at the end + // of the call data, which in turns means it needs to be removed from it + // when handling said data. + + /** + * @dev Replacement for msg.sender. Returns the actual sender of a transaction: msg.sender for regular transactions, + * and the end-user for GSN relayed calls (where msg.sender is actually `RelayHub`). + * + * IMPORTANT: Contracts derived from {GSNRecipient} should never use `msg.sender`, and use {_msgSender} instead. + */ + function _msgSender() internal view virtual override returns (address payable) { + if (msg.sender != _relayHub) { + return msg.sender; + } else { + return _getRelayedCallSender(); + } + } + + /** + * @dev Replacement for msg.data. Returns the actual calldata of a transaction: msg.data for regular transactions, + * and a reduced version for GSN relayed calls (where msg.data contains additional information). + * + * IMPORTANT: Contracts derived from {GSNRecipient} should never use `msg.data`, and use {_msgData} instead. + */ + function _msgData() internal view virtual override returns (bytes memory) { + if (msg.sender != _relayHub) { + return msg.data; + } else { + return _getRelayedCallData(); + } + } + + // Base implementations for pre and post relayedCall: only RelayHub can invoke them, and data is forwarded to the + // internal hook. + + /** + * @dev See `IRelayRecipient.preRelayedCall`. + * + * This function should not be overridden directly, use `_preRelayedCall` instead. + * + * * Requirements: + * + * - the caller must be the `RelayHub` contract. + */ + function preRelayedCall(bytes memory context) public virtual override returns (bytes32) { + require(msg.sender == getHubAddr(), "GSNRecipient: caller is not RelayHub"); + return _preRelayedCall(context); + } + + /** + * @dev See `IRelayRecipient.preRelayedCall`. + * + * Called by `GSNRecipient.preRelayedCall`, which asserts the caller is the `RelayHub` contract. Derived contracts + * must implement this function with any relayed-call preprocessing they may wish to do. + * + */ + function _preRelayedCall(bytes memory context) internal virtual returns (bytes32); + + /** + * @dev See `IRelayRecipient.postRelayedCall`. + * + * This function should not be overridden directly, use `_postRelayedCall` instead. + * + * * Requirements: + * + * - the caller must be the `RelayHub` contract. + */ + function postRelayedCall(bytes memory context, bool success, uint256 actualCharge, bytes32 preRetVal) public virtual override { + require(msg.sender == getHubAddr(), "GSNRecipient: caller is not RelayHub"); + _postRelayedCall(context, success, actualCharge, preRetVal); + } + + /** + * @dev See `IRelayRecipient.postRelayedCall`. + * + * Called by `GSNRecipient.postRelayedCall`, which asserts the caller is the `RelayHub` contract. Derived contracts + * must implement this function with any relayed-call postprocessing they may wish to do. + * + */ + function _postRelayedCall(bytes memory context, bool success, uint256 actualCharge, bytes32 preRetVal) internal virtual; + + /** + * @dev Return this in acceptRelayedCall to proceed with the execution of a relayed call. Note that this contract + * will be charged a fee by RelayHub + */ + function _approveRelayedCall() internal pure returns (uint256, bytes memory) { + return _approveRelayedCall(""); + } + + /** + * @dev See `GSNRecipient._approveRelayedCall`. + * + * This overload forwards `context` to _preRelayedCall and _postRelayedCall. + */ + function _approveRelayedCall(bytes memory context) internal pure returns (uint256, bytes memory) { + return (_RELAYED_CALL_ACCEPTED, context); + } + + /** + * @dev Return this in acceptRelayedCall to impede execution of a relayed call. No fees will be charged. + */ + function _rejectRelayedCall(uint256 errorCode) internal pure returns (uint256, bytes memory) { + return (_RELAYED_CALL_REJECTED + errorCode, ""); + } + + /* + * @dev Calculates how much RelayHub will charge a recipient for using `gas` at a `gasPrice`, given a relayer's + * `serviceFee`. + */ + function _computeCharge(uint256 gas, uint256 gasPrice, uint256 serviceFee) internal pure returns (uint256) { + // The fee is expressed as a percentage. E.g. a value of 40 stands for a 40% fee, so the recipient will be + // charged for 1.4 times the spent amount. + return (gas * gasPrice * (100 + serviceFee)) / 100; + } + + function _getRelayedCallSender() private pure returns (address payable result) { + // We need to read 20 bytes (an address) located at array index msg.data.length - 20. In memory, the array + // is prefixed with a 32-byte length value, so we first add 32 to get the memory read index. However, doing + // so would leave the address in the upper 20 bytes of the 32-byte word, which is inconvenient and would + // require bit shifting. We therefore subtract 12 from the read index so the address lands on the lower 20 + // bytes. This can always be done due to the 32-byte prefix. + + // The final memory read index is msg.data.length - 20 + 32 - 12 = msg.data.length. Using inline assembly is the + // easiest/most-efficient way to perform this operation. + + // These fields are not accessible from assembly + bytes memory array = msg.data; + uint256 index = msg.data.length; + + // solhint-disable-next-line no-inline-assembly + assembly { + // Load the 32 bytes word from memory with the address on the lower 20 bytes, and mask those. + result := and(mload(add(array, index)), 0xffffffffffffffffffffffffffffffffffffffff) + } + return result; + } + + function _getRelayedCallData() private pure returns (bytes memory) { + // RelayHub appends the sender address at the end of the calldata, so in order to retrieve the actual msg.data, + // we must strip the last 20 bytes (length of an address type) from it. + + uint256 actualDataLength = msg.data.length - 20; + bytes memory actualData = new bytes(actualDataLength); + + for (uint256 i = 0; i < actualDataLength; ++i) { + actualData[i] = msg.data[i]; + } + + return actualData; + } +} diff --git a/src/node_modules/@openzeppelin/contracts/GSN/GSNRecipientERC20Fee.sol b/src/node_modules/@openzeppelin/contracts/GSN/GSNRecipientERC20Fee.sol new file mode 100644 index 00000000..24053255 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/GSN/GSNRecipientERC20Fee.sol @@ -0,0 +1,152 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./GSNRecipient.sol"; +import "../math/SafeMath.sol"; +import "../access/Ownable.sol"; +import "../token/ERC20/SafeERC20.sol"; +import "../token/ERC20/ERC20.sol"; + +/** + * @dev A xref:ROOT:gsn-strategies.adoc#gsn-strategies[GSN strategy] that charges transaction fees in a special purpose ERC20 + * token, which we refer to as the gas payment token. The amount charged is exactly the amount of Ether charged to the + * recipient. This means that the token is essentially pegged to the value of Ether. + * + * The distribution strategy of the gas payment token to users is not defined by this contract. It's a mintable token + * whose only minter is the recipient, so the strategy must be implemented in a derived contract, making use of the + * internal {_mint} function. + */ +contract GSNRecipientERC20Fee is GSNRecipient { + using SafeERC20 for __unstable__ERC20Owned; + using SafeMath for uint256; + + enum GSNRecipientERC20FeeErrorCodes { + INSUFFICIENT_BALANCE + } + + __unstable__ERC20Owned private _token; + + /** + * @dev The arguments to the constructor are the details that the gas payment token will have: `name` and `symbol`. `decimals` is hard-coded to 18. + */ + constructor(string memory name, string memory symbol) public { + _token = new __unstable__ERC20Owned(name, symbol); + } + + /** + * @dev Returns the gas payment token. + */ + function token() public view returns (IERC20) { + return IERC20(_token); + } + + /** + * @dev Internal function that mints the gas payment token. Derived contracts should expose this function in their public API, with proper access control mechanisms. + */ + function _mint(address account, uint256 amount) internal virtual { + _token.mint(account, amount); + } + + /** + * @dev Ensures that only users with enough gas payment token balance can have transactions relayed through the GSN. + */ + function acceptRelayedCall( + address, + address from, + bytes memory, + uint256 transactionFee, + uint256 gasPrice, + uint256, + uint256, + bytes memory, + uint256 maxPossibleCharge + ) + public + view + virtual + override + returns (uint256, bytes memory) + { + if (_token.balanceOf(from) < maxPossibleCharge) { + return _rejectRelayedCall(uint256(GSNRecipientERC20FeeErrorCodes.INSUFFICIENT_BALANCE)); + } + + return _approveRelayedCall(abi.encode(from, maxPossibleCharge, transactionFee, gasPrice)); + } + + /** + * @dev Implements the precharge to the user. The maximum possible charge (depending on gas limit, gas price, and + * fee) will be deducted from the user balance of gas payment token. Note that this is an overestimation of the + * actual charge, necessary because we cannot predict how much gas the execution will actually need. The remainder + * is returned to the user in {_postRelayedCall}. + */ + function _preRelayedCall(bytes memory context) internal virtual override returns (bytes32) { + (address from, uint256 maxPossibleCharge) = abi.decode(context, (address, uint256)); + + // The maximum token charge is pre-charged from the user + _token.safeTransferFrom(from, address(this), maxPossibleCharge); + } + + /** + * @dev Returns to the user the extra amount that was previously charged, once the actual execution cost is known. + */ + function _postRelayedCall(bytes memory context, bool, uint256 actualCharge, bytes32) internal virtual override { + (address from, uint256 maxPossibleCharge, uint256 transactionFee, uint256 gasPrice) = + abi.decode(context, (address, uint256, uint256, uint256)); + + // actualCharge is an _estimated_ charge, which assumes postRelayedCall will use all available gas. + // This implementation's gas cost can be roughly estimated as 10k gas, for the two SSTORE operations in an + // ERC20 transfer. + uint256 overestimation = _computeCharge(_POST_RELAYED_CALL_MAX_GAS.sub(10000), gasPrice, transactionFee); + actualCharge = actualCharge.sub(overestimation); + + // After the relayed call has been executed and the actual charge estimated, the excess pre-charge is returned + _token.safeTransfer(from, maxPossibleCharge.sub(actualCharge)); + } +} + +/** + * @title __unstable__ERC20Owned + * @dev An ERC20 token owned by another contract, which has minting permissions and can use transferFrom to receive + * anyone's tokens. This contract is an internal helper for GSNRecipientERC20Fee, and should not be used + * outside of this context. + */ +// solhint-disable-next-line contract-name-camelcase +contract __unstable__ERC20Owned is ERC20, Ownable { + uint256 private constant _UINT256_MAX = 2**256 - 1; + + constructor(string memory name, string memory symbol) public ERC20(name, symbol) { } + + // The owner (GSNRecipientERC20Fee) can mint tokens + function mint(address account, uint256 amount) public onlyOwner { + _mint(account, amount); + } + + // The owner has 'infinite' allowance for all token holders + function allowance(address tokenOwner, address spender) public view override returns (uint256) { + if (spender == owner()) { + return _UINT256_MAX; + } else { + return super.allowance(tokenOwner, spender); + } + } + + // Allowance for the owner cannot be changed (it is always 'infinite') + function _approve(address tokenOwner, address spender, uint256 value) internal override { + if (spender == owner()) { + return; + } else { + super._approve(tokenOwner, spender, value); + } + } + + function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) { + if (recipient == owner()) { + _transfer(sender, recipient, amount); + return true; + } else { + return super.transferFrom(sender, recipient, amount); + } + } +} diff --git a/src/node_modules/@openzeppelin/contracts/GSN/GSNRecipientSignature.sol b/src/node_modules/@openzeppelin/contracts/GSN/GSNRecipientSignature.sol new file mode 100644 index 00000000..ee514de6 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/GSN/GSNRecipientSignature.sol @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./GSNRecipient.sol"; +import "../cryptography/ECDSA.sol"; + +/** + * @dev A xref:ROOT:gsn-strategies.adoc#gsn-strategies[GSN strategy] that allows relayed transactions through when they are + * accompanied by the signature of a trusted signer. The intent is for this signature to be generated by a server that + * performs validations off-chain. Note that nothing is charged to the user in this scheme. Thus, the server should make + * sure to account for this in their economic and threat model. + */ +contract GSNRecipientSignature is GSNRecipient { + using ECDSA for bytes32; + + address private _trustedSigner; + + enum GSNRecipientSignatureErrorCodes { + INVALID_SIGNER + } + + /** + * @dev Sets the trusted signer that is going to be producing signatures to approve relayed calls. + */ + constructor(address trustedSigner) public { + require(trustedSigner != address(0), "GSNRecipientSignature: trusted signer is the zero address"); + _trustedSigner = trustedSigner; + } + + /** + * @dev Ensures that only transactions with a trusted signature can be relayed through the GSN. + */ + function acceptRelayedCall( + address relay, + address from, + bytes memory encodedFunction, + uint256 transactionFee, + uint256 gasPrice, + uint256 gasLimit, + uint256 nonce, + bytes memory approvalData, + uint256 + ) + public + view + virtual + override + returns (uint256, bytes memory) + { + bytes memory blob = abi.encodePacked( + relay, + from, + encodedFunction, + transactionFee, + gasPrice, + gasLimit, + nonce, // Prevents replays on RelayHub + getHubAddr(), // Prevents replays in multiple RelayHubs + address(this) // Prevents replays in multiple recipients + ); + if (keccak256(blob).toEthSignedMessageHash().recover(approvalData) == _trustedSigner) { + return _approveRelayedCall(); + } else { + return _rejectRelayedCall(uint256(GSNRecipientSignatureErrorCodes.INVALID_SIGNER)); + } + } + + function _preRelayedCall(bytes memory) internal virtual override returns (bytes32) { } + + function _postRelayedCall(bytes memory, bool, uint256, bytes32) internal virtual override { } +} diff --git a/src/node_modules/@openzeppelin/contracts/GSN/IRelayHub.sol b/src/node_modules/@openzeppelin/contracts/GSN/IRelayHub.sol new file mode 100644 index 00000000..7e4be7cf --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/GSN/IRelayHub.sol @@ -0,0 +1,269 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev Interface for `RelayHub`, the core contract of the GSN. Users should not need to interact with this contract + * directly. + * + * See the https://github.com/OpenZeppelin/openzeppelin-gsn-helpers[OpenZeppelin GSN helpers] for more information on + * how to deploy an instance of `RelayHub` on your local test network. + */ +interface IRelayHub { + // Relay management + + /** + * @dev Adds stake to a relay and sets its `unstakeDelay`. If the relay does not exist, it is created, and the caller + * of this function becomes its owner. If the relay already exists, only the owner can call this function. A relay + * cannot be its own owner. + * + * All Ether in this function call will be added to the relay's stake. + * Its unstake delay will be assigned to `unstakeDelay`, but the new value must be greater or equal to the current one. + * + * Emits a {Staked} event. + */ + function stake(address relayaddr, uint256 unstakeDelay) external payable; + + /** + * @dev Emitted when a relay's stake or unstakeDelay are increased + */ + event Staked(address indexed relay, uint256 stake, uint256 unstakeDelay); + + /** + * @dev Registers the caller as a relay. + * The relay must be staked for, and not be a contract (i.e. this function must be called directly from an EOA). + * + * This function can be called multiple times, emitting new {RelayAdded} events. Note that the received + * `transactionFee` is not enforced by {relayCall}. + * + * Emits a {RelayAdded} event. + */ + function registerRelay(uint256 transactionFee, string calldata url) external; + + /** + * @dev Emitted when a relay is registered or re-registered. Looking at these events (and filtering out + * {RelayRemoved} events) lets a client discover the list of available relays. + */ + event RelayAdded(address indexed relay, address indexed owner, uint256 transactionFee, uint256 stake, uint256 unstakeDelay, string url); + + /** + * @dev Removes (deregisters) a relay. Unregistered (but staked for) relays can also be removed. + * + * Can only be called by the owner of the relay. After the relay's `unstakeDelay` has elapsed, {unstake} will be + * callable. + * + * Emits a {RelayRemoved} event. + */ + function removeRelayByOwner(address relay) external; + + /** + * @dev Emitted when a relay is removed (deregistered). `unstakeTime` is the time when unstake will be callable. + */ + event RelayRemoved(address indexed relay, uint256 unstakeTime); + + /** Deletes the relay from the system, and gives back its stake to the owner. + * + * Can only be called by the relay owner, after `unstakeDelay` has elapsed since {removeRelayByOwner} was called. + * + * Emits an {Unstaked} event. + */ + function unstake(address relay) external; + + /** + * @dev Emitted when a relay is unstaked for, including the returned stake. + */ + event Unstaked(address indexed relay, uint256 stake); + + // States a relay can be in + enum RelayState { + Unknown, // The relay is unknown to the system: it has never been staked for + Staked, // The relay has been staked for, but it is not yet active + Registered, // The relay has registered itself, and is active (can relay calls) + Removed // The relay has been removed by its owner and can no longer relay calls. It must wait for its unstakeDelay to elapse before it can unstake + } + + /** + * @dev Returns a relay's status. Note that relays can be deleted when unstaked or penalized, causing this function + * to return an empty entry. + */ + function getRelay(address relay) external view returns (uint256 totalStake, uint256 unstakeDelay, uint256 unstakeTime, address payable owner, RelayState state); + + // Balance management + + /** + * @dev Deposits Ether for a contract, so that it can receive (and pay for) relayed transactions. + * + * Unused balance can only be withdrawn by the contract itself, by calling {withdraw}. + * + * Emits a {Deposited} event. + */ + function depositFor(address target) external payable; + + /** + * @dev Emitted when {depositFor} is called, including the amount and account that was funded. + */ + event Deposited(address indexed recipient, address indexed from, uint256 amount); + + /** + * @dev Returns an account's deposits. These can be either a contract's funds, or a relay owner's revenue. + */ + function balanceOf(address target) external view returns (uint256); + + /** + * Withdraws from an account's balance, sending it back to it. Relay owners call this to retrieve their revenue, and + * contracts can use it to reduce their funding. + * + * Emits a {Withdrawn} event. + */ + function withdraw(uint256 amount, address payable dest) external; + + /** + * @dev Emitted when an account withdraws funds from `RelayHub`. + */ + event Withdrawn(address indexed account, address indexed dest, uint256 amount); + + // Relaying + + /** + * @dev Checks if the `RelayHub` will accept a relayed operation. + * Multiple things must be true for this to happen: + * - all arguments must be signed for by the sender (`from`) + * - the sender's nonce must be the current one + * - the recipient must accept this transaction (via {acceptRelayedCall}) + * + * Returns a `PreconditionCheck` value (`OK` when the transaction can be relayed), or a recipient-specific error + * code if it returns one in {acceptRelayedCall}. + */ + function canRelay( + address relay, + address from, + address to, + bytes calldata encodedFunction, + uint256 transactionFee, + uint256 gasPrice, + uint256 gasLimit, + uint256 nonce, + bytes calldata signature, + bytes calldata approvalData + ) external view returns (uint256 status, bytes memory recipientContext); + + // Preconditions for relaying, checked by canRelay and returned as the corresponding numeric values. + enum PreconditionCheck { + OK, // All checks passed, the call can be relayed + WrongSignature, // The transaction to relay is not signed by requested sender + WrongNonce, // The provided nonce has already been used by the sender + AcceptRelayedCallReverted, // The recipient rejected this call via acceptRelayedCall + InvalidRecipientStatusCode // The recipient returned an invalid (reserved) status code + } + + /** + * @dev Relays a transaction. + * + * For this to succeed, multiple conditions must be met: + * - {canRelay} must `return PreconditionCheck.OK` + * - the sender must be a registered relay + * - the transaction's gas price must be larger or equal to the one that was requested by the sender + * - the transaction must have enough gas to not run out of gas if all internal transactions (calls to the + * recipient) use all gas available to them + * - the recipient must have enough balance to pay the relay for the worst-case scenario (i.e. when all gas is + * spent) + * + * If all conditions are met, the call will be relayed and the recipient charged. {preRelayedCall}, the encoded + * function and {postRelayedCall} will be called in that order. + * + * Parameters: + * - `from`: the client originating the request + * - `to`: the target {IRelayRecipient} contract + * - `encodedFunction`: the function call to relay, including data + * - `transactionFee`: fee (%) the relay takes over actual gas cost + * - `gasPrice`: gas price the client is willing to pay + * - `gasLimit`: gas to forward when calling the encoded function + * - `nonce`: client's nonce + * - `signature`: client's signature over all previous params, plus the relay and RelayHub addresses + * - `approvalData`: dapp-specific data forwarded to {acceptRelayedCall}. This value is *not* verified by the + * `RelayHub`, but it still can be used for e.g. a signature. + * + * Emits a {TransactionRelayed} event. + */ + function relayCall( + address from, + address to, + bytes calldata encodedFunction, + uint256 transactionFee, + uint256 gasPrice, + uint256 gasLimit, + uint256 nonce, + bytes calldata signature, + bytes calldata approvalData + ) external; + + /** + * @dev Emitted when an attempt to relay a call failed. + * + * This can happen due to incorrect {relayCall} arguments, or the recipient not accepting the relayed call. The + * actual relayed call was not executed, and the recipient not charged. + * + * The `reason` parameter contains an error code: values 1-10 correspond to `PreconditionCheck` entries, and values + * over 10 are custom recipient error codes returned from {acceptRelayedCall}. + */ + event CanRelayFailed(address indexed relay, address indexed from, address indexed to, bytes4 selector, uint256 reason); + + /** + * @dev Emitted when a transaction is relayed. + * Useful when monitoring a relay's operation and relayed calls to a contract + * + * Note that the actual encoded function might be reverted: this is indicated in the `status` parameter. + * + * `charge` is the Ether value deducted from the recipient's balance, paid to the relay's owner. + */ + event TransactionRelayed(address indexed relay, address indexed from, address indexed to, bytes4 selector, RelayCallStatus status, uint256 charge); + + // Reason error codes for the TransactionRelayed event + enum RelayCallStatus { + OK, // The transaction was successfully relayed and execution successful - never included in the event + RelayedCallFailed, // The transaction was relayed, but the relayed call failed + PreRelayedFailed, // The transaction was not relayed due to preRelatedCall reverting + PostRelayedFailed, // The transaction was relayed and reverted due to postRelatedCall reverting + RecipientBalanceChanged // The transaction was relayed and reverted due to the recipient's balance changing + } + + /** + * @dev Returns how much gas should be forwarded to a call to {relayCall}, in order to relay a transaction that will + * spend up to `relayedCallStipend` gas. + */ + function requiredGas(uint256 relayedCallStipend) external view returns (uint256); + + /** + * @dev Returns the maximum recipient charge, given the amount of gas forwarded, gas price and relay fee. + */ + function maxPossibleCharge(uint256 relayedCallStipend, uint256 gasPrice, uint256 transactionFee) external view returns (uint256); + + // Relay penalization. + // Any account can penalize relays, removing them from the system immediately, and rewarding the + // reporter with half of the relay's stake. The other half is burned so that, even if the relay penalizes itself, it + // still loses half of its stake. + + /** + * @dev Penalize a relay that signed two transactions using the same nonce (making only the first one valid) and + * different data (gas price, gas limit, etc. may be different). + * + * The (unsigned) transaction data and signature for both transactions must be provided. + */ + function penalizeRepeatedNonce(bytes calldata unsignedTx1, bytes calldata signature1, bytes calldata unsignedTx2, bytes calldata signature2) external; + + /** + * @dev Penalize a relay that sent a transaction that didn't target ``RelayHub``'s {registerRelay} or {relayCall}. + */ + function penalizeIllegalTransaction(bytes calldata unsignedTx, bytes calldata signature) external; + + /** + * @dev Emitted when a relay is penalized. + */ + event Penalized(address indexed relay, address sender, uint256 amount); + + /** + * @dev Returns an account's nonce in `RelayHub`. + */ + function getNonce(address from) external view returns (uint256); +} + diff --git a/src/node_modules/@openzeppelin/contracts/GSN/IRelayRecipient.sol b/src/node_modules/@openzeppelin/contracts/GSN/IRelayRecipient.sol new file mode 100644 index 00000000..4ecaf1e2 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/GSN/IRelayRecipient.sol @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev Base interface for a contract that will be called via the GSN from {IRelayHub}. + * + * TIP: You don't need to write an implementation yourself! Inherit from {GSNRecipient} instead. + */ +interface IRelayRecipient { + /** + * @dev Returns the address of the {IRelayHub} instance this recipient interacts with. + */ + function getHubAddr() external view returns (address); + + /** + * @dev Called by {IRelayHub} to validate if this recipient accepts being charged for a relayed call. Note that the + * recipient will be charged regardless of the execution result of the relayed call (i.e. if it reverts or not). + * + * The relay request was originated by `from` and will be served by `relay`. `encodedFunction` is the relayed call + * calldata, so its first four bytes are the function selector. The relayed call will be forwarded `gasLimit` gas, + * and the transaction executed with a gas price of at least `gasPrice`. ``relay``'s fee is `transactionFee`, and the + * recipient will be charged at most `maxPossibleCharge` (in wei). `nonce` is the sender's (`from`) nonce for + * replay attack protection in {IRelayHub}, and `approvalData` is a optional parameter that can be used to hold a signature + * over all or some of the previous values. + * + * Returns a tuple, where the first value is used to indicate approval (0) or rejection (custom non-zero error code, + * values 1 to 10 are reserved) and the second one is data to be passed to the other {IRelayRecipient} functions. + * + * {acceptRelayedCall} is called with 50k gas: if it runs out during execution, the request will be considered + * rejected. A regular revert will also trigger a rejection. + */ + function acceptRelayedCall( + address relay, + address from, + bytes calldata encodedFunction, + uint256 transactionFee, + uint256 gasPrice, + uint256 gasLimit, + uint256 nonce, + bytes calldata approvalData, + uint256 maxPossibleCharge + ) + external + view + returns (uint256, bytes memory); + + /** + * @dev Called by {IRelayHub} on approved relay call requests, before the relayed call is executed. This allows to e.g. + * pre-charge the sender of the transaction. + * + * `context` is the second value returned in the tuple by {acceptRelayedCall}. + * + * Returns a value to be passed to {postRelayedCall}. + * + * {preRelayedCall} is called with 100k gas: if it runs out during execution or otherwise reverts, the relayed call + * will not be executed, but the recipient will still be charged for the transaction's cost. + */ + function preRelayedCall(bytes calldata context) external returns (bytes32); + + /** + * @dev Called by {IRelayHub} on approved relay call requests, after the relayed call is executed. This allows to e.g. + * charge the user for the relayed call costs, return any overcharges from {preRelayedCall}, or perform + * contract-specific bookkeeping. + * + * `context` is the second value returned in the tuple by {acceptRelayedCall}. `success` is the execution status of + * the relayed call. `actualCharge` is an estimate of how much the recipient will be charged for the transaction, + * not including any gas used by {postRelayedCall} itself. `preRetVal` is {preRelayedCall}'s return value. + * + * + * {postRelayedCall} is called with 100k gas: if it runs out during execution or otherwise reverts, the relayed call + * and the call to {preRelayedCall} will be reverted retroactively, but the recipient will still be charged for the + * transaction's cost. + */ + function postRelayedCall(bytes calldata context, bool success, uint256 actualCharge, bytes32 preRetVal) external; +} diff --git a/src/node_modules/@openzeppelin/contracts/GSN/README.adoc b/src/node_modules/@openzeppelin/contracts/GSN/README.adoc new file mode 100644 index 00000000..cba87cf2 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/GSN/README.adoc @@ -0,0 +1,31 @@ += Gas Station Network (GSN) + +[.readme-notice] +NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/gsn + +This set of contracts provide all the tools required to make a contract callable via the https://gsn.openzeppelin.com[Gas Station Network]. + +TIP: If you're new to the GSN, head over to our xref:learn::sending-gasless-transactions.adoc[overview of the system] and basic guide to xref:ROOT:gsn.adoc[creating a GSN-capable contract]. + +The core contract a recipient must inherit from is {GSNRecipient}: it includes all necessary interfaces, as well as some helper methods to make interacting with the GSN easier. + +Utilities to make writing xref:ROOT:gsn-strategies.adoc[GSN strategies] easy are available in {GSNRecipient}, or you can simply use one of our pre-made strategies: + +* {GSNRecipientERC20Fee} charges the end user for gas costs in an application-specific xref:ROOT:tokens.adoc#ERC20[ERC20 token] +* {GSNRecipientSignature} accepts all relayed calls that have been signed by a trusted third party (e.g. a private key in a backend) + +You can also take a look at the two contract interfaces that make up the GSN protocol: {IRelayRecipient} and {IRelayHub}, but you won't need to use those directly. + +== Recipient + +{{GSNRecipient}} + +== Strategies + +{{GSNRecipientSignature}} +{{GSNRecipientERC20Fee}} + +== Protocol + +{{IRelayRecipient}} +{{IRelayHub}} diff --git a/src/node_modules/@openzeppelin/contracts/README.md b/src/node_modules/@openzeppelin/contracts/README.md new file mode 100644 index 00000000..ad52447f --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/README.md @@ -0,0 +1,78 @@ +# OpenZeppelin + +[![Docs](https://img.shields.io/badge/docs-%F0%9F%93%84-blue)](https://docs.openzeppelin.com/contracts) +[![NPM Package](https://img.shields.io/npm/v/@openzeppelin/contracts.svg)](https://www.npmjs.org/package/@openzeppelin/contracts) +[![Build Status](https://circleci.com/gh/OpenZeppelin/openzeppelin-contracts.svg?style=shield)](https://circleci.com/gh/OpenZeppelin/openzeppelin-contracts) +[![Coverage Status](https://codecov.io/gh/OpenZeppelin/openzeppelin-contracts/graph/badge.svg)](https://codecov.io/gh/OpenZeppelin/openzeppelin-contracts) + +**A library for secure smart contract development.** Build on a solid foundation of community-vetted code. + + * Implementations of standards like [ERC20](https://docs.openzeppelin.com/contracts/erc20) and [ERC721](https://docs.openzeppelin.com/contracts/erc721). + * Flexible [role-based permissioning](https://docs.openzeppelin.com/contracts/access-control) scheme. + * Reusable [Solidity components](https://docs.openzeppelin.com/contracts/utilities) to build custom contracts and complex decentralized systems. + * First-class integration with the [Gas Station Network](https://docs.openzeppelin.com/contracts/gsn) for systems with no gas fees! + * Audited by leading security firms (_last full audit on v2.0.0_). + +## Overview + +### Installation + +```console +$ npm install @openzeppelin/contracts +``` + +OpenZeppelin Contracts features a [stable API](https://docs.openzeppelin.com/contracts/releases-stability#api-stability), which means your contracts won't break unexpectedly when upgrading to a newer minor version. + +### Usage + +Once installed, you can use the contracts in the library by importing them: + +```solidity +pragma solidity ^0.6.0; + +import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; + +contract MyCollectible is ERC721 { + constructor() ERC721("MyCollectible", "MCO") public { + } +} +``` + +_If you're new to smart contract development, head to [Developing Smart Contracts](https://docs.openzeppelin.com/learn/developing-smart-contracts) to learn about creating a new project and compiling your contracts._ + +To keep your system secure, you should **always** use the installed code as-is, and neither copy-paste it from online sources, nor modify it yourself. The library is designed so that only the contracts and functions you use are deployed, so you don't need to worry about it needlessly increasing gas costs. + +## Learn More + +The guides in the [docs site](https://docs.openzeppelin.com/contracts) will teach about different concepts, and how to use the related contracts that OpenZeppelin Contracts provides: + +* [Access Control](https://docs.openzeppelin.com/contracts/access-control): decide who can perform each of the actions on your system. +* [Tokens](https://docs.openzeppelin.com/contracts/tokens): create tradeable assets or collectives, and distribute them via [Crowdsales](https://docs.openzeppelin.com/contracts/crowdsales). +* [Gas Station Network](https://docs.openzeppelin.com/contracts/gsn): let your users interact with your contracts without having to pay for gas themselves. +* [Utilities](https://docs.openzeppelin.com/contracts/utilities): generic useful tools, including non-overflowing math, signature verification, and trustless paying systems. + +The [full API](https://docs.openzeppelin.com/contracts/api/token/ERC20) is also thoroughly documented, and serves as a great reference when developing your smart contract application. You can also ask for help or follow Contracts's development in the [community forum](https://forum.openzeppelin.com). + +Finally, you may want to take a look at the [guides on our blog](https://blog.openzeppelin.com/guides), which cover several common use cases and good practices.. The following articles provide great background reading, though please note, some of the referenced tools have changed as the tooling in the ecosystem continues to rapidly evolve. + +* [The Hitchhiker’s Guide to Smart Contracts in Ethereum](https://blog.openzeppelin.com/the-hitchhikers-guide-to-smart-contracts-in-ethereum-848f08001f05) will help you get an overview of the various tools available for smart contract development, and help you set up your environment. +* [A Gentle Introduction to Ethereum Programming, Part 1](https://blog.openzeppelin.com/a-gentle-introduction-to-ethereum-programming-part-1-783cc7796094) provides very useful information on an introductory level, including many basic concepts from the Ethereum platform. +* For a more in-depth dive, you may read the guide [Designing the Architecture for Your Ethereum Application](https://blog.openzeppelin.com/designing-the-architecture-for-your-ethereum-application-9cec086f8317), which discusses how to better structure your application and its relationship to the real world. + +## Security + +This project is maintained by [OpenZeppelin](https://openzeppelin.com), and developed following our high standards for code quality and security. OpenZeppelin is meant to provide tested and community-audited code, but please use common sense when doing anything that deals with real money! We take no responsibility for your implementation decisions and any security problems you might experience. + +The core development principles and strategies that OpenZeppelin is based on include: security in depth, simple and modular code, clarity-driven naming conventions, comprehensive unit testing, pre-and-post-condition sanity checks, code consistency, and regular audits. + +The latest audit was done on October 2018 on version 2.0.0. + +Please report any security issues you find to security@openzeppelin.org. + +## Contribute + +OpenZeppelin exists thanks to its contributors. There are many ways you can participate and help build high quality software. Check out the [contribution guide](CONTRIBUTING.md)! + +## License + +OpenZeppelin is released under the [MIT License](LICENSE). diff --git a/src/node_modules/@openzeppelin/contracts/access/AccessControl.sol b/src/node_modules/@openzeppelin/contracts/access/AccessControl.sol new file mode 100644 index 00000000..27ba9e8d --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/access/AccessControl.sol @@ -0,0 +1,217 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../utils/EnumerableSet.sol"; +import "../utils/Address.sol"; +import "../GSN/Context.sol"; + +/** + * @dev Contract module that allows children to implement role-based access + * control mechanisms. + * + * Roles are referred to by their `bytes32` identifier. These should be exposed + * in the external API and be unique. The best way to achieve this is by + * using `public constant` hash digests: + * + * ``` + * bytes32 public constant MY_ROLE = keccak256("MY_ROLE"); + * ``` + * + * Roles can be used to represent a set of permissions. To restrict access to a + * function call, use {hasRole}: + * + * ``` + * function foo() public { + * require(hasRole(MY_ROLE, msg.sender)); + * ... + * } + * ``` + * + * Roles can be granted and revoked dynamically via the {grantRole} and + * {revokeRole} functions. Each role has an associated admin role, and only + * accounts that have a role's admin role can call {grantRole} and {revokeRole}. + * + * By default, the admin role for all roles is `DEFAULT_ADMIN_ROLE`, which means + * that only accounts with this role will be able to grant or revoke other + * roles. More complex role relationships can be created by using + * {_setRoleAdmin}. + * + * WARNING: The `DEFAULT_ADMIN_ROLE` is also its own admin: it has permission to + * grant and revoke this role. Extra precautions should be taken to secure + * accounts that have been granted it. + */ +abstract contract AccessControl is Context { + using EnumerableSet for EnumerableSet.AddressSet; + using Address for address; + + struct RoleData { + EnumerableSet.AddressSet members; + bytes32 adminRole; + } + + mapping (bytes32 => RoleData) private _roles; + + bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00; + + /** + * @dev Emitted when `newAdminRole` is set as ``role``'s admin role, replacing `previousAdminRole` + * + * `DEFAULT_ADMIN_ROLE` is the starting admin for all roles, despite + * {RoleAdminChanged} not being emitted signaling this. + * + * _Available since v3.1._ + */ + event RoleAdminChanged(bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole); + + /** + * @dev Emitted when `account` is granted `role`. + * + * `sender` is the account that originated the contract call, an admin role + * bearer except when using {_setupRole}. + */ + event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender); + + /** + * @dev Emitted when `account` is revoked `role`. + * + * `sender` is the account that originated the contract call: + * - if using `revokeRole`, it is the admin role bearer + * - if using `renounceRole`, it is the role bearer (i.e. `account`) + */ + event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender); + + /** + * @dev Returns `true` if `account` has been granted `role`. + */ + function hasRole(bytes32 role, address account) public view returns (bool) { + return _roles[role].members.contains(account); + } + + /** + * @dev Returns the number of accounts that have `role`. Can be used + * together with {getRoleMember} to enumerate all bearers of a role. + */ + function getRoleMemberCount(bytes32 role) public view returns (uint256) { + return _roles[role].members.length(); + } + + /** + * @dev Returns one of the accounts that have `role`. `index` must be a + * value between 0 and {getRoleMemberCount}, non-inclusive. + * + * Role bearers are not sorted in any particular way, and their ordering may + * change at any point. + * + * WARNING: When using {getRoleMember} and {getRoleMemberCount}, make sure + * you perform all queries on the same block. See the following + * https://forum.openzeppelin.com/t/iterating-over-elements-on-enumerableset-in-openzeppelin-contracts/2296[forum post] + * for more information. + */ + function getRoleMember(bytes32 role, uint256 index) public view returns (address) { + return _roles[role].members.at(index); + } + + /** + * @dev Returns the admin role that controls `role`. See {grantRole} and + * {revokeRole}. + * + * To change a role's admin, use {_setRoleAdmin}. + */ + function getRoleAdmin(bytes32 role) public view returns (bytes32) { + return _roles[role].adminRole; + } + + /** + * @dev Grants `role` to `account`. + * + * If `account` had not been already granted `role`, emits a {RoleGranted} + * event. + * + * Requirements: + * + * - the caller must have ``role``'s admin role. + */ + function grantRole(bytes32 role, address account) public virtual { + require(hasRole(_roles[role].adminRole, _msgSender()), "AccessControl: sender must be an admin to grant"); + + _grantRole(role, account); + } + + /** + * @dev Revokes `role` from `account`. + * + * If `account` had been granted `role`, emits a {RoleRevoked} event. + * + * Requirements: + * + * - the caller must have ``role``'s admin role. + */ + function revokeRole(bytes32 role, address account) public virtual { + require(hasRole(_roles[role].adminRole, _msgSender()), "AccessControl: sender must be an admin to revoke"); + + _revokeRole(role, account); + } + + /** + * @dev Revokes `role` from the calling account. + * + * Roles are often managed via {grantRole} and {revokeRole}: this function's + * purpose is to provide a mechanism for accounts to lose their privileges + * if they are compromised (such as when a trusted device is misplaced). + * + * If the calling account had been granted `role`, emits a {RoleRevoked} + * event. + * + * Requirements: + * + * - the caller must be `account`. + */ + function renounceRole(bytes32 role, address account) public virtual { + require(account == _msgSender(), "AccessControl: can only renounce roles for self"); + + _revokeRole(role, account); + } + + /** + * @dev Grants `role` to `account`. + * + * If `account` had not been already granted `role`, emits a {RoleGranted} + * event. Note that unlike {grantRole}, this function doesn't perform any + * checks on the calling account. + * + * [WARNING] + * ==== + * This function should only be called from the constructor when setting + * up the initial roles for the system. + * + * Using this function in any other way is effectively circumventing the admin + * system imposed by {AccessControl}. + * ==== + */ + function _setupRole(bytes32 role, address account) internal virtual { + _grantRole(role, account); + } + + /** + * @dev Sets `adminRole` as ``role``'s admin role. + * + * Emits a {RoleAdminChanged} event. + */ + function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual { + emit RoleAdminChanged(role, _roles[role].adminRole, adminRole); + _roles[role].adminRole = adminRole; + } + + function _grantRole(bytes32 role, address account) private { + if (_roles[role].members.add(account)) { + emit RoleGranted(role, account, _msgSender()); + } + } + + function _revokeRole(bytes32 role, address account) private { + if (_roles[role].members.remove(account)) { + emit RoleRevoked(role, account, _msgSender()); + } + } +} diff --git a/src/node_modules/@openzeppelin/contracts/access/Ownable.sol b/src/node_modules/@openzeppelin/contracts/access/Ownable.sol new file mode 100644 index 00000000..353e79c2 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/access/Ownable.sol @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../GSN/Context.sol"; +/** + * @dev Contract module which provides a basic access control mechanism, where + * there is an account (an owner) that can be granted exclusive access to + * specific functions. + * + * By default, the owner account will be the one that deploys the contract. This + * can later be changed with {transferOwnership}. + * + * This module is used through inheritance. It will make available the modifier + * `onlyOwner`, which can be applied to your functions to restrict their use to + * the owner. + */ +contract Ownable is Context { + address private _owner; + + event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); + + /** + * @dev Initializes the contract setting the deployer as the initial owner. + */ + constructor () internal { + address msgSender = _msgSender(); + _owner = msgSender; + emit OwnershipTransferred(address(0), msgSender); + } + + /** + * @dev Returns the address of the current owner. + */ + function owner() public view returns (address) { + return _owner; + } + + /** + * @dev Throws if called by any account other than the owner. + */ + modifier onlyOwner() { + require(_owner == _msgSender(), "Ownable: caller is not the owner"); + _; + } + + /** + * @dev Leaves the contract without owner. It will not be possible to call + * `onlyOwner` functions anymore. Can only be called by the current owner. + * + * NOTE: Renouncing ownership will leave the contract without an owner, + * thereby removing any functionality that is only available to the owner. + */ + function renounceOwnership() public virtual onlyOwner { + emit OwnershipTransferred(_owner, address(0)); + _owner = address(0); + } + + /** + * @dev Transfers ownership of the contract to a new account (`newOwner`). + * Can only be called by the current owner. + */ + function transferOwnership(address newOwner) public virtual onlyOwner { + require(newOwner != address(0), "Ownable: new owner is the zero address"); + emit OwnershipTransferred(_owner, newOwner); + _owner = newOwner; + } +} diff --git a/src/node_modules/@openzeppelin/contracts/access/README.adoc b/src/node_modules/@openzeppelin/contracts/access/README.adoc new file mode 100644 index 00000000..42e65846 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/access/README.adoc @@ -0,0 +1,12 @@ += Access + +[.readme-notice] +NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/access + +Contract modules for authorization and access control mechanisms. + +== Contracts + +{{Ownable}} + +{{AccessControl}} diff --git a/src/node_modules/@openzeppelin/contracts/cryptography/ECDSA.sol b/src/node_modules/@openzeppelin/contracts/cryptography/ECDSA.sol new file mode 100644 index 00000000..5a85ed96 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/cryptography/ECDSA.sol @@ -0,0 +1,83 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev Elliptic Curve Digital Signature Algorithm (ECDSA) operations. + * + * These functions can be used to verify that a message was signed by the holder + * of the private keys of a given address. + */ +library ECDSA { + /** + * @dev Returns the address that signed a hashed message (`hash`) with + * `signature`. This address can then be used for verification purposes. + * + * The `ecrecover` EVM opcode allows for malleable (non-unique) signatures: + * this function rejects them by requiring the `s` value to be in the lower + * half order, and the `v` value to be either 27 or 28. + * + * IMPORTANT: `hash` _must_ be the result of a hash operation for the + * verification to be secure: it is possible to craft signatures that + * recover to arbitrary addresses for non-hashed data. A safe way to ensure + * this is by receiving a hash of the original message (which may otherwise + * be too long), and then calling {toEthSignedMessageHash} on it. + */ + function recover(bytes32 hash, bytes memory signature) internal pure returns (address) { + // Check the signature length + if (signature.length != 65) { + revert("ECDSA: invalid signature length"); + } + + // Divide the signature in r, s and v variables + bytes32 r; + bytes32 s; + uint8 v; + + // ecrecover takes the signature parameters, and the only way to get them + // currently is to use assembly. + // solhint-disable-next-line no-inline-assembly + assembly { + r := mload(add(signature, 0x20)) + s := mload(add(signature, 0x40)) + v := byte(0, mload(add(signature, 0x60))) + } + + // EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature + // unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines + // the valid range for s in (281): 0 < s < secp256k1n ÷ 2 + 1, and for v in (282): v ∈ {27, 28}. Most + // signatures from current libraries generate a unique signature with an s-value in the lower half order. + // + // If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value + // with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or + // vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept + // these malleable signatures as well. + if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) { + revert("ECDSA: invalid signature 's' value"); + } + + if (v != 27 && v != 28) { + revert("ECDSA: invalid signature 'v' value"); + } + + // If the signature is valid (and not malleable), return the signer address + address signer = ecrecover(hash, v, r, s); + require(signer != address(0), "ECDSA: invalid signature"); + + return signer; + } + + /** + * @dev Returns an Ethereum Signed Message, created from a `hash`. This + * replicates the behavior of the + * https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign[`eth_sign`] + * JSON-RPC method. + * + * See {recover}. + */ + function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) { + // 32 is the length in bytes of hash, + // enforced by the type signature above + return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash)); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/cryptography/MerkleProof.sol b/src/node_modules/@openzeppelin/contracts/cryptography/MerkleProof.sol new file mode 100644 index 00000000..8a5fc90f --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/cryptography/MerkleProof.sol @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev These functions deal with verification of Merkle trees (hash trees), + */ +library MerkleProof { + /** + * @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree + * defined by `root`. For this, a `proof` must be provided, containing + * sibling hashes on the branch from the leaf to the root of the tree. Each + * pair of leaves and each pair of pre-images are assumed to be sorted. + */ + function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf) internal pure returns (bool) { + bytes32 computedHash = leaf; + + for (uint256 i = 0; i < proof.length; i++) { + bytes32 proofElement = proof[i]; + + if (computedHash <= proofElement) { + // Hash(current computed hash + current element of the proof) + computedHash = keccak256(abi.encodePacked(computedHash, proofElement)); + } else { + // Hash(current element of the proof + current computed hash) + computedHash = keccak256(abi.encodePacked(proofElement, computedHash)); + } + } + + // Check if the computed hash (root) is equal to the provided root + return computedHash == root; + } +} diff --git a/src/node_modules/@openzeppelin/contracts/cryptography/README.adoc b/src/node_modules/@openzeppelin/contracts/cryptography/README.adoc new file mode 100644 index 00000000..996e9dad --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/cryptography/README.adoc @@ -0,0 +1,12 @@ += Cryptography + +[.readme-notice] +NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/cryptography + +This collection of libraries provides simple and safe ways to use different cryptographic primitives. + +== Libraries + +{{ECDSA}} + +{{MerkleProof}} diff --git a/src/node_modules/@openzeppelin/contracts/introspection/ERC165.sol b/src/node_modules/@openzeppelin/contracts/introspection/ERC165.sol new file mode 100644 index 00000000..8f34a88a --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/introspection/ERC165.sol @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./IERC165.sol"; + +/** + * @dev Implementation of the {IERC165} interface. + * + * Contracts may inherit from this and call {_registerInterface} to declare + * their support of an interface. + */ +contract ERC165 is IERC165 { + /* + * bytes4(keccak256('supportsInterface(bytes4)')) == 0x01ffc9a7 + */ + bytes4 private constant _INTERFACE_ID_ERC165 = 0x01ffc9a7; + + /** + * @dev Mapping of interface ids to whether or not it's supported. + */ + mapping(bytes4 => bool) private _supportedInterfaces; + + constructor () internal { + // Derived contracts need only register support for their own interfaces, + // we register support for ERC165 itself here + _registerInterface(_INTERFACE_ID_ERC165); + } + + /** + * @dev See {IERC165-supportsInterface}. + * + * Time complexity O(1), guaranteed to always use less than 30 000 gas. + */ + function supportsInterface(bytes4 interfaceId) public view override returns (bool) { + return _supportedInterfaces[interfaceId]; + } + + /** + * @dev Registers the contract as an implementer of the interface defined by + * `interfaceId`. Support of the actual ERC165 interface is automatic and + * registering its interface id is not required. + * + * See {IERC165-supportsInterface}. + * + * Requirements: + * + * - `interfaceId` cannot be the ERC165 invalid interface (`0xffffffff`). + */ + function _registerInterface(bytes4 interfaceId) internal virtual { + require(interfaceId != 0xffffffff, "ERC165: invalid interface id"); + _supportedInterfaces[interfaceId] = true; + } +} diff --git a/src/node_modules/@openzeppelin/contracts/introspection/ERC165Checker.sol b/src/node_modules/@openzeppelin/contracts/introspection/ERC165Checker.sol new file mode 100644 index 00000000..5469e020 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/introspection/ERC165Checker.sol @@ -0,0 +1,106 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.2; + +/** + * @dev Library used to query support of an interface declared via {IERC165}. + * + * Note that these functions return the actual result of the query: they do not + * `revert` if an interface is not supported. It is up to the caller to decide + * what to do in these cases. + */ +library ERC165Checker { + // As per the EIP-165 spec, no interface should ever match 0xffffffff + bytes4 private constant _INTERFACE_ID_INVALID = 0xffffffff; + + /* + * bytes4(keccak256('supportsInterface(bytes4)')) == 0x01ffc9a7 + */ + bytes4 private constant _INTERFACE_ID_ERC165 = 0x01ffc9a7; + + /** + * @dev Returns true if `account` supports the {IERC165} interface, + */ + function supportsERC165(address account) internal view returns (bool) { + // Any contract that implements ERC165 must explicitly indicate support of + // InterfaceId_ERC165 and explicitly indicate non-support of InterfaceId_Invalid + return _supportsERC165Interface(account, _INTERFACE_ID_ERC165) && + !_supportsERC165Interface(account, _INTERFACE_ID_INVALID); + } + + /** + * @dev Returns true if `account` supports the interface defined by + * `interfaceId`. Support for {IERC165} itself is queried automatically. + * + * See {IERC165-supportsInterface}. + */ + function supportsInterface(address account, bytes4 interfaceId) internal view returns (bool) { + // query support of both ERC165 as per the spec and support of _interfaceId + return supportsERC165(account) && + _supportsERC165Interface(account, interfaceId); + } + + /** + * @dev Returns true if `account` supports all the interfaces defined in + * `interfaceIds`. Support for {IERC165} itself is queried automatically. + * + * Batch-querying can lead to gas savings by skipping repeated checks for + * {IERC165} support. + * + * See {IERC165-supportsInterface}. + */ + function supportsAllInterfaces(address account, bytes4[] memory interfaceIds) internal view returns (bool) { + // query support of ERC165 itself + if (!supportsERC165(account)) { + return false; + } + + // query support of each interface in _interfaceIds + for (uint256 i = 0; i < interfaceIds.length; i++) { + if (!_supportsERC165Interface(account, interfaceIds[i])) { + return false; + } + } + + // all interfaces supported + return true; + } + + /** + * @notice Query if a contract implements an interface, does not check ERC165 support + * @param account The address of the contract to query for support of an interface + * @param interfaceId The interface identifier, as specified in ERC-165 + * @return true if the contract at account indicates support of the interface with + * identifier interfaceId, false otherwise + * @dev Assumes that account contains a contract that supports ERC165, otherwise + * the behavior of this method is undefined. This precondition can be checked + * with {supportsERC165}. + * Interface identification is specified in ERC-165. + */ + function _supportsERC165Interface(address account, bytes4 interfaceId) private view returns (bool) { + // success determines whether the staticcall succeeded and result determines + // whether the contract at account indicates support of _interfaceId + (bool success, bool result) = _callERC165SupportsInterface(account, interfaceId); + + return (success && result); + } + + /** + * @notice Calls the function with selector 0x01ffc9a7 (ERC165) and suppresses throw + * @param account The address of the contract to query for support of an interface + * @param interfaceId The interface identifier, as specified in ERC-165 + * @return success true if the STATICCALL succeeded, false otherwise + * @return result true if the STATICCALL succeeded and the contract at account + * indicates support of the interface with identifier interfaceId, false otherwise + */ + function _callERC165SupportsInterface(address account, bytes4 interfaceId) + private + view + returns (bool, bool) + { + bytes memory encodedParams = abi.encodeWithSelector(_INTERFACE_ID_ERC165, interfaceId); + (bool success, bytes memory result) = account.staticcall{ gas: 30000 }(encodedParams); + if (result.length < 32) return (false, false); + return (success, abi.decode(result, (bool))); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/introspection/ERC1820Implementer.sol b/src/node_modules/@openzeppelin/contracts/introspection/ERC1820Implementer.sol new file mode 100644 index 00000000..df740a7a --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/introspection/ERC1820Implementer.sol @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./IERC1820Implementer.sol"; + +/** + * @dev Implementation of the {IERC1820Implementer} interface. + * + * Contracts may inherit from this and call {_registerInterfaceForAddress} to + * declare their willingness to be implementers. + * {IERC1820Registry-setInterfaceImplementer} should then be called for the + * registration to be complete. + */ +contract ERC1820Implementer is IERC1820Implementer { + bytes32 constant private _ERC1820_ACCEPT_MAGIC = keccak256(abi.encodePacked("ERC1820_ACCEPT_MAGIC")); + + mapping(bytes32 => mapping(address => bool)) private _supportedInterfaces; + + /** + * See {IERC1820Implementer-canImplementInterfaceForAddress}. + */ + function canImplementInterfaceForAddress(bytes32 interfaceHash, address account) public view override returns (bytes32) { + return _supportedInterfaces[interfaceHash][account] ? _ERC1820_ACCEPT_MAGIC : bytes32(0x00); + } + + /** + * @dev Declares the contract as willing to be an implementer of + * `interfaceHash` for `account`. + * + * See {IERC1820Registry-setInterfaceImplementer} and + * {IERC1820Registry-interfaceHash}. + */ + function _registerInterfaceForAddress(bytes32 interfaceHash, address account) internal virtual { + _supportedInterfaces[interfaceHash][account] = true; + } +} diff --git a/src/node_modules/@openzeppelin/contracts/introspection/IERC165.sol b/src/node_modules/@openzeppelin/contracts/introspection/IERC165.sol new file mode 100644 index 00000000..425458d6 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/introspection/IERC165.sol @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev Interface of the ERC165 standard, as defined in the + * https://eips.ethereum.org/EIPS/eip-165[EIP]. + * + * Implementers can declare support of contract interfaces, which can then be + * queried by others ({ERC165Checker}). + * + * For an implementation, see {ERC165}. + */ +interface IERC165 { + /** + * @dev Returns true if this contract implements the interface defined by + * `interfaceId`. See the corresponding + * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section] + * to learn more about how these ids are created. + * + * This function call must use less than 30 000 gas. + */ + function supportsInterface(bytes4 interfaceId) external view returns (bool); +} diff --git a/src/node_modules/@openzeppelin/contracts/introspection/IERC1820Implementer.sol b/src/node_modules/@openzeppelin/contracts/introspection/IERC1820Implementer.sol new file mode 100644 index 00000000..1eb89e78 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/introspection/IERC1820Implementer.sol @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev Interface for an ERC1820 implementer, as defined in the + * https://eips.ethereum.org/EIPS/eip-1820#interface-implementation-erc1820implementerinterface[EIP]. + * Used by contracts that will be registered as implementers in the + * {IERC1820Registry}. + */ +interface IERC1820Implementer { + /** + * @dev Returns a special value (`ERC1820_ACCEPT_MAGIC`) if this contract + * implements `interfaceHash` for `account`. + * + * See {IERC1820Registry-setInterfaceImplementer}. + */ + function canImplementInterfaceForAddress(bytes32 interfaceHash, address account) external view returns (bytes32); +} diff --git a/src/node_modules/@openzeppelin/contracts/introspection/IERC1820Registry.sol b/src/node_modules/@openzeppelin/contracts/introspection/IERC1820Registry.sol new file mode 100644 index 00000000..be92e465 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/introspection/IERC1820Registry.sol @@ -0,0 +1,111 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev Interface of the global ERC1820 Registry, as defined in the + * https://eips.ethereum.org/EIPS/eip-1820[EIP]. Accounts may register + * implementers for interfaces in this registry, as well as query support. + * + * Implementers may be shared by multiple accounts, and can also implement more + * than a single interface for each account. Contracts can implement interfaces + * for themselves, but externally-owned accounts (EOA) must delegate this to a + * contract. + * + * {IERC165} interfaces can also be queried via the registry. + * + * For an in-depth explanation and source code analysis, see the EIP text. + */ +interface IERC1820Registry { + /** + * @dev Sets `newManager` as the manager for `account`. A manager of an + * account is able to set interface implementers for it. + * + * By default, each account is its own manager. Passing a value of `0x0` in + * `newManager` will reset the manager to this initial state. + * + * Emits a {ManagerChanged} event. + * + * Requirements: + * + * - the caller must be the current manager for `account`. + */ + function setManager(address account, address newManager) external; + + /** + * @dev Returns the manager for `account`. + * + * See {setManager}. + */ + function getManager(address account) external view returns (address); + + /** + * @dev Sets the `implementer` contract as ``account``'s implementer for + * `interfaceHash`. + * + * `account` being the zero address is an alias for the caller's address. + * The zero address can also be used in `implementer` to remove an old one. + * + * See {interfaceHash} to learn how these are created. + * + * Emits an {InterfaceImplementerSet} event. + * + * Requirements: + * + * - the caller must be the current manager for `account`. + * - `interfaceHash` must not be an {IERC165} interface id (i.e. it must not + * end in 28 zeroes). + * - `implementer` must implement {IERC1820Implementer} and return true when + * queried for support, unless `implementer` is the caller. See + * {IERC1820Implementer-canImplementInterfaceForAddress}. + */ + function setInterfaceImplementer(address account, bytes32 interfaceHash, address implementer) external; + + /** + * @dev Returns the implementer of `interfaceHash` for `account`. If no such + * implementer is registered, returns the zero address. + * + * If `interfaceHash` is an {IERC165} interface id (i.e. it ends with 28 + * zeroes), `account` will be queried for support of it. + * + * `account` being the zero address is an alias for the caller's address. + */ + function getInterfaceImplementer(address account, bytes32 interfaceHash) external view returns (address); + + /** + * @dev Returns the interface hash for an `interfaceName`, as defined in the + * corresponding + * https://eips.ethereum.org/EIPS/eip-1820#interface-name[section of the EIP]. + */ + function interfaceHash(string calldata interfaceName) external pure returns (bytes32); + + /** + * @notice Updates the cache with whether the contract implements an ERC165 interface or not. + * @param account Address of the contract for which to update the cache. + * @param interfaceId ERC165 interface for which to update the cache. + */ + function updateERC165Cache(address account, bytes4 interfaceId) external; + + /** + * @notice Checks whether a contract implements an ERC165 interface or not. + * If the result is not cached a direct lookup on the contract address is performed. + * If the result is not cached or the cached value is out-of-date, the cache MUST be updated manually by calling + * {updateERC165Cache} with the contract address. + * @param account Address of the contract to check. + * @param interfaceId ERC165 interface to check. + * @return True if `account` implements `interfaceId`, false otherwise. + */ + function implementsERC165Interface(address account, bytes4 interfaceId) external view returns (bool); + + /** + * @notice Checks whether a contract implements an ERC165 interface or not without using nor updating the cache. + * @param account Address of the contract to check. + * @param interfaceId ERC165 interface to check. + * @return True if `account` implements `interfaceId`, false otherwise. + */ + function implementsERC165InterfaceNoCache(address account, bytes4 interfaceId) external view returns (bool); + + event InterfaceImplementerSet(address indexed account, bytes32 indexed interfaceHash, address indexed implementer); + + event ManagerChanged(address indexed account, address indexed newManager); +} diff --git a/src/node_modules/@openzeppelin/contracts/introspection/README.adoc b/src/node_modules/@openzeppelin/contracts/introspection/README.adoc new file mode 100644 index 00000000..38bd7828 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/introspection/README.adoc @@ -0,0 +1,31 @@ += Introspection + +[.readme-notice] +NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/introspection + +This set of interfaces and contracts deal with https://en.wikipedia.org/wiki/Type_introspection[type introspection] of contracts, that is, examining which functions can be called on them. This is usually referred to as a contract's _interface_. + +Ethereum contracts have no native concept of an interface, so applications must usually simply trust they are not making an incorrect call. For trusted setups this is a non-issue, but often unknown and untrusted third-party addresses need to be interacted with. There may even not be any direct calls to them! (e.g. `ERC20` tokens may be sent to a contract that lacks a way to transfer them out of it, locking them forever). In these cases, a contract _declaring_ its interface can be very helpful in preventing errors. + +There are two main ways to approach this. + +* Locally, where a contract implements `IERC165` and declares an interface, and a second one queries it directly via `ERC165Checker`. +* Globally, where a global and unique registry (`IERC1820Registry`) is used to register implementers of a certain interface (`IERC1820Implementer`). It is then the registry that is queried, which allows for more complex setups, like contracts implementing interfaces for externally-owned accounts. + +Note that, in all cases, accounts simply _declare_ their interfaces, but they are not required to actually implement them. This mechanism can therefore be used to both prevent errors and allow for complex interactions (see `ERC777`), but it must not be relied on for security. + +== Local + +{{IERC165}} + +{{ERC165}} + +{{ERC165Checker}} + +== Global + +{{IERC1820Registry}} + +{{IERC1820Implementer}} + +{{ERC1820Implementer}} diff --git a/src/node_modules/@openzeppelin/contracts/math/Math.sol b/src/node_modules/@openzeppelin/contracts/math/Math.sol new file mode 100644 index 00000000..ec4ac227 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/math/Math.sol @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev Standard math utilities missing in the Solidity language. + */ +library Math { + /** + * @dev Returns the largest of two numbers. + */ + function max(uint256 a, uint256 b) internal pure returns (uint256) { + return a >= b ? a : b; + } + + /** + * @dev Returns the smallest of two numbers. + */ + function min(uint256 a, uint256 b) internal pure returns (uint256) { + return a < b ? a : b; + } + + /** + * @dev Returns the average of two numbers. The result is rounded towards + * zero. + */ + function average(uint256 a, uint256 b) internal pure returns (uint256) { + // (a + b) / 2 can overflow, so we distribute + return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/math/README.adoc b/src/node_modules/@openzeppelin/contracts/math/README.adoc new file mode 100644 index 00000000..b03d441b --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/math/README.adoc @@ -0,0 +1,14 @@ += Math + +[.readme-notice] +NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/math + +These are math-related utilities. + +== Libraries + +{{SafeMath}} + +{{SignedSafeMath}} + +{{Math}} diff --git a/src/node_modules/@openzeppelin/contracts/math/SafeMath.sol b/src/node_modules/@openzeppelin/contracts/math/SafeMath.sol new file mode 100644 index 00000000..80490eff --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/math/SafeMath.sol @@ -0,0 +1,159 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev Wrappers over Solidity's arithmetic operations with added overflow + * checks. + * + * Arithmetic operations in Solidity wrap on overflow. This can easily result + * in bugs, because programmers usually assume that an overflow raises an + * error, which is the standard behavior in high level programming languages. + * `SafeMath` restores this intuition by reverting the transaction when an + * operation overflows. + * + * Using this library instead of the unchecked operations eliminates an entire + * class of bugs, so it's recommended to use it always. + */ +library SafeMath { + /** + * @dev Returns the addition of two unsigned integers, reverting on + * overflow. + * + * Counterpart to Solidity's `+` operator. + * + * Requirements: + * + * - Addition cannot overflow. + */ + function add(uint256 a, uint256 b) internal pure returns (uint256) { + uint256 c = a + b; + require(c >= a, "SafeMath: addition overflow"); + + return c; + } + + /** + * @dev Returns the subtraction of two unsigned integers, reverting on + * overflow (when the result is negative). + * + * Counterpart to Solidity's `-` operator. + * + * Requirements: + * + * - Subtraction cannot overflow. + */ + function sub(uint256 a, uint256 b) internal pure returns (uint256) { + return sub(a, b, "SafeMath: subtraction overflow"); + } + + /** + * @dev Returns the subtraction of two unsigned integers, reverting with custom message on + * overflow (when the result is negative). + * + * Counterpart to Solidity's `-` operator. + * + * Requirements: + * + * - Subtraction cannot overflow. + */ + function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { + require(b <= a, errorMessage); + uint256 c = a - b; + + return c; + } + + /** + * @dev Returns the multiplication of two unsigned integers, reverting on + * overflow. + * + * Counterpart to Solidity's `*` operator. + * + * Requirements: + * + * - Multiplication cannot overflow. + */ + function mul(uint256 a, uint256 b) internal pure returns (uint256) { + // Gas optimization: this is cheaper than requiring 'a' not being zero, but the + // benefit is lost if 'b' is also tested. + // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 + if (a == 0) { + return 0; + } + + uint256 c = a * b; + require(c / a == b, "SafeMath: multiplication overflow"); + + return c; + } + + /** + * @dev Returns the integer division of two unsigned integers. Reverts on + * division by zero. The result is rounded towards zero. + * + * Counterpart to Solidity's `/` operator. Note: this function uses a + * `revert` opcode (which leaves remaining gas untouched) while Solidity + * uses an invalid opcode to revert (consuming all remaining gas). + * + * Requirements: + * + * - The divisor cannot be zero. + */ + function div(uint256 a, uint256 b) internal pure returns (uint256) { + return div(a, b, "SafeMath: division by zero"); + } + + /** + * @dev Returns the integer division of two unsigned integers. Reverts with custom message on + * division by zero. The result is rounded towards zero. + * + * Counterpart to Solidity's `/` operator. Note: this function uses a + * `revert` opcode (which leaves remaining gas untouched) while Solidity + * uses an invalid opcode to revert (consuming all remaining gas). + * + * Requirements: + * + * - The divisor cannot be zero. + */ + function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { + require(b > 0, errorMessage); + uint256 c = a / b; + // assert(a == b * c + a % b); // There is no case in which this doesn't hold + + return c; + } + + /** + * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), + * Reverts when dividing by zero. + * + * Counterpart to Solidity's `%` operator. This function uses a `revert` + * opcode (which leaves remaining gas untouched) while Solidity uses an + * invalid opcode to revert (consuming all remaining gas). + * + * Requirements: + * + * - The divisor cannot be zero. + */ + function mod(uint256 a, uint256 b) internal pure returns (uint256) { + return mod(a, b, "SafeMath: modulo by zero"); + } + + /** + * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), + * Reverts with custom message when dividing by zero. + * + * Counterpart to Solidity's `%` operator. This function uses a `revert` + * opcode (which leaves remaining gas untouched) while Solidity uses an + * invalid opcode to revert (consuming all remaining gas). + * + * Requirements: + * + * - The divisor cannot be zero. + */ + function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { + require(b != 0, errorMessage); + return a % b; + } +} diff --git a/src/node_modules/@openzeppelin/contracts/math/SignedSafeMath.sol b/src/node_modules/@openzeppelin/contracts/math/SignedSafeMath.sol new file mode 100644 index 00000000..ab87c27c --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/math/SignedSafeMath.sol @@ -0,0 +1,92 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @title SignedSafeMath + * @dev Signed math operations with safety checks that revert on error. + */ +library SignedSafeMath { + int256 constant private _INT256_MIN = -2**255; + + /** + * @dev Returns the multiplication of two signed integers, reverting on + * overflow. + * + * Counterpart to Solidity's `*` operator. + * + * Requirements: + * + * - Multiplication cannot overflow. + */ + function mul(int256 a, int256 b) internal pure returns (int256) { + // Gas optimization: this is cheaper than requiring 'a' not being zero, but the + // benefit is lost if 'b' is also tested. + // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 + if (a == 0) { + return 0; + } + + require(!(a == -1 && b == _INT256_MIN), "SignedSafeMath: multiplication overflow"); + + int256 c = a * b; + require(c / a == b, "SignedSafeMath: multiplication overflow"); + + return c; + } + + /** + * @dev Returns the integer division of two signed integers. Reverts on + * division by zero. The result is rounded towards zero. + * + * Counterpart to Solidity's `/` operator. Note: this function uses a + * `revert` opcode (which leaves remaining gas untouched) while Solidity + * uses an invalid opcode to revert (consuming all remaining gas). + * + * Requirements: + * + * - The divisor cannot be zero. + */ + function div(int256 a, int256 b) internal pure returns (int256) { + require(b != 0, "SignedSafeMath: division by zero"); + require(!(b == -1 && a == _INT256_MIN), "SignedSafeMath: division overflow"); + + int256 c = a / b; + + return c; + } + + /** + * @dev Returns the subtraction of two signed integers, reverting on + * overflow. + * + * Counterpart to Solidity's `-` operator. + * + * Requirements: + * + * - Subtraction cannot overflow. + */ + function sub(int256 a, int256 b) internal pure returns (int256) { + int256 c = a - b; + require((b >= 0 && c <= a) || (b < 0 && c > a), "SignedSafeMath: subtraction overflow"); + + return c; + } + + /** + * @dev Returns the addition of two signed integers, reverting on + * overflow. + * + * Counterpart to Solidity's `+` operator. + * + * Requirements: + * + * - Addition cannot overflow. + */ + function add(int256 a, int256 b) internal pure returns (int256) { + int256 c = a + b; + require((b >= 0 && c >= a) || (b < 0 && c < a), "SignedSafeMath: addition overflow"); + + return c; + } +} diff --git a/src/node_modules/@openzeppelin/contracts/package.json b/src/node_modules/@openzeppelin/contracts/package.json new file mode 100644 index 00000000..3a298aec --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/package.json @@ -0,0 +1,33 @@ +{ + "name": "@openzeppelin/contracts", + "version": "3.2.0", + "description": "Secure Smart Contract library for Solidity", + "files": [ + "**/*.sol", + "/build/contracts/*.json", + "!/mocks", + "!/examples" + ], + "scripts": { + "prepare": "bash ../scripts/prepare-contracts-package.sh", + "prepare-docs": "cd ..; npm run prepare-docs" + }, + "repository": { + "type": "git", + "url": "https://github.com/OpenZeppelin/openzeppelin-contracts.git" + }, + "keywords": [ + "solidity", + "ethereum", + "smart", + "contracts", + "security", + "zeppelin" + ], + "author": "OpenZeppelin Community ", + "license": "MIT", + "bugs": { + "url": "https://github.com/OpenZeppelin/openzeppelin-contracts/issues" + }, + "homepage": "https://openzeppelin.com/contracts/" +} diff --git a/src/node_modules/@openzeppelin/contracts/payment/PaymentSplitter.sol b/src/node_modules/@openzeppelin/contracts/payment/PaymentSplitter.sol new file mode 100644 index 00000000..cba96218 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/payment/PaymentSplitter.sol @@ -0,0 +1,134 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../GSN/Context.sol"; +import "../math/SafeMath.sol"; + +/** + * @title PaymentSplitter + * @dev This contract allows to split Ether payments among a group of accounts. The sender does not need to be aware + * that the Ether will be split in this way, since it is handled transparently by the contract. + * + * The split can be in equal parts or in any other arbitrary proportion. The way this is specified is by assigning each + * account to a number of shares. Of all the Ether that this contract receives, each account will then be able to claim + * an amount proportional to the percentage of total shares they were assigned. + * + * `PaymentSplitter` follows a _pull payment_ model. This means that payments are not automatically forwarded to the + * accounts but kept in this contract, and the actual transfer is triggered as a separate step by calling the {release} + * function. + */ +contract PaymentSplitter is Context { + using SafeMath for uint256; + + event PayeeAdded(address account, uint256 shares); + event PaymentReleased(address to, uint256 amount); + event PaymentReceived(address from, uint256 amount); + + uint256 private _totalShares; + uint256 private _totalReleased; + + mapping(address => uint256) private _shares; + mapping(address => uint256) private _released; + address[] private _payees; + + /** + * @dev Creates an instance of `PaymentSplitter` where each account in `payees` is assigned the number of shares at + * the matching position in the `shares` array. + * + * All addresses in `payees` must be non-zero. Both arrays must have the same non-zero length, and there must be no + * duplicates in `payees`. + */ + constructor (address[] memory payees, uint256[] memory shares) public payable { + // solhint-disable-next-line max-line-length + require(payees.length == shares.length, "PaymentSplitter: payees and shares length mismatch"); + require(payees.length > 0, "PaymentSplitter: no payees"); + + for (uint256 i = 0; i < payees.length; i++) { + _addPayee(payees[i], shares[i]); + } + } + + /** + * @dev The Ether received will be logged with {PaymentReceived} events. Note that these events are not fully + * reliable: it's possible for a contract to receive Ether without triggering this function. This only affects the + * reliability of the events, and not the actual splitting of Ether. + * + * To learn more about this see the Solidity documentation for + * https://solidity.readthedocs.io/en/latest/contracts.html#fallback-function[fallback + * functions]. + */ + receive () external payable virtual { + emit PaymentReceived(_msgSender(), msg.value); + } + + /** + * @dev Getter for the total shares held by payees. + */ + function totalShares() public view returns (uint256) { + return _totalShares; + } + + /** + * @dev Getter for the total amount of Ether already released. + */ + function totalReleased() public view returns (uint256) { + return _totalReleased; + } + + /** + * @dev Getter for the amount of shares held by an account. + */ + function shares(address account) public view returns (uint256) { + return _shares[account]; + } + + /** + * @dev Getter for the amount of Ether already released to a payee. + */ + function released(address account) public view returns (uint256) { + return _released[account]; + } + + /** + * @dev Getter for the address of the payee number `index`. + */ + function payee(uint256 index) public view returns (address) { + return _payees[index]; + } + + /** + * @dev Triggers a transfer to `account` of the amount of Ether they are owed, according to their percentage of the + * total shares and their previous withdrawals. + */ + function release(address payable account) public virtual { + require(_shares[account] > 0, "PaymentSplitter: account has no shares"); + + uint256 totalReceived = address(this).balance.add(_totalReleased); + uint256 payment = totalReceived.mul(_shares[account]).div(_totalShares).sub(_released[account]); + + require(payment != 0, "PaymentSplitter: account is not due payment"); + + _released[account] = _released[account].add(payment); + _totalReleased = _totalReleased.add(payment); + + account.transfer(payment); + emit PaymentReleased(account, payment); + } + + /** + * @dev Add a new payee to the contract. + * @param account The address of the payee to add. + * @param shares_ The number of shares owned by the payee. + */ + function _addPayee(address account, uint256 shares_) private { + require(account != address(0), "PaymentSplitter: account is the zero address"); + require(shares_ > 0, "PaymentSplitter: shares are 0"); + require(_shares[account] == 0, "PaymentSplitter: account already has shares"); + + _payees.push(account); + _shares[account] = shares_; + _totalShares = _totalShares.add(shares_); + emit PayeeAdded(account, shares_); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/payment/PullPayment.sol b/src/node_modules/@openzeppelin/contracts/payment/PullPayment.sol new file mode 100644 index 00000000..605edce6 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/payment/PullPayment.sol @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.2; + +import "./escrow/Escrow.sol"; + +/** + * @dev Simple implementation of a + * https://consensys.github.io/smart-contract-best-practices/recommendations/#favor-pull-over-push-for-external-calls[pull-payment] + * strategy, where the paying contract doesn't interact directly with the + * receiver account, which must withdraw its payments itself. + * + * Pull-payments are often considered the best practice when it comes to sending + * Ether, security-wise. It prevents recipients from blocking execution, and + * eliminates reentrancy concerns. + * + * TIP: If you would like to learn more about reentrancy and alternative ways + * to protect against it, check out our blog post + * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul]. + * + * To use, derive from the `PullPayment` contract, and use {_asyncTransfer} + * instead of Solidity's `transfer` function. Payees can query their due + * payments with {payments}, and retrieve them with {withdrawPayments}. + */ +contract PullPayment { + Escrow private _escrow; + + constructor () internal { + _escrow = new Escrow(); + } + + /** + * @dev Withdraw accumulated payments, forwarding all gas to the recipient. + * + * Note that _any_ account can call this function, not just the `payee`. + * This means that contracts unaware of the `PullPayment` protocol can still + * receive funds this way, by having a separate account call + * {withdrawPayments}. + * + * WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities. + * Make sure you trust the recipient, or are either following the + * checks-effects-interactions pattern or using {ReentrancyGuard}. + * + * @param payee Whose payments will be withdrawn. + */ + function withdrawPayments(address payable payee) public virtual { + _escrow.withdraw(payee); + } + + /** + * @dev Returns the payments owed to an address. + * @param dest The creditor's address. + */ + function payments(address dest) public view returns (uint256) { + return _escrow.depositsOf(dest); + } + + /** + * @dev Called by the payer to store the sent amount as credit to be pulled. + * Funds sent in this way are stored in an intermediate {Escrow} contract, so + * there is no danger of them being spent before withdrawal. + * + * @param dest The destination address of the funds. + * @param amount The amount to transfer. + */ + function _asyncTransfer(address dest, uint256 amount) internal virtual { + _escrow.deposit{ value: amount }(dest); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/payment/README.adoc b/src/node_modules/@openzeppelin/contracts/payment/README.adoc new file mode 100644 index 00000000..7d6151d9 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/payment/README.adoc @@ -0,0 +1,22 @@ += Payment + +[.readme-notice] +NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/payment + +Utilities related to sending and receiving payments. Examples are {PullPayment}, which implements the best security practices when sending funds to third parties, and {PaymentSplitter} to receive incoming payments among a number of beneficiaries. + +TIP: When transferring funds to and from untrusted third parties, there is always a security risk of reentrancy. If you would like to learn more about this and ways to protect against it, check out our blog post https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul]. + +== Utilities + +{{PaymentSplitter}} + +{{PullPayment}} + +== Escrow + +{{Escrow}} + +{{ConditionalEscrow}} + +{{RefundEscrow}} diff --git a/src/node_modules/@openzeppelin/contracts/payment/escrow/ConditionalEscrow.sol b/src/node_modules/@openzeppelin/contracts/payment/escrow/ConditionalEscrow.sol new file mode 100644 index 00000000..54bf1747 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/payment/escrow/ConditionalEscrow.sol @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./Escrow.sol"; + +/** + * @title ConditionalEscrow + * @dev Base abstract escrow to only allow withdrawal if a condition is met. + * @dev Intended usage: See {Escrow}. Same usage guidelines apply here. + */ +abstract contract ConditionalEscrow is Escrow { + /** + * @dev Returns whether an address is allowed to withdraw their funds. To be + * implemented by derived contracts. + * @param payee The destination address of the funds. + */ + function withdrawalAllowed(address payee) public view virtual returns (bool); + + function withdraw(address payable payee) public virtual override { + require(withdrawalAllowed(payee), "ConditionalEscrow: payee is not allowed to withdraw"); + super.withdraw(payee); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/payment/escrow/Escrow.sol b/src/node_modules/@openzeppelin/contracts/payment/escrow/Escrow.sol new file mode 100644 index 00000000..c9fc76e9 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/payment/escrow/Escrow.sol @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../../math/SafeMath.sol"; +import "../../access/Ownable.sol"; +import "../../utils/Address.sol"; + + /** + * @title Escrow + * @dev Base escrow contract, holds funds designated for a payee until they + * withdraw them. + * + * Intended usage: This contract (and derived escrow contracts) should be a + * standalone contract, that only interacts with the contract that instantiated + * it. That way, it is guaranteed that all Ether will be handled according to + * the `Escrow` rules, and there is no need to check for payable functions or + * transfers in the inheritance tree. The contract that uses the escrow as its + * payment method should be its owner, and provide public methods redirecting + * to the escrow's deposit and withdraw. + */ +contract Escrow is Ownable { + using SafeMath for uint256; + using Address for address payable; + + event Deposited(address indexed payee, uint256 weiAmount); + event Withdrawn(address indexed payee, uint256 weiAmount); + + mapping(address => uint256) private _deposits; + + function depositsOf(address payee) public view returns (uint256) { + return _deposits[payee]; + } + + /** + * @dev Stores the sent amount as credit to be withdrawn. + * @param payee The destination address of the funds. + */ + function deposit(address payee) public virtual payable onlyOwner { + uint256 amount = msg.value; + _deposits[payee] = _deposits[payee].add(amount); + + emit Deposited(payee, amount); + } + + /** + * @dev Withdraw accumulated balance for a payee, forwarding all gas to the + * recipient. + * + * WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities. + * Make sure you trust the recipient, or are either following the + * checks-effects-interactions pattern or using {ReentrancyGuard}. + * + * @param payee The address whose funds will be withdrawn and transferred to. + */ + function withdraw(address payable payee) public virtual onlyOwner { + uint256 payment = _deposits[payee]; + + _deposits[payee] = 0; + + payee.sendValue(payment); + + emit Withdrawn(payee, payment); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/payment/escrow/RefundEscrow.sol b/src/node_modules/@openzeppelin/contracts/payment/escrow/RefundEscrow.sol new file mode 100644 index 00000000..e581e753 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/payment/escrow/RefundEscrow.sol @@ -0,0 +1,93 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./ConditionalEscrow.sol"; + +/** + * @title RefundEscrow + * @dev Escrow that holds funds for a beneficiary, deposited from multiple + * parties. + * @dev Intended usage: See {Escrow}. Same usage guidelines apply here. + * @dev The owner account (that is, the contract that instantiates this + * contract) may deposit, close the deposit period, and allow for either + * withdrawal by the beneficiary, or refunds to the depositors. All interactions + * with `RefundEscrow` will be made through the owner contract. + */ +contract RefundEscrow is ConditionalEscrow { + enum State { Active, Refunding, Closed } + + event RefundsClosed(); + event RefundsEnabled(); + + State private _state; + address payable private _beneficiary; + + /** + * @dev Constructor. + * @param beneficiary The beneficiary of the deposits. + */ + constructor (address payable beneficiary) public { + require(beneficiary != address(0), "RefundEscrow: beneficiary is the zero address"); + _beneficiary = beneficiary; + _state = State.Active; + } + + /** + * @return The current state of the escrow. + */ + function state() public view returns (State) { + return _state; + } + + /** + * @return The beneficiary of the escrow. + */ + function beneficiary() public view returns (address) { + return _beneficiary; + } + + /** + * @dev Stores funds that may later be refunded. + * @param refundee The address funds will be sent to if a refund occurs. + */ + function deposit(address refundee) public payable virtual override { + require(_state == State.Active, "RefundEscrow: can only deposit while active"); + super.deposit(refundee); + } + + /** + * @dev Allows for the beneficiary to withdraw their funds, rejecting + * further deposits. + */ + function close() public onlyOwner virtual { + require(_state == State.Active, "RefundEscrow: can only close while active"); + _state = State.Closed; + emit RefundsClosed(); + } + + /** + * @dev Allows for refunds to take place, rejecting further deposits. + */ + function enableRefunds() public onlyOwner virtual { + require(_state == State.Active, "RefundEscrow: can only enable refunds while active"); + _state = State.Refunding; + emit RefundsEnabled(); + } + + /** + * @dev Withdraws the beneficiary's funds. + */ + function beneficiaryWithdraw() public virtual { + require(_state == State.Closed, "RefundEscrow: beneficiary can only withdraw while closed"); + _beneficiary.transfer(address(this).balance); + } + + /** + * @dev Returns whether refundees can withdraw their deposits (be refunded). The overridden function receives a + * 'payee' argument, but we ignore it here since the condition is global, not per-payee. + */ + function withdrawalAllowed(address) public view override returns (bool) { + return _state == State.Refunding; + } +} diff --git a/src/node_modules/@openzeppelin/contracts/presets/ERC1155PresetMinterPauser.sol b/src/node_modules/@openzeppelin/contracts/presets/ERC1155PresetMinterPauser.sol new file mode 100644 index 00000000..078406ae --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/presets/ERC1155PresetMinterPauser.sol @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../access/AccessControl.sol"; +import "../GSN/Context.sol"; +import "../token/ERC1155/ERC1155.sol"; +import "../token/ERC1155/ERC1155Burnable.sol"; +import "../token/ERC1155/ERC1155Pausable.sol"; + +/** + * @dev {ERC1155} token, including: + * + * - ability for holders to burn (destroy) their tokens + * - a minter role that allows for token minting (creation) + * - a pauser role that allows to stop all token transfers + * + * This contract uses {AccessControl} to lock permissioned functions using the + * different roles - head to its documentation for details. + * + * The account that deploys the contract will be granted the minter and pauser + * roles, as well as the default admin role, which will let it grant both minter + * and pauser roles to other accounts. + */ +contract ERC1155PresetMinterPauser is Context, AccessControl, ERC1155Burnable, ERC1155Pausable { + bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE"); + bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE"); + + /** + * @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE`, and `PAUSER_ROLE` to the account that + * deploys the contract. + */ + constructor(string memory uri) public ERC1155(uri) { + _setupRole(DEFAULT_ADMIN_ROLE, _msgSender()); + + _setupRole(MINTER_ROLE, _msgSender()); + _setupRole(PAUSER_ROLE, _msgSender()); + } + + /** + * @dev Creates `amount` new tokens for `to`, of token type `id`. + * + * See {ERC1155-_mint}. + * + * Requirements: + * + * - the caller must have the `MINTER_ROLE`. + */ + function mint(address to, uint256 id, uint256 amount, bytes memory data) public virtual { + require(hasRole(MINTER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have minter role to mint"); + + _mint(to, id, amount, data); + } + + /** + * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] variant of {mint}. + */ + function mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) public virtual { + require(hasRole(MINTER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have minter role to mint"); + + _mintBatch(to, ids, amounts, data); + } + + /** + * @dev Pauses all token transfers. + * + * See {ERC1155Pausable} and {Pausable-_pause}. + * + * Requirements: + * + * - the caller must have the `PAUSER_ROLE`. + */ + function pause() public virtual { + require(hasRole(PAUSER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have pauser role to pause"); + _pause(); + } + + /** + * @dev Unpauses all token transfers. + * + * See {ERC1155Pausable} and {Pausable-_unpause}. + * + * Requirements: + * + * - the caller must have the `PAUSER_ROLE`. + */ + function unpause() public virtual { + require(hasRole(PAUSER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have pauser role to unpause"); + _unpause(); + } + + function _beforeTokenTransfer( + address operator, + address from, + address to, + uint256[] memory ids, + uint256[] memory amounts, + bytes memory data + ) + internal virtual override(ERC1155, ERC1155Pausable) + { + super._beforeTokenTransfer(operator, from, to, ids, amounts, data); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/presets/ERC20PresetMinterPauser.sol b/src/node_modules/@openzeppelin/contracts/presets/ERC20PresetMinterPauser.sol new file mode 100644 index 00000000..00175955 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/presets/ERC20PresetMinterPauser.sol @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../access/AccessControl.sol"; +import "../GSN/Context.sol"; +import "../token/ERC20/ERC20.sol"; +import "../token/ERC20/ERC20Burnable.sol"; +import "../token/ERC20/ERC20Pausable.sol"; + +/** + * @dev {ERC20} token, including: + * + * - ability for holders to burn (destroy) their tokens + * - a minter role that allows for token minting (creation) + * - a pauser role that allows to stop all token transfers + * + * This contract uses {AccessControl} to lock permissioned functions using the + * different roles - head to its documentation for details. + * + * The account that deploys the contract will be granted the minter and pauser + * roles, as well as the default admin role, which will let it grant both minter + * and pauser roles to other accounts. + */ +contract ERC20PresetMinterPauser is Context, AccessControl, ERC20Burnable, ERC20Pausable { + bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE"); + bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE"); + + /** + * @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the + * account that deploys the contract. + * + * See {ERC20-constructor}. + */ + constructor(string memory name, string memory symbol) public ERC20(name, symbol) { + _setupRole(DEFAULT_ADMIN_ROLE, _msgSender()); + + _setupRole(MINTER_ROLE, _msgSender()); + _setupRole(PAUSER_ROLE, _msgSender()); + } + + /** + * @dev Creates `amount` new tokens for `to`. + * + * See {ERC20-_mint}. + * + * Requirements: + * + * - the caller must have the `MINTER_ROLE`. + */ + function mint(address to, uint256 amount) public virtual { + require(hasRole(MINTER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have minter role to mint"); + _mint(to, amount); + } + + /** + * @dev Pauses all token transfers. + * + * See {ERC20Pausable} and {Pausable-_pause}. + * + * Requirements: + * + * - the caller must have the `PAUSER_ROLE`. + */ + function pause() public virtual { + require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to pause"); + _pause(); + } + + /** + * @dev Unpauses all token transfers. + * + * See {ERC20Pausable} and {Pausable-_unpause}. + * + * Requirements: + * + * - the caller must have the `PAUSER_ROLE`. + */ + function unpause() public virtual { + require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to unpause"); + _unpause(); + } + + function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override(ERC20, ERC20Pausable) { + super._beforeTokenTransfer(from, to, amount); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/presets/ERC721PresetMinterPauserAutoId.sol b/src/node_modules/@openzeppelin/contracts/presets/ERC721PresetMinterPauserAutoId.sol new file mode 100644 index 00000000..18605c7b --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/presets/ERC721PresetMinterPauserAutoId.sol @@ -0,0 +1,102 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../access/AccessControl.sol"; +import "../GSN/Context.sol"; +import "../utils/Counters.sol"; +import "../token/ERC721/ERC721.sol"; +import "../token/ERC721/ERC721Burnable.sol"; +import "../token/ERC721/ERC721Pausable.sol"; + +/** + * @dev {ERC721} token, including: + * + * - ability for holders to burn (destroy) their tokens + * - a minter role that allows for token minting (creation) + * - a pauser role that allows to stop all token transfers + * - token ID and URI autogeneration + * + * This contract uses {AccessControl} to lock permissioned functions using the + * different roles - head to its documentation for details. + * + * The account that deploys the contract will be granted the minter and pauser + * roles, as well as the default admin role, which will let it grant both minter + * and pauser roles to other accounts. + */ +contract ERC721PresetMinterPauserAutoId is Context, AccessControl, ERC721Burnable, ERC721Pausable { + using Counters for Counters.Counter; + + bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE"); + bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE"); + + Counters.Counter private _tokenIdTracker; + + /** + * @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the + * account that deploys the contract. + * + * Token URIs will be autogenerated based on `baseURI` and their token IDs. + * See {ERC721-tokenURI}. + */ + constructor(string memory name, string memory symbol, string memory baseURI) public ERC721(name, symbol) { + _setupRole(DEFAULT_ADMIN_ROLE, _msgSender()); + + _setupRole(MINTER_ROLE, _msgSender()); + _setupRole(PAUSER_ROLE, _msgSender()); + + _setBaseURI(baseURI); + } + + /** + * @dev Creates a new token for `to`. Its token ID will be automatically + * assigned (and available on the emitted {IERC721-Transfer} event), and the token + * URI autogenerated based on the base URI passed at construction. + * + * See {ERC721-_mint}. + * + * Requirements: + * + * - the caller must have the `MINTER_ROLE`. + */ + function mint(address to) public virtual { + require(hasRole(MINTER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have minter role to mint"); + + // We cannot just use balanceOf to create the new tokenId because tokens + // can be burned (destroyed), so we need a separate counter. + _mint(to, _tokenIdTracker.current()); + _tokenIdTracker.increment(); + } + + /** + * @dev Pauses all token transfers. + * + * See {ERC721Pausable} and {Pausable-_pause}. + * + * Requirements: + * + * - the caller must have the `PAUSER_ROLE`. + */ + function pause() public virtual { + require(hasRole(PAUSER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have pauser role to pause"); + _pause(); + } + + /** + * @dev Unpauses all token transfers. + * + * See {ERC721Pausable} and {Pausable-_unpause}. + * + * Requirements: + * + * - the caller must have the `PAUSER_ROLE`. + */ + function unpause() public virtual { + require(hasRole(PAUSER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have pauser role to unpause"); + _unpause(); + } + + function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal virtual override(ERC721, ERC721Pausable) { + super._beforeTokenTransfer(from, to, tokenId); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/presets/README.adoc b/src/node_modules/@openzeppelin/contracts/presets/README.adoc new file mode 100644 index 00000000..df294190 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/presets/README.adoc @@ -0,0 +1,18 @@ += Presets + +[.readme-notice] +NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/presets + +These contracts integrate different Ethereum standards (ERCs) with custom extensions and modules, showcasing common configurations that are ready to deploy **without having to write any Solidity code**. + +They can be used as-is for quick prototyping and testing, but are **also suitable for production environments**. + +TIP: Intermediate and advanced users can use these as starting points when writing their own contracts, extending them with custom functionality as they see fit. + +== Tokens + +{{ERC20PresetMinterPauser}} + +{{ERC721PresetMinterPauserAutoId}} + +{{ERC1155PresetMinterPauser}} diff --git a/src/node_modules/@openzeppelin/contracts/proxy/Initializable.sol b/src/node_modules/@openzeppelin/contracts/proxy/Initializable.sol new file mode 100644 index 00000000..75a28a1f --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/proxy/Initializable.sol @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: MIT + +pragma solidity >=0.4.24 <0.7.0; + + +/** + * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed + * behind a proxy. Since a proxied contract can't have a constructor, it's common to move constructor logic to an + * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer + * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect. + * + * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as + * possible by providing the encoded function call as the `_data` argument to {UpgradeableProxy-constructor}. + * + * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure + * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity. + */ +abstract contract Initializable { + + /** + * @dev Indicates that the contract has been initialized. + */ + bool private _initialized; + + /** + * @dev Indicates that the contract is in the process of being initialized. + */ + bool private _initializing; + + /** + * @dev Modifier to protect an initializer function from being invoked twice. + */ + modifier initializer() { + require(_initializing || _isConstructor() || !_initialized, "Initializable: contract is already initialized"); + + bool isTopLevelCall = !_initializing; + if (isTopLevelCall) { + _initializing = true; + _initialized = true; + } + + _; + + if (isTopLevelCall) { + _initializing = false; + } + } + + /// @dev Returns true if and only if the function is running in the constructor + function _isConstructor() private view returns (bool) { + // extcodesize checks the size of the code stored in an address, and + // address returns the current address. Since the code is still not + // deployed when running a constructor, any checks on its code size will + // yield zero, making it an effective way to detect if a contract is + // under construction or not. + address self = address(this); + uint256 cs; + // solhint-disable-next-line no-inline-assembly + assembly { cs := extcodesize(self) } + return cs == 0; + } +} diff --git a/src/node_modules/@openzeppelin/contracts/proxy/Proxy.sol b/src/node_modules/@openzeppelin/contracts/proxy/Proxy.sol new file mode 100644 index 00000000..6b618af7 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/proxy/Proxy.sol @@ -0,0 +1,83 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev This abstract contract provides a fallback function that delegates all calls to another contract using the EVM + * instruction `delegatecall`. We refer to the second contract as the _implementation_ behind the proxy, and it has to + * be specified by overriding the virtual {_implementation} function. + * + * Additionally, delegation to the implementation can be triggered manually through the {_fallback} function, or to a + * different contract through the {_delegate} function. + * + * The success and return data of the delegated call will be returned back to the caller of the proxy. + */ +abstract contract Proxy { + /** + * @dev Delegates the current call to `implementation`. + * + * This function does not return to its internall call site, it will return directly to the external caller. + */ + function _delegate(address implementation) internal { + // solhint-disable-next-line no-inline-assembly + assembly { + // Copy msg.data. We take full control of memory in this inline assembly + // block because it will not return to Solidity code. We overwrite the + // Solidity scratch pad at memory position 0. + calldatacopy(0, 0, calldatasize()) + + // Call the implementation. + // out and outsize are 0 because we don't know the size yet. + let result := delegatecall(gas(), implementation, 0, calldatasize(), 0, 0) + + // Copy the returned data. + returndatacopy(0, 0, returndatasize()) + + switch result + // delegatecall returns 0 on error. + case 0 { revert(0, returndatasize()) } + default { return(0, returndatasize()) } + } + } + + /** + * @dev This is a virtual function that should be overriden so it returns the address to which the fallback function + * and {_fallback} should delegate. + */ + function _implementation() internal virtual view returns (address); + + /** + * @dev Delegates the current call to the address returned by `_implementation()`. + * + * This function does not return to its internall call site, it will return directly to the external caller. + */ + function _fallback() internal { + _beforeFallback(); + _delegate(_implementation()); + } + + /** + * @dev Fallback function that delegates calls to the address returned by `_implementation()`. Will run if no other + * function in the contract matches the call data. + */ + fallback () payable external { + _fallback(); + } + + /** + * @dev Fallback function that delegates calls to the address returned by `_implementation()`. Will run if call data + * is empty. + */ + receive () payable external { + _fallback(); + } + + /** + * @dev Hook that is called before falling back to the implementation. Can happen as part of a manual `_fallback` + * call, or as part of the Solidity `fallback` or `receive` functions. + * + * If overriden should call `super._beforeFallback()`. + */ + function _beforeFallback() internal virtual { + } +} diff --git a/src/node_modules/@openzeppelin/contracts/proxy/ProxyAdmin.sol b/src/node_modules/@openzeppelin/contracts/proxy/ProxyAdmin.sol new file mode 100644 index 00000000..26af407f --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/proxy/ProxyAdmin.sol @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../access/Ownable.sol"; +import "./TransparentUpgradeableProxy.sol"; + +/** + * @dev This is an auxiliary contract meant to be assigned as the admin of a {TransparentUpgradeableProxy}. For an + * explanation of why you would want to use this see the documentation for {TransparentUpgradeableProxy}. + */ +contract ProxyAdmin is Ownable { + + /** + * @dev Returns the current implementation of `proxy`. + * + * Requirements: + * + * - This contract must be the admin of `proxy`. + */ + function getProxyImplementation(TransparentUpgradeableProxy proxy) public view returns (address) { + // We need to manually run the static call since the getter cannot be flagged as view + // bytes4(keccak256("implementation()")) == 0x5c60da1b + (bool success, bytes memory returndata) = address(proxy).staticcall(hex"5c60da1b"); + require(success); + return abi.decode(returndata, (address)); + } + + /** + * @dev Returns the current admin of `proxy`. + * + * Requirements: + * + * - This contract must be the admin of `proxy`. + */ + function getProxyAdmin(TransparentUpgradeableProxy proxy) public view returns (address) { + // We need to manually run the static call since the getter cannot be flagged as view + // bytes4(keccak256("admin()")) == 0xf851a440 + (bool success, bytes memory returndata) = address(proxy).staticcall(hex"f851a440"); + require(success); + return abi.decode(returndata, (address)); + } + + /** + * @dev Changes the admin of `proxy` to `newAdmin`. + * + * Requirements: + * + * - This contract must be the current admin of `proxy`. + */ + function changeProxyAdmin(TransparentUpgradeableProxy proxy, address newAdmin) public onlyOwner { + proxy.changeAdmin(newAdmin); + } + + /** + * @dev Upgrades `proxy` to `implementation`. See {TransparentUpgradeableProxy-upgradeTo}. + * + * Requirements: + * + * - This contract must be the admin of `proxy`. + */ + function upgrade(TransparentUpgradeableProxy proxy, address implementation) public onlyOwner { + proxy.upgradeTo(implementation); + } + + /** + * @dev Upgrades `proxy` to `implementation` and calls a function on the new implementation. See + * {TransparentUpgradeableProxy-upgradeToAndCall}. + * + * Requirements: + * + * - This contract must be the admin of `proxy`. + */ + function upgradeAndCall(TransparentUpgradeableProxy proxy, address implementation, bytes memory data) public payable onlyOwner { + proxy.upgradeToAndCall{value: msg.value}(implementation, data); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/proxy/README.adoc b/src/node_modules/@openzeppelin/contracts/proxy/README.adoc new file mode 100644 index 00000000..68d55854 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/proxy/README.adoc @@ -0,0 +1,26 @@ += Proxies + +[.readme-notice] +NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/proxy + +This is a low-level set of contracts implementing the proxy pattern for upgradeability. For an in-depth overview of this pattern check out the xref:upgrades-plugins::proxies.adoc[Proxy Upgrade Pattern] page. + +The abstract {Proxy} contract implements the core delegation functionality. If the concrete proxies that we provide below are not suitable, we encourage building on top of this base contract since it contains an assembly block that may be hard to get right. + +Upgradeability is implemented in the {UpgradeableProxy} contract, although it provides only an internal upgrade interface. For an upgrade interface exposed externally to an admin, we provide {TransparentUpgradeableProxy}. Both of these contracts use the storage slots specified in https://eips.ethereum.org/EIPS/eip-1967[EIP1967] to avoid clashes with the storage of the implementation contract behind the proxy. + +CAUTION: Using upgradeable proxies correctly and securely is a difficult task that requires deep knowledge of the proxy pattern, Solidity, and the EVM. Unless you want a lot of low level control, we recommend using the xref:upgrades-plugins::index.adoc[OpenZeppelin Upgrades Plugins] for Truffle and Buidler. + +== Core + +{{Proxy}} + +{{UpgradeableProxy}} + +{{TransparentUpgradeableProxy}} + +== Utilities + +{{Initializable}} + +{{ProxyAdmin}} diff --git a/src/node_modules/@openzeppelin/contracts/proxy/TransparentUpgradeableProxy.sol b/src/node_modules/@openzeppelin/contracts/proxy/TransparentUpgradeableProxy.sol new file mode 100644 index 00000000..363c2586 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/proxy/TransparentUpgradeableProxy.sol @@ -0,0 +1,153 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./UpgradeableProxy.sol"; + +/** + * @dev This contract implements a proxy that is upgradeable by an admin. + * + * To avoid https://medium.com/nomic-labs-blog/malicious-backdoors-in-ethereum-proxies-62629adf3357[proxy selector + * clashing], which can potentially be used in an attack, this contract uses the + * https://blog.openzeppelin.com/the-transparent-proxy-pattern/[transparent proxy pattern]. This pattern implies two + * things that go hand in hand: + * + * 1. If any account other than the admin calls the proxy, the call will be forwarded to the implementation, even if + * that call matches one of the admin functions exposed by the proxy itself. + * 2. If the admin calls the proxy, it can access the admin functions, but its calls will never be forwarded to the + * implementation. If the admin tries to call a function on the implementation it will fail with an error that says + * "admin cannot fallback to proxy target". + * + * These properties mean that the admin account can only be used for admin actions like upgrading the proxy or changing + * the admin, so it's best if it's a dedicated account that is not used for anything else. This will avoid headaches due + * to sudden errors when trying to call a function from the proxy implementation. + * + * Our recommendation is for the dedicated account to be an instance of the {ProxyAdmin} contract. If set up this way, + * you should think of the `ProxyAdmin` instance as the real administrative inerface of your proxy. + */ +contract TransparentUpgradeableProxy is UpgradeableProxy { + /** + * @dev Initializes an upgradeable proxy managed by `_admin`, backed by the implementation at `_logic`, and + * optionally initialized with `_data` as explained in {UpgradeableProxy-constructor}. + */ + constructor(address _logic, address _admin, bytes memory _data) public payable UpgradeableProxy(_logic, _data) { + assert(_ADMIN_SLOT == bytes32(uint256(keccak256("eip1967.proxy.admin")) - 1)); + _setAdmin(_admin); + } + + /** + * @dev Emitted when the admin account has changed. + */ + event AdminChanged(address previousAdmin, address newAdmin); + + /** + * @dev Storage slot with the admin of the contract. + * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is + * validated in the constructor. + */ + bytes32 private constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103; + + /** + * @dev Modifier used internally that will delegate the call to the implementation unless the sender is the admin. + */ + modifier ifAdmin() { + if (msg.sender == _admin()) { + _; + } else { + _fallback(); + } + } + + /** + * @dev Returns the current admin. + * + * NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyAdmin}. + * + * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the + * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call. + * `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103` + */ + function admin() external ifAdmin returns (address) { + return _admin(); + } + + /** + * @dev Returns the current implementation. + * + * NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyImplementation}. + * + * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the + * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call. + * `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc` + */ + function implementation() external ifAdmin returns (address) { + return _implementation(); + } + + /** + * @dev Changes the admin of the proxy. + * + * Emits an {AdminChanged} event. + * + * NOTE: Only the admin can call this function. See {ProxyAdmin-changeProxyAdmin}. + */ + function changeAdmin(address newAdmin) external ifAdmin { + require(newAdmin != address(0), "TransparentUpgradeableProxy: new admin is the zero address"); + emit AdminChanged(_admin(), newAdmin); + _setAdmin(newAdmin); + } + + /** + * @dev Upgrade the implementation of the proxy. + * + * NOTE: Only the admin can call this function. See {ProxyAdmin-upgrade}. + */ + function upgradeTo(address newImplementation) external ifAdmin { + _upgradeTo(newImplementation); + } + + /** + * @dev Upgrade the implementation of the proxy, and then call a function from the new implementation as specified + * by `data`, which should be an encoded function call. This is useful to initialize new storage variables in the + * proxied contract. + * + * NOTE: Only the admin can call this function. See {ProxyAdmin-upgradeAndCall}. + */ + function upgradeToAndCall(address newImplementation, bytes calldata data) external payable ifAdmin { + _upgradeTo(newImplementation); + // solhint-disable-next-line avoid-low-level-calls + (bool success,) = newImplementation.delegatecall(data); + require(success); + } + + /** + * @dev Returns the current admin. + */ + function _admin() internal view returns (address adm) { + bytes32 slot = _ADMIN_SLOT; + // solhint-disable-next-line no-inline-assembly + assembly { + adm := sload(slot) + } + } + + /** + * @dev Stores a new address in the EIP1967 admin slot. + */ + function _setAdmin(address newAdmin) private { + bytes32 slot = _ADMIN_SLOT; + + // solhint-disable-next-line no-inline-assembly + assembly { + sstore(slot, newAdmin) + } + } + + /** + * @dev Makes sure the admin cannot access the fallback function. See {Proxy-_beforeFallback}. + */ + function _beforeFallback() internal override virtual { + require(msg.sender != _admin(), "TransparentUpgradeableProxy: admin cannot fallback to proxy target"); + super._beforeFallback(); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/proxy/UpgradeableProxy.sol b/src/node_modules/@openzeppelin/contracts/proxy/UpgradeableProxy.sol new file mode 100644 index 00000000..e031d412 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/proxy/UpgradeableProxy.sol @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./Proxy.sol"; +import "../utils/Address.sol"; + +/** + * @dev This contract implements an upgradeable proxy. It is upgradeable because calls are delegated to an + * implementation address that can be changed. This address is stored in storage in the location specified by + * https://eips.ethereum.org/EIPS/eip-1967[EIP1967], so that it doesn't conflict with the storage layout of the + * implementation behind the proxy. + * + * Upgradeability is only provided internally through {_upgradeTo}. For an externally upgradeable proxy see + * {TransparentUpgradeableProxy}. + */ +contract UpgradeableProxy is Proxy { + /** + * @dev Initializes the upgradeable proxy with an initial implementation specified by `_logic`. + * + * If `_data` is nonempty, it's used as data in a delegate call to `_logic`. This will typically be an encoded + * function call, and allows initializating the storage of the proxy like a Solidity constructor. + */ + constructor(address _logic, bytes memory _data) public payable { + assert(_IMPLEMENTATION_SLOT == bytes32(uint256(keccak256("eip1967.proxy.implementation")) - 1)); + _setImplementation(_logic); + if(_data.length > 0) { + // solhint-disable-next-line avoid-low-level-calls + (bool success,) = _logic.delegatecall(_data); + require(success); + } + } + + /** + * @dev Emitted when the implementation is upgraded. + */ + event Upgraded(address indexed implementation); + + /** + * @dev Storage slot with the address of the current implementation. + * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is + * validated in the constructor. + */ + bytes32 private constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc; + + /** + * @dev Returns the current implementation address. + */ + function _implementation() internal override view returns (address impl) { + bytes32 slot = _IMPLEMENTATION_SLOT; + // solhint-disable-next-line no-inline-assembly + assembly { + impl := sload(slot) + } + } + + /** + * @dev Upgrades the proxy to a new implementation. + * + * Emits an {Upgraded} event. + */ + function _upgradeTo(address newImplementation) internal { + _setImplementation(newImplementation); + emit Upgraded(newImplementation); + } + + /** + * @dev Stores a new address in the EIP1967 implementation slot. + */ + function _setImplementation(address newImplementation) private { + require(Address.isContract(newImplementation), "UpgradeableProxy: new implementation is not a contract"); + + bytes32 slot = _IMPLEMENTATION_SLOT; + + // solhint-disable-next-line no-inline-assembly + assembly { + sstore(slot, newImplementation) + } + } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC1155/ERC1155.sol b/src/node_modules/@openzeppelin/contracts/token/ERC1155/ERC1155.sol new file mode 100644 index 00000000..6cb5ca97 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC1155/ERC1155.sol @@ -0,0 +1,413 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./IERC1155.sol"; +import "./IERC1155MetadataURI.sol"; +import "./IERC1155Receiver.sol"; +import "../../GSN/Context.sol"; +import "../../introspection/ERC165.sol"; +import "../../math/SafeMath.sol"; +import "../../utils/Address.sol"; + +/** + * + * @dev Implementation of the basic standard multi-token. + * See https://eips.ethereum.org/EIPS/eip-1155 + * Originally based on code by Enjin: https://github.com/enjin/erc-1155 + * + * _Available since v3.1._ + */ +contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI { + using SafeMath for uint256; + using Address for address; + + // Mapping from token ID to account balances + mapping (uint256 => mapping(address => uint256)) private _balances; + + // Mapping from account to operator approvals + mapping (address => mapping(address => bool)) private _operatorApprovals; + + // Used as the URI for all token types by relying on ID substitution, e.g. https://token-cdn-domain/{id}.json + string private _uri; + + /* + * bytes4(keccak256('balanceOf(address,uint256)')) == 0x00fdd58e + * bytes4(keccak256('balanceOfBatch(address[],uint256[])')) == 0x4e1273f4 + * bytes4(keccak256('setApprovalForAll(address,bool)')) == 0xa22cb465 + * bytes4(keccak256('isApprovedForAll(address,address)')) == 0xe985e9c5 + * bytes4(keccak256('safeTransferFrom(address,address,uint256,uint256,bytes)')) == 0xf242432a + * bytes4(keccak256('safeBatchTransferFrom(address,address,uint256[],uint256[],bytes)')) == 0x2eb2c2d6 + * + * => 0x00fdd58e ^ 0x4e1273f4 ^ 0xa22cb465 ^ + * 0xe985e9c5 ^ 0xf242432a ^ 0x2eb2c2d6 == 0xd9b67a26 + */ + bytes4 private constant _INTERFACE_ID_ERC1155 = 0xd9b67a26; + + /* + * bytes4(keccak256('uri(uint256)')) == 0x0e89341c + */ + bytes4 private constant _INTERFACE_ID_ERC1155_METADATA_URI = 0x0e89341c; + + /** + * @dev See {_setURI}. + */ + constructor (string memory uri) public { + _setURI(uri); + + // register the supported interfaces to conform to ERC1155 via ERC165 + _registerInterface(_INTERFACE_ID_ERC1155); + + // register the supported interfaces to conform to ERC1155MetadataURI via ERC165 + _registerInterface(_INTERFACE_ID_ERC1155_METADATA_URI); + } + + /** + * @dev See {IERC1155MetadataURI-uri}. + * + * This implementation returns the same URI for *all* token types. It relies + * on the token type ID substitution mechanism + * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP]. + * + * Clients calling this function must replace the `\{id\}` substring with the + * actual token type ID. + */ + function uri(uint256) external view override returns (string memory) { + return _uri; + } + + /** + * @dev See {IERC1155-balanceOf}. + * + * Requirements: + * + * - `account` cannot be the zero address. + */ + function balanceOf(address account, uint256 id) public view override returns (uint256) { + require(account != address(0), "ERC1155: balance query for the zero address"); + return _balances[id][account]; + } + + /** + * @dev See {IERC1155-balanceOfBatch}. + * + * Requirements: + * + * - `accounts` and `ids` must have the same length. + */ + function balanceOfBatch( + address[] memory accounts, + uint256[] memory ids + ) + public + view + override + returns (uint256[] memory) + { + require(accounts.length == ids.length, "ERC1155: accounts and ids length mismatch"); + + uint256[] memory batchBalances = new uint256[](accounts.length); + + for (uint256 i = 0; i < accounts.length; ++i) { + require(accounts[i] != address(0), "ERC1155: batch balance query for the zero address"); + batchBalances[i] = _balances[ids[i]][accounts[i]]; + } + + return batchBalances; + } + + /** + * @dev See {IERC1155-setApprovalForAll}. + */ + function setApprovalForAll(address operator, bool approved) public virtual override { + require(_msgSender() != operator, "ERC1155: setting approval status for self"); + + _operatorApprovals[_msgSender()][operator] = approved; + emit ApprovalForAll(_msgSender(), operator, approved); + } + + /** + * @dev See {IERC1155-isApprovedForAll}. + */ + function isApprovedForAll(address account, address operator) public view override returns (bool) { + return _operatorApprovals[account][operator]; + } + + /** + * @dev See {IERC1155-safeTransferFrom}. + */ + function safeTransferFrom( + address from, + address to, + uint256 id, + uint256 amount, + bytes memory data + ) + public + virtual + override + { + require(to != address(0), "ERC1155: transfer to the zero address"); + require( + from == _msgSender() || isApprovedForAll(from, _msgSender()), + "ERC1155: caller is not owner nor approved" + ); + + address operator = _msgSender(); + + _beforeTokenTransfer(operator, from, to, _asSingletonArray(id), _asSingletonArray(amount), data); + + _balances[id][from] = _balances[id][from].sub(amount, "ERC1155: insufficient balance for transfer"); + _balances[id][to] = _balances[id][to].add(amount); + + emit TransferSingle(operator, from, to, id, amount); + + _doSafeTransferAcceptanceCheck(operator, from, to, id, amount, data); + } + + /** + * @dev See {IERC1155-safeBatchTransferFrom}. + */ + function safeBatchTransferFrom( + address from, + address to, + uint256[] memory ids, + uint256[] memory amounts, + bytes memory data + ) + public + virtual + override + { + require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch"); + require(to != address(0), "ERC1155: transfer to the zero address"); + require( + from == _msgSender() || isApprovedForAll(from, _msgSender()), + "ERC1155: transfer caller is not owner nor approved" + ); + + address operator = _msgSender(); + + _beforeTokenTransfer(operator, from, to, ids, amounts, data); + + for (uint256 i = 0; i < ids.length; ++i) { + uint256 id = ids[i]; + uint256 amount = amounts[i]; + + _balances[id][from] = _balances[id][from].sub( + amount, + "ERC1155: insufficient balance for transfer" + ); + _balances[id][to] = _balances[id][to].add(amount); + } + + emit TransferBatch(operator, from, to, ids, amounts); + + _doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, data); + } + + /** + * @dev Sets a new URI for all token types, by relying on the token type ID + * substitution mechanism + * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP]. + * + * By this mechanism, any occurrence of the `\{id\}` substring in either the + * URI or any of the amounts in the JSON file at said URI will be replaced by + * clients with the token type ID. + * + * For example, the `https://token-cdn-domain/\{id\}.json` URI would be + * interpreted by clients as + * `https://token-cdn-domain/000000000000000000000000000000000000000000000000000000000004cce0.json` + * for token type ID 0x4cce0. + * + * See {uri}. + * + * Because these URIs cannot be meaningfully represented by the {URI} event, + * this function emits no events. + */ + function _setURI(string memory newuri) internal virtual { + _uri = newuri; + } + + /** + * @dev Creates `amount` tokens of token type `id`, and assigns them to `account`. + * + * Emits a {TransferSingle} event. + * + * Requirements: + * + * - `account` cannot be the zero address. + * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the + * acceptance magic value. + */ + function _mint(address account, uint256 id, uint256 amount, bytes memory data) internal virtual { + require(account != address(0), "ERC1155: mint to the zero address"); + + address operator = _msgSender(); + + _beforeTokenTransfer(operator, address(0), account, _asSingletonArray(id), _asSingletonArray(amount), data); + + _balances[id][account] = _balances[id][account].add(amount); + emit TransferSingle(operator, address(0), account, id, amount); + + _doSafeTransferAcceptanceCheck(operator, address(0), account, id, amount, data); + } + + /** + * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_mint}. + * + * Requirements: + * + * - `ids` and `amounts` must have the same length. + * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the + * acceptance magic value. + */ + function _mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) internal virtual { + require(to != address(0), "ERC1155: mint to the zero address"); + require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch"); + + address operator = _msgSender(); + + _beforeTokenTransfer(operator, address(0), to, ids, amounts, data); + + for (uint i = 0; i < ids.length; i++) { + _balances[ids[i]][to] = amounts[i].add(_balances[ids[i]][to]); + } + + emit TransferBatch(operator, address(0), to, ids, amounts); + + _doSafeBatchTransferAcceptanceCheck(operator, address(0), to, ids, amounts, data); + } + + /** + * @dev Destroys `amount` tokens of token type `id` from `account` + * + * Requirements: + * + * - `account` cannot be the zero address. + * - `account` must have at least `amount` tokens of token type `id`. + */ + function _burn(address account, uint256 id, uint256 amount) internal virtual { + require(account != address(0), "ERC1155: burn from the zero address"); + + address operator = _msgSender(); + + _beforeTokenTransfer(operator, account, address(0), _asSingletonArray(id), _asSingletonArray(amount), ""); + + _balances[id][account] = _balances[id][account].sub( + amount, + "ERC1155: burn amount exceeds balance" + ); + + emit TransferSingle(operator, account, address(0), id, amount); + } + + /** + * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_burn}. + * + * Requirements: + * + * - `ids` and `amounts` must have the same length. + */ + function _burnBatch(address account, uint256[] memory ids, uint256[] memory amounts) internal virtual { + require(account != address(0), "ERC1155: burn from the zero address"); + require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch"); + + address operator = _msgSender(); + + _beforeTokenTransfer(operator, account, address(0), ids, amounts, ""); + + for (uint i = 0; i < ids.length; i++) { + _balances[ids[i]][account] = _balances[ids[i]][account].sub( + amounts[i], + "ERC1155: burn amount exceeds balance" + ); + } + + emit TransferBatch(operator, account, address(0), ids, amounts); + } + + /** + * @dev Hook that is called before any token transfer. This includes minting + * and burning, as well as batched variants. + * + * The same hook is called on both single and batched variants. For single + * transfers, the length of the `id` and `amount` arrays will be 1. + * + * Calling conditions (for each `id` and `amount` pair): + * + * - When `from` and `to` are both non-zero, `amount` of ``from``'s tokens + * of token type `id` will be transferred to `to`. + * - When `from` is zero, `amount` tokens of token type `id` will be minted + * for `to`. + * - when `to` is zero, `amount` of ``from``'s tokens of token type `id` + * will be burned. + * - `from` and `to` are never both zero. + * - `ids` and `amounts` have the same, non-zero length. + * + * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks]. + */ + function _beforeTokenTransfer( + address operator, + address from, + address to, + uint256[] memory ids, + uint256[] memory amounts, + bytes memory data + ) + internal virtual + { } + + function _doSafeTransferAcceptanceCheck( + address operator, + address from, + address to, + uint256 id, + uint256 amount, + bytes memory data + ) + private + { + if (to.isContract()) { + try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) { + if (response != IERC1155Receiver(to).onERC1155Received.selector) { + revert("ERC1155: ERC1155Receiver rejected tokens"); + } + } catch Error(string memory reason) { + revert(reason); + } catch { + revert("ERC1155: transfer to non ERC1155Receiver implementer"); + } + } + } + + function _doSafeBatchTransferAcceptanceCheck( + address operator, + address from, + address to, + uint256[] memory ids, + uint256[] memory amounts, + bytes memory data + ) + private + { + if (to.isContract()) { + try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (bytes4 response) { + if (response != IERC1155Receiver(to).onERC1155BatchReceived.selector) { + revert("ERC1155: ERC1155Receiver rejected tokens"); + } + } catch Error(string memory reason) { + revert(reason); + } catch { + revert("ERC1155: transfer to non ERC1155Receiver implementer"); + } + } + } + + function _asSingletonArray(uint256 element) private pure returns (uint256[] memory) { + uint256[] memory array = new uint256[](1); + array[0] = element; + + return array; + } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC1155/ERC1155Burnable.sol b/src/node_modules/@openzeppelin/contracts/token/ERC1155/ERC1155Burnable.sol new file mode 100644 index 00000000..83edd477 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC1155/ERC1155Burnable.sol @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./ERC1155.sol"; + +/** + * @dev Extension of {ERC1155} that allows token holders to destroy both their + * own tokens and those that they have been approved to use. + * + * _Available since v3.1._ + */ +abstract contract ERC1155Burnable is ERC1155 { + function burn(address account, uint256 id, uint256 value) public virtual { + require( + account == _msgSender() || isApprovedForAll(account, _msgSender()), + "ERC1155: caller is not owner nor approved" + ); + + _burn(account, id, value); + } + + function burnBatch(address account, uint256[] memory ids, uint256[] memory values) public virtual { + require( + account == _msgSender() || isApprovedForAll(account, _msgSender()), + "ERC1155: caller is not owner nor approved" + ); + + _burnBatch(account, ids, values); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC1155/ERC1155Holder.sol b/src/node_modules/@openzeppelin/contracts/token/ERC1155/ERC1155Holder.sol new file mode 100644 index 00000000..82d5979a --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC1155/ERC1155Holder.sol @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./ERC1155Receiver.sol"; + +/** + * @dev _Available since v3.1._ + */ +contract ERC1155Holder is ERC1155Receiver { + function onERC1155Received(address, address, uint256, uint256, bytes memory) public virtual override returns (bytes4) { + return this.onERC1155Received.selector; + } + + function onERC1155BatchReceived(address, address, uint256[] memory, uint256[] memory, bytes memory) public virtual override returns (bytes4) { + return this.onERC1155BatchReceived.selector; + } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC1155/ERC1155Pausable.sol b/src/node_modules/@openzeppelin/contracts/token/ERC1155/ERC1155Pausable.sol new file mode 100644 index 00000000..a2626cc5 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC1155/ERC1155Pausable.sol @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./ERC1155.sol"; +import "../../utils/Pausable.sol"; + +/** + * @dev ERC1155 token with pausable token transfers, minting and burning. + * + * Useful for scenarios such as preventing trades until the end of an evaluation + * period, or having an emergency switch for freezing all token transfers in the + * event of a large bug. + * + * _Available since v3.1._ + */ +abstract contract ERC1155Pausable is ERC1155, Pausable { + /** + * @dev See {ERC1155-_beforeTokenTransfer}. + * + * Requirements: + * + * - the contract must not be paused. + */ + function _beforeTokenTransfer( + address operator, + address from, + address to, + uint256[] memory ids, + uint256[] memory amounts, + bytes memory data + ) + internal virtual override + { + super._beforeTokenTransfer(operator, from, to, ids, amounts, data); + + require(!paused(), "ERC1155Pausable: token transfer while paused"); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC1155/ERC1155Receiver.sol b/src/node_modules/@openzeppelin/contracts/token/ERC1155/ERC1155Receiver.sol new file mode 100644 index 00000000..201660bc --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC1155/ERC1155Receiver.sol @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./IERC1155Receiver.sol"; +import "../../introspection/ERC165.sol"; + +/** + * @dev _Available since v3.1._ + */ +abstract contract ERC1155Receiver is ERC165, IERC1155Receiver { + constructor() public { + _registerInterface( + ERC1155Receiver(0).onERC1155Received.selector ^ + ERC1155Receiver(0).onERC1155BatchReceived.selector + ); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC1155/IERC1155.sol b/src/node_modules/@openzeppelin/contracts/token/ERC1155/IERC1155.sol new file mode 100644 index 00000000..5262661e --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC1155/IERC1155.sol @@ -0,0 +1,103 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.2; + +import "../../introspection/IERC165.sol"; + +/** + * @dev Required interface of an ERC1155 compliant contract, as defined in the + * https://eips.ethereum.org/EIPS/eip-1155[EIP]. + * + * _Available since v3.1._ + */ +interface IERC1155 is IERC165 { + /** + * @dev Emitted when `value` tokens of token type `id` are transferred from `from` to `to` by `operator`. + */ + event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value); + + /** + * @dev Equivalent to multiple {TransferSingle} events, where `operator`, `from` and `to` are the same for all + * transfers. + */ + event TransferBatch(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] values); + + /** + * @dev Emitted when `account` grants or revokes permission to `operator` to transfer their tokens, according to + * `approved`. + */ + event ApprovalForAll(address indexed account, address indexed operator, bool approved); + + /** + * @dev Emitted when the URI for token type `id` changes to `value`, if it is a non-programmatic URI. + * + * If an {URI} event was emitted for `id`, the standard + * https://eips.ethereum.org/EIPS/eip-1155#metadata-extensions[guarantees] that `value` will equal the value + * returned by {IERC1155MetadataURI-uri}. + */ + event URI(string value, uint256 indexed id); + + /** + * @dev Returns the amount of tokens of token type `id` owned by `account`. + * + * Requirements: + * + * - `account` cannot be the zero address. + */ + function balanceOf(address account, uint256 id) external view returns (uint256); + + /** + * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {balanceOf}. + * + * Requirements: + * + * - `accounts` and `ids` must have the same length. + */ + function balanceOfBatch(address[] calldata accounts, uint256[] calldata ids) external view returns (uint256[] memory); + + /** + * @dev Grants or revokes permission to `operator` to transfer the caller's tokens, according to `approved`, + * + * Emits an {ApprovalForAll} event. + * + * Requirements: + * + * - `operator` cannot be the caller. + */ + function setApprovalForAll(address operator, bool approved) external; + + /** + * @dev Returns true if `operator` is approved to transfer ``account``'s tokens. + * + * See {setApprovalForAll}. + */ + function isApprovedForAll(address account, address operator) external view returns (bool); + + /** + * @dev Transfers `amount` tokens of token type `id` from `from` to `to`. + * + * Emits a {TransferSingle} event. + * + * Requirements: + * + * - `to` cannot be the zero address. + * - If the caller is not `from`, it must be have been approved to spend ``from``'s tokens via {setApprovalForAll}. + * - `from` must have a balance of tokens of type `id` of at least `amount`. + * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the + * acceptance magic value. + */ + function safeTransferFrom(address from, address to, uint256 id, uint256 amount, bytes calldata data) external; + + /** + * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {safeTransferFrom}. + * + * Emits a {TransferBatch} event. + * + * Requirements: + * + * - `ids` and `amounts` must have the same length. + * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the + * acceptance magic value. + */ + function safeBatchTransferFrom(address from, address to, uint256[] calldata ids, uint256[] calldata amounts, bytes calldata data) external; +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC1155/IERC1155MetadataURI.sol b/src/node_modules/@openzeppelin/contracts/token/ERC1155/IERC1155MetadataURI.sol new file mode 100644 index 00000000..2716c770 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC1155/IERC1155MetadataURI.sol @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.2; + +import "./IERC1155.sol"; + +/** + * @dev Interface of the optional ERC1155MetadataExtension interface, as defined + * in the https://eips.ethereum.org/EIPS/eip-1155#metadata-extensions[EIP]. + * + * _Available since v3.1._ + */ +interface IERC1155MetadataURI is IERC1155 { + /** + * @dev Returns the URI for token type `id`. + * + * If the `\{id\}` substring is present in the URI, it must be replaced by + * clients with the actual token type ID. + */ + function uri(uint256 id) external view returns (string memory); +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol b/src/node_modules/@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol new file mode 100644 index 00000000..6bd3dc2c --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../../introspection/IERC165.sol"; + +/** + * _Available since v3.1._ + */ +interface IERC1155Receiver is IERC165 { + + /** + @dev Handles the receipt of a single ERC1155 token type. This function is + called at the end of a `safeTransferFrom` after the balance has been updated. + To accept the transfer, this must return + `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))` + (i.e. 0xf23a6e61, or its own function selector). + @param operator The address which initiated the transfer (i.e. msg.sender) + @param from The address which previously owned the token + @param id The ID of the token being transferred + @param value The amount of tokens being transferred + @param data Additional data with no specified format + @return `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))` if transfer is allowed + */ + function onERC1155Received( + address operator, + address from, + uint256 id, + uint256 value, + bytes calldata data + ) + external + returns(bytes4); + + /** + @dev Handles the receipt of a multiple ERC1155 token types. This function + is called at the end of a `safeBatchTransferFrom` after the balances have + been updated. To accept the transfer(s), this must return + `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))` + (i.e. 0xbc197c81, or its own function selector). + @param operator The address which initiated the batch transfer (i.e. msg.sender) + @param from The address which previously owned the token + @param ids An array containing ids of each token being transferred (order and length must match values array) + @param values An array containing amounts of each token being transferred (order and length must match ids array) + @param data Additional data with no specified format + @return `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))` if transfer is allowed + */ + function onERC1155BatchReceived( + address operator, + address from, + uint256[] calldata ids, + uint256[] calldata values, + bytes calldata data + ) + external + returns(bytes4); +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC1155/README.adoc b/src/node_modules/@openzeppelin/contracts/token/ERC1155/README.adoc new file mode 100644 index 00000000..e8047469 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC1155/README.adoc @@ -0,0 +1,37 @@ += ERC 1155 + +[.readme-notice] +NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/token/erc1155 + +This set of interfaces and contracts are all related to the https://eips.ethereum.org/EIPS/eip-1155[ERC1155 Multi Token Standard]. + +The EIP consists of three interfaces which fulfill different roles, found here as {IERC1155}, {IERC1155MetadataURI} and {IERC1155Receiver}. + +{ERC1155} implements the mandatory {IERC1155} interface, as well as the optional extension {IERC1155MetadataURI}, by relying on the substitution mechanism to use the same URI for all token types, dramatically reducing gas costs. + +Additionally there are multiple custom extensions, including: + +* designation of addresses that can pause token transfers for all users ({ERC1155Pausable}). +* destruction of own tokens ({ERC1155Burnable}). + +NOTE: This core set of contracts is designed to be unopinionated, allowing developers to access the internal functions in ERC1155 (such as <>) and expose them as external functions in the way they prefer. On the other hand, xref:ROOT:erc1155.adoc#Presets[ERC1155 Presets] (such as {ERC1155PresetMinterPauser}) are designed using opinionated patterns to provide developers with ready to use, deployable contracts. + +== Core + +{{IERC1155}} + +{{IERC1155MetadataURI}} + +{{ERC1155}} + +{{IERC1155Receiver}} + +== Extensions + +{{ERC1155Pausable}} + +{{ERC1155Burnable}} + +== Convenience + +{{ERC1155Holder}} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol b/src/node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol new file mode 100644 index 00000000..dce6f459 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol @@ -0,0 +1,307 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../../GSN/Context.sol"; +import "./IERC20.sol"; +import "../../math/SafeMath.sol"; +import "../../utils/Address.sol"; + +/** + * @dev Implementation of the {IERC20} interface. + * + * This implementation is agnostic to the way tokens are created. This means + * that a supply mechanism has to be added in a derived contract using {_mint}. + * For a generic mechanism see {ERC20PresetMinterPauser}. + * + * TIP: For a detailed writeup see our guide + * https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How + * to implement supply mechanisms]. + * + * We have followed general OpenZeppelin guidelines: functions revert instead + * of returning `false` on failure. This behavior is nonetheless conventional + * and does not conflict with the expectations of ERC20 applications. + * + * Additionally, an {Approval} event is emitted on calls to {transferFrom}. + * This allows applications to reconstruct the allowance for all accounts just + * by listening to said events. Other implementations of the EIP may not emit + * these events, as it isn't required by the specification. + * + * Finally, the non-standard {decreaseAllowance} and {increaseAllowance} + * functions have been added to mitigate the well-known issues around setting + * allowances. See {IERC20-approve}. + */ +contract ERC20 is Context, IERC20 { + using SafeMath for uint256; + using Address for address; + + mapping (address => uint256) private _balances; + + mapping (address => mapping (address => uint256)) private _allowances; + + uint256 private _totalSupply; + + string private _name; + string private _symbol; + uint8 private _decimals; + + /** + * @dev Sets the values for {name} and {symbol}, initializes {decimals} with + * a default value of 18. + * + * To select a different value for {decimals}, use {_setupDecimals}. + * + * All three of these values are immutable: they can only be set once during + * construction. + */ + constructor (string memory name, string memory symbol) public { + _name = name; + _symbol = symbol; + _decimals = 18; + } + + /** + * @dev Returns the name of the token. + */ + function name() public view returns (string memory) { + return _name; + } + + /** + * @dev Returns the symbol of the token, usually a shorter version of the + * name. + */ + function symbol() public view returns (string memory) { + return _symbol; + } + + /** + * @dev Returns the number of decimals used to get its user representation. + * For example, if `decimals` equals `2`, a balance of `505` tokens should + * be displayed to a user as `5,05` (`505 / 10 ** 2`). + * + * Tokens usually opt for a value of 18, imitating the relationship between + * Ether and Wei. This is the value {ERC20} uses, unless {_setupDecimals} is + * called. + * + * NOTE: This information is only used for _display_ purposes: it in + * no way affects any of the arithmetic of the contract, including + * {IERC20-balanceOf} and {IERC20-transfer}. + */ + function decimals() public view returns (uint8) { + return _decimals; + } + + /** + * @dev See {IERC20-totalSupply}. + */ + function totalSupply() public view override returns (uint256) { + return _totalSupply; + } + + /** + * @dev See {IERC20-balanceOf}. + */ + function balanceOf(address account) public view override returns (uint256) { + return _balances[account]; + } + + /** + * @dev See {IERC20-transfer}. + * + * Requirements: + * + * - `recipient` cannot be the zero address. + * - the caller must have a balance of at least `amount`. + */ + function transfer(address recipient, uint256 amount) public virtual override returns (bool) { + _transfer(_msgSender(), recipient, amount); + return true; + } + + /** + * @dev See {IERC20-allowance}. + */ + function allowance(address owner, address spender) public view virtual override returns (uint256) { + return _allowances[owner][spender]; + } + + /** + * @dev See {IERC20-approve}. + * + * Requirements: + * + * - `spender` cannot be the zero address. + */ + function approve(address spender, uint256 amount) public virtual override returns (bool) { + _approve(_msgSender(), spender, amount); + return true; + } + + /** + * @dev See {IERC20-transferFrom}. + * + * Emits an {Approval} event indicating the updated allowance. This is not + * required by the EIP. See the note at the beginning of {ERC20}; + * + * Requirements: + * - `sender` and `recipient` cannot be the zero address. + * - `sender` must have a balance of at least `amount`. + * - the caller must have allowance for ``sender``'s tokens of at least + * `amount`. + */ + function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) { + _transfer(sender, recipient, amount); + _approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance")); + return true; + } + + /** + * @dev Atomically increases the allowance granted to `spender` by the caller. + * + * This is an alternative to {approve} that can be used as a mitigation for + * problems described in {IERC20-approve}. + * + * Emits an {Approval} event indicating the updated allowance. + * + * Requirements: + * + * - `spender` cannot be the zero address. + */ + function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) { + _approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue)); + return true; + } + + /** + * @dev Atomically decreases the allowance granted to `spender` by the caller. + * + * This is an alternative to {approve} that can be used as a mitigation for + * problems described in {IERC20-approve}. + * + * Emits an {Approval} event indicating the updated allowance. + * + * Requirements: + * + * - `spender` cannot be the zero address. + * - `spender` must have allowance for the caller of at least + * `subtractedValue`. + */ + function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) { + _approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero")); + return true; + } + + /** + * @dev Moves tokens `amount` from `sender` to `recipient`. + * + * This is internal function is equivalent to {transfer}, and can be used to + * e.g. implement automatic token fees, slashing mechanisms, etc. + * + * Emits a {Transfer} event. + * + * Requirements: + * + * - `sender` cannot be the zero address. + * - `recipient` cannot be the zero address. + * - `sender` must have a balance of at least `amount`. + */ + function _transfer(address sender, address recipient, uint256 amount) internal virtual { + require(sender != address(0), "ERC20: transfer from the zero address"); + require(recipient != address(0), "ERC20: transfer to the zero address"); + + _beforeTokenTransfer(sender, recipient, amount); + + _balances[sender] = _balances[sender].sub(amount, "ERC20: transfer amount exceeds balance"); + _balances[recipient] = _balances[recipient].add(amount); + emit Transfer(sender, recipient, amount); + } + + /** @dev Creates `amount` tokens and assigns them to `account`, increasing + * the total supply. + * + * Emits a {Transfer} event with `from` set to the zero address. + * + * Requirements + * + * - `to` cannot be the zero address. + */ + function _mint(address account, uint256 amount) internal virtual { + require(account != address(0), "ERC20: mint to the zero address"); + + _beforeTokenTransfer(address(0), account, amount); + + _totalSupply = _totalSupply.add(amount); + _balances[account] = _balances[account].add(amount); + emit Transfer(address(0), account, amount); + } + + /** + * @dev Destroys `amount` tokens from `account`, reducing the + * total supply. + * + * Emits a {Transfer} event with `to` set to the zero address. + * + * Requirements + * + * - `account` cannot be the zero address. + * - `account` must have at least `amount` tokens. + */ + function _burn(address account, uint256 amount) internal virtual { + require(account != address(0), "ERC20: burn from the zero address"); + + _beforeTokenTransfer(account, address(0), amount); + + _balances[account] = _balances[account].sub(amount, "ERC20: burn amount exceeds balance"); + _totalSupply = _totalSupply.sub(amount); + emit Transfer(account, address(0), amount); + } + + /** + * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens. + * + * This internal function is equivalent to `approve`, and can be used to + * e.g. set automatic allowances for certain subsystems, etc. + * + * Emits an {Approval} event. + * + * Requirements: + * + * - `owner` cannot be the zero address. + * - `spender` cannot be the zero address. + */ + function _approve(address owner, address spender, uint256 amount) internal virtual { + require(owner != address(0), "ERC20: approve from the zero address"); + require(spender != address(0), "ERC20: approve to the zero address"); + + _allowances[owner][spender] = amount; + emit Approval(owner, spender, amount); + } + + /** + * @dev Sets {decimals} to a value other than the default one of 18. + * + * WARNING: This function should only be called from the constructor. Most + * applications that interact with token contracts will not expect + * {decimals} to ever change, and may work incorrectly if it does. + */ + function _setupDecimals(uint8 decimals_) internal { + _decimals = decimals_; + } + + /** + * @dev Hook that is called before any transfer of tokens. This includes + * minting and burning. + * + * Calling conditions: + * + * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens + * will be to transferred to `to`. + * - when `from` is zero, `amount` tokens will be minted for `to`. + * - when `to` is zero, `amount` of ``from``'s tokens will be burned. + * - `from` and `to` are never both zero. + * + * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks]. + */ + function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual { } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC20/ERC20Burnable.sol b/src/node_modules/@openzeppelin/contracts/token/ERC20/ERC20Burnable.sol new file mode 100644 index 00000000..2ce6bac0 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC20/ERC20Burnable.sol @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../../GSN/Context.sol"; +import "./ERC20.sol"; + +/** + * @dev Extension of {ERC20} that allows token holders to destroy both their own + * tokens and those that they have an allowance for, in a way that can be + * recognized off-chain (via event analysis). + */ +abstract contract ERC20Burnable is Context, ERC20 { + /** + * @dev Destroys `amount` tokens from the caller. + * + * See {ERC20-_burn}. + */ + function burn(uint256 amount) public virtual { + _burn(_msgSender(), amount); + } + + /** + * @dev Destroys `amount` tokens from `account`, deducting from the caller's + * allowance. + * + * See {ERC20-_burn} and {ERC20-allowance}. + * + * Requirements: + * + * - the caller must have allowance for ``accounts``'s tokens of at least + * `amount`. + */ + function burnFrom(address account, uint256 amount) public virtual { + uint256 decreasedAllowance = allowance(account, _msgSender()).sub(amount, "ERC20: burn amount exceeds allowance"); + + _approve(account, _msgSender(), decreasedAllowance); + _burn(account, amount); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC20/ERC20Capped.sol b/src/node_modules/@openzeppelin/contracts/token/ERC20/ERC20Capped.sol new file mode 100644 index 00000000..3cff3b73 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC20/ERC20Capped.sol @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./ERC20.sol"; + +/** + * @dev Extension of {ERC20} that adds a cap to the supply of tokens. + */ +abstract contract ERC20Capped is ERC20 { + uint256 private _cap; + + /** + * @dev Sets the value of the `cap`. This value is immutable, it can only be + * set once during construction. + */ + constructor (uint256 cap) public { + require(cap > 0, "ERC20Capped: cap is 0"); + _cap = cap; + } + + /** + * @dev Returns the cap on the token's total supply. + */ + function cap() public view returns (uint256) { + return _cap; + } + + /** + * @dev See {ERC20-_beforeTokenTransfer}. + * + * Requirements: + * + * - minted tokens must not cause the total supply to go over the cap. + */ + function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override { + super._beforeTokenTransfer(from, to, amount); + + if (from == address(0)) { // When minting tokens + require(totalSupply().add(amount) <= _cap, "ERC20Capped: cap exceeded"); + } + } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC20/ERC20Pausable.sol b/src/node_modules/@openzeppelin/contracts/token/ERC20/ERC20Pausable.sol new file mode 100644 index 00000000..a08b9df6 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC20/ERC20Pausable.sol @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./ERC20.sol"; +import "../../utils/Pausable.sol"; + +/** + * @dev ERC20 token with pausable token transfers, minting and burning. + * + * Useful for scenarios such as preventing trades until the end of an evaluation + * period, or having an emergency switch for freezing all token transfers in the + * event of a large bug. + */ +abstract contract ERC20Pausable is ERC20, Pausable { + /** + * @dev See {ERC20-_beforeTokenTransfer}. + * + * Requirements: + * + * - the contract must not be paused. + */ + function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override { + super._beforeTokenTransfer(from, to, amount); + + require(!paused(), "ERC20Pausable: token transfer while paused"); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC20/ERC20Snapshot.sol b/src/node_modules/@openzeppelin/contracts/token/ERC20/ERC20Snapshot.sol new file mode 100644 index 00000000..0f8e7b6f --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC20/ERC20Snapshot.sol @@ -0,0 +1,181 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../../math/SafeMath.sol"; +import "../../utils/Arrays.sol"; +import "../../utils/Counters.sol"; +import "./ERC20.sol"; + +/** + * @dev This contract extends an ERC20 token with a snapshot mechanism. When a snapshot is created, the balances and + * total supply at the time are recorded for later access. + * + * This can be used to safely create mechanisms based on token balances such as trustless dividends or weighted voting. + * In naive implementations it's possible to perform a "double spend" attack by reusing the same balance from different + * accounts. By using snapshots to calculate dividends or voting power, those attacks no longer apply. It can also be + * used to create an efficient ERC20 forking mechanism. + * + * Snapshots are created by the internal {_snapshot} function, which will emit the {Snapshot} event and return a + * snapshot id. To get the total supply at the time of a snapshot, call the function {totalSupplyAt} with the snapshot + * id. To get the balance of an account at the time of a snapshot, call the {balanceOfAt} function with the snapshot id + * and the account address. + * + * ==== Gas Costs + * + * Snapshots are efficient. Snapshot creation is _O(1)_. Retrieval of balances or total supply from a snapshot is _O(log + * n)_ in the number of snapshots that have been created, although _n_ for a specific account will generally be much + * smaller since identical balances in subsequent snapshots are stored as a single entry. + * + * There is a constant overhead for normal ERC20 transfers due to the additional snapshot bookkeeping. This overhead is + * only significant for the first transfer that immediately follows a snapshot for a particular account. Subsequent + * transfers will have normal cost until the next snapshot, and so on. + */ +abstract contract ERC20Snapshot is ERC20 { + // Inspired by Jordi Baylina's MiniMeToken to record historical balances: + // https://github.com/Giveth/minimd/blob/ea04d950eea153a04c51fa510b068b9dded390cb/contracts/MiniMeToken.sol + + using SafeMath for uint256; + using Arrays for uint256[]; + using Counters for Counters.Counter; + + // Snapshotted values have arrays of ids and the value corresponding to that id. These could be an array of a + // Snapshot struct, but that would impede usage of functions that work on an array. + struct Snapshots { + uint256[] ids; + uint256[] values; + } + + mapping (address => Snapshots) private _accountBalanceSnapshots; + Snapshots private _totalSupplySnapshots; + + // Snapshot ids increase monotonically, with the first value being 1. An id of 0 is invalid. + Counters.Counter private _currentSnapshotId; + + /** + * @dev Emitted by {_snapshot} when a snapshot identified by `id` is created. + */ + event Snapshot(uint256 id); + + /** + * @dev Creates a new snapshot and returns its snapshot id. + * + * Emits a {Snapshot} event that contains the same id. + * + * {_snapshot} is `internal` and you have to decide how to expose it externally. Its usage may be restricted to a + * set of accounts, for example using {AccessControl}, or it may be open to the public. + * + * [WARNING] + * ==== + * While an open way of calling {_snapshot} is required for certain trust minimization mechanisms such as forking, + * you must consider that it can potentially be used by attackers in two ways. + * + * First, it can be used to increase the cost of retrieval of values from snapshots, although it will grow + * logarithmically thus rendering this attack ineffective in the long term. Second, it can be used to target + * specific accounts and increase the cost of ERC20 transfers for them, in the ways specified in the Gas Costs + * section above. + * + * We haven't measured the actual numbers; if this is something you're interested in please reach out to us. + * ==== + */ + function _snapshot() internal virtual returns (uint256) { + _currentSnapshotId.increment(); + + uint256 currentId = _currentSnapshotId.current(); + emit Snapshot(currentId); + return currentId; + } + + /** + * @dev Retrieves the balance of `account` at the time `snapshotId` was created. + */ + function balanceOfAt(address account, uint256 snapshotId) public view returns (uint256) { + (bool snapshotted, uint256 value) = _valueAt(snapshotId, _accountBalanceSnapshots[account]); + + return snapshotted ? value : balanceOf(account); + } + + /** + * @dev Retrieves the total supply at the time `snapshotId` was created. + */ + function totalSupplyAt(uint256 snapshotId) public view returns(uint256) { + (bool snapshotted, uint256 value) = _valueAt(snapshotId, _totalSupplySnapshots); + + return snapshotted ? value : totalSupply(); + } + + + // Update balance and/or total supply snapshots before the values are modified. This is implemented + // in the _beforeTokenTransfer hook, which is executed for _mint, _burn, and _transfer operations. + function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override { + super._beforeTokenTransfer(from, to, amount); + + if (from == address(0)) { + // mint + _updateAccountSnapshot(to); + _updateTotalSupplySnapshot(); + } else if (to == address(0)) { + // burn + _updateAccountSnapshot(from); + _updateTotalSupplySnapshot(); + } else { + // transfer + _updateAccountSnapshot(from); + _updateAccountSnapshot(to); + } + } + + function _valueAt(uint256 snapshotId, Snapshots storage snapshots) + private view returns (bool, uint256) + { + require(snapshotId > 0, "ERC20Snapshot: id is 0"); + // solhint-disable-next-line max-line-length + require(snapshotId <= _currentSnapshotId.current(), "ERC20Snapshot: nonexistent id"); + + // When a valid snapshot is queried, there are three possibilities: + // a) The queried value was not modified after the snapshot was taken. Therefore, a snapshot entry was never + // created for this id, and all stored snapshot ids are smaller than the requested one. The value that corresponds + // to this id is the current one. + // b) The queried value was modified after the snapshot was taken. Therefore, there will be an entry with the + // requested id, and its value is the one to return. + // c) More snapshots were created after the requested one, and the queried value was later modified. There will be + // no entry for the requested id: the value that corresponds to it is that of the smallest snapshot id that is + // larger than the requested one. + // + // In summary, we need to find an element in an array, returning the index of the smallest value that is larger if + // it is not found, unless said value doesn't exist (e.g. when all values are smaller). Arrays.findUpperBound does + // exactly this. + + uint256 index = snapshots.ids.findUpperBound(snapshotId); + + if (index == snapshots.ids.length) { + return (false, 0); + } else { + return (true, snapshots.values[index]); + } + } + + function _updateAccountSnapshot(address account) private { + _updateSnapshot(_accountBalanceSnapshots[account], balanceOf(account)); + } + + function _updateTotalSupplySnapshot() private { + _updateSnapshot(_totalSupplySnapshots, totalSupply()); + } + + function _updateSnapshot(Snapshots storage snapshots, uint256 currentValue) private { + uint256 currentId = _currentSnapshotId.current(); + if (_lastSnapshotId(snapshots.ids) < currentId) { + snapshots.ids.push(currentId); + snapshots.values.push(currentValue); + } + } + + function _lastSnapshotId(uint256[] storage ids) private view returns (uint256) { + if (ids.length == 0) { + return 0; + } else { + return ids[ids.length - 1]; + } + } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC20/IERC20.sol b/src/node_modules/@openzeppelin/contracts/token/ERC20/IERC20.sol new file mode 100644 index 00000000..a1f9ca7f --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC20/IERC20.sol @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev Interface of the ERC20 standard as defined in the EIP. + */ +interface IERC20 { + /** + * @dev Returns the amount of tokens in existence. + */ + function totalSupply() external view returns (uint256); + + /** + * @dev Returns the amount of tokens owned by `account`. + */ + function balanceOf(address account) external view returns (uint256); + + /** + * @dev Moves `amount` tokens from the caller's account to `recipient`. + * + * Returns a boolean value indicating whether the operation succeeded. + * + * Emits a {Transfer} event. + */ + function transfer(address recipient, uint256 amount) external returns (bool); + + /** + * @dev Returns the remaining number of tokens that `spender` will be + * allowed to spend on behalf of `owner` through {transferFrom}. This is + * zero by default. + * + * This value changes when {approve} or {transferFrom} are called. + */ + function allowance(address owner, address spender) external view returns (uint256); + + /** + * @dev Sets `amount` as the allowance of `spender` over the caller's tokens. + * + * Returns a boolean value indicating whether the operation succeeded. + * + * IMPORTANT: Beware that changing an allowance with this method brings the risk + * that someone may use both the old and the new allowance by unfortunate + * transaction ordering. One possible solution to mitigate this race + * condition is to first reduce the spender's allowance to 0 and set the + * desired value afterwards: + * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 + * + * Emits an {Approval} event. + */ + function approve(address spender, uint256 amount) external returns (bool); + + /** + * @dev Moves `amount` tokens from `sender` to `recipient` using the + * allowance mechanism. `amount` is then deducted from the caller's + * allowance. + * + * Returns a boolean value indicating whether the operation succeeded. + * + * Emits a {Transfer} event. + */ + function transferFrom(address sender, address recipient, uint256 amount) external returns (bool); + + /** + * @dev Emitted when `value` tokens are moved from one account (`from`) to + * another (`to`). + * + * Note that `value` may be zero. + */ + event Transfer(address indexed from, address indexed to, uint256 value); + + /** + * @dev Emitted when the allowance of a `spender` for an `owner` is set by + * a call to {approve}. `value` is the new allowance. + */ + event Approval(address indexed owner, address indexed spender, uint256 value); +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC20/README.adoc b/src/node_modules/@openzeppelin/contracts/token/ERC20/README.adoc new file mode 100644 index 00000000..f47f94b9 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC20/README.adoc @@ -0,0 +1,49 @@ += ERC 20 + +[.readme-notice] +NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/token/erc20 + +This set of interfaces, contracts, and utilities are all related to the https://eips.ethereum.org/EIPS/eip-20[ERC20 Token Standard]. + +TIP: For an overview of ERC20 tokens and a walk through on how to create a token contract read our xref:ROOT:erc20.adoc[ERC20 guide]. + +There a few core contracts that implement the behavior specified in the EIP: + +* {IERC20}: the interface all ERC20 implementations should conform to. +* {ERC20}: the implementation of the ERC20 interface, including the <>, <> and <> optional standard extension to the base interface. + +Additionally there are multiple custom extensions, including: + +* designation of addresses that can pause token transfers for all users ({ERC20Pausable}). +* efficient storage of past token balances to be later queried at any point in time ({ERC20Snapshot}). +* destruction of own tokens ({ERC20Burnable}). +* enforcement of a cap to the total supply when minting tokens ({ERC20Capped}). + +Finally, there are some utilities to interact with ERC20 contracts in various ways. + +* {SafeERC20} is a wrapper around the interface that eliminates the need to handle boolean return values. +* {TokenTimelock} can hold tokens for a beneficiary until a specified time. + +NOTE: This core set of contracts is designed to be unopinionated, allowing developers to access the internal functions in ERC20 (such as <>) and expose them as external functions in the way they prefer. On the other hand, xref:ROOT:erc20.adoc#Presets[ERC20 Presets] (such as {ERC20PresetMinterPauser}) are designed using opinionated patterns to provide developers with ready to use, deployable contracts. + +== Core + +{{IERC20}} + +{{ERC20}} + +== Extensions + +{{ERC20Snapshot}} + +{{ERC20Pausable}} + +{{ERC20Burnable}} + +{{ERC20Capped}} + +== Utilities + +{{SafeERC20}} + +{{TokenTimelock}} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC20/SafeERC20.sol b/src/node_modules/@openzeppelin/contracts/token/ERC20/SafeERC20.sol new file mode 100644 index 00000000..e5f0d23a --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC20/SafeERC20.sol @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./IERC20.sol"; +import "../../math/SafeMath.sol"; +import "../../utils/Address.sol"; + +/** + * @title SafeERC20 + * @dev Wrappers around ERC20 operations that throw on failure (when the token + * contract returns false). Tokens that return no value (and instead revert or + * throw on failure) are also supported, non-reverting calls are assumed to be + * successful. + * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract, + * which allows you to call the safe operations as `token.safeTransfer(...)`, etc. + */ +library SafeERC20 { + using SafeMath for uint256; + using Address for address; + + function safeTransfer(IERC20 token, address to, uint256 value) internal { + _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value)); + } + + function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal { + _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value)); + } + + /** + * @dev Deprecated. This function has issues similar to the ones found in + * {IERC20-approve}, and its usage is discouraged. + * + * Whenever possible, use {safeIncreaseAllowance} and + * {safeDecreaseAllowance} instead. + */ + function safeApprove(IERC20 token, address spender, uint256 value) internal { + // safeApprove should only be called when setting an initial allowance, + // or when resetting it to zero. To increase and decrease it, use + // 'safeIncreaseAllowance' and 'safeDecreaseAllowance' + // solhint-disable-next-line max-line-length + require((value == 0) || (token.allowance(address(this), spender) == 0), + "SafeERC20: approve from non-zero to non-zero allowance" + ); + _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value)); + } + + function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal { + uint256 newAllowance = token.allowance(address(this), spender).add(value); + _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance)); + } + + function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal { + uint256 newAllowance = token.allowance(address(this), spender).sub(value, "SafeERC20: decreased allowance below zero"); + _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance)); + } + + /** + * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement + * on the return value: the return value is optional (but if data is returned, it must not be false). + * @param token The token targeted by the call. + * @param data The call data (encoded using abi.encode or one of its variants). + */ + function _callOptionalReturn(IERC20 token, bytes memory data) private { + // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since + // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that + // the target address contains contract code and also asserts for success in the low-level call. + + bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed"); + if (returndata.length > 0) { // Return data is optional + // solhint-disable-next-line max-line-length + require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed"); + } + } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC20/TokenTimelock.sol b/src/node_modules/@openzeppelin/contracts/token/ERC20/TokenTimelock.sol new file mode 100644 index 00000000..0d05ed82 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC20/TokenTimelock.sol @@ -0,0 +1,67 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./SafeERC20.sol"; + +/** + * @dev A token holder contract that will allow a beneficiary to extract the + * tokens after a given release time. + * + * Useful for simple vesting schedules like "advisors get all of their tokens + * after 1 year". + */ +contract TokenTimelock { + using SafeERC20 for IERC20; + + // ERC20 basic token contract being held + IERC20 private _token; + + // beneficiary of tokens after they are released + address private _beneficiary; + + // timestamp when token release is enabled + uint256 private _releaseTime; + + constructor (IERC20 token, address beneficiary, uint256 releaseTime) public { + // solhint-disable-next-line not-rely-on-time + require(releaseTime > block.timestamp, "TokenTimelock: release time is before current time"); + _token = token; + _beneficiary = beneficiary; + _releaseTime = releaseTime; + } + + /** + * @return the token being held. + */ + function token() public view returns (IERC20) { + return _token; + } + + /** + * @return the beneficiary of the tokens. + */ + function beneficiary() public view returns (address) { + return _beneficiary; + } + + /** + * @return the time when the tokens are released. + */ + function releaseTime() public view returns (uint256) { + return _releaseTime; + } + + /** + * @notice Transfers tokens held by timelock to beneficiary. + */ + function release() public virtual { + // solhint-disable-next-line not-rely-on-time + require(block.timestamp >= _releaseTime, "TokenTimelock: current time is before release time"); + + uint256 amount = _token.balanceOf(address(this)); + require(amount > 0, "TokenTimelock: no tokens to release"); + + _token.safeTransfer(_beneficiary, amount); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC721/ERC721.sol b/src/node_modules/@openzeppelin/contracts/token/ERC721/ERC721.sol new file mode 100644 index 00000000..fc04f1dd --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC721/ERC721.sol @@ -0,0 +1,473 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../../GSN/Context.sol"; +import "./IERC721.sol"; +import "./IERC721Metadata.sol"; +import "./IERC721Enumerable.sol"; +import "./IERC721Receiver.sol"; +import "../../introspection/ERC165.sol"; +import "../../math/SafeMath.sol"; +import "../../utils/Address.sol"; +import "../../utils/EnumerableSet.sol"; +import "../../utils/EnumerableMap.sol"; +import "../../utils/Strings.sol"; + +/** + * @title ERC721 Non-Fungible Token Standard basic implementation + * @dev see https://eips.ethereum.org/EIPS/eip-721 + */ +contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable { + using SafeMath for uint256; + using Address for address; + using EnumerableSet for EnumerableSet.UintSet; + using EnumerableMap for EnumerableMap.UintToAddressMap; + using Strings for uint256; + + // Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))` + // which can be also obtained as `IERC721Receiver(0).onERC721Received.selector` + bytes4 private constant _ERC721_RECEIVED = 0x150b7a02; + + // Mapping from holder address to their (enumerable) set of owned tokens + mapping (address => EnumerableSet.UintSet) private _holderTokens; + + // Enumerable mapping from token ids to their owners + EnumerableMap.UintToAddressMap private _tokenOwners; + + // Mapping from token ID to approved address + mapping (uint256 => address) private _tokenApprovals; + + // Mapping from owner to operator approvals + mapping (address => mapping (address => bool)) private _operatorApprovals; + + // Token name + string private _name; + + // Token symbol + string private _symbol; + + // Optional mapping for token URIs + mapping (uint256 => string) private _tokenURIs; + + // Base URI + string private _baseURI; + + /* + * bytes4(keccak256('balanceOf(address)')) == 0x70a08231 + * bytes4(keccak256('ownerOf(uint256)')) == 0x6352211e + * bytes4(keccak256('approve(address,uint256)')) == 0x095ea7b3 + * bytes4(keccak256('getApproved(uint256)')) == 0x081812fc + * bytes4(keccak256('setApprovalForAll(address,bool)')) == 0xa22cb465 + * bytes4(keccak256('isApprovedForAll(address,address)')) == 0xe985e9c5 + * bytes4(keccak256('transferFrom(address,address,uint256)')) == 0x23b872dd + * bytes4(keccak256('safeTransferFrom(address,address,uint256)')) == 0x42842e0e + * bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)')) == 0xb88d4fde + * + * => 0x70a08231 ^ 0x6352211e ^ 0x095ea7b3 ^ 0x081812fc ^ + * 0xa22cb465 ^ 0xe985e9c5 ^ 0x23b872dd ^ 0x42842e0e ^ 0xb88d4fde == 0x80ac58cd + */ + bytes4 private constant _INTERFACE_ID_ERC721 = 0x80ac58cd; + + /* + * bytes4(keccak256('name()')) == 0x06fdde03 + * bytes4(keccak256('symbol()')) == 0x95d89b41 + * bytes4(keccak256('tokenURI(uint256)')) == 0xc87b56dd + * + * => 0x06fdde03 ^ 0x95d89b41 ^ 0xc87b56dd == 0x5b5e139f + */ + bytes4 private constant _INTERFACE_ID_ERC721_METADATA = 0x5b5e139f; + + /* + * bytes4(keccak256('totalSupply()')) == 0x18160ddd + * bytes4(keccak256('tokenOfOwnerByIndex(address,uint256)')) == 0x2f745c59 + * bytes4(keccak256('tokenByIndex(uint256)')) == 0x4f6ccce7 + * + * => 0x18160ddd ^ 0x2f745c59 ^ 0x4f6ccce7 == 0x780e9d63 + */ + bytes4 private constant _INTERFACE_ID_ERC721_ENUMERABLE = 0x780e9d63; + + /** + * @dev Initializes the contract by setting a `name` and a `symbol` to the token collection. + */ + constructor (string memory name, string memory symbol) public { + _name = name; + _symbol = symbol; + + // register the supported interfaces to conform to ERC721 via ERC165 + _registerInterface(_INTERFACE_ID_ERC721); + _registerInterface(_INTERFACE_ID_ERC721_METADATA); + _registerInterface(_INTERFACE_ID_ERC721_ENUMERABLE); + } + + /** + * @dev See {IERC721-balanceOf}. + */ + function balanceOf(address owner) public view override returns (uint256) { + require(owner != address(0), "ERC721: balance query for the zero address"); + + return _holderTokens[owner].length(); + } + + /** + * @dev See {IERC721-ownerOf}. + */ + function ownerOf(uint256 tokenId) public view override returns (address) { + return _tokenOwners.get(tokenId, "ERC721: owner query for nonexistent token"); + } + + /** + * @dev See {IERC721Metadata-name}. + */ + function name() public view override returns (string memory) { + return _name; + } + + /** + * @dev See {IERC721Metadata-symbol}. + */ + function symbol() public view override returns (string memory) { + return _symbol; + } + + /** + * @dev See {IERC721Metadata-tokenURI}. + */ + function tokenURI(uint256 tokenId) public view override returns (string memory) { + require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token"); + + string memory _tokenURI = _tokenURIs[tokenId]; + + // If there is no base URI, return the token URI. + if (bytes(_baseURI).length == 0) { + return _tokenURI; + } + // If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked). + if (bytes(_tokenURI).length > 0) { + return string(abi.encodePacked(_baseURI, _tokenURI)); + } + // If there is a baseURI but no tokenURI, concatenate the tokenID to the baseURI. + return string(abi.encodePacked(_baseURI, tokenId.toString())); + } + + /** + * @dev Returns the base URI set via {_setBaseURI}. This will be + * automatically added as a prefix in {tokenURI} to each token's URI, or + * to the token ID if no specific URI is set for that token ID. + */ + function baseURI() public view returns (string memory) { + return _baseURI; + } + + /** + * @dev See {IERC721Enumerable-tokenOfOwnerByIndex}. + */ + function tokenOfOwnerByIndex(address owner, uint256 index) public view override returns (uint256) { + return _holderTokens[owner].at(index); + } + + /** + * @dev See {IERC721Enumerable-totalSupply}. + */ + function totalSupply() public view override returns (uint256) { + // _tokenOwners are indexed by tokenIds, so .length() returns the number of tokenIds + return _tokenOwners.length(); + } + + /** + * @dev See {IERC721Enumerable-tokenByIndex}. + */ + function tokenByIndex(uint256 index) public view override returns (uint256) { + (uint256 tokenId, ) = _tokenOwners.at(index); + return tokenId; + } + + /** + * @dev See {IERC721-approve}. + */ + function approve(address to, uint256 tokenId) public virtual override { + address owner = ownerOf(tokenId); + require(to != owner, "ERC721: approval to current owner"); + + require(_msgSender() == owner || isApprovedForAll(owner, _msgSender()), + "ERC721: approve caller is not owner nor approved for all" + ); + + _approve(to, tokenId); + } + + /** + * @dev See {IERC721-getApproved}. + */ + function getApproved(uint256 tokenId) public view override returns (address) { + require(_exists(tokenId), "ERC721: approved query for nonexistent token"); + + return _tokenApprovals[tokenId]; + } + + /** + * @dev See {IERC721-setApprovalForAll}. + */ + function setApprovalForAll(address operator, bool approved) public virtual override { + require(operator != _msgSender(), "ERC721: approve to caller"); + + _operatorApprovals[_msgSender()][operator] = approved; + emit ApprovalForAll(_msgSender(), operator, approved); + } + + /** + * @dev See {IERC721-isApprovedForAll}. + */ + function isApprovedForAll(address owner, address operator) public view override returns (bool) { + return _operatorApprovals[owner][operator]; + } + + /** + * @dev See {IERC721-transferFrom}. + */ + function transferFrom(address from, address to, uint256 tokenId) public virtual override { + //solhint-disable-next-line max-line-length + require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved"); + + _transfer(from, to, tokenId); + } + + /** + * @dev See {IERC721-safeTransferFrom}. + */ + function safeTransferFrom(address from, address to, uint256 tokenId) public virtual override { + safeTransferFrom(from, to, tokenId, ""); + } + + /** + * @dev See {IERC721-safeTransferFrom}. + */ + function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public virtual override { + require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved"); + _safeTransfer(from, to, tokenId, _data); + } + + /** + * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients + * are aware of the ERC721 protocol to prevent tokens from being forever locked. + * + * `_data` is additional data, it has no specified format and it is sent in call to `to`. + * + * This internal function is equivalent to {safeTransferFrom}, and can be used to e.g. + * implement alternative mechanisms to perform token transfer, such as signature-based. + * + * Requirements: + * + * - `from` cannot be the zero address. + * - `to` cannot be the zero address. + * - `tokenId` token must exist and be owned by `from`. + * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer. + * + * Emits a {Transfer} event. + */ + function _safeTransfer(address from, address to, uint256 tokenId, bytes memory _data) internal virtual { + _transfer(from, to, tokenId); + require(_checkOnERC721Received(from, to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer"); + } + + /** + * @dev Returns whether `tokenId` exists. + * + * Tokens can be managed by their owner or approved accounts via {approve} or {setApprovalForAll}. + * + * Tokens start existing when they are minted (`_mint`), + * and stop existing when they are burned (`_burn`). + */ + function _exists(uint256 tokenId) internal view returns (bool) { + return _tokenOwners.contains(tokenId); + } + + /** + * @dev Returns whether `spender` is allowed to manage `tokenId`. + * + * Requirements: + * + * - `tokenId` must exist. + */ + function _isApprovedOrOwner(address spender, uint256 tokenId) internal view returns (bool) { + require(_exists(tokenId), "ERC721: operator query for nonexistent token"); + address owner = ownerOf(tokenId); + return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender)); + } + + /** + * @dev Safely mints `tokenId` and transfers it to `to`. + * + * Requirements: + d* + * - `tokenId` must not exist. + * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer. + * + * Emits a {Transfer} event. + */ + function _safeMint(address to, uint256 tokenId) internal virtual { + _safeMint(to, tokenId, ""); + } + + /** + * @dev Same as {xref-ERC721-_safeMint-address-uint256-}[`_safeMint`], with an additional `data` parameter which is + * forwarded in {IERC721Receiver-onERC721Received} to contract recipients. + */ + function _safeMint(address to, uint256 tokenId, bytes memory _data) internal virtual { + _mint(to, tokenId); + require(_checkOnERC721Received(address(0), to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer"); + } + + /** + * @dev Mints `tokenId` and transfers it to `to`. + * + * WARNING: Usage of this method is discouraged, use {_safeMint} whenever possible + * + * Requirements: + * + * - `tokenId` must not exist. + * - `to` cannot be the zero address. + * + * Emits a {Transfer} event. + */ + function _mint(address to, uint256 tokenId) internal virtual { + require(to != address(0), "ERC721: mint to the zero address"); + require(!_exists(tokenId), "ERC721: token already minted"); + + _beforeTokenTransfer(address(0), to, tokenId); + + _holderTokens[to].add(tokenId); + + _tokenOwners.set(tokenId, to); + + emit Transfer(address(0), to, tokenId); + } + + /** + * @dev Destroys `tokenId`. + * The approval is cleared when the token is burned. + * + * Requirements: + * + * - `tokenId` must exist. + * + * Emits a {Transfer} event. + */ + function _burn(uint256 tokenId) internal virtual { + address owner = ownerOf(tokenId); + + _beforeTokenTransfer(owner, address(0), tokenId); + + // Clear approvals + _approve(address(0), tokenId); + + // Clear metadata (if any) + if (bytes(_tokenURIs[tokenId]).length != 0) { + delete _tokenURIs[tokenId]; + } + + _holderTokens[owner].remove(tokenId); + + _tokenOwners.remove(tokenId); + + emit Transfer(owner, address(0), tokenId); + } + + /** + * @dev Transfers `tokenId` from `from` to `to`. + * As opposed to {transferFrom}, this imposes no restrictions on msg.sender. + * + * Requirements: + * + * - `to` cannot be the zero address. + * - `tokenId` token must be owned by `from`. + * + * Emits a {Transfer} event. + */ + function _transfer(address from, address to, uint256 tokenId) internal virtual { + require(ownerOf(tokenId) == from, "ERC721: transfer of token that is not own"); + require(to != address(0), "ERC721: transfer to the zero address"); + + _beforeTokenTransfer(from, to, tokenId); + + // Clear approvals from the previous owner + _approve(address(0), tokenId); + + _holderTokens[from].remove(tokenId); + _holderTokens[to].add(tokenId); + + _tokenOwners.set(tokenId, to); + + emit Transfer(from, to, tokenId); + } + + /** + * @dev Sets `_tokenURI` as the tokenURI of `tokenId`. + * + * Requirements: + * + * - `tokenId` must exist. + */ + function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual { + require(_exists(tokenId), "ERC721Metadata: URI set of nonexistent token"); + _tokenURIs[tokenId] = _tokenURI; + } + + /** + * @dev Internal function to set the base URI for all token IDs. It is + * automatically added as a prefix to the value returned in {tokenURI}, + * or to the token ID if {tokenURI} is empty. + */ + function _setBaseURI(string memory baseURI_) internal virtual { + _baseURI = baseURI_; + } + + /** + * @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address. + * The call is not executed if the target address is not a contract. + * + * @param from address representing the previous owner of the given token ID + * @param to target address that will receive the tokens + * @param tokenId uint256 ID of the token to be transferred + * @param _data bytes optional data to send along with the call + * @return bool whether the call correctly returned the expected magic value + */ + function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory _data) + private returns (bool) + { + if (!to.isContract()) { + return true; + } + bytes memory returndata = to.functionCall(abi.encodeWithSelector( + IERC721Receiver(to).onERC721Received.selector, + _msgSender(), + from, + tokenId, + _data + ), "ERC721: transfer to non ERC721Receiver implementer"); + bytes4 retval = abi.decode(returndata, (bytes4)); + return (retval == _ERC721_RECEIVED); + } + + function _approve(address to, uint256 tokenId) private { + _tokenApprovals[tokenId] = to; + emit Approval(ownerOf(tokenId), to, tokenId); + } + + /** + * @dev Hook that is called before any token transfer. This includes minting + * and burning. + * + * Calling conditions: + * + * - When `from` and `to` are both non-zero, ``from``'s `tokenId` will be + * transferred to `to`. + * - When `from` is zero, `tokenId` will be minted for `to`. + * - When `to` is zero, ``from``'s `tokenId` will be burned. + * - `from` cannot be the zero address. + * - `to` cannot be the zero address. + * + * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks]. + */ + function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal virtual { } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC721/ERC721Burnable.sol b/src/node_modules/@openzeppelin/contracts/token/ERC721/ERC721Burnable.sol new file mode 100644 index 00000000..6f767d23 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC721/ERC721Burnable.sol @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../../GSN/Context.sol"; +import "./ERC721.sol"; + +/** + * @title ERC721 Burnable Token + * @dev ERC721 Token that can be irreversibly burned (destroyed). + */ +abstract contract ERC721Burnable is Context, ERC721 { + /** + * @dev Burns `tokenId`. See {ERC721-_burn}. + * + * Requirements: + * + * - The caller must own `tokenId` or be an approved operator. + */ + function burn(uint256 tokenId) public virtual { + //solhint-disable-next-line max-line-length + require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721Burnable: caller is not owner nor approved"); + _burn(tokenId); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC721/ERC721Holder.sol b/src/node_modules/@openzeppelin/contracts/token/ERC721/ERC721Holder.sol new file mode 100644 index 00000000..3684bd1c --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC721/ERC721Holder.sol @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./IERC721Receiver.sol"; + + /** + * @dev Implementation of the {IERC721Receiver} interface. + * + * Accepts all token transfers. + * Make sure the contract is able to use its token with {IERC721-safeTransferFrom}, {IERC721-approve} or {IERC721-setApprovalForAll}. + */ +contract ERC721Holder is IERC721Receiver { + + /** + * @dev See {IERC721Receiver-onERC721Received}. + * + * Always returns `IERC721Receiver.onERC721Received.selector`. + */ + function onERC721Received(address, address, uint256, bytes memory) public virtual override returns (bytes4) { + return this.onERC721Received.selector; + } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC721/ERC721Pausable.sol b/src/node_modules/@openzeppelin/contracts/token/ERC721/ERC721Pausable.sol new file mode 100644 index 00000000..c5906474 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC721/ERC721Pausable.sol @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./ERC721.sol"; +import "../../utils/Pausable.sol"; + +/** + * @dev ERC721 token with pausable token transfers, minting and burning. + * + * Useful for scenarios such as preventing trades until the end of an evaluation + * period, or having an emergency switch for freezing all token transfers in the + * event of a large bug. + */ +abstract contract ERC721Pausable is ERC721, Pausable { + /** + * @dev See {ERC721-_beforeTokenTransfer}. + * + * Requirements: + * + * - the contract must not be paused. + */ + function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal virtual override { + super._beforeTokenTransfer(from, to, tokenId); + + require(!paused(), "ERC721Pausable: token transfer while paused"); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC721/IERC721.sol b/src/node_modules/@openzeppelin/contracts/token/ERC721/IERC721.sol new file mode 100644 index 00000000..5e7d20e3 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC721/IERC721.sol @@ -0,0 +1,129 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.2; + +import "../../introspection/IERC165.sol"; + +/** + * @dev Required interface of an ERC721 compliant contract. + */ +interface IERC721 is IERC165 { + /** + * @dev Emitted when `tokenId` token is transferred from `from` to `to`. + */ + event Transfer(address indexed from, address indexed to, uint256 indexed tokenId); + + /** + * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token. + */ + event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId); + + /** + * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets. + */ + event ApprovalForAll(address indexed owner, address indexed operator, bool approved); + + /** + * @dev Returns the number of tokens in ``owner``'s account. + */ + function balanceOf(address owner) external view returns (uint256 balance); + + /** + * @dev Returns the owner of the `tokenId` token. + * + * Requirements: + * + * - `tokenId` must exist. + */ + function ownerOf(uint256 tokenId) external view returns (address owner); + + /** + * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients + * are aware of the ERC721 protocol to prevent tokens from being forever locked. + * + * Requirements: + * + * - `from` cannot be the zero address. + * - `to` cannot be the zero address. + * - `tokenId` token must exist and be owned by `from`. + * - If the caller is not `from`, it must be have been allowed to move this token by either {approve} or {setApprovalForAll}. + * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer. + * + * Emits a {Transfer} event. + */ + function safeTransferFrom(address from, address to, uint256 tokenId) external; + + /** + * @dev Transfers `tokenId` token from `from` to `to`. + * + * WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible. + * + * Requirements: + * + * - `from` cannot be the zero address. + * - `to` cannot be the zero address. + * - `tokenId` token must be owned by `from`. + * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}. + * + * Emits a {Transfer} event. + */ + function transferFrom(address from, address to, uint256 tokenId) external; + + /** + * @dev Gives permission to `to` to transfer `tokenId` token to another account. + * The approval is cleared when the token is transferred. + * + * Only a single account can be approved at a time, so approving the zero address clears previous approvals. + * + * Requirements: + * + * - The caller must own the token or be an approved operator. + * - `tokenId` must exist. + * + * Emits an {Approval} event. + */ + function approve(address to, uint256 tokenId) external; + + /** + * @dev Returns the account approved for `tokenId` token. + * + * Requirements: + * + * - `tokenId` must exist. + */ + function getApproved(uint256 tokenId) external view returns (address operator); + + /** + * @dev Approve or remove `operator` as an operator for the caller. + * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller. + * + * Requirements: + * + * - The `operator` cannot be the caller. + * + * Emits an {ApprovalForAll} event. + */ + function setApprovalForAll(address operator, bool _approved) external; + + /** + * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`. + * + * See {setApprovalForAll} + */ + function isApprovedForAll(address owner, address operator) external view returns (bool); + + /** + * @dev Safely transfers `tokenId` token from `from` to `to`. + * + * Requirements: + * + * - `from` cannot be the zero address. + * - `to` cannot be the zero address. + * - `tokenId` token must exist and be owned by `from`. + * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}. + * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer. + * + * Emits a {Transfer} event. + */ + function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external; +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC721/IERC721Enumerable.sol b/src/node_modules/@openzeppelin/contracts/token/ERC721/IERC721Enumerable.sol new file mode 100644 index 00000000..bd3c86d1 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC721/IERC721Enumerable.sol @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.2; + +import "./IERC721.sol"; + +/** + * @title ERC-721 Non-Fungible Token Standard, optional enumeration extension + * @dev See https://eips.ethereum.org/EIPS/eip-721 + */ +interface IERC721Enumerable is IERC721 { + + /** + * @dev Returns the total amount of tokens stored by the contract. + */ + function totalSupply() external view returns (uint256); + + /** + * @dev Returns a token ID owned by `owner` at a given `index` of its token list. + * Use along with {balanceOf} to enumerate all of ``owner``'s tokens. + */ + function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256 tokenId); + + /** + * @dev Returns a token ID at a given `index` of all the tokens stored by the contract. + * Use along with {totalSupply} to enumerate all tokens. + */ + function tokenByIndex(uint256 index) external view returns (uint256); +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC721/IERC721Metadata.sol b/src/node_modules/@openzeppelin/contracts/token/ERC721/IERC721Metadata.sol new file mode 100644 index 00000000..f2ebb495 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC721/IERC721Metadata.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.2; + +import "./IERC721.sol"; + +/** + * @title ERC-721 Non-Fungible Token Standard, optional metadata extension + * @dev See https://eips.ethereum.org/EIPS/eip-721 + */ +interface IERC721Metadata is IERC721 { + + /** + * @dev Returns the token collection name. + */ + function name() external view returns (string memory); + + /** + * @dev Returns the token collection symbol. + */ + function symbol() external view returns (string memory); + + /** + * @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token. + */ + function tokenURI(uint256 tokenId) external view returns (string memory); +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol b/src/node_modules/@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol new file mode 100644 index 00000000..f98053e5 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @title ERC721 token receiver interface + * @dev Interface for any contract that wants to support safeTransfers + * from ERC721 asset contracts. + */ +interface IERC721Receiver { + /** + * @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom} + * by `operator` from `from`, this function is called. + * + * It must return its Solidity selector to confirm the token transfer. + * If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted. + * + * The selector can be obtained in Solidity with `IERC721.onERC721Received.selector`. + */ + function onERC721Received(address operator, address from, uint256 tokenId, bytes calldata data) + external returns (bytes4); +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC721/README.adoc b/src/node_modules/@openzeppelin/contracts/token/ERC721/README.adoc new file mode 100644 index 00000000..5f584eee --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC721/README.adoc @@ -0,0 +1,42 @@ += ERC 721 + +[.readme-notice] +NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/token/erc721 + +This set of interfaces, contracts, and utilities are all related to the https://eips.ethereum.org/EIPS/eip-721[ERC721 Non-Fungible Token Standard]. + +TIP: For a walk through on how to create an ERC721 token read our xref:ROOT:erc721.adoc[ERC721 guide]. + +The EIP consists of three interfaces, found here as {IERC721}, {IERC721Metadata}, and {IERC721Enumerable}. Only the first one is required in a contract to be ERC721 compliant. However, all three are implemented in {ERC721}. + +Additionally, {IERC721Receiver} can be used to prevent tokens from becoming forever locked in contracts. Imagine sending an in-game item to an exchange address that can't send it back!. When using <>, the token contract checks to see that the receiver is an {IERC721Receiver}, which implies that it knows how to handle {ERC721} tokens. If you're writing a contract that needs to receive {ERC721} tokens, you'll want to include this interface. + +Additionally there are multiple custom extensions, including: + +* designation of addresses that can pause token transfers for all users ({ERC721Pausable}). +* destruction of own tokens ({ERC721Burnable}). + +NOTE: This core set of contracts is designed to be unopinionated, allowing developers to access the internal functions in ERC721 (such as <>) and expose them as external functions in the way they prefer. On the other hand, xref:ROOT:erc721.adoc#Presets[ERC721 Presets] (such as {ERC721PresetMinterPauserAutoId}) are designed using opinionated patterns to provide developers with ready to use, deployable contracts. + + +== Core + +{{IERC721}} + +{{IERC721Metadata}} + +{{IERC721Enumerable}} + +{{ERC721}} + +{{IERC721Receiver}} + +== Extensions + +{{ERC721Pausable}} + +{{ERC721Burnable}} + +== Convenience + +{{ERC721Holder}} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC777/ERC777.sol b/src/node_modules/@openzeppelin/contracts/token/ERC777/ERC777.sol new file mode 100644 index 00000000..5adb8ff0 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC777/ERC777.sol @@ -0,0 +1,503 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../../GSN/Context.sol"; +import "./IERC777.sol"; +import "./IERC777Recipient.sol"; +import "./IERC777Sender.sol"; +import "../../token/ERC20/IERC20.sol"; +import "../../math/SafeMath.sol"; +import "../../utils/Address.sol"; +import "../../introspection/IERC1820Registry.sol"; + +/** + * @dev Implementation of the {IERC777} interface. + * + * This implementation is agnostic to the way tokens are created. This means + * that a supply mechanism has to be added in a derived contract using {_mint}. + * + * Support for ERC20 is included in this contract, as specified by the EIP: both + * the ERC777 and ERC20 interfaces can be safely used when interacting with it. + * Both {IERC777-Sent} and {IERC20-Transfer} events are emitted on token + * movements. + * + * Additionally, the {IERC777-granularity} value is hard-coded to `1`, meaning that there + * are no special restrictions in the amount of tokens that created, moved, or + * destroyed. This makes integration with ERC20 applications seamless. + */ +contract ERC777 is Context, IERC777, IERC20 { + using SafeMath for uint256; + using Address for address; + + IERC1820Registry constant internal _ERC1820_REGISTRY = IERC1820Registry(0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24); + + mapping(address => uint256) private _balances; + + uint256 private _totalSupply; + + string private _name; + string private _symbol; + + // We inline the result of the following hashes because Solidity doesn't resolve them at compile time. + // See https://github.com/ethereum/solidity/issues/4024. + + // keccak256("ERC777TokensSender") + bytes32 constant private _TOKENS_SENDER_INTERFACE_HASH = + 0x29ddb589b1fb5fc7cf394961c1adf5f8c6454761adf795e67fe149f658abe895; + + // keccak256("ERC777TokensRecipient") + bytes32 constant private _TOKENS_RECIPIENT_INTERFACE_HASH = + 0xb281fc8c12954d22544db45de3159a39272895b169a852b314f9cc762e44c53b; + + // This isn't ever read from - it's only used to respond to the defaultOperators query. + address[] private _defaultOperatorsArray; + + // Immutable, but accounts may revoke them (tracked in __revokedDefaultOperators). + mapping(address => bool) private _defaultOperators; + + // For each account, a mapping of its operators and revoked default operators. + mapping(address => mapping(address => bool)) private _operators; + mapping(address => mapping(address => bool)) private _revokedDefaultOperators; + + // ERC20-allowances + mapping (address => mapping (address => uint256)) private _allowances; + + /** + * @dev `defaultOperators` may be an empty array. + */ + constructor( + string memory name, + string memory symbol, + address[] memory defaultOperators + ) public { + _name = name; + _symbol = symbol; + + _defaultOperatorsArray = defaultOperators; + for (uint256 i = 0; i < _defaultOperatorsArray.length; i++) { + _defaultOperators[_defaultOperatorsArray[i]] = true; + } + + // register interfaces + _ERC1820_REGISTRY.setInterfaceImplementer(address(this), keccak256("ERC777Token"), address(this)); + _ERC1820_REGISTRY.setInterfaceImplementer(address(this), keccak256("ERC20Token"), address(this)); + } + + /** + * @dev See {IERC777-name}. + */ + function name() public view override returns (string memory) { + return _name; + } + + /** + * @dev See {IERC777-symbol}. + */ + function symbol() public view override returns (string memory) { + return _symbol; + } + + /** + * @dev See {ERC20-decimals}. + * + * Always returns 18, as per the + * [ERC777 EIP](https://eips.ethereum.org/EIPS/eip-777#backward-compatibility). + */ + function decimals() public pure returns (uint8) { + return 18; + } + + /** + * @dev See {IERC777-granularity}. + * + * This implementation always returns `1`. + */ + function granularity() public view override returns (uint256) { + return 1; + } + + /** + * @dev See {IERC777-totalSupply}. + */ + function totalSupply() public view override(IERC20, IERC777) returns (uint256) { + return _totalSupply; + } + + /** + * @dev Returns the amount of tokens owned by an account (`tokenHolder`). + */ + function balanceOf(address tokenHolder) public view override(IERC20, IERC777) returns (uint256) { + return _balances[tokenHolder]; + } + + /** + * @dev See {IERC777-send}. + * + * Also emits a {IERC20-Transfer} event for ERC20 compatibility. + */ + function send(address recipient, uint256 amount, bytes memory data) public override { + _send(_msgSender(), recipient, amount, data, "", true); + } + + /** + * @dev See {IERC20-transfer}. + * + * Unlike `send`, `recipient` is _not_ required to implement the {IERC777Recipient} + * interface if it is a contract. + * + * Also emits a {Sent} event. + */ + function transfer(address recipient, uint256 amount) public override returns (bool) { + require(recipient != address(0), "ERC777: transfer to the zero address"); + + address from = _msgSender(); + + _callTokensToSend(from, from, recipient, amount, "", ""); + + _move(from, from, recipient, amount, "", ""); + + _callTokensReceived(from, from, recipient, amount, "", "", false); + + return true; + } + + /** + * @dev See {IERC777-burn}. + * + * Also emits a {IERC20-Transfer} event for ERC20 compatibility. + */ + function burn(uint256 amount, bytes memory data) public override { + _burn(_msgSender(), amount, data, ""); + } + + /** + * @dev See {IERC777-isOperatorFor}. + */ + function isOperatorFor( + address operator, + address tokenHolder + ) public view override returns (bool) { + return operator == tokenHolder || + (_defaultOperators[operator] && !_revokedDefaultOperators[tokenHolder][operator]) || + _operators[tokenHolder][operator]; + } + + /** + * @dev See {IERC777-authorizeOperator}. + */ + function authorizeOperator(address operator) public override { + require(_msgSender() != operator, "ERC777: authorizing self as operator"); + + if (_defaultOperators[operator]) { + delete _revokedDefaultOperators[_msgSender()][operator]; + } else { + _operators[_msgSender()][operator] = true; + } + + emit AuthorizedOperator(operator, _msgSender()); + } + + /** + * @dev See {IERC777-revokeOperator}. + */ + function revokeOperator(address operator) public override { + require(operator != _msgSender(), "ERC777: revoking self as operator"); + + if (_defaultOperators[operator]) { + _revokedDefaultOperators[_msgSender()][operator] = true; + } else { + delete _operators[_msgSender()][operator]; + } + + emit RevokedOperator(operator, _msgSender()); + } + + /** + * @dev See {IERC777-defaultOperators}. + */ + function defaultOperators() public view override returns (address[] memory) { + return _defaultOperatorsArray; + } + + /** + * @dev See {IERC777-operatorSend}. + * + * Emits {Sent} and {IERC20-Transfer} events. + */ + function operatorSend( + address sender, + address recipient, + uint256 amount, + bytes memory data, + bytes memory operatorData + ) + public override + { + require(isOperatorFor(_msgSender(), sender), "ERC777: caller is not an operator for holder"); + _send(sender, recipient, amount, data, operatorData, true); + } + + /** + * @dev See {IERC777-operatorBurn}. + * + * Emits {Burned} and {IERC20-Transfer} events. + */ + function operatorBurn(address account, uint256 amount, bytes memory data, bytes memory operatorData) public override { + require(isOperatorFor(_msgSender(), account), "ERC777: caller is not an operator for holder"); + _burn(account, amount, data, operatorData); + } + + /** + * @dev See {IERC20-allowance}. + * + * Note that operator and allowance concepts are orthogonal: operators may + * not have allowance, and accounts with allowance may not be operators + * themselves. + */ + function allowance(address holder, address spender) public view override returns (uint256) { + return _allowances[holder][spender]; + } + + /** + * @dev See {IERC20-approve}. + * + * Note that accounts cannot have allowance issued by their operators. + */ + function approve(address spender, uint256 value) public override returns (bool) { + address holder = _msgSender(); + _approve(holder, spender, value); + return true; + } + + /** + * @dev See {IERC20-transferFrom}. + * + * Note that operator and allowance concepts are orthogonal: operators cannot + * call `transferFrom` (unless they have allowance), and accounts with + * allowance cannot call `operatorSend` (unless they are operators). + * + * Emits {Sent}, {IERC20-Transfer} and {IERC20-Approval} events. + */ + function transferFrom(address holder, address recipient, uint256 amount) public override returns (bool) { + require(recipient != address(0), "ERC777: transfer to the zero address"); + require(holder != address(0), "ERC777: transfer from the zero address"); + + address spender = _msgSender(); + + _callTokensToSend(spender, holder, recipient, amount, "", ""); + + _move(spender, holder, recipient, amount, "", ""); + _approve(holder, spender, _allowances[holder][spender].sub(amount, "ERC777: transfer amount exceeds allowance")); + + _callTokensReceived(spender, holder, recipient, amount, "", "", false); + + return true; + } + + /** + * @dev Creates `amount` tokens and assigns them to `account`, increasing + * the total supply. + * + * If a send hook is registered for `account`, the corresponding function + * will be called with `operator`, `data` and `operatorData`. + * + * See {IERC777Sender} and {IERC777Recipient}. + * + * Emits {Minted} and {IERC20-Transfer} events. + * + * Requirements + * + * - `account` cannot be the zero address. + * - if `account` is a contract, it must implement the {IERC777Recipient} + * interface. + */ + function _mint( + address account, + uint256 amount, + bytes memory userData, + bytes memory operatorData + ) + internal virtual + { + require(account != address(0), "ERC777: mint to the zero address"); + + address operator = _msgSender(); + + _beforeTokenTransfer(operator, address(0), account, amount); + + // Update state variables + _totalSupply = _totalSupply.add(amount); + _balances[account] = _balances[account].add(amount); + + _callTokensReceived(operator, address(0), account, amount, userData, operatorData, true); + + emit Minted(operator, account, amount, userData, operatorData); + emit Transfer(address(0), account, amount); + } + + /** + * @dev Send tokens + * @param from address token holder address + * @param to address recipient address + * @param amount uint256 amount of tokens to transfer + * @param userData bytes extra information provided by the token holder (if any) + * @param operatorData bytes extra information provided by the operator (if any) + * @param requireReceptionAck if true, contract recipients are required to implement ERC777TokensRecipient + */ + function _send( + address from, + address to, + uint256 amount, + bytes memory userData, + bytes memory operatorData, + bool requireReceptionAck + ) + internal + { + require(from != address(0), "ERC777: send from the zero address"); + require(to != address(0), "ERC777: send to the zero address"); + + address operator = _msgSender(); + + _callTokensToSend(operator, from, to, amount, userData, operatorData); + + _move(operator, from, to, amount, userData, operatorData); + + _callTokensReceived(operator, from, to, amount, userData, operatorData, requireReceptionAck); + } + + /** + * @dev Burn tokens + * @param from address token holder address + * @param amount uint256 amount of tokens to burn + * @param data bytes extra information provided by the token holder + * @param operatorData bytes extra information provided by the operator (if any) + */ + function _burn( + address from, + uint256 amount, + bytes memory data, + bytes memory operatorData + ) + internal virtual + { + require(from != address(0), "ERC777: burn from the zero address"); + + address operator = _msgSender(); + + _beforeTokenTransfer(operator, from, address(0), amount); + + _callTokensToSend(operator, from, address(0), amount, data, operatorData); + + // Update state variables + _balances[from] = _balances[from].sub(amount, "ERC777: burn amount exceeds balance"); + _totalSupply = _totalSupply.sub(amount); + + emit Burned(operator, from, amount, data, operatorData); + emit Transfer(from, address(0), amount); + } + + function _move( + address operator, + address from, + address to, + uint256 amount, + bytes memory userData, + bytes memory operatorData + ) + private + { + _beforeTokenTransfer(operator, from, to, amount); + + _balances[from] = _balances[from].sub(amount, "ERC777: transfer amount exceeds balance"); + _balances[to] = _balances[to].add(amount); + + emit Sent(operator, from, to, amount, userData, operatorData); + emit Transfer(from, to, amount); + } + + /** + * @dev See {ERC20-_approve}. + * + * Note that accounts cannot have allowance issued by their operators. + */ + function _approve(address holder, address spender, uint256 value) internal { + require(holder != address(0), "ERC777: approve from the zero address"); + require(spender != address(0), "ERC777: approve to the zero address"); + + _allowances[holder][spender] = value; + emit Approval(holder, spender, value); + } + + /** + * @dev Call from.tokensToSend() if the interface is registered + * @param operator address operator requesting the transfer + * @param from address token holder address + * @param to address recipient address + * @param amount uint256 amount of tokens to transfer + * @param userData bytes extra information provided by the token holder (if any) + * @param operatorData bytes extra information provided by the operator (if any) + */ + function _callTokensToSend( + address operator, + address from, + address to, + uint256 amount, + bytes memory userData, + bytes memory operatorData + ) + private + { + address implementer = _ERC1820_REGISTRY.getInterfaceImplementer(from, _TOKENS_SENDER_INTERFACE_HASH); + if (implementer != address(0)) { + IERC777Sender(implementer).tokensToSend(operator, from, to, amount, userData, operatorData); + } + } + + /** + * @dev Call to.tokensReceived() if the interface is registered. Reverts if the recipient is a contract but + * tokensReceived() was not registered for the recipient + * @param operator address operator requesting the transfer + * @param from address token holder address + * @param to address recipient address + * @param amount uint256 amount of tokens to transfer + * @param userData bytes extra information provided by the token holder (if any) + * @param operatorData bytes extra information provided by the operator (if any) + * @param requireReceptionAck if true, contract recipients are required to implement ERC777TokensRecipient + */ + function _callTokensReceived( + address operator, + address from, + address to, + uint256 amount, + bytes memory userData, + bytes memory operatorData, + bool requireReceptionAck + ) + private + { + address implementer = _ERC1820_REGISTRY.getInterfaceImplementer(to, _TOKENS_RECIPIENT_INTERFACE_HASH); + if (implementer != address(0)) { + IERC777Recipient(implementer).tokensReceived(operator, from, to, amount, userData, operatorData); + } else if (requireReceptionAck) { + require(!to.isContract(), "ERC777: token recipient contract has no implementer for ERC777TokensRecipient"); + } + } + + /** + * @dev Hook that is called before any token transfer. This includes + * calls to {send}, {transfer}, {operatorSend}, minting and burning. + * + * Calling conditions: + * + * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens + * will be to transferred to `to`. + * - when `from` is zero, `amount` tokens will be minted for `to`. + * - when `to` is zero, `amount` of ``from``'s tokens will be burned. + * - `from` and `to` are never both zero. + * + * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks]. + */ + function _beforeTokenTransfer(address operator, address from, address to, uint256 amount) internal virtual { } +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC777/IERC777.sol b/src/node_modules/@openzeppelin/contracts/token/ERC777/IERC777.sol new file mode 100644 index 00000000..f3dcbe93 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC777/IERC777.sol @@ -0,0 +1,188 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev Interface of the ERC777Token standard as defined in the EIP. + * + * This contract uses the + * https://eips.ethereum.org/EIPS/eip-1820[ERC1820 registry standard] to let + * token holders and recipients react to token movements by using setting implementers + * for the associated interfaces in said registry. See {IERC1820Registry} and + * {ERC1820Implementer}. + */ +interface IERC777 { + /** + * @dev Returns the name of the token. + */ + function name() external view returns (string memory); + + /** + * @dev Returns the symbol of the token, usually a shorter version of the + * name. + */ + function symbol() external view returns (string memory); + + /** + * @dev Returns the smallest part of the token that is not divisible. This + * means all token operations (creation, movement and destruction) must have + * amounts that are a multiple of this number. + * + * For most token contracts, this value will equal 1. + */ + function granularity() external view returns (uint256); + + /** + * @dev Returns the amount of tokens in existence. + */ + function totalSupply() external view returns (uint256); + + /** + * @dev Returns the amount of tokens owned by an account (`owner`). + */ + function balanceOf(address owner) external view returns (uint256); + + /** + * @dev Moves `amount` tokens from the caller's account to `recipient`. + * + * If send or receive hooks are registered for the caller and `recipient`, + * the corresponding functions will be called with `data` and empty + * `operatorData`. See {IERC777Sender} and {IERC777Recipient}. + * + * Emits a {Sent} event. + * + * Requirements + * + * - the caller must have at least `amount` tokens. + * - `recipient` cannot be the zero address. + * - if `recipient` is a contract, it must implement the {IERC777Recipient} + * interface. + */ + function send(address recipient, uint256 amount, bytes calldata data) external; + + /** + * @dev Destroys `amount` tokens from the caller's account, reducing the + * total supply. + * + * If a send hook is registered for the caller, the corresponding function + * will be called with `data` and empty `operatorData`. See {IERC777Sender}. + * + * Emits a {Burned} event. + * + * Requirements + * + * - the caller must have at least `amount` tokens. + */ + function burn(uint256 amount, bytes calldata data) external; + + /** + * @dev Returns true if an account is an operator of `tokenHolder`. + * Operators can send and burn tokens on behalf of their owners. All + * accounts are their own operator. + * + * See {operatorSend} and {operatorBurn}. + */ + function isOperatorFor(address operator, address tokenHolder) external view returns (bool); + + /** + * @dev Make an account an operator of the caller. + * + * See {isOperatorFor}. + * + * Emits an {AuthorizedOperator} event. + * + * Requirements + * + * - `operator` cannot be calling address. + */ + function authorizeOperator(address operator) external; + + /** + * @dev Revoke an account's operator status for the caller. + * + * See {isOperatorFor} and {defaultOperators}. + * + * Emits a {RevokedOperator} event. + * + * Requirements + * + * - `operator` cannot be calling address. + */ + function revokeOperator(address operator) external; + + /** + * @dev Returns the list of default operators. These accounts are operators + * for all token holders, even if {authorizeOperator} was never called on + * them. + * + * This list is immutable, but individual holders may revoke these via + * {revokeOperator}, in which case {isOperatorFor} will return false. + */ + function defaultOperators() external view returns (address[] memory); + + /** + * @dev Moves `amount` tokens from `sender` to `recipient`. The caller must + * be an operator of `sender`. + * + * If send or receive hooks are registered for `sender` and `recipient`, + * the corresponding functions will be called with `data` and + * `operatorData`. See {IERC777Sender} and {IERC777Recipient}. + * + * Emits a {Sent} event. + * + * Requirements + * + * - `sender` cannot be the zero address. + * - `sender` must have at least `amount` tokens. + * - the caller must be an operator for `sender`. + * - `recipient` cannot be the zero address. + * - if `recipient` is a contract, it must implement the {IERC777Recipient} + * interface. + */ + function operatorSend( + address sender, + address recipient, + uint256 amount, + bytes calldata data, + bytes calldata operatorData + ) external; + + /** + * @dev Destroys `amount` tokens from `account`, reducing the total supply. + * The caller must be an operator of `account`. + * + * If a send hook is registered for `account`, the corresponding function + * will be called with `data` and `operatorData`. See {IERC777Sender}. + * + * Emits a {Burned} event. + * + * Requirements + * + * - `account` cannot be the zero address. + * - `account` must have at least `amount` tokens. + * - the caller must be an operator for `account`. + */ + function operatorBurn( + address account, + uint256 amount, + bytes calldata data, + bytes calldata operatorData + ) external; + + event Sent( + address indexed operator, + address indexed from, + address indexed to, + uint256 amount, + bytes data, + bytes operatorData + ); + + event Minted(address indexed operator, address indexed to, uint256 amount, bytes data, bytes operatorData); + + event Burned(address indexed operator, address indexed from, uint256 amount, bytes data, bytes operatorData); + + event AuthorizedOperator(address indexed operator, address indexed tokenHolder); + + event RevokedOperator(address indexed operator, address indexed tokenHolder); +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC777/IERC777Recipient.sol b/src/node_modules/@openzeppelin/contracts/token/ERC777/IERC777Recipient.sol new file mode 100644 index 00000000..179ed662 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC777/IERC777Recipient.sol @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev Interface of the ERC777TokensRecipient standard as defined in the EIP. + * + * Accounts can be notified of {IERC777} tokens being sent to them by having a + * contract implement this interface (contract holders can be their own + * implementer) and registering it on the + * https://eips.ethereum.org/EIPS/eip-1820[ERC1820 global registry]. + * + * See {IERC1820Registry} and {ERC1820Implementer}. + */ +interface IERC777Recipient { + /** + * @dev Called by an {IERC777} token contract whenever tokens are being + * moved or created into a registered account (`to`). The type of operation + * is conveyed by `from` being the zero address or not. + * + * This call occurs _after_ the token contract's state is updated, so + * {IERC777-balanceOf}, etc., can be used to query the post-operation state. + * + * This function may revert to prevent the operation from being executed. + */ + function tokensReceived( + address operator, + address from, + address to, + uint256 amount, + bytes calldata userData, + bytes calldata operatorData + ) external; +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC777/IERC777Sender.sol b/src/node_modules/@openzeppelin/contracts/token/ERC777/IERC777Sender.sol new file mode 100644 index 00000000..841ca208 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC777/IERC777Sender.sol @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev Interface of the ERC777TokensSender standard as defined in the EIP. + * + * {IERC777} Token holders can be notified of operations performed on their + * tokens by having a contract implement this interface (contract holders can be + * their own implementer) and registering it on the + * https://eips.ethereum.org/EIPS/eip-1820[ERC1820 global registry]. + * + * See {IERC1820Registry} and {ERC1820Implementer}. + */ +interface IERC777Sender { + /** + * @dev Called by an {IERC777} token contract whenever a registered holder's + * (`from`) tokens are about to be moved or destroyed. The type of operation + * is conveyed by `to` being the zero address or not. + * + * This call occurs _before_ the token contract's state is updated, so + * {IERC777-balanceOf}, etc., can be used to query the pre-operation state. + * + * This function may revert to prevent the operation from being executed. + */ + function tokensToSend( + address operator, + address from, + address to, + uint256 amount, + bytes calldata userData, + bytes calldata operatorData + ) external; +} diff --git a/src/node_modules/@openzeppelin/contracts/token/ERC777/README.adoc b/src/node_modules/@openzeppelin/contracts/token/ERC777/README.adoc new file mode 100644 index 00000000..4dee8a66 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/token/ERC777/README.adoc @@ -0,0 +1,24 @@ += ERC 777 + +[.readme-notice] +NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/token/erc777 + +This set of interfaces and contracts are all related to the [ERC777 token standard](https://eips.ethereum.org/EIPS/eip-777). + +TIP: For an overview of ERC777 tokens and a walk through on how to create a token contract read our xref:ROOT:erc777.adoc[ERC777 guide]. + +The token behavior itself is implemented in the core contracts: {IERC777}, {ERC777}. + +Additionally there are interfaces used to develop contracts that react to token movements: {IERC777Sender}, {IERC777Recipient}. + +== Core + +{{IERC777}} + +{{ERC777}} + +== Hooks + +{{IERC777Sender}} + +{{IERC777Recipient}} diff --git a/src/node_modules/@openzeppelin/contracts/utils/Address.sol b/src/node_modules/@openzeppelin/contracts/utils/Address.sol new file mode 100644 index 00000000..fe3ba653 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/utils/Address.sol @@ -0,0 +1,141 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.2; + +/** + * @dev Collection of functions related to the address type + */ +library Address { + /** + * @dev Returns true if `account` is a contract. + * + * [IMPORTANT] + * ==== + * It is unsafe to assume that an address for which this function returns + * false is an externally-owned account (EOA) and not a contract. + * + * Among others, `isContract` will return false for the following + * types of addresses: + * + * - an externally-owned account + * - a contract in construction + * - an address where a contract will be created + * - an address where a contract lived, but was destroyed + * ==== + */ + function isContract(address account) internal view returns (bool) { + // This method relies in extcodesize, which returns 0 for contracts in + // construction, since the code is only stored at the end of the + // constructor execution. + + uint256 size; + // solhint-disable-next-line no-inline-assembly + assembly { size := extcodesize(account) } + return size > 0; + } + + /** + * @dev Replacement for Solidity's `transfer`: sends `amount` wei to + * `recipient`, forwarding all available gas and reverting on errors. + * + * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost + * of certain opcodes, possibly making contracts go over the 2300 gas limit + * imposed by `transfer`, making them unable to receive funds via + * `transfer`. {sendValue} removes this limitation. + * + * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more]. + * + * IMPORTANT: because control is transferred to `recipient`, care must be + * taken to not create reentrancy vulnerabilities. Consider using + * {ReentrancyGuard} or the + * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern]. + */ + function sendValue(address payable recipient, uint256 amount) internal { + require(address(this).balance >= amount, "Address: insufficient balance"); + + // solhint-disable-next-line avoid-low-level-calls, avoid-call-value + (bool success, ) = recipient.call{ value: amount }(""); + require(success, "Address: unable to send value, recipient may have reverted"); + } + + /** + * @dev Performs a Solidity function call using a low level `call`. A + * plain`call` is an unsafe replacement for a function call: use this + * function instead. + * + * If `target` reverts with a revert reason, it is bubbled up by this + * function (like regular Solidity function calls). + * + * Returns the raw returned data. To convert to the expected return value, + * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`]. + * + * Requirements: + * + * - `target` must be a contract. + * - calling `target` with `data` must not revert. + * + * _Available since v3.1._ + */ + function functionCall(address target, bytes memory data) internal returns (bytes memory) { + return functionCall(target, data, "Address: low-level call failed"); + } + + /** + * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with + * `errorMessage` as a fallback revert reason when `target` reverts. + * + * _Available since v3.1._ + */ + function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) { + return _functionCallWithValue(target, data, 0, errorMessage); + } + + /** + * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], + * but also transferring `value` wei to `target`. + * + * Requirements: + * + * - the calling contract must have an ETH balance of at least `value`. + * - the called Solidity function must be `payable`. + * + * _Available since v3.1._ + */ + function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) { + return functionCallWithValue(target, data, value, "Address: low-level call with value failed"); + } + + /** + * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but + * with `errorMessage` as a fallback revert reason when `target` reverts. + * + * _Available since v3.1._ + */ + function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) { + require(address(this).balance >= value, "Address: insufficient balance for call"); + return _functionCallWithValue(target, data, value, errorMessage); + } + + function _functionCallWithValue(address target, bytes memory data, uint256 weiValue, string memory errorMessage) private returns (bytes memory) { + require(isContract(target), "Address: call to non-contract"); + + // solhint-disable-next-line avoid-low-level-calls + (bool success, bytes memory returndata) = target.call{ value: weiValue }(data); + if (success) { + return returndata; + } else { + // Look for revert reason and bubble it up if present + if (returndata.length > 0) { + // The easiest way to bubble the revert reason is using memory via assembly + + // solhint-disable-next-line no-inline-assembly + assembly { + let returndata_size := mload(returndata) + revert(add(32, returndata), returndata_size) + } + } else { + revert(errorMessage); + } + } + } +} diff --git a/src/node_modules/@openzeppelin/contracts/utils/Arrays.sol b/src/node_modules/@openzeppelin/contracts/utils/Arrays.sol new file mode 100644 index 00000000..8d586e9e --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/utils/Arrays.sol @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../math/Math.sol"; + +/** + * @dev Collection of functions related to array types. + */ +library Arrays { + /** + * @dev Searches a sorted `array` and returns the first index that contains + * a value greater or equal to `element`. If no such index exists (i.e. all + * values in the array are strictly less than `element`), the array length is + * returned. Time complexity O(log n). + * + * `array` is expected to be sorted in ascending order, and to contain no + * repeated elements. + */ + function findUpperBound(uint256[] storage array, uint256 element) internal view returns (uint256) { + if (array.length == 0) { + return 0; + } + + uint256 low = 0; + uint256 high = array.length; + + while (low < high) { + uint256 mid = Math.average(low, high); + + // Note that mid will always be strictly less than high (i.e. it will be a valid array index) + // because Math.average rounds down (it does integer division with truncation). + if (array[mid] > element) { + high = mid; + } else { + low = mid + 1; + } + } + + // At this point `low` is the exclusive upper bound. We will return the inclusive upper bound. + if (low > 0 && array[low - 1] == element) { + return low - 1; + } else { + return low; + } + } +} diff --git a/src/node_modules/@openzeppelin/contracts/utils/Counters.sol b/src/node_modules/@openzeppelin/contracts/utils/Counters.sol new file mode 100644 index 00000000..82cb62bf --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/utils/Counters.sol @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../math/SafeMath.sol"; + +/** + * @title Counters + * @author Matt Condon (@shrugs) + * @dev Provides counters that can only be incremented or decremented by one. This can be used e.g. to track the number + * of elements in a mapping, issuing ERC721 ids, or counting request ids. + * + * Include with `using Counters for Counters.Counter;` + * Since it is not possible to overflow a 256 bit integer with increments of one, `increment` can skip the {SafeMath} + * overflow check, thereby saving gas. This does assume however correct usage, in that the underlying `_value` is never + * directly accessed. + */ +library Counters { + using SafeMath for uint256; + + struct Counter { + // This variable should never be directly accessed by users of the library: interactions must be restricted to + // the library's function. As of Solidity v0.5.2, this cannot be enforced, though there is a proposal to add + // this feature: see https://github.com/ethereum/solidity/issues/4637 + uint256 _value; // default: 0 + } + + function current(Counter storage counter) internal view returns (uint256) { + return counter._value; + } + + function increment(Counter storage counter) internal { + // The {SafeMath} overflow check can be skipped here, see the comment at the top + counter._value += 1; + } + + function decrement(Counter storage counter) internal { + counter._value = counter._value.sub(1); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/utils/Create2.sol b/src/node_modules/@openzeppelin/contracts/utils/Create2.sol new file mode 100644 index 00000000..8f9efa10 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/utils/Create2.sol @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev Helper to make usage of the `CREATE2` EVM opcode easier and safer. + * `CREATE2` can be used to compute in advance the address where a smart + * contract will be deployed, which allows for interesting new mechanisms known + * as 'counterfactual interactions'. + * + * See the https://eips.ethereum.org/EIPS/eip-1014#motivation[EIP] for more + * information. + */ +library Create2 { + /** + * @dev Deploys a contract using `CREATE2`. The address where the contract + * will be deployed can be known in advance via {computeAddress}. + * + * The bytecode for a contract can be obtained from Solidity with + * `type(contractName).creationCode`. + * + * Requirements: + * + * - `bytecode` must not be empty. + * - `salt` must have not been used for `bytecode` already. + * - the factory must have a balance of at least `amount`. + * - if `amount` is non-zero, `bytecode` must have a `payable` constructor. + */ + function deploy(uint256 amount, bytes32 salt, bytes memory bytecode) internal returns (address) { + address addr; + require(address(this).balance >= amount, "Create2: insufficient balance"); + require(bytecode.length != 0, "Create2: bytecode length is zero"); + // solhint-disable-next-line no-inline-assembly + assembly { + addr := create2(amount, add(bytecode, 0x20), mload(bytecode), salt) + } + require(addr != address(0), "Create2: Failed on deploy"); + return addr; + } + + /** + * @dev Returns the address where a contract will be stored if deployed via {deploy}. Any change in the + * `bytecodeHash` or `salt` will result in a new destination address. + */ + function computeAddress(bytes32 salt, bytes32 bytecodeHash) internal view returns (address) { + return computeAddress(salt, bytecodeHash, address(this)); + } + + /** + * @dev Returns the address where a contract will be stored if deployed via {deploy} from a contract located at + * `deployer`. If `deployer` is this contract's address, returns the same value as {computeAddress}. + */ + function computeAddress(bytes32 salt, bytes32 bytecodeHash, address deployer) internal pure returns (address) { + bytes32 _data = keccak256( + abi.encodePacked(bytes1(0xff), deployer, salt, bytecodeHash) + ); + return address(uint256(_data)); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/utils/EnumerableMap.sol b/src/node_modules/@openzeppelin/contracts/utils/EnumerableMap.sol new file mode 100644 index 00000000..82eb04c9 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/utils/EnumerableMap.sol @@ -0,0 +1,237 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev Library for managing an enumerable variant of Solidity's + * https://solidity.readthedocs.io/en/latest/types.html#mapping-types[`mapping`] + * type. + * + * Maps have the following properties: + * + * - Entries are added, removed, and checked for existence in constant time + * (O(1)). + * - Entries are enumerated in O(n). No guarantees are made on the ordering. + * + * ``` + * contract Example { + * // Add the library methods + * using EnumerableMap for EnumerableMap.UintToAddressMap; + * + * // Declare a set state variable + * EnumerableMap.UintToAddressMap private myMap; + * } + * ``` + * + * As of v3.0.0, only maps of type `uint256 -> address` (`UintToAddressMap`) are + * supported. + */ +library EnumerableMap { + // To implement this library for multiple types with as little code + // repetition as possible, we write it in terms of a generic Map type with + // bytes32 keys and values. + // The Map implementation uses private functions, and user-facing + // implementations (such as Uint256ToAddressMap) are just wrappers around + // the underlying Map. + // This means that we can only create new EnumerableMaps for types that fit + // in bytes32. + + struct MapEntry { + bytes32 _key; + bytes32 _value; + } + + struct Map { + // Storage of map keys and values + MapEntry[] _entries; + + // Position of the entry defined by a key in the `entries` array, plus 1 + // because index 0 means a key is not in the map. + mapping (bytes32 => uint256) _indexes; + } + + /** + * @dev Adds a key-value pair to a map, or updates the value for an existing + * key. O(1). + * + * Returns true if the key was added to the map, that is if it was not + * already present. + */ + function _set(Map storage map, bytes32 key, bytes32 value) private returns (bool) { + // We read and store the key's index to prevent multiple reads from the same storage slot + uint256 keyIndex = map._indexes[key]; + + if (keyIndex == 0) { // Equivalent to !contains(map, key) + map._entries.push(MapEntry({ _key: key, _value: value })); + // The entry is stored at length-1, but we add 1 to all indexes + // and use 0 as a sentinel value + map._indexes[key] = map._entries.length; + return true; + } else { + map._entries[keyIndex - 1]._value = value; + return false; + } + } + + /** + * @dev Removes a key-value pair from a map. O(1). + * + * Returns true if the key was removed from the map, that is if it was present. + */ + function _remove(Map storage map, bytes32 key) private returns (bool) { + // We read and store the key's index to prevent multiple reads from the same storage slot + uint256 keyIndex = map._indexes[key]; + + if (keyIndex != 0) { // Equivalent to contains(map, key) + // To delete a key-value pair from the _entries array in O(1), we swap the entry to delete with the last one + // in the array, and then remove the last entry (sometimes called as 'swap and pop'). + // This modifies the order of the array, as noted in {at}. + + uint256 toDeleteIndex = keyIndex - 1; + uint256 lastIndex = map._entries.length - 1; + + // When the entry to delete is the last one, the swap operation is unnecessary. However, since this occurs + // so rarely, we still do the swap anyway to avoid the gas cost of adding an 'if' statement. + + MapEntry storage lastEntry = map._entries[lastIndex]; + + // Move the last entry to the index where the entry to delete is + map._entries[toDeleteIndex] = lastEntry; + // Update the index for the moved entry + map._indexes[lastEntry._key] = toDeleteIndex + 1; // All indexes are 1-based + + // Delete the slot where the moved entry was stored + map._entries.pop(); + + // Delete the index for the deleted slot + delete map._indexes[key]; + + return true; + } else { + return false; + } + } + + /** + * @dev Returns true if the key is in the map. O(1). + */ + function _contains(Map storage map, bytes32 key) private view returns (bool) { + return map._indexes[key] != 0; + } + + /** + * @dev Returns the number of key-value pairs in the map. O(1). + */ + function _length(Map storage map) private view returns (uint256) { + return map._entries.length; + } + + /** + * @dev Returns the key-value pair stored at position `index` in the map. O(1). + * + * Note that there are no guarantees on the ordering of entries inside the + * array, and it may change when more entries are added or removed. + * + * Requirements: + * + * - `index` must be strictly less than {length}. + */ + function _at(Map storage map, uint256 index) private view returns (bytes32, bytes32) { + require(map._entries.length > index, "EnumerableMap: index out of bounds"); + + MapEntry storage entry = map._entries[index]; + return (entry._key, entry._value); + } + + /** + * @dev Returns the value associated with `key`. O(1). + * + * Requirements: + * + * - `key` must be in the map. + */ + function _get(Map storage map, bytes32 key) private view returns (bytes32) { + return _get(map, key, "EnumerableMap: nonexistent key"); + } + + /** + * @dev Same as {_get}, with a custom error message when `key` is not in the map. + */ + function _get(Map storage map, bytes32 key, string memory errorMessage) private view returns (bytes32) { + uint256 keyIndex = map._indexes[key]; + require(keyIndex != 0, errorMessage); // Equivalent to contains(map, key) + return map._entries[keyIndex - 1]._value; // All indexes are 1-based + } + + // UintToAddressMap + + struct UintToAddressMap { + Map _inner; + } + + /** + * @dev Adds a key-value pair to a map, or updates the value for an existing + * key. O(1). + * + * Returns true if the key was added to the map, that is if it was not + * already present. + */ + function set(UintToAddressMap storage map, uint256 key, address value) internal returns (bool) { + return _set(map._inner, bytes32(key), bytes32(uint256(value))); + } + + /** + * @dev Removes a value from a set. O(1). + * + * Returns true if the key was removed from the map, that is if it was present. + */ + function remove(UintToAddressMap storage map, uint256 key) internal returns (bool) { + return _remove(map._inner, bytes32(key)); + } + + /** + * @dev Returns true if the key is in the map. O(1). + */ + function contains(UintToAddressMap storage map, uint256 key) internal view returns (bool) { + return _contains(map._inner, bytes32(key)); + } + + /** + * @dev Returns the number of elements in the map. O(1). + */ + function length(UintToAddressMap storage map) internal view returns (uint256) { + return _length(map._inner); + } + + /** + * @dev Returns the element stored at position `index` in the set. O(1). + * Note that there are no guarantees on the ordering of values inside the + * array, and it may change when more values are added or removed. + * + * Requirements: + * + * - `index` must be strictly less than {length}. + */ + function at(UintToAddressMap storage map, uint256 index) internal view returns (uint256, address) { + (bytes32 key, bytes32 value) = _at(map._inner, index); + return (uint256(key), address(uint256(value))); + } + + /** + * @dev Returns the value associated with `key`. O(1). + * + * Requirements: + * + * - `key` must be in the map. + */ + function get(UintToAddressMap storage map, uint256 key) internal view returns (address) { + return address(uint256(_get(map._inner, bytes32(key)))); + } + + /** + * @dev Same as {get}, with a custom error message when `key` is not in the map. + */ + function get(UintToAddressMap storage map, uint256 key, string memory errorMessage) internal view returns (address) { + return address(uint256(_get(map._inner, bytes32(key), errorMessage))); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/utils/EnumerableSet.sol b/src/node_modules/@openzeppelin/contracts/utils/EnumerableSet.sol new file mode 100644 index 00000000..7f4c761a --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/utils/EnumerableSet.sol @@ -0,0 +1,243 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev Library for managing + * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive + * types. + * + * Sets have the following properties: + * + * - Elements are added, removed, and checked for existence in constant time + * (O(1)). + * - Elements are enumerated in O(n). No guarantees are made on the ordering. + * + * ``` + * contract Example { + * // Add the library methods + * using EnumerableSet for EnumerableSet.AddressSet; + * + * // Declare a set state variable + * EnumerableSet.AddressSet private mySet; + * } + * ``` + * + * As of v3.0.0, only sets of type `address` (`AddressSet`) and `uint256` + * (`UintSet`) are supported. + */ +library EnumerableSet { + // To implement this library for multiple types with as little code + // repetition as possible, we write it in terms of a generic Set type with + // bytes32 values. + // The Set implementation uses private functions, and user-facing + // implementations (such as AddressSet) are just wrappers around the + // underlying Set. + // This means that we can only create new EnumerableSets for types that fit + // in bytes32. + + struct Set { + // Storage of set values + bytes32[] _values; + + // Position of the value in the `values` array, plus 1 because index 0 + // means a value is not in the set. + mapping (bytes32 => uint256) _indexes; + } + + /** + * @dev Add a value to a set. O(1). + * + * Returns true if the value was added to the set, that is if it was not + * already present. + */ + function _add(Set storage set, bytes32 value) private returns (bool) { + if (!_contains(set, value)) { + set._values.push(value); + // The value is stored at length-1, but we add 1 to all indexes + // and use 0 as a sentinel value + set._indexes[value] = set._values.length; + return true; + } else { + return false; + } + } + + /** + * @dev Removes a value from a set. O(1). + * + * Returns true if the value was removed from the set, that is if it was + * present. + */ + function _remove(Set storage set, bytes32 value) private returns (bool) { + // We read and store the value's index to prevent multiple reads from the same storage slot + uint256 valueIndex = set._indexes[value]; + + if (valueIndex != 0) { // Equivalent to contains(set, value) + // To delete an element from the _values array in O(1), we swap the element to delete with the last one in + // the array, and then remove the last element (sometimes called as 'swap and pop'). + // This modifies the order of the array, as noted in {at}. + + uint256 toDeleteIndex = valueIndex - 1; + uint256 lastIndex = set._values.length - 1; + + // When the value to delete is the last one, the swap operation is unnecessary. However, since this occurs + // so rarely, we still do the swap anyway to avoid the gas cost of adding an 'if' statement. + + bytes32 lastvalue = set._values[lastIndex]; + + // Move the last value to the index where the value to delete is + set._values[toDeleteIndex] = lastvalue; + // Update the index for the moved value + set._indexes[lastvalue] = toDeleteIndex + 1; // All indexes are 1-based + + // Delete the slot where the moved value was stored + set._values.pop(); + + // Delete the index for the deleted slot + delete set._indexes[value]; + + return true; + } else { + return false; + } + } + + /** + * @dev Returns true if the value is in the set. O(1). + */ + function _contains(Set storage set, bytes32 value) private view returns (bool) { + return set._indexes[value] != 0; + } + + /** + * @dev Returns the number of values on the set. O(1). + */ + function _length(Set storage set) private view returns (uint256) { + return set._values.length; + } + + /** + * @dev Returns the value stored at position `index` in the set. O(1). + * + * Note that there are no guarantees on the ordering of values inside the + * array, and it may change when more values are added or removed. + * + * Requirements: + * + * - `index` must be strictly less than {length}. + */ + function _at(Set storage set, uint256 index) private view returns (bytes32) { + require(set._values.length > index, "EnumerableSet: index out of bounds"); + return set._values[index]; + } + + // AddressSet + + struct AddressSet { + Set _inner; + } + + /** + * @dev Add a value to a set. O(1). + * + * Returns true if the value was added to the set, that is if it was not + * already present. + */ + function add(AddressSet storage set, address value) internal returns (bool) { + return _add(set._inner, bytes32(uint256(value))); + } + + /** + * @dev Removes a value from a set. O(1). + * + * Returns true if the value was removed from the set, that is if it was + * present. + */ + function remove(AddressSet storage set, address value) internal returns (bool) { + return _remove(set._inner, bytes32(uint256(value))); + } + + /** + * @dev Returns true if the value is in the set. O(1). + */ + function contains(AddressSet storage set, address value) internal view returns (bool) { + return _contains(set._inner, bytes32(uint256(value))); + } + + /** + * @dev Returns the number of values in the set. O(1). + */ + function length(AddressSet storage set) internal view returns (uint256) { + return _length(set._inner); + } + + /** + * @dev Returns the value stored at position `index` in the set. O(1). + * + * Note that there are no guarantees on the ordering of values inside the + * array, and it may change when more values are added or removed. + * + * Requirements: + * + * - `index` must be strictly less than {length}. + */ + function at(AddressSet storage set, uint256 index) internal view returns (address) { + return address(uint256(_at(set._inner, index))); + } + + + // UintSet + + struct UintSet { + Set _inner; + } + + /** + * @dev Add a value to a set. O(1). + * + * Returns true if the value was added to the set, that is if it was not + * already present. + */ + function add(UintSet storage set, uint256 value) internal returns (bool) { + return _add(set._inner, bytes32(value)); + } + + /** + * @dev Removes a value from a set. O(1). + * + * Returns true if the value was removed from the set, that is if it was + * present. + */ + function remove(UintSet storage set, uint256 value) internal returns (bool) { + return _remove(set._inner, bytes32(value)); + } + + /** + * @dev Returns true if the value is in the set. O(1). + */ + function contains(UintSet storage set, uint256 value) internal view returns (bool) { + return _contains(set._inner, bytes32(value)); + } + + /** + * @dev Returns the number of values on the set. O(1). + */ + function length(UintSet storage set) internal view returns (uint256) { + return _length(set._inner); + } + + /** + * @dev Returns the value stored at position `index` in the set. O(1). + * + * Note that there are no guarantees on the ordering of values inside the + * array, and it may change when more values are added or removed. + * + * Requirements: + * + * - `index` must be strictly less than {length}. + */ + function at(UintSet storage set, uint256 index) internal view returns (uint256) { + return uint256(_at(set._inner, index)); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/utils/Pausable.sol b/src/node_modules/@openzeppelin/contracts/utils/Pausable.sol new file mode 100644 index 00000000..9e1b42ab --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/utils/Pausable.sol @@ -0,0 +1,90 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../GSN/Context.sol"; + +/** + * @dev Contract module which allows children to implement an emergency stop + * mechanism that can be triggered by an authorized account. + * + * This module is used through inheritance. It will make available the + * modifiers `whenNotPaused` and `whenPaused`, which can be applied to + * the functions of your contract. Note that they will not be pausable by + * simply including this module, only once the modifiers are put in place. + */ +contract Pausable is Context { + /** + * @dev Emitted when the pause is triggered by `account`. + */ + event Paused(address account); + + /** + * @dev Emitted when the pause is lifted by `account`. + */ + event Unpaused(address account); + + bool private _paused; + + /** + * @dev Initializes the contract in unpaused state. + */ + constructor () internal { + _paused = false; + } + + /** + * @dev Returns true if the contract is paused, and false otherwise. + */ + function paused() public view returns (bool) { + return _paused; + } + + /** + * @dev Modifier to make a function callable only when the contract is not paused. + * + * Requirements: + * + * - The contract must not be paused. + */ + modifier whenNotPaused() { + require(!_paused, "Pausable: paused"); + _; + } + + /** + * @dev Modifier to make a function callable only when the contract is paused. + * + * Requirements: + * + * - The contract must be paused. + */ + modifier whenPaused() { + require(_paused, "Pausable: not paused"); + _; + } + + /** + * @dev Triggers stopped state. + * + * Requirements: + * + * - The contract must not be paused. + */ + function _pause() internal virtual whenNotPaused { + _paused = true; + emit Paused(_msgSender()); + } + + /** + * @dev Returns to normal state. + * + * Requirements: + * + * - The contract must be paused. + */ + function _unpause() internal virtual whenPaused { + _paused = false; + emit Unpaused(_msgSender()); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/utils/README.adoc b/src/node_modules/@openzeppelin/contracts/utils/README.adoc new file mode 100644 index 00000000..3773c411 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/utils/README.adoc @@ -0,0 +1,52 @@ += Utilities + +[.readme-notice] +NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/utils + +Miscellaneous contracts and libraries containing utility functions you can use to improve security, work with new data types, or safely use low-level primitives. + +Security tools include: + + * {Pausable}: provides a simple way to halt activity in your contracts (often in response to an external threat). + * {ReentrancyGuard}: protects you from https://blog.openzeppelin.com/reentrancy-after-istanbul/[reentrant calls]. + +The {Address}, {Arrays} and {Strings} libraries provide more operations related to these native data types, while {SafeCast} adds ways to safely convert between the different signed and unsigned numeric types. + +For new data types: + + * {Counters}: a simple way to get a counter that can only be incremented or decremented. Very useful for ID generation, counting contract activity, among others. + * {EnumerableMap}: like Solidity's https://solidity.readthedocs.io/en/latest/types.html#mapping-types[`mapping`] type, but with key-value _enumeration_: this will let you know how many entries a mapping has, and iterate over them (which is not possible with `mapping`). + * {EnumerableSet}: like {EnumerableMap}, but for https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets]. Can be used to store privileged accounts, issued IDs, etc. + +[NOTE] +==== +Because Solidity does not support generic types, {EnumerableMap} and {EnumerableSet} are specialized to a limited number of key-value types. + +As of v3.0, {EnumerableMap} supports `uint256 -> address` (`UintToAddressMap`), and {EnumerableSet} supports `address` and `uint256` (`AddressSet` and `UintSet`). +==== + +Finally, {Create2} contains all necessary utilities to safely use the https://blog.openzeppelin.com/getting-the-most-out-of-create2/[`CREATE2` EVM opcode], without having to deal with low-level assembly. + +== Contracts + +{{Pausable}} + +{{ReentrancyGuard}} + +== Libraries + +{{Address}} + +{{Arrays}} + +{{Counters}} + +{{Create2}} + +{{EnumerableMap}} + +{{EnumerableSet}} + +{{SafeCast}} + +{{Strings}} diff --git a/src/node_modules/@openzeppelin/contracts/utils/ReentrancyGuard.sol b/src/node_modules/@openzeppelin/contracts/utils/ReentrancyGuard.sol new file mode 100644 index 00000000..b7bea91f --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/utils/ReentrancyGuard.sol @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev Contract module that helps prevent reentrant calls to a function. + * + * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier + * available, which can be applied to functions to make sure there are no nested + * (reentrant) calls to them. + * + * Note that because there is a single `nonReentrant` guard, functions marked as + * `nonReentrant` may not call one another. This can be worked around by making + * those functions `private`, and then adding `external` `nonReentrant` entry + * points to them. + * + * TIP: If you would like to learn more about reentrancy and alternative ways + * to protect against it, check out our blog post + * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul]. + */ +contract ReentrancyGuard { + // Booleans are more expensive than uint256 or any type that takes up a full + // word because each write operation emits an extra SLOAD to first read the + // slot's contents, replace the bits taken up by the boolean, and then write + // back. This is the compiler's defense against contract upgrades and + // pointer aliasing, and it cannot be disabled. + + // The values being non-zero value makes deployment a bit more expensive, + // but in exchange the refund on every call to nonReentrant will be lower in + // amount. Since refunds are capped to a percentage of the total + // transaction's gas, it is best to keep them low in cases like this one, to + // increase the likelihood of the full refund coming into effect. + uint256 private constant _NOT_ENTERED = 1; + uint256 private constant _ENTERED = 2; + + uint256 private _status; + + constructor () internal { + _status = _NOT_ENTERED; + } + + /** + * @dev Prevents a contract from calling itself, directly or indirectly. + * Calling a `nonReentrant` function from another `nonReentrant` + * function is not supported. It is possible to prevent this from happening + * by making the `nonReentrant` function external, and make it call a + * `private` function that does the actual work. + */ + modifier nonReentrant() { + // On the first call to nonReentrant, _notEntered will be true + require(_status != _ENTERED, "ReentrancyGuard: reentrant call"); + + // Any calls to nonReentrant after this point will fail + _status = _ENTERED; + + _; + + // By storing the original value once again, a refund is triggered (see + // https://eips.ethereum.org/EIPS/eip-2200) + _status = _NOT_ENTERED; + } +} diff --git a/src/node_modules/@openzeppelin/contracts/utils/SafeCast.sol b/src/node_modules/@openzeppelin/contracts/utils/SafeCast.sol new file mode 100644 index 00000000..9d283ff2 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/utils/SafeCast.sol @@ -0,0 +1,211 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + + +/** + * @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow + * checks. + * + * Downcasting from uint256/int256 in Solidity does not revert on overflow. This can + * easily result in undesired exploitation or bugs, since developers usually + * assume that overflows raise errors. `SafeCast` restores this intuition by + * reverting the transaction when such an operation overflows. + * + * Using this library instead of the unchecked operations eliminates an entire + * class of bugs, so it's recommended to use it always. + * + * Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller types, by performing + * all math on `uint256` and `int256` and then downcasting. + */ +library SafeCast { + + /** + * @dev Returns the downcasted uint128 from uint256, reverting on + * overflow (when the input is greater than largest uint128). + * + * Counterpart to Solidity's `uint128` operator. + * + * Requirements: + * + * - input must fit into 128 bits + */ + function toUint128(uint256 value) internal pure returns (uint128) { + require(value < 2**128, "SafeCast: value doesn\'t fit in 128 bits"); + return uint128(value); + } + + /** + * @dev Returns the downcasted uint64 from uint256, reverting on + * overflow (when the input is greater than largest uint64). + * + * Counterpart to Solidity's `uint64` operator. + * + * Requirements: + * + * - input must fit into 64 bits + */ + function toUint64(uint256 value) internal pure returns (uint64) { + require(value < 2**64, "SafeCast: value doesn\'t fit in 64 bits"); + return uint64(value); + } + + /** + * @dev Returns the downcasted uint32 from uint256, reverting on + * overflow (when the input is greater than largest uint32). + * + * Counterpart to Solidity's `uint32` operator. + * + * Requirements: + * + * - input must fit into 32 bits + */ + function toUint32(uint256 value) internal pure returns (uint32) { + require(value < 2**32, "SafeCast: value doesn\'t fit in 32 bits"); + return uint32(value); + } + + /** + * @dev Returns the downcasted uint16 from uint256, reverting on + * overflow (when the input is greater than largest uint16). + * + * Counterpart to Solidity's `uint16` operator. + * + * Requirements: + * + * - input must fit into 16 bits + */ + function toUint16(uint256 value) internal pure returns (uint16) { + require(value < 2**16, "SafeCast: value doesn\'t fit in 16 bits"); + return uint16(value); + } + + /** + * @dev Returns the downcasted uint8 from uint256, reverting on + * overflow (when the input is greater than largest uint8). + * + * Counterpart to Solidity's `uint8` operator. + * + * Requirements: + * + * - input must fit into 8 bits. + */ + function toUint8(uint256 value) internal pure returns (uint8) { + require(value < 2**8, "SafeCast: value doesn\'t fit in 8 bits"); + return uint8(value); + } + + /** + * @dev Converts a signed int256 into an unsigned uint256. + * + * Requirements: + * + * - input must be greater than or equal to 0. + */ + function toUint256(int256 value) internal pure returns (uint256) { + require(value >= 0, "SafeCast: value must be positive"); + return uint256(value); + } + + /** + * @dev Returns the downcasted int128 from int256, reverting on + * overflow (when the input is less than smallest int128 or + * greater than largest int128). + * + * Counterpart to Solidity's `int128` operator. + * + * Requirements: + * + * - input must fit into 128 bits + * + * _Available since v3.1._ + */ + function toInt128(int256 value) internal pure returns (int128) { + require(value >= -2**127 && value < 2**127, "SafeCast: value doesn\'t fit in 128 bits"); + return int128(value); + } + + /** + * @dev Returns the downcasted int64 from int256, reverting on + * overflow (when the input is less than smallest int64 or + * greater than largest int64). + * + * Counterpart to Solidity's `int64` operator. + * + * Requirements: + * + * - input must fit into 64 bits + * + * _Available since v3.1._ + */ + function toInt64(int256 value) internal pure returns (int64) { + require(value >= -2**63 && value < 2**63, "SafeCast: value doesn\'t fit in 64 bits"); + return int64(value); + } + + /** + * @dev Returns the downcasted int32 from int256, reverting on + * overflow (when the input is less than smallest int32 or + * greater than largest int32). + * + * Counterpart to Solidity's `int32` operator. + * + * Requirements: + * + * - input must fit into 32 bits + * + * _Available since v3.1._ + */ + function toInt32(int256 value) internal pure returns (int32) { + require(value >= -2**31 && value < 2**31, "SafeCast: value doesn\'t fit in 32 bits"); + return int32(value); + } + + /** + * @dev Returns the downcasted int16 from int256, reverting on + * overflow (when the input is less than smallest int16 or + * greater than largest int16). + * + * Counterpart to Solidity's `int16` operator. + * + * Requirements: + * + * - input must fit into 16 bits + * + * _Available since v3.1._ + */ + function toInt16(int256 value) internal pure returns (int16) { + require(value >= -2**15 && value < 2**15, "SafeCast: value doesn\'t fit in 16 bits"); + return int16(value); + } + + /** + * @dev Returns the downcasted int8 from int256, reverting on + * overflow (when the input is less than smallest int8 or + * greater than largest int8). + * + * Counterpart to Solidity's `int8` operator. + * + * Requirements: + * + * - input must fit into 8 bits. + * + * _Available since v3.1._ + */ + function toInt8(int256 value) internal pure returns (int8) { + require(value >= -2**7 && value < 2**7, "SafeCast: value doesn\'t fit in 8 bits"); + return int8(value); + } + + /** + * @dev Converts an unsigned uint256 into a signed int256. + * + * Requirements: + * + * - input must be less than or equal to maxInt256. + */ + function toInt256(uint256 value) internal pure returns (int256) { + require(value < 2**255, "SafeCast: value doesn't fit in an int256"); + return int256(value); + } +} diff --git a/src/node_modules/@openzeppelin/contracts/utils/Strings.sol b/src/node_modules/@openzeppelin/contracts/utils/Strings.sol new file mode 100644 index 00000000..aacce819 --- /dev/null +++ b/src/node_modules/@openzeppelin/contracts/utils/Strings.sol @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +/** + * @dev String operations. + */ +library Strings { + /** + * @dev Converts a `uint256` to its ASCII `string` representation. + */ + function toString(uint256 value) internal pure returns (string memory) { + // Inspired by OraclizeAPI's implementation - MIT licence + // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol + + if (value == 0) { + return "0"; + } + uint256 temp = value; + uint256 digits; + while (temp != 0) { + digits++; + temp /= 10; + } + bytes memory buffer = new bytes(digits); + uint256 index = digits - 1; + temp = value; + while (temp != 0) { + buffer[index--] = byte(uint8(48 + temp % 10)); + temp /= 10; + } + return string(buffer); + } +} diff --git a/src/package-lock.json b/src/package-lock.json new file mode 100644 index 00000000..25780f9f --- /dev/null +++ b/src/package-lock.json @@ -0,0 +1,27 @@ +{ + "name": "hbswap", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "@openzeppelin/contracts": "^3.2.0" + } + }, + "node_modules/@openzeppelin/contracts": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-3.2.0.tgz", + "integrity": "sha512-bUOmkSoPkjnUyMiKo6RYnb0VHBk5D9KKDAgNLzF41aqAM3TeE0yGdFF5dVRcV60pZdJLlyFT/jjXIZCWyyEzAQ==" + } + }, + "dependencies": { + "@openzeppelin/contracts": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-3.2.0.tgz", + "integrity": "sha512-bUOmkSoPkjnUyMiKo6RYnb0VHBk5D9KKDAgNLzF41aqAM3TeE0yGdFF5dVRcV60pZdJLlyFT/jjXIZCWyyEzAQ==" + } + } +} diff --git a/src/package.json b/src/package.json new file mode 100644 index 00000000..548f8294 --- /dev/null +++ b/src/package.json @@ -0,0 +1,14 @@ +{ + "name": "hbswap", + "version": "1.0.0", + "description": "", + "main": "truffle-config.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "@openzeppelin/contracts": "^3.2.0" + } +} diff --git a/src/python/.gitignore b/src/python/.gitignore new file mode 100644 index 00000000..3864a164 --- /dev/null +++ b/src/python/.gitignore @@ -0,0 +1,2 @@ +*.egg* +*.pyc diff --git a/src/python/Makefile b/src/python/Makefile new file mode 100644 index 00000000..92c9f9ba --- /dev/null +++ b/src/python/Makefile @@ -0,0 +1,85 @@ +.PHONY: clean clean-test clean-pyc clean-build docs help +.DEFAULT_GOAL := help + +define BROWSER_PYSCRIPT +import os, webbrowser, sys + +from urllib.request import pathname2url + +webbrowser.open("http://localhost:57781/" + sys.argv[1]) +endef +export BROWSER_PYSCRIPT + +define PRINT_HELP_PYSCRIPT +import re, sys + +for line in sys.stdin: + match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line) + if match: + target, help = match.groups() + print("%-20s %s" % (target, help)) +endef +export PRINT_HELP_PYSCRIPT + +BROWSER := python -c "$$BROWSER_PYSCRIPT" + +help: + @python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) + +clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts + +clean-build: ## remove build artifacts + rm -fr build/ + rm -fr dist/ + rm -fr .eggs/ + find . -name '*.egg-info' -exec rm -fr {} + + find . -name '*.egg' -exec rm -f {} + + +clean-pyc: ## remove Python file artifacts + find . -name '*.pyc' -exec rm -f {} + + find . -name '*.pyo' -exec rm -f {} + + find . -name '*~' -exec rm -f {} + + find . -name '__pycache__' -exec rm -fr {} + + +clean-test: ## remove test and coverage artifacts + rm -fr .tox/ + rm -f .coverage + rm -fr htmlcov/ + rm -fr .pytest_cache + +lint: ## check style with flake8 + flake8 honeybadgerswap tests + +test: ## run tests quickly with the default Python + pytest + +test-all: ## run tests on every Python version with tox + tox + +coverage: ## check code coverage quickly with the default Python + pytest --verbose --cov --cov-report=term-missing --cov-report=html + +docs: ## generate Sphinx HTML documentation + docker-compose -f docs.yml run --rm builddocs make -C docs clean + docker-compose -f docs.yml up builddocs + docker-compose -f docs.yml stop viewdocs + docker-compose -f docs.yml up -d viewdocs + +servedocs: docs ## compile the docs watching for changes + docker-compose -f docs.yml stop viewdocs + docker-compose -f docs.yml up -d viewdocs + $(BROWSER) index.html + +release: dist ## package and upload a release + twine upload dist/* + +dist: clean ## builds source and wheel package + python setup.py sdist + python setup.py bdist_wheel + ls -l dist + +install: clean ## install the package to the active Python's site-packages + python setup.py install + +develop: clean + pip install --editable . diff --git a/src/python/README.md b/src/python/README.md new file mode 100644 index 00000000..54b971e1 --- /dev/null +++ b/src/python/README.md @@ -0,0 +1 @@ +# HoneyBadgerSwap Python Functions diff --git a/src/python/honeybadgerswap/__init__.py b/src/python/honeybadgerswap/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/python/honeybadgerswap/client/Client.py b/src/python/honeybadgerswap/client/Client.py new file mode 100644 index 00000000..92e9eba6 --- /dev/null +++ b/src/python/honeybadgerswap/client/Client.py @@ -0,0 +1,123 @@ +import asyncio +import re +import toml + +from aiohttp import ClientSession + +from ..utils import get_inverse, p + + +class Client: + def __init__(self, n, t, servers): + self.n = n + self.t = t + self.servers = servers + + @classmethod + def from_toml_config(self, config_file): + config = toml.load(config_file) + + n = config["n"] + t = config["t"] + servers = config["servers"] + + return Client(n, t, servers) + + async def send_request(self, url): + async with ClientSession() as session: + async with session.get(url) as resp: + json_response = await resp.json() + return json_response + + def interpolate(self, shares): + inputmask = 0 + for i in range(1, self.n + 1): + tot = 1 + for j in range(1, self.n + 1): + if i == j: + continue + tot = tot * j * get_inverse(j - i) % p + inputmask = (inputmask + shares[i - 1] * tot) % p + return inputmask + + # **** call from remote client **** + async def req_inputmask_shares(self, host, port, inputmask_idxes): + url = f"http://{host}:{port}/inputmasks/{inputmask_idxes}" + result = await self.send_request(url) + return re.split(",", result["inputmask_shares"]) + + async def get_inputmasks(self, inputmask_idxes): + tasks = [] + for server in self.servers: + host = server["host"] + port = server["http_port"] + + task = asyncio.ensure_future( + self.req_inputmask_shares(host, port, inputmask_idxes) + ) + tasks.append(task) + + for task in tasks: + await task + + inputmask_shares = [] + for task in tasks: + inputmask_shares.append(task.result()) + + inputmasks = [] + for i in range(len(tasks[0].result())): + shares = [] + for j in range(len(self.servers)): + shares.append(int(inputmask_shares[j][i])) + inputmasks.append(self.interpolate(shares)) + return inputmasks + + async def req_balance_shares(self, host, port, token, user): + url = f"http://{host}:{port}/balance/{token}{user}" + result = await self.send_request(url) + return result["balance"] + + async def get_balance(self, token, user): + tasks = [] + for server in self.servers: + host = server["host"] + port = server["http_port"] + + task = asyncio.ensure_future( + self.req_balance_shares(host, port, token, user) + ) + tasks.append(task) + + for task in tasks: + await task + + shares = [] + for task in tasks: + shares.append(int(task.result())) + + balance = self.interpolate(shares) + return balance + + async def req_price(self, host, port, seq): + url = f"http://{host}:{port}/price/{seq}" + result = await self.send_request(url) + return result["price"] + + async def get_price(self, seq): + tasks = [] + for server in self.servers: + host = server["host"] + port = server["http_port"] + + task = asyncio.ensure_future(self.req_price(host, port, seq)) + tasks.append(task) + + for task in tasks: + await task + + shares = [] + for task in tasks: + shares.append(int(task.result())) + + price = self.interpolate(shares) + return price diff --git a/src/python/honeybadgerswap/client/__init__.py b/src/python/honeybadgerswap/client/__init__.py new file mode 100644 index 00000000..153b5ff7 --- /dev/null +++ b/src/python/honeybadgerswap/client/__init__.py @@ -0,0 +1 @@ +from .Client import Client # noqa F401 diff --git a/src/python/honeybadgerswap/client/req_balance.py b/src/python/honeybadgerswap/client/req_balance.py new file mode 100644 index 00000000..043c9856 --- /dev/null +++ b/src/python/honeybadgerswap/client/req_balance.py @@ -0,0 +1,18 @@ +import asyncio +import os +import sys + +from .Client import Client + +from ..utils import fp + +if __name__ == "__main__": + token = sys.argv[1] + user = sys.argv[2] + + config = os.getenv("HBSWAP_CLIENT_CONFIG", "/opt/hbswap/conf/client.toml") + client = Client.from_toml_config(config) + + balance = asyncio.run(client.get_balance(token, user)) + balance = 1.0 * balance / (2 ** fp) + print(balance) diff --git a/src/python/honeybadgerswap/client/req_inputmasks.py b/src/python/honeybadgerswap/client/req_inputmasks.py new file mode 100644 index 00000000..ae60beef --- /dev/null +++ b/src/python/honeybadgerswap/client/req_inputmasks.py @@ -0,0 +1,31 @@ +import asyncio +import os +import sys + +from .Client import Client + +from ..utils import from_float + +if __name__ == "__main__": + config = os.getenv("HBSWAP_CLIENT_CONFIG", "/opt/hbswap/conf/client.toml") + client = Client.from_toml_config(config) + + inputmask_indexes = "" + for inputmask_index in sys.argv[1::2]: + inputmask_indexes += f"{inputmask_index}," + inputmask_indexes = inputmask_indexes[:-1] + + inputmasks = asyncio.run(client.get_inputmasks(inputmask_indexes)) + + values = [] + for value in sys.argv[2::2]: + values.append(from_float(value)) + + masked_values = [] + for (inputmask, value) in zip(inputmasks, values): + masked_values.append(value + inputmask) + + out = "" + for masked_value in masked_values: + out += f"{masked_value} " + print(out) diff --git a/src/python/honeybadgerswap/client/req_price.py b/src/python/honeybadgerswap/client/req_price.py new file mode 100644 index 00000000..1c6bebcb --- /dev/null +++ b/src/python/honeybadgerswap/client/req_price.py @@ -0,0 +1,17 @@ +import asyncio +import os +import sys + +from Client import Client + +from ..utils import fp + +if __name__ == "__main__": + seq = sys.argv[1] + + config = os.getenv("HBSWAP_CLIENT_CONFIG", "/opt/hbswap/conf/client.toml") + client = Client.from_toml_config(config) + + price = asyncio.run(client.get_price(seq)) + price = 1.0 * price / (2 ** fp) + print(price) diff --git a/src/python/honeybadgerswap/server/Server.py b/src/python/honeybadgerswap/server/Server.py new file mode 100644 index 00000000..85dd9f78 --- /dev/null +++ b/src/python/honeybadgerswap/server/Server.py @@ -0,0 +1,80 @@ +import asyncio +import re +import time + +from aiohttp import web + +from ..utils import location_db, openDB, from_hex + + +class Server: + def __init__(self, n, t, server_id, host, http_port): + self.n = n + self.t = t + self.server_id = server_id + + self.host = host + self.http_port = http_port + + print(f"http server {server_id} is running...") + + def dbGet(self, key): + while True: + try: + db = openDB(location_db(self.server_id)) + return from_hex(bytes(db.Get(key))) + except: + print(f"Inputmask share {key} not ready. Try again...") + time.sleep(5) + + async def http_server(self): + routes = web.RouteTableDef() + + @routes.get("/inputmasks/{mask_idxes}") + # async def inputmask_handler(request): + async def _handler(request): + print(f"request: {request}") + mask_idxes = re.split(",", request.match_info.get("mask_idxes")) + res = "" + for mask_idx in mask_idxes: + res += f"{',' if len(res) > 0 else ''}{self.dbGet(f'inputmask_{mask_idx}'.encode())}" + data = { + "inputmask_shares": res, + } + print(f"request: {request}") + print(f"response: {res}") + return web.json_response(data) + + @routes.get("/balance/{token_user}") + # async def balance_handler(request): + async def _handler(request): + print(f"request: {request}") + token_user = request.match_info.get("token_user") + res = self.dbGet(f"balance{token_user}".encode()) + data = { + "balance": f"{res}", + } + print(f"request: {request}") + print(f"response: {res}") + return web.json_response(data) + + @routes.get("/price/{trade_seq}") + # async def price_handler(request): + async def _handler(request): + print(f"request: {request}") + trade_seq = request.match_info.get("trade_seq") + res = self.dbGet(f"price_{trade_seq}".encode()) + data = { + "price": f"{res}", + } + print(f"request: {request}") + print(f"response: {res}") + return web.json_response(data) + + app = web.Application() + app.add_routes(routes) + runner = web.AppRunner(app) + await runner.setup() + site = web.TCPSite(runner, host=self.host, port=self.http_port) + await site.start() + await asyncio.sleep(100 * 3600) diff --git a/src/python/honeybadgerswap/server/__init__.py b/src/python/honeybadgerswap/server/__init__.py new file mode 100644 index 00000000..b91e4c1c --- /dev/null +++ b/src/python/honeybadgerswap/server/__init__.py @@ -0,0 +1 @@ +from .Server import Server # noqa F401 diff --git a/src/python/honeybadgerswap/server/add_liquidity_org_data.py b/src/python/honeybadgerswap/server/add_liquidity_org_data.py new file mode 100644 index 00000000..de3f9560 --- /dev/null +++ b/src/python/honeybadgerswap/server/add_liquidity_org_data.py @@ -0,0 +1,40 @@ +import sys + +from ..utils import ( + key_pool, + key_total_supply, + location_db, + location_sharefile, + openDB, + from_hex, + to_hex, + sz, +) + +input_parameter_num = 5 + +if __name__ == "__main__": + server_id = sys.argv[1] + token_A = sys.argv[2] + token_B = sys.argv[3] + + file = location_sharefile(server_id) + with open(file, "rb") as f: + f.seek(input_parameter_num * sz) + pool_A = f.read(sz) + pool_B = f.read(sz) + amt_A = from_hex(f.read(sz)) + amt_B = from_hex(f.read(sz)) + amt_liquidity = from_hex(f.read(sz)) + + db = openDB(location_db(server_id)) + + db.Put(key_pool(token_A, token_B, token_A), pool_A) + db.Put(key_pool(token_A, token_B, token_B), pool_B) + + key = key_total_supply(token_A, token_B) + total_supply = from_hex(bytes(db.Get(key))) + total_supply += amt_liquidity + db.Put(key, to_hex(total_supply)) + + print(amt_A, amt_B, amt_liquidity) diff --git a/src/python/honeybadgerswap/server/add_liquidity_set_data.py b/src/python/honeybadgerswap/server/add_liquidity_set_data.py new file mode 100644 index 00000000..0c227d91 --- /dev/null +++ b/src/python/honeybadgerswap/server/add_liquidity_set_data.py @@ -0,0 +1,33 @@ +import sys + +from ..utils import ( + key_pool, + key_total_supply, + location_db, + location_sharefile, + openDB, + from_float, + from_hex, + to_hex, +) + +if __name__ == "__main__": + server_id = sys.argv[1] + user = sys.argv[2] + token_A = sys.argv[3] + token_B = sys.argv[4] + amt_A = to_hex(from_float(sys.argv[5])) + amt_B = to_hex(from_float(sys.argv[6])) + + db = openDB(location_db(server_id)) + + pool_A = bytes(db.Get(key_pool(token_A, token_B, token_A))) + pool_B = bytes(db.Get(key_pool(token_A, token_B, token_B))) + + key = key_total_supply(token_A, token_B) + total_supply = bytes(db.Get(key)) + print(from_hex(total_supply)) + + file = location_sharefile(server_id) + with open(file, "wb") as f: + f.write(pool_A + pool_B + amt_A + amt_B + total_supply) diff --git a/src/python/honeybadgerswap/server/calc_price_set_data.py b/src/python/honeybadgerswap/server/calc_price_set_data.py new file mode 100644 index 00000000..b9016371 --- /dev/null +++ b/src/python/honeybadgerswap/server/calc_price_set_data.py @@ -0,0 +1,30 @@ +import sys + +from ..utils import ( + key_total_price, + key_trade_cnt, + location_db, + location_sharefile, + openDB, + get_value, +) + +if __name__ == "__main__": + server_id = sys.argv[1] + token_A = sys.argv[2] + token_B = sys.argv[3] + + db = openDB(location_db(server_id)) + + k_total_price = key_total_price(token_A, token_B) + total_price = get_value(db, k_total_price) + + k_trade_cnt = key_trade_cnt(token_A, token_B) + trade_cnt = get_value(db, k_trade_cnt) + + file = location_sharefile(server_id) + with open(file, "wb") as f: + f.write(total_price + trade_cnt) + + db.Delete(k_total_price) + db.Delete(k_trade_cnt) diff --git a/src/python/honeybadgerswap/server/check_balance_org_data.py b/src/python/honeybadgerswap/server/check_balance_org_data.py new file mode 100644 index 00000000..507b7889 --- /dev/null +++ b/src/python/honeybadgerswap/server/check_balance_org_data.py @@ -0,0 +1,13 @@ +import sys + +from ..utils import location_private_output, from_hex, sz + +if __name__ == "__main__": + server_id = sys.argv[1] + + file = location_private_output(server_id) + + with open(file, "rb") as f: + enough_balance = from_hex(f.read(sz)) + + print(enough_balance) diff --git a/src/python/honeybadgerswap/server/check_balance_set_data.py b/src/python/honeybadgerswap/server/check_balance_set_data.py new file mode 100644 index 00000000..6ade4691 --- /dev/null +++ b/src/python/honeybadgerswap/server/check_balance_set_data.py @@ -0,0 +1,24 @@ +import sys + +from ..utils import ( + key_balance, + location_db, + location_sharefile, + openDB, + get_value, + from_float, + to_hex, +) + +if __name__ == "__main__": + server_id = sys.argv[1] + token = sys.argv[2] + user = sys.argv[3] + amt = to_hex(from_float(sys.argv[4])) + + db = openDB(location_db(server_id)) + balance = get_value(db, key_balance(token, user)) + + file = location_sharefile(server_id) + with open(file, "wb") as f: + f.write(balance + amt) diff --git a/src/python/honeybadgerswap/server/init_pool.py b/src/python/honeybadgerswap/server/init_pool.py new file mode 100644 index 00000000..02b66ab4 --- /dev/null +++ b/src/python/honeybadgerswap/server/init_pool.py @@ -0,0 +1,22 @@ +import sys + +from ..utils import key_pool, key_total_supply, location_db, openDB, from_float, to_hex + +if __name__ == "__main__": + server_id = sys.argv[1] + token_A = sys.argv[2] + token_B = sys.argv[3] + amt_A = to_hex(from_float(sys.argv[4])) + amt_B = to_hex(from_float(sys.argv[5])) + amt_liquidity = to_hex(from_float(sys.argv[6])) + + db = openDB(location_db(server_id)) + + key_A = key_pool(token_A, token_B, token_A) + db.Put(key_A, amt_A) + + key_B = key_pool(token_A, token_B, token_B) + db.Put(key_B, amt_B) + + key_liquidity = key_total_supply(token_A, token_B) + db.Put(key_liquidity, amt_liquidity) diff --git a/src/python/honeybadgerswap/server/proc_inputmask.py b/src/python/honeybadgerswap/server/proc_inputmask.py new file mode 100644 index 00000000..6d4ea4be --- /dev/null +++ b/src/python/honeybadgerswap/server/proc_inputmask.py @@ -0,0 +1,18 @@ +import sys + +from ..utils import key_inputmask, location_db, location_inputmask, openDB, to_hex, p + +if __name__ == "__main__": + server_id = sys.argv[1] + init_idx = int(sys.argv[2]) + + db = openDB(location_db(server_id)) + + file = location_inputmask(server_id) + with open(file, "r") as f: + idx = init_idx + for line in f.readlines(): + key = key_inputmask(idx) + share = to_hex(int(line) % p) + db.Put(key, share) + idx += 1 diff --git a/src/python/honeybadgerswap/server/remove_liquidity_org_data.py b/src/python/honeybadgerswap/server/remove_liquidity_org_data.py new file mode 100644 index 00000000..fa16db51 --- /dev/null +++ b/src/python/honeybadgerswap/server/remove_liquidity_org_data.py @@ -0,0 +1,41 @@ +import sys + +from ..utils import ( + key_pool, + key_total_supply, + location_db, + location_sharefile, + openDB, + from_float, + from_hex, + to_hex, + sz, +) + +input_parameter_num = 4 + +if __name__ == "__main__": + server_id = sys.argv[1] + token_A = sys.argv[2] + token_B = sys.argv[3] + amt_liquidity = from_float(sys.argv[4]) + + file = location_sharefile(server_id) + with open(file, "rb") as f: + f.seek(input_parameter_num * sz) + pool_A = f.read(sz) + pool_B = f.read(sz) + amt_A = from_hex(f.read(sz)) + amt_B = from_hex(f.read(sz)) + + db = openDB(location_db(server_id)) + + db.Put(key_pool(token_A, token_B, token_A), pool_A) + db.Put(key_pool(token_A, token_B, token_B), pool_B) + + key = key_total_supply(token_A, token_B) + total_supply = from_hex(bytes(db.Get(key))) + total_supply -= amt_liquidity + db.Put(key, to_hex(total_supply)) + + print(amt_A, amt_B) diff --git a/src/python/honeybadgerswap/server/remove_liquidity_set_data.py b/src/python/honeybadgerswap/server/remove_liquidity_set_data.py new file mode 100644 index 00000000..d459687a --- /dev/null +++ b/src/python/honeybadgerswap/server/remove_liquidity_set_data.py @@ -0,0 +1,30 @@ +import sys + +from ..utils import ( + key_pool, + key_total_supply, + location_db, + location_sharefile, + openDB, + from_float, + to_hex, +) + +if __name__ == "__main__": + server_id = sys.argv[1] + user = sys.argv[2] + token_A = sys.argv[3] + token_B = sys.argv[4] + amt_liquidity = to_hex(from_float(sys.argv[5])) + + db = openDB(location_db(server_id)) + + pool_A = bytes(db.Get(key_pool(token_A, token_B, token_A))) + pool_B = bytes(db.Get(key_pool(token_A, token_B, token_B))) + + key = key_total_supply(token_A, token_B) + total_supply = bytes(db.Get(key)) + + file = location_sharefile(server_id) + with open(file, "wb") as f: + f.write(pool_A + pool_B + amt_liquidity + total_supply) diff --git a/src/python/honeybadgerswap/server/start_server.py b/src/python/honeybadgerswap/server/start_server.py new file mode 100644 index 00000000..b92445b5 --- /dev/null +++ b/src/python/honeybadgerswap/server/start_server.py @@ -0,0 +1,31 @@ +import asyncio +import os +import sys +import toml + +from .Server import Server + + +async def main(node_id, config_file): + config = toml.load(config_file) + + n = config["n"] + t = config["t"] + + server_config = config["servers"][node_id] + server = Server( + n, t, server_id, server_config["http_host"], server_config["http_port"] + ) + + tasks = [] + tasks.append(asyncio.ensure_future(server.http_server())) + + for task in tasks: + await task + + +# TODO Use argparse to accept config file to use. +if __name__ == "__main__": + server_id = int(sys.argv[1]) + config_file = os.getenv("HBSWAP_SERVER_CONFIG", "/opt/hbswap/conf/server.toml") + asyncio.run(main(server_id, config_file)) diff --git a/src/python/honeybadgerswap/server/trade_org_data.py b/src/python/honeybadgerswap/server/trade_org_data.py new file mode 100644 index 00000000..f5075213 --- /dev/null +++ b/src/python/honeybadgerswap/server/trade_org_data.py @@ -0,0 +1,56 @@ +import sys + +from ..utils import ( + key_pool, + key_price, + key_total_price, + key_trade_cnt, + location_db, + location_sharefile, + openDB, + get_value, + from_hex, + to_hex, + sz, +) + +input_parameter_num = 8 + +if __name__ == "__main__": + server_id = sys.argv[1] + token_A = sys.argv[2] + token_B = sys.argv[3] + trade_seq = sys.argv[4] + + file = location_sharefile(server_id) + with open(file, "rb") as f: + f.seek(input_parameter_num * sz) + pool_A = f.read(sz) + pool_B = f.read(sz) + change_A = from_hex(f.read(sz)) + change_B = from_hex(f.read(sz)) + trade_price = from_hex(f.read(sz)) + + print(change_A, change_B) + + db = openDB(location_db(server_id)) + + db.Put(key_pool(token_A, token_B, token_A), pool_A) + db.Put(key_pool(token_A, token_B, token_B), pool_B) + + k_price = key_price(trade_seq) + db.Put(k_price, k_price) + + k_total_price = key_total_price(token_A, token_B) + total_price = from_hex(get_value(db, k_total_price)) + print("total_price before", total_price) + total_price += trade_price + print("total_price after", total_price) + db.Put(k_total_price, to_hex(total_price)) + + k_trade_cnt = key_trade_cnt(token_A, token_B) + trade_cnt = from_hex(get_value(db, k_trade_cnt)) + print("total_cnt before", trade_cnt) + trade_cnt += 1 + print("total_cnt after", trade_cnt) + db.Put(k_trade_cnt, to_hex(trade_cnt)) diff --git a/src/python/honeybadgerswap/server/trade_set_data.py b/src/python/honeybadgerswap/server/trade_set_data.py new file mode 100644 index 00000000..e3c8d213 --- /dev/null +++ b/src/python/honeybadgerswap/server/trade_set_data.py @@ -0,0 +1,46 @@ +import sys + +from ..utils import ( + key_balance, + key_inputmask, + key_pool, + location_db, + location_sharefile, + openDB, + get_value, + to_hex, +) + +if __name__ == "__main__": + server_id = sys.argv[1] + user = sys.argv[2] + token_A = sys.argv[3] + token_B = sys.argv[4] + idx_A = sys.argv[5] + idx_B = sys.argv[6] + masked_amt_A = to_hex(sys.argv[7]) + masked_amt_B = to_hex(sys.argv[8]) + + db = openDB(location_db(server_id)) + + pool_A = bytes(db.Get(key_pool(token_A, token_B, token_A))) + pool_B = bytes(db.Get(key_pool(token_A, token_B, token_B))) + + mask_share_A = bytes(db.Get(key_inputmask(idx_A))) + mask_share_B = bytes(db.Get(key_inputmask(idx_B))) + + balance_A = get_value(db, key_balance(token_A, user)) + balance_B = get_value(db, key_balance(token_B, user)) + + file = location_sharefile(server_id) + with open(file, "wb") as f: + f.write( + pool_A + + pool_B + + balance_A + + balance_B + + mask_share_A + + mask_share_B + + masked_amt_A + + masked_amt_B + ) diff --git a/src/python/honeybadgerswap/server/update_balance.py b/src/python/honeybadgerswap/server/update_balance.py new file mode 100644 index 00000000..c7d8ef0a --- /dev/null +++ b/src/python/honeybadgerswap/server/update_balance.py @@ -0,0 +1,28 @@ +import sys + +from ..utils import ( + key_balance, + location_db, + openDB, + get_value, + from_float, + from_hex, + to_hex, +) + +if __name__ == "__main__": + server_id = sys.argv[1] + token = sys.argv[2] + user = sys.argv[3] + amt = sys.argv[4] + flag = bool(int(sys.argv[5])) + + db = openDB(location_db(server_id)) + + key = key_balance(token, user) + balance = from_hex(get_value(db, key)) + + print("old balance:", balance) + balance += from_float(amt) if flag else int(amt) + print("updated balance:", balance) + db.Put(key, to_hex(balance)) diff --git a/src/python/honeybadgerswap/utils.py b/src/python/honeybadgerswap/utils.py new file mode 100644 index 00000000..b377b8ef --- /dev/null +++ b/src/python/honeybadgerswap/utils.py @@ -0,0 +1,140 @@ +import os +import time + +import leveldb + +from gmpy import binary, mpz +from gmpy2 import mpz_from_old_binary + +############################################################################## +# keys # +############################################################################## + + +def key_balance(token, user): + return f"balance_{token}_{user}".encode() + + +def key_inputmask(idx): + return f"inputmask_{idx}".encode() + + +def key_pool(token_A, token_B, token): + return f"pool_{token_A}_{token_B}_{token}".encode() + + +def key_price(trade_seq): + return f"price_{trade_seq}".encode() + + +def key_total_price(token_A, token_B): + return f"trade_price_{token_A}_{token_B}".encode() + + +def key_total_supply(token_A, token_B): + return f"total_supply_{token_A}_{token_B}".encode() + + +def key_trade_cnt(token_A, token_B): + return f"trade_cnt_{token_A}-{token_B}".encode() + + +############################################################################## +# files # +############################################################################## + + +def location_db(server_id): + db_path = os.getenv("DB_PATH", "/opt/hbswap/db") + return f"{db_path}/server-{server_id}" + + +def location_inputmask(server_id): + inputmask_shares_dir = os.getenv( + "INPUTMASK_SHARES", "/opt/hbswap/inputmask-shares", + ) + return f"{inputmask_shares_dir}/4-MSp-255/Randoms-MSp-P{server_id}" + + +def location_private_output(server_id): + prep_dir = os.getenv("PREP_DIR", "/opt/hbswap/preprocessing-data") + return f"{prep_dir}/Private-Output-{server_id}" + + +def location_sharefile(server_id): + return f"Persistence/Transactions-P{server_id}.data" + + +############################################################################## +# functions # +############################################################################## + + +def openDB(location): + while True: + try: + return leveldb.LevelDB(location) + except leveldb.LevelDBError: + time.sleep(3) + + +def get_value(db, key): + try: + return bytes(db.Get(key)) + except KeyError: + return to_hex(0) + + +def get_inverse(a): + ret = 1 + b = p - 2 + while b: + if b % 2 == 1: + ret = (ret * a) % p + b //= 2 + a = (a * a) % p + return ret + + +def from_float(x): + return int(round(float(x) * (2 ** fp))) + + +def from_hex(x): + return int((mpz_from_old_binary(x) * inverse_R) % p) + + +def to_hex(x): + x = mpz(x) + x = (x * R) % p + x = binary(int(x)) + x += b"\x00" * (32 - len(x)) + return x + + +def check_consistency(shares): + value = reconstruct(shares, t + 1) + for i in range(t + 2, n + 1): + if reconstruct(shares, i) != value: + print("inconsistent") + + +def reconstruct(shares, n): + inputmask = 0 + for i in range(1, n + 1): + tot = 1 + for j in range(1, n + 1): + if i == j: + continue + tot = tot * j * get_inverse(j - i) % p + inputmask = (inputmask + shares[i - 1] * tot) % p + return inputmask + + +n = 4 +t = 1 +p = 52435875175126190479447740508185965837690552500527637822603658699938581184513 +R = 10920338887063814464675503992315976177888879664585288394250266608035967270910 +inverse_R = get_inverse(R) +fp = 16 +sz = 32 diff --git a/src/python/pyproject.toml b/src/python/pyproject.toml new file mode 100644 index 00000000..fed528d4 --- /dev/null +++ b/src/python/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" diff --git a/src/python/setup.cfg b/src/python/setup.cfg new file mode 100644 index 00000000..e6af2928 --- /dev/null +++ b/src/python/setup.cfg @@ -0,0 +1,29 @@ +[metadata] +name = honeybadgerswap +version = 0.0.1.dev0 + +[options] +packages = find: +install_requires = + aiohttp + gmpy + gmpy2 + leveldb + toml + +[options.entry_points] +console_scripts = + hbswap-check-balance-set-data=honeybadgerswap.server.check_balance_set_data:main + hbswap-check-balance-org-data=honeybadgerswap.server.check_balance_org_data:main + hbswap-update-balance=honeybadgerswap.server.update_balance:main + hbswap-proc-inputmasks=honeybadgerswap.server.proc_inputmask:main + hbswap-init-pool=honeybadgerswap.server.init_pool:main + hbswap-add-liquidity-set-data=honeybadgerswap.server.add_liquidity_set_data:main + hbswap-add-liquidity-org-data=honeybadgerswap.server.add_liquidity_org_data:main + hbswap-remove-liquidity-set-data=honeybadgerswap.server.remove_liquidity_set_data:main + hbswap-remove-liquidity-org-data=honeybadgerswap.server.remove_liquidity_org_data:main + hbswap-trade-set-data=honeybadgerswap.server.trade_set_data:main + hbswap-trade-org-data=honeybadgerswap.server.trade_org_data:main + hbswap-calc-price-set-data=honeybadgerswap.server.calc_price_set_data:main + hbswap-start-server=honeybadgerswap.server.start_server:main + hbswap-req-inputmasks=honeybadgerswap.client.req_inputmasks:main diff --git a/src/python/setup.py b/src/python/setup.py new file mode 100644 index 00000000..b908cbe5 --- /dev/null +++ b/src/python/setup.py @@ -0,0 +1,3 @@ +import setuptools + +setuptools.setup() diff --git a/src/python/tests/check.py b/src/python/tests/check.py new file mode 100644 index 00000000..7c11d960 --- /dev/null +++ b/src/python/tests/check.py @@ -0,0 +1,41 @@ +from ..utils import reconstruct, n + +# def reconstruct(shares, n): +# inputmask = 0 +# for i in range(1, n + 1): +# tot = 1 +# for j in range(1, n + 1): +# if i == j: +# continue +# tot = tot * j * get_inverse(j - i) % p +# inputmask = (inputmask + shares[i - 1] * tot) % p +# return inputmask +# +# def check(shares): +# value = reconstruct(shares, t + 1) +# for i in range(t + 2, n + 1): +# if reconstruct(shares, i) != value: +# print('ohoh') + +# mask = [] +# for server_id in range(4): +# file = f'Player-Data/4-MSp-255/Randoms-MSp-P{server_id}' +# tmp = [] +# with open(file, 'r') as f: +# for line in f.readlines(): +# data = int(line) % p +# tmp.append(data) +# mask.append(tmp) +# +# for k in range(0, len(mask[0])): +# shares = [] +# for i in range(n): +# shares.append(mask[i][k]) +# +shares = [ + 17783221102966205198584993843005729747643599811630564308773575716113800647019, + 35566442205932410397169987686011459495287199623261128617547151432227600310998, + 913788133772425116307241020831223405240246934364055103717068448402818790464, + 18697009236738630314892234863836953152883846745994619412490644164516618454443, +] +print(reconstruct(shares, n)) diff --git a/src/python/tests/debug.sh b/src/python/tests/debug.sh new file mode 100755 index 00000000..161f8576 --- /dev/null +++ b/src/python/tests/debug.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +set -e + +pkill -f malicious-shamir-party.x || true + +#mpc_prog=hbswap_calc_price +#python_prog=Scripts/hbswap/python/server/calc_price_set_data.py + +mpc_prog=hbswap_test +python_prog=Scripts/hbswap/python/test/set_data.py + +./compile.py -v -C -F 256 $mpc_prog + +#python3 $python_prog 0 0x0000000000000000000000000000000000000000 0x6b5c9637e0207c72Ee1a275b6C3b686ba8D87385 +#python3 $python_prog 1 0x0000000000000000000000000000000000000000 0x6b5c9637e0207c72Ee1a275b6C3b686ba8D87385 +#python3 $python_prog 2 0x0000000000000000000000000000000000000000 0x6b5c9637e0207c72Ee1a275b6C3b686ba8D87385 +#python3 $python_prog 3 0x0000000000000000000000000000000000000000 0x6b5c9637e0207c72Ee1a275b6C3b686ba8D87385 + +./malicious-shamir-party.x -N 4 -T 1 -p 0 -pn 5000 -P 52435875175126190479447740508185965837690552500527637822603658699938581184513 $mpc_prog & +./malicious-shamir-party.x -N 4 -T 1 -p 1 -pn 5000 -P 52435875175126190479447740508185965837690552500527637822603658699938581184513 $mpc_prog & +./malicious-shamir-party.x -N 4 -T 1 -p 2 -pn 5000 -P 52435875175126190479447740508185965837690552500527637822603658699938581184513 $mpc_prog & +./malicious-shamir-party.x -N 4 -T 1 -p 3 -pn 5000 -P 52435875175126190479447740508185965837690552500527637822603658699938581184513 $mpc_prog \ No newline at end of file diff --git a/src/python/tests/debug_trade.sh b/src/python/tests/debug_trade.sh new file mode 100755 index 00000000..dc25b7c9 --- /dev/null +++ b/src/python/tests/debug_trade.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +set -e + +pkill -f malicious-shamir-party.x || true + +./compile.py -v -C -F 256 hbswap_trade + +./malicious-shamir-party.x -N 4 -T 1 -p 0 -pn 5000 -P 52435875175126190479447740508185965837690552500527637822603658699938581184513 hbswap_trade & +./malicious-shamir-party.x -N 4 -T 1 -p 1 -pn 5000 -P 52435875175126190479447740508185965837690552500527637822603658699938581184513 hbswap_trade & +./malicious-shamir-party.x -N 4 -T 1 -p 2 -pn 5000 -P 52435875175126190479447740508185965837690552500527637822603658699938581184513 hbswap_trade & +./malicious-shamir-party.x -N 4 -T 1 -p 3 -pn 5000 -P 52435875175126190479447740508185965837690552500527637822603658699938581184513 hbswap_trade \ No newline at end of file diff --git a/src/python/tests/inputmask.sh b/src/python/tests/inputmask.sh new file mode 100755 index 00000000..37ab52c2 --- /dev/null +++ b/src/python/tests/inputmask.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +./random-shamir.x -i 0 -N 4 -T 1 --nshares 1000 & +./random-shamir.x -i 1 -N 4 -T 1 --nshares 1000 & +./random-shamir.x -i 2 -N 4 -T 1 --nshares 1000 & +./random-shamir.x -i 3 -N 4 -T 1 --nshares 1000 + +python3 Scripts/hbswap/python/test/check.py \ No newline at end of file diff --git a/src/python/tests/set_data.py b/src/python/tests/set_data.py new file mode 100644 index 00000000..476acf95 --- /dev/null +++ b/src/python/tests/set_data.py @@ -0,0 +1,21 @@ +import sys + +from ..utils import from_hex, to_hex # , sz + +if __name__ == "__main__": + server_id = sys.argv[1] + + print(from_hex(to_hex(1123411))) + + # file = f"Persistence/Transactions-P{server_id}.data" + # # with open(file, 'wb') as f: + # # f.write(to_hex(0)) + + # file = f"Player-Data/Private-Output-{server_id}" + # with open(file, 'rb') as f: + # zero = f.read(sz + # print(zero) + # print(from_hex(zero)) + # + # import os + # print('filesize:', os.path.getsize(file)) diff --git a/src/python/tests/test.py b/src/python/tests/test.py new file mode 100644 index 00000000..78f509d5 --- /dev/null +++ b/src/python/tests/test.py @@ -0,0 +1,23 @@ +from ..utils import get_inverse, n, t, p + +x = [] +y = [] + +for i in range(n): + file = f"PreProcessing-Data/4-MSp-255/Randoms-MSp-P{i}" + with open(file, "r") as f: + x.append(i + 1) + for line in f.readlines(): + y.append(int(line)) + break + +for nn in range(t + 1, n + 1): + inputmask = 0 + for i in range(nn): + tot = 1 + for j in range(nn): + if i == j: + continue + tot = tot * x[j] * get_inverse(x[j] - x[i]) % p + inputmask = (inputmask + y[i] * tot) % p + print(inputmask) diff --git a/src/truffle-config.js b/src/truffle-config.js new file mode 100644 index 00000000..29840026 --- /dev/null +++ b/src/truffle-config.js @@ -0,0 +1,99 @@ +/** + * Use this file to configure your truffle project. It's seeded with some + * common settings for different networks and features like migrations, + * compilation and testing. Uncomment the ones you need or modify + * them to suit your project as necessary. + * + * More information about configuration can be found at: + * + * truffleframework.com/docs/advanced/configuration + * + * To deploy via Infura you'll need a wallet provider (like @truffle/hdwallet-provider) + * to sign your transactions before they're sent to a remote public node. Infura accounts + * are available for free at: infura.io/register. + * + * You'll also need a mnemonic - the twelve word phrase the wallet uses to generate + * public/private key pairs. If you're publishing your code to GitHub make sure you load this + * phrase from a file you've .gitignored so it doesn't accidentally become public. + * + */ + +// const HDWalletProvider = require('@truffle/hdwallet-provider'); +// const infuraKey = "fj4jll3k....."; +// +// const fs = require('fs'); +// const mnemonic = fs.readFileSync(".secret").toString().trim(); + +module.exports = { + /** + * Networks define how you connect to your ethereum client and let you set the + * defaults web3 uses to send transactions. If you don't specify one truffle + * will spin up a development blockchain for you on port 9545 when you + * run `develop` or `test`. You can ask a truffle command to use a specific + * network from the command line, e.g + * + * $ truffle test --network + */ + + networks: { + // Useful for testing. The `development` name is special - truffle uses it by default + // if it's defined here and no other network is specified at the command line. + // You should run a client (like ganache-cli, geth or parity) in a separate terminal + // tab if you use this network and you must also set the `host`, `port` and `network_id` + // options below to some value. + // + // development: { + // host: "127.0.0.1", // Localhost (default: none) + // port: 8545, // Standard Ethereum port (default: none) + // network_id: "*", // Any network (default: none) + // }, + + // Another network with more advanced options... + // advanced: { + // port: 8777, // Custom port + // network_id: 1342, // Custom network + // gas: 8500000, // Gas sent with each transaction (default: ~6700000) + // gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei) + // from:
, // Account to send txs from (default: accounts[0]) + // websockets: true // Enable EventEmitter interface for web3 (default: false) + // }, + + // Useful for deploying to a public network. + // NB: It's important to wrap the provider as a function. + // ropsten: { + // provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/YOUR-PROJECT-ID`), + // network_id: 3, // Ropsten's id + // gas: 5500000, // Ropsten has a lower block limit than mainnet + // confirmations: 2, // # of confs to wait between deployments. (default: 0) + // timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50) + // skipDryRun: true // Skip dry run before migrations? (default: false for public nets ) + // }, + + // Useful for private networks + // private: { + // provider: () => new HDWalletProvider(mnemonic, `https://network.io`), + // network_id: 2111, // This network is yours, in the cloud. + // production: true // Treats this network as if it was a public net. (default: false) + // } + }, + + // Set default mocha options here, use special reporters etc. + mocha: { + // timeout: 100000 + }, + + // Configure your compilers + compilers: { + solc: { + version: "0.6.10", // Fetch exact version from solc-bin (default: truffle's version) + // docker: true, // Use "0.5.1" you've installed locally with docker (default: false) + // settings: { // See the solidity docs for advice about optimization and evmVersion + // optimizer: { + // enabled: false, + // runs: 200 + // }, + // evmVersion: "byzantium" + // } + } + } +} diff --git a/trader.yml b/trader.yml new file mode 100644 index 00000000..5e027de2 --- /dev/null +++ b/trader.yml @@ -0,0 +1,88 @@ +version: '3.8' + +services: + public.deposit: + image: hbswap-go + build: + context: . + dockerfile: Dockerfile + target: go-deps + environment: + POADIR: /opt/poa + POA_KEYSTORE: /opt/poa/keystore + networks: + - hbswap + working_dir: /usr/src/scripts + volumes: + - ./poa:/opt/poa + - ./scripts/wait-for-it.sh:/usr/local/bin/wait-for-it + - ./scripts:/usr/src/scripts + - ./src:/go/src/github.com/initc3/HoneyBadgerSwap/src + entrypoint: "" + # TODO wait for contract to be deployed/available + command: ["bash", "client-public-deposit.sh", "ethnode"] + + secret.deposit: + image: hbswap-go + build: + context: . + dockerfile: Dockerfile + target: go-deps + environment: + POADIR: /opt/poa + POA_KEYSTORE: /opt/poa/keystore + networks: + - hbswap + working_dir: /usr/src/scripts + volumes: + - ./poa:/opt/poa + - ./scripts/wait-for-it.sh:/usr/local/bin/wait-for-it + - ./scripts:/usr/src/scripts + - ./src:/go/src/github.com/initc3/HoneyBadgerSwap/src + entrypoint: "" + # TODO wait for contract to be deployed/available + command: ["bash", "client-secret-deposit.sh", "ethnode"] + + trade-1: + image: hbswap + build: + context: . + dockerfile: Dockerfile + # FIXME is the poa stuff needed? + environment: + POADIR: /opt/poa + POA_KEYSTORE: /opt/poa/keystore + networks: + - hbswap + # FIXME is the poa stuff needed? + volumes: + - ./poa:/opt/poa + - ./conf/client.toml:/opt/hbswap/conf/client.toml + - ./scripts:/usr/src/scripts + - ./scripts/wait-for-it.sh:/usr/local/bin/wait-for-it + - ./src:/go/src/github.com/initc3/HoneyBadgerSwap/src + working_dir: /usr/src/scripts + command: ["wait-for-it", "mpcnode3:8080", "--", "bash", "trade-1.sh", "ethnode"] + #command: ["wait-for-it", "mpcnode3:8080", "--", "bash", "trade.sh", "ethnode", "0", "1.1", "-2.5"] + #command: ["wait-for-it", "mpcnode3:8080", "--", "bash", "trade.sh", "ethnode", "0", "-1.1", "2"] + + trade-2: + image: hbswap + build: + context: . + dockerfile: Dockerfile + # FIXME is the poa stuff needed? + environment: + POADIR: /opt/poa + POA_KEYSTORE: /opt/poa/keystore + networks: + - hbswap + # FIXME is the poa stuff needed? + volumes: + - ./poa:/opt/poa + - ./conf/client.toml:/opt/hbswap/conf/client.toml + - ./scripts:/usr/src/scripts + - ./scripts/wait-for-it.sh:/usr/local/bin/wait-for-it + - ./src:/go/src/github.com/initc3/HoneyBadgerSwap/src + working_dir: /usr/src/scripts + command: ["wait-for-it", "mpcnode3:8080", "--", "bash", "trade-2.sh", "ethnode"]