Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State channels demo #12 #26

Merged
merged 15 commits into from Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/SmartDataProvider/package.json
Expand Up @@ -9,7 +9,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@aeternity/aepp-sdk": "1.0.1",
"@aeternity/aepp-sdk": "1.0.1",
"forgae": "git+https://github.com/aeternity/aepp-forgae-js.git"
}
}
8 changes: 8 additions & 0 deletions examples/StateChannelsBar/.babelrc
@@ -0,0 +1,8 @@
{
"presets": ["@babel/preset-env"],
"plugins": [
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-transform-runtime",
"@babel/plugin-proposal-export-default-from"
]
}
61 changes: 61 additions & 0 deletions examples/StateChannelsBar/.gitignore
@@ -0,0 +1,61 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next
34 changes: 34 additions & 0 deletions examples/StateChannelsBar/README.md
@@ -0,0 +1,34 @@
# ae-state-channels-demo
STATE CHANNEL DEMO

То run this demo you should update your docker image
```docker pull aeternity/aeternity:v1.4.0```

also you should install "Allow CORS: Access-Control-Allow-origin" browser extension and allow CORS for localhost.
PS: fix for chrome is this extension ```Moesif Orign & CORS Changer: https://chrome.google.com/webstore/detail/moesif-orign-cors-changer/digfbfaphojjndkpccljibejjbppifbc/related```

clone this repo
```git clone this_repo```

install packages
```npm install```

run node
```docker-compose up -d --force-recreate```

run the demo
```npm start```

The demo app can be started on http://localhost:4000

You as client and "the bar" as back end, in a browser you can paste your private/secret key to OPEN a channel, sign ON and OFF chain txs are in the front end.
Back end, check and sign transactions too with own private/secret key.

To open and close a state channel, 30k aettos are needed.

The app demonstrates how you deposit some aettos, create on chain tx and after that you can purchase some items/products from "the bar " where all txs are very fast and transparent. You have basic history and can watch your balance

If there is no off chain transaction for less than a minute channel close itself.
There are some logs in the browser and terminal.

!!! PS: if you cannot shutdown/close the state channel (throw an error), you should RESTART the node :)
4 changes: 4 additions & 0 deletions examples/StateChannelsBar/config/keyPair.js
@@ -0,0 +1,4 @@
module.exports = {
publicKey: 'ak_2mwRmUeYmfuW93ti9HMSUJzCk1EYcQEfikVSzgo6k2VghsWhgU',
secretKey: 'bb9f0b01c8c9553cfbaf7ef81a50f977b1326801ebf7294d1c2cbccdedf27476e9bbf604e611b5460a3b3999e9771b6f60417d73ce7c5519e12f7e127a1225ca'
}
20 changes: 20 additions & 0 deletions examples/StateChannelsBar/config/nodeConfig.js
@@ -0,0 +1,20 @@

let useTestNetConfiguration = false;

const wsAddress = 'wss://sdk-testnet.aepps.com/channel';

const API_URL = useTestNetConfiguration ? 'https://sdk-testnet.aepps.com' : 'http://localhost:3001'
const INTERNAL_API_URL = useTestNetConfiguration ? 'https://sdk-testnet.aepps.com' : 'http://localhost:3113'
const STATE_CHANNEL_URL = useTestNetConfiguration ? wsAddress : 'ws://localhost:3001'
const NETWORK_ID = useTestNetConfiguration ? 'ae_uat' : 'ae_devnet' // 'ae_uat'; //'ae_docker' , ae_devnet
const RESPONDER_HOST = useTestNetConfiguration ? wsAddress : 'localhost'
const RESPONDER_PORT = useTestNetConfiguration ? '' : 3333

module.exports = {
API_URL,
INTERNAL_API_URL,
STATE_CHANNEL_URL,
NETWORK_ID,
RESPONDER_HOST,
RESPONDER_PORT
}
5 changes: 5 additions & 0 deletions examples/StateChannelsBar/config/products.js
@@ -0,0 +1,5 @@
module.exports = {
beer: 5000,
water: 1000,
chips: 3000
}
40 changes: 40 additions & 0 deletions examples/StateChannelsBar/config/stateChannelConfig.js
@@ -0,0 +1,40 @@

const keyPair = require('./keyPair');
const nodeConfig = require('./nodeConfig');

// fee 20000000000000
const MINIMUM_DEPOSIT = 400000000000100 // 1000000; //'10000000000010000000';
const channelReserve = MINIMUM_DEPOSIT / 10; //'1000000000001000000'; // parseInt(MINIMUM_DEPOSIT * 0.25);

module.exports = {
params: {
// Public key of initiator
// (in this case `initiatorAddress` defined earlier)
initiatorId: '',
// Public key of responder
// (in this case `responderAddress` defined earlier)
responderId: keyPair.publicKey,
// Initial deposit in favour of the responder by the initiator
pushAmount: 0,
// Amount of tokens initiator will deposit into state channel
initiatorAmount: MINIMUM_DEPOSIT,
// Amount of tokens responder will deposit into state channel
responderAmount: MINIMUM_DEPOSIT,
// Minimum amount both peers need to maintain
channelReserve: channelReserve,
// Minimum block height to include the channel_create_tx
ttl: 1000,
// Amount of blocks for disputing a solo close
lockPeriod: 20,
// Host of the responder's node
host: nodeConfig.RESPONDER_HOST,
// Port of the responders node
port: nodeConfig.RESPONDER_PORT,
//fee: 1000,
//nonce: 1000
},
amounts :{
deposit: MINIMUM_DEPOSIT,
reserve: channelReserve
}
}
28 changes: 28 additions & 0 deletions examples/StateChannelsBar/controllers/config-controller.js
@@ -0,0 +1,28 @@
const config = require('./../config/stateChannelConfig');
const products = require('./../config/products');

async function getParams(req, res) {

let params = config.params

res.send(params);
}

function getProducts (req, res) {
let productArr = [];
for (productName in products) {
productArr.push({
name: productName,
price: products[productName]
});
}

res.send(productArr);
}

module.exports = {
get: {
getParams,
getProducts
}
}