25 |
--------------------------------------------------------------------------------
/contracts/MetaCoin.sol:
--------------------------------------------------------------------------------
1 | import "ConvertLib.sol";
2 |
3 | // This is just a simple example of a coin-like contract.
4 | // It is not standards compatible and cannot be expected to talk to other
5 | // coin/token contracts. If you want to create a standards-compliant
6 | // token, see: https://github.com/ConsenSys/Tokens. Cheers!
7 |
8 | contract MetaCoin {
9 | mapping (address => uint) balances;
10 |
11 | function MetaCoin() {
12 | balances[tx.origin] = 10000;
13 | }
14 |
15 | function sendCoin(address receiver, uint amount) returns(bool sufficient) {
16 | if (balances[msg.sender] < amount) return false;
17 | balances[msg.sender] -= amount;
18 | balances[receiver] += amount;
19 | return true;
20 | }
21 | function getBalanceInEth(address addr) returns(uint){
22 | return ConvertLib.convert(getBalance(addr),2);
23 | }
24 | function getBalance(address addr) returns(uint) {
25 | return balances[addr];
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | # Created by https://www.gitignore.io/api/meteorjs,node
3 |
4 | ### MeteorJS ###
5 | # default meteor build and local packages
6 | .meteor/local
7 |
8 | # meteor settings file
9 | settings.json
10 |
11 | # meteor build output files
12 | *.tar.gz
13 |
14 | # general swp files from vim
15 | *.swp
16 |
17 |
18 | ### Node ###
19 | # Logs
20 | logs
21 | *.log
22 | npm-debug.log*
23 |
24 | # Runtime data
25 | pids
26 | *.pid
27 | *.seed
28 |
29 | # Directory for instrumented libs generated by jscoverage/JSCover
30 | lib-cov
31 |
32 | # Coverage directory used by tools like istanbul
33 | coverage
34 |
35 | # nyc test coverage
36 | .nyc_output
37 |
38 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
39 | .grunt
40 |
41 | # node-waf configuration
42 | .lock-wscript
43 |
44 | # Compiled binary addons (http://nodejs.org/api/addons.html)
45 | build/Release
46 |
47 | # Dependency directories
48 | node_modules
49 | jspm_packages
50 |
51 | # Optional npm cache directory
52 | .npm
53 |
54 | # Optional REPL history
55 | .node_repl_history
56 |
57 | build/
58 | environments/*/contracts/
59 | environments/*/meteor/
60 |
61 |
--------------------------------------------------------------------------------
/app/.meteor/packages:
--------------------------------------------------------------------------------
1 | # Meteor packages used by this project, one per line.
2 | # Check this file (and the other files in this directory) into your repository.
3 | #
4 | # 'meteor add' and 'meteor remove' will edit this file for you,
5 | # but you can also edit it by hand.
6 |
7 | meteor-base # Packages every Meteor app needs to have
8 | mobile-experience # Packages for a great mobile UX
9 | mongo # The database Meteor supports right now
10 | blaze-html-templates # Compile .html files into Meteor Blaze views
11 | reactive-var # Reactive variable for tracker
12 | jquery # Helpful client-side library
13 | tracker # Meteor's client-side reactive programming library
14 |
15 | standard-minifier-css # CSS minifier run for production mode
16 | standard-minifier-js # JS minifier run for production mode
17 | es5-shim # ECMAScript 5 compatibility for older browsers.
18 | ecmascript # Enable ECMAScript2015+ syntax in app code
19 |
20 | autopublish # Publish all data to the clients (for prototyping)
21 | insecure # Allow all DB writes from clients (for prototyping)
22 | ethereum:elements
23 |
--------------------------------------------------------------------------------
/app/.meteor/versions:
--------------------------------------------------------------------------------
1 | 3stack:bignumber@2.0.0
2 | alexvandesande:identicon@2.0.2
3 | allow-deny@1.0.4
4 | amplify@1.0.0
5 | autopublish@1.0.7
6 | autoupdate@1.2.9
7 | babel-compiler@6.6.4
8 | babel-runtime@0.1.8
9 | base64@1.0.8
10 | binary-heap@1.0.8
11 | blaze@2.1.7
12 | blaze-html-templates@1.0.4
13 | blaze-tools@1.0.8
14 | boilerplate-generator@1.0.8
15 | caching-compiler@1.0.4
16 | caching-html-compiler@1.0.6
17 | callback-hook@1.0.8
18 | check@1.2.1
19 | ddp@1.2.5
20 | ddp-client@1.2.7
21 | ddp-common@1.2.5
22 | ddp-server@1.2.6
23 | deps@1.0.12
24 | diff-sequence@1.0.5
25 | ecmascript@0.4.3
26 | ecmascript-runtime@0.2.10
27 | ejson@1.0.11
28 | es5-shim@4.5.10
29 | ethereum:elements@0.7.1
30 | ethereum:tools@0.4.1
31 | ethereum:web3@0.15.3
32 | fastclick@1.0.11
33 | frozeman:animation-helper@0.2.6
34 | frozeman:persistent-minimongo@0.1.8
35 | frozeman:storage@0.1.9
36 | frozeman:template-var@1.2.3
37 | geojson-utils@1.0.8
38 | hot-code-push@1.0.4
39 | html-tools@1.0.9
40 | htmljs@1.0.9
41 | http@1.1.5
42 | id-map@1.0.7
43 | insecure@1.0.7
44 | jquery@1.11.8
45 | launch-screen@1.0.11
46 | less@2.6.0
47 | livedata@1.0.18
48 | localstorage@1.0.9
49 | logging@1.0.12
50 | meteor@1.1.14
51 | meteor-base@1.0.4
52 | minifier-css@1.1.11
53 | minifier-js@1.1.11
54 | minimongo@1.0.16
55 | mobile-experience@1.0.4
56 | mobile-status-bar@1.0.12
57 | modules@0.6.1
58 | modules-runtime@0.6.3
59 | mongo@1.1.7
60 | mongo-id@1.0.4
61 | npm-mongo@1.4.43
62 | observe-sequence@1.0.11
63 | ordered-dict@1.0.7
64 | promise@0.6.7
65 | random@1.0.9
66 | reactive-var@1.0.9
67 | reload@1.1.8
68 | retry@1.0.7
69 | routepolicy@1.0.10
70 | spacebars@1.0.11
71 | spacebars-compiler@1.0.11
72 | standard-minifier-css@1.0.6
73 | standard-minifier-js@1.0.6
74 | standard-minifiers@1.0.6
75 | templating@1.1.9
76 | templating-tools@1.0.4
77 | tracker@1.0.13
78 | ui@1.0.11
79 | underscore@1.0.8
80 | url@1.0.9
81 | webapp@1.2.8
82 | webapp-hashing@1.0.9
83 |
--------------------------------------------------------------------------------
/truffle-meteor-build.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # By Rob Myers
4 | # CC0 2016
5 | # To the extent possible under law, the person who associated CC0 with this
6 | # work has waived all copyright and related or neighboring rights to this work.
7 |
8 | # We copy the .meteor/ dir from app/ into the specified environment's build/ dir
9 | # then call meteor-build-client in there, building into a meteor/ directory
10 | # next to build/ .
11 |
12 | if [ "${1}" = "-h" ] || [ "${1}" = "--help" ]
13 | then
14 | echo "Usage: truffle-meteor-build [environment]"
15 | echo " Copies the .meteor directory from app into the truffle build,"
16 | echo " then calls meteor-build-client."
17 | echo "ARGS: [environment] - The truffle environment to use (default developmpment)."
18 | echo " Make sure you have npm install -g meteor-build-client"
19 | echo " and meteor init in the truffle app/ directory."
20 | fi
21 |
22 | environment="${1:-development}"
23 | base_dir="$(pwd)"
24 |
25 | if [ ! -f "${base_dir}/truffle.json" ]
26 | then
27 | echo "Please call from within the top level of a Truffle project."
28 | exit 1
29 | fi
30 |
31 | app_dir="${base_dir}/app"
32 | dot_metoer_dir="${app_dir}/.meteor"
33 | environment_dir="${base_dir}/environments/${environment}"
34 | truffle_build_dir="${environment_dir}/build"
35 | meteor_build_dir="${environment_dir}/meteor"
36 |
37 | if [ ! -d "${environment_dir}" ]
38 | then
39 | echo "Cannot find directory for environment ${environment}."
40 | exit 1
41 | fi
42 |
43 | pushd "${base_dir}" > /dev/null
44 | echo "Truffle: building ${environment} in ${truffle_build_dir}"
45 | truffle build "${environment}"
46 | cp -r "${app_dir}/.meteor" "${truffle_build_dir}"
47 | pushd "${truffle_build_dir}" > /dev/null
48 | echo "Meteor: building client in ${meteor_build_dir}"
49 | meteor-build-client "${meteor_build_dir}" -p ''
50 | popd > /dev/null
51 | popd > /dev/null
--------------------------------------------------------------------------------
/test/metacoin.js:
--------------------------------------------------------------------------------
1 | contract('MetaCoin', function(accounts) {
2 | it("should put 10000 MetaCoin in the first account", function(done) {
3 | var meta = MetaCoin.deployed();
4 |
5 | meta.getBalance.call(accounts[0]).then(function(balance) {
6 | assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account");
7 | }).then(done).catch(done);
8 | });
9 | it("should call a function that depends on a linked library ", function(done){
10 | var meta = MetaCoin.deployed();
11 | var metaCoinBalance;
12 | var metaCoinEthBalance;
13 |
14 | meta.getBalance.call(accounts[0]).then(function(outCoinBalance){
15 | metaCoinBalance = outCoinBalance.toNumber();
16 | return meta.getBalanceInEth.call(accounts[0]);
17 | }).then(function(outCoinBalanceEth){
18 | metaCoinEthBalance = outCoinBalanceEth.toNumber();
19 |
20 | }).then(function(){
21 | assert.equal(metaCoinEthBalance,2*metaCoinBalance,"Library function returned unexpeced function, linkage may be broken");
22 |
23 | }).then(done).catch(done);
24 | });
25 | it("should send coin correctly", function(done) {
26 | var meta = MetaCoin.deployed();
27 |
28 | // Get initial balances of first and second account.
29 | var account_one = accounts[0];
30 | var account_two = accounts[1];
31 |
32 | var account_one_starting_balance;
33 | var account_two_starting_balance;
34 | var account_one_ending_balance;
35 | var account_two_ending_balance;
36 |
37 | var amount = 10;
38 |
39 | meta.getBalance.call(account_one).then(function(balance) {
40 | account_one_starting_balance = balance.toNumber();
41 | return meta.getBalance.call(account_two);
42 | }).then(function(balance) {
43 | account_two_starting_balance = balance.toNumber();
44 | return meta.sendCoin(account_two, amount, {from: account_one});
45 | }).then(function() {
46 | return meta.getBalance.call(account_one);
47 | }).then(function(balance) {
48 | account_one_ending_balance = balance.toNumber();
49 | return meta.getBalance.call(account_two);
50 | }).then(function(balance) {
51 | account_two_ending_balance = balance.toNumber();
52 |
53 | assert.equal(account_one_ending_balance, account_one_starting_balance - amount, "Amount wasn't correctly taken from the sender");
54 | assert.equal(account_two_ending_balance, account_two_starting_balance + amount, "Amount wasn't correctly sent to the receiver");
55 | }).then(done).catch(done);
56 | });
57 | });
58 |
--------------------------------------------------------------------------------
/contracts/marketContract.solc:
--------------------------------------------------------------------------------
1 | /*
2 | This file is part of the DTX(Digital Token Exchange).
3 |
4 | DTX is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU lesser General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | DTX is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU lesser General Public License for more details.
13 |
14 | You should have received a copy of the GNU lesser General Public License
15 | along with the DTX. If not, see .
16 | */
17 |
18 | contract Market {
19 |
20 | // Only for testing
21 | address public owner;
22 |
23 | struct Order {
24 | address owner;
25 | uint32 id;
26 | uint price;
27 | uint amount;
28 | uint32 nextOrder;
29 | }
30 |
31 | mapping(uint32 => Order) public bids;
32 | mapping(uint32 => Order) public asks;
33 |
34 | uint32 public highestBidId;
35 | uint32 public lowestAskId;
36 |
37 | uint32 public numberOfTrades;
38 | uint32 public asksCounter;
39 | uint32 public bidsCounter;
40 |
41 | function Market() {
42 | owner = msg.sender;
43 | numberOfTrades = 0;
44 | highestBidId = 0;
45 | lowestAskId = 0;
46 | }
47 |
48 | // main function
49 | function trade(bool ask, uint amount, uint price) {
50 |
51 | if (amount < 1) throw;
52 |
53 | var amountLeft = amount;
54 |
55 | uint amountToExchange = 0;
56 | uint etherToExchange = 0;
57 |
58 | if (ask == false) {
59 | // bid, fill matching asks and place order
60 | while (lowestAskId != 0 && amountLeft > 0 && asks[lowestAskId].price <= price) {
61 | if (asks[lowestAskId].amount <= amountLeft) {
62 | amountToExchange = asks[lowestAskId].amount;
63 | amountLeft -= amountToExchange;
64 | etherToExchange = amountToExchange * asks[lowestAskId].price;
65 | uint32 newLowestAskId = asks[lowestAskId].nextOrder;
66 | delete asks[lowestAskId];
67 | lowestAskId = newLowestAskId;
68 | } else {
69 | asks[lowestAskId].amount -= amountLeft;
70 | amountToExchange = amountLeft;
71 | etherToExchange = amountToExchange * asks[lowestAskId].price;
72 | amountLeft = 0;
73 | }
74 | // TODO: send some ether to owner
75 | // TODO: send amountToExchange tokens to msg.sender
76 | // TODO: send event to light clients
77 | }
78 | if (amountLeft > 0) placeOrder(ask, amountLeft, price, msg.sender);
79 | } else { // ask
80 | while (highestBidId != 0 && amountLeft > 0 && bids[highestBidId].price >= price) {
81 | if (bids[highestBidId].amount <= amountLeft) {
82 | amountLeft -= bids[highestBidId].amount;
83 | amountToExchange = bids[highestBidId].amount;
84 | etherToExchange = amountToExchange * bids[highestBidId].price;
85 | uint32 newHighestBidId = bids[highestBidId].nextOrder;
86 | delete bids[highestBidId];
87 | highestBidId = newHighestBidId;
88 | } else {
89 | bids[highestBidId].amount -= amountLeft;
90 | amountToExchange = amountLeft;
91 | etherToExchange = amountToExchange * asks[highestBidId].price;
92 | amountLeft = 0;
93 | }
94 | // TODO: send some ether to owner
95 | // TODO: send amountToExchange tokens to msg.sender
96 | // TODO: send event to light clients
97 | }
98 | if (amountLeft > 0) placeOrder(ask, amountLeft, price, msg.sender);
99 | }
100 | }
101 |
102 | // needs to be superoptimised
103 | function placeOrder(bool ask, uint amount, uint price, address owner) internal {
104 | if (ask == false) {
105 | // place bid order
106 | uint32 previousBidId = 0;
107 | uint32 bidId = highestBidId;
108 | while (bidId != 0 && bids[bidId].price >= price) {
109 | previousBidId = bidId;
110 | bidId = bids[bidId].nextOrder;
111 | }
112 | bidsCounter += 1;
113 | bids[bidsCounter] = Order({
114 | owner: owner,
115 | id: bidsCounter,
116 | price: price,
117 | amount: amount,
118 | nextOrder: bidId
119 | });
120 | if (previousBidId != 0) {
121 | bids[previousBidId].nextOrder = bidsCounter;
122 | }
123 | if (highestBidId == 0 || bids[highestBidId].price < price) {
124 | highestBidId = bidsCounter;
125 | }
126 | } else {
127 | uint32 previousAskId = 0;
128 | uint32 askId = lowestAskId;
129 | while (askId != 0 && asks[askId].price <= price) {
130 | previousAskId = askId;
131 | askId = asks[askId].nextOrder;
132 | }
133 | asksCounter += 1;
134 | asks[asksCounter] = Order({
135 | owner: owner,
136 | id: asksCounter,
137 | price: price,
138 | amount: amount,
139 | nextOrder: askId
140 | });
141 | if (previousAskId != 0) {
142 | asks[previousAskId].nextOrder = asksCounter;
143 | }
144 | if (lowestAskId == 0 || asks[lowestAskId].price > price) {
145 | lowestAskId = asksCounter;
146 | }
147 | }
148 | }
149 |
150 | function kill() {
151 | // For testing purposes only
152 | if (msg.sender == owner) {
153 | selfdestruct(owner);
154 | }
155 | }
156 |
157 |
158 | function () {
159 | // This function gets executed if a
160 | // transaction with invalid data is sent to
161 | // the contract or just ether without data.
162 | // We revert the send so that no-one
163 | // accidentally loses money when using the
164 | // contract.
165 | throw;
166 | }
167 |
168 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU LESSER GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 |
9 | This version of the GNU Lesser General Public License incorporates
10 | the terms and conditions of version 3 of the GNU General Public
11 | License, supplemented by the additional permissions listed below.
12 |
13 | 0. Additional Definitions.
14 |
15 | As used herein, "this License" refers to version 3 of the GNU Lesser
16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU
17 | General Public License.
18 |
19 | "The Library" refers to a covered work governed by this License,
20 | other than an Application or a Combined Work as defined below.
21 |
22 | An "Application" is any work that makes use of an interface provided
23 | by the Library, but which is not otherwise based on the Library.
24 | Defining a subclass of a class defined by the Library is deemed a mode
25 | of using an interface provided by the Library.
26 |
27 | A "Combined Work" is a work produced by combining or linking an
28 | Application with the Library. The particular version of the Library
29 | with which the Combined Work was made is also called the "Linked
30 | Version".
31 |
32 | The "Minimal Corresponding Source" for a Combined Work means the
33 | Corresponding Source for the Combined Work, excluding any source code
34 | for portions of the Combined Work that, considered in isolation, are
35 | based on the Application, and not on the Linked Version.
36 |
37 | The "Corresponding Application Code" for a Combined Work means the
38 | object code and/or source code for the Application, including any data
39 | and utility programs needed for reproducing the Combined Work from the
40 | Application, but excluding the System Libraries of the Combined Work.
41 |
42 | 1. Exception to Section 3 of the GNU GPL.
43 |
44 | You may convey a covered work under sections 3 and 4 of this License
45 | without being bound by section 3 of the GNU GPL.
46 |
47 | 2. Conveying Modified Versions.
48 |
49 | If you modify a copy of the Library, and, in your modifications, a
50 | facility refers to a function or data to be supplied by an Application
51 | that uses the facility (other than as an argument passed when the
52 | facility is invoked), then you may convey a copy of the modified
53 | version:
54 |
55 | a) under this License, provided that you make a good faith effort to
56 | ensure that, in the event an Application does not supply the
57 | function or data, the facility still operates, and performs
58 | whatever part of its purpose remains meaningful, or
59 |
60 | b) under the GNU GPL, with none of the additional permissions of
61 | this License applicable to that copy.
62 |
63 | 3. Object Code Incorporating Material from Library Header Files.
64 |
65 | The object code form of an Application may incorporate material from
66 | a header file that is part of the Library. You may convey such object
67 | code under terms of your choice, provided that, if the incorporated
68 | material is not limited to numerical parameters, data structure
69 | layouts and accessors, or small macros, inline functions and templates
70 | (ten or fewer lines in length), you do both of the following:
71 |
72 | a) Give prominent notice with each copy of the object code that the
73 | Library is used in it and that the Library and its use are
74 | covered by this License.
75 |
76 | b) Accompany the object code with a copy of the GNU GPL and this license
77 | document.
78 |
79 | 4. Combined Works.
80 |
81 | You may convey a Combined Work under terms of your choice that,
82 | taken together, effectively do not restrict modification of the
83 | portions of the Library contained in the Combined Work and reverse
84 | engineering for debugging such modifications, if you also do each of
85 | the following:
86 |
87 | a) Give prominent notice with each copy of the Combined Work that
88 | the Library is used in it and that the Library and its use are
89 | covered by this License.
90 |
91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license
92 | document.
93 |
94 | c) For a Combined Work that displays copyright notices during
95 | execution, include the copyright notice for the Library among
96 | these notices, as well as a reference directing the user to the
97 | copies of the GNU GPL and this license document.
98 |
99 | d) Do one of the following:
100 |
101 | 0) Convey the Minimal Corresponding Source under the terms of this
102 | License, and the Corresponding Application Code in a form
103 | suitable for, and under terms that permit, the user to
104 | recombine or relink the Application with a modified version of
105 | the Linked Version to produce a modified Combined Work, in the
106 | manner specified by section 6 of the GNU GPL for conveying
107 | Corresponding Source.
108 |
109 | 1) Use a suitable shared library mechanism for linking with the
110 | Library. A suitable mechanism is one that (a) uses at run time
111 | a copy of the Library already present on the user's computer
112 | system, and (b) will operate properly with a modified version
113 | of the Library that is interface-compatible with the Linked
114 | Version.
115 |
116 | e) Provide Installation Information, but only if you would otherwise
117 | be required to provide such information under section 6 of the
118 | GNU GPL, and only to the extent that such information is
119 | necessary to install and execute a modified version of the
120 | Combined Work produced by recombining or relinking the
121 | Application with a modified version of the Linked Version. (If
122 | you use option 4d0, the Installation Information must accompany
123 | the Minimal Corresponding Source and Corresponding Application
124 | Code. If you use option 4d1, you must provide the Installation
125 | Information in the manner specified by section 6 of the GNU GPL
126 | for conveying Corresponding Source.)
127 |
128 | 5. Combined Libraries.
129 |
130 | You may place library facilities that are a work based on the
131 | Library side by side in a single library together with other library
132 | facilities that are not Applications and are not covered by this
133 | License, and convey such a combined library under terms of your
134 | choice, if you do both of the following:
135 |
136 | a) Accompany the combined library with a copy of the same work based
137 | on the Library, uncombined with any other library facilities,
138 | conveyed under the terms of this License.
139 |
140 | b) Give prominent notice with the combined library that part of it
141 | is a work based on the Library, and explaining where to find the
142 | accompanying uncombined form of the same work.
143 |
144 | 6. Revised Versions of the GNU Lesser General Public License.
145 |
146 | The Free Software Foundation may publish revised and/or new versions
147 | of the GNU Lesser General Public License from time to time. Such new
148 | versions will be similar in spirit to the present version, but may
149 | differ in detail to address new problems or concerns.
150 |
151 | Each version is given a distinguishing version number. If the
152 | Library as you received it specifies that a certain numbered version
153 | of the GNU Lesser General Public License "or any later version"
154 | applies to it, you have the option of following the terms and
155 | conditions either of that published version or of any later version
156 | published by the Free Software Foundation. If the Library as you
157 | received it does not specify a version number of the GNU Lesser
158 | General Public License, you may choose any version of the GNU Lesser
159 | General Public License ever published by the Free Software Foundation.
160 |
161 | If the Library as you received it specifies that a proxy can decide
162 | whether future versions of the GNU Lesser General Public License shall
163 | apply, that proxy's public statement of acceptance of any version is
164 | permanent authorization for you to choose that version for the
165 | Library.
166 |
--------------------------------------------------------------------------------