Skip to content

Commit

Permalink
initial import based on Chris's porting from web3.
Browse files Browse the repository at this point in the history
  • Loading branch information
aion-jin committed Feb 1, 2018
1 parent 3b1c8f1 commit 4545c79
Show file tree
Hide file tree
Showing 73 changed files with 13,470 additions and 6 deletions.
8 changes: 4 additions & 4 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.

Expand Down Expand Up @@ -645,7 +645,7 @@ the "copyright" line and a pointer to where the full notice is found.
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
along with this program. If not, see <https://www.gnu.org/licenses/>.

Also add information on how to contact you by electronic and paper mail.

Expand All @@ -664,11 +664,11 @@ might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<http://www.gnu.org/licenses/>.
<https://www.gnu.org/licenses/>.

The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
<https://www.gnu.org/licenses/why-not-lgpl.html>.
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
# aion_web3
aion web3 api.
# Aion Compatible Web3

## Requirements

* nodejs
* npm

## Setup

```bash
git clone https://github.com/aionnetwork/aion_web3
go to download folder then enter "npm install"
gulp build
```

## Usage

### for web
* include `dist/web3.min.js` in your html file.

### for nodejs
* npm install {path-to-aion-web3-folder}

## Develop

Set a provider (HttpProvider)

```js
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
var coinbase = web3.eth.coinbase;
var balance = web3.eth.getBalance(coinbase);
```
11 changes: 11 additions & 0 deletions example/compile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = function(w3, sol){
return new Promise((resolve, reject)=>{
w3.eth.compile.solidity(sol, (err, res)=>{
if(err)
reject(err)
if(res){
resolve(res)
}
})
})
}
14 changes: 14 additions & 0 deletions example/token-on-aion/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const core = require('./core.js');
const log = require('./core.js').log;
const parse = require('./parser.js').parse;
const execute = require('./executor.js').execute;

(async () => {
try {
const arguments = process.argv.slice(2);
const statement = parse(arguments);
execute(statement);
} catch (e) {
log(e);
}
})();
4 changes: 4 additions & 0 deletions example/token-on-aion/commands.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node app.js DEPLOY
node app.js MINT 0x8f30ce8eb81c57388bc25820b0f8d0612451c9f90091224028b9c562fc9c7036 100
node app.js TRANSFER 0x0000000000000000000000000000000000000000000000000000000000000000 100
node app.js BALANCE 0x0000000000000000000000000000000000000000000000000000000000000000
19 changes: 19 additions & 0 deletions example/token-on-aion/compile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = function(w3, sol){
return new Promise((resolve, reject)=>{
w3.eth.compile.solidity(sol, (err, res)=>{
if(err)
reject(err)
if(res){
let name = Object.keys(res)[0]
let compiled = res[name]
let abi = compiled.info.abiDefinition
let code = compiled.code
resolve({
name: name,
abi: abi,
binary: code
});
}
})
})
}
4 changes: 4 additions & 0 deletions example/token-on-aion/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports.provider = "http://127.0.0.1:8545";
module.exports.userAddress = "";
module.exports.password = "";
module.exports.contractAddress = "";
149 changes: 149 additions & 0 deletions example/token-on-aion/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
const path = require('path');
const request = require('request');
const moment = require('moment');
const Web3 = require('../../lib/web3.js');
const config = require(path.resolve(__dirname, 'config.js'));
const compile = require(path.resolve(__dirname, 'compile.js'));

const web3 = new Web3(new Web3.providers.HttpProvider(config.provider));
// token abi
const abi = [{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint128"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint128"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint128"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"recipient","type":"address"},{"name":"value","type":"uint128"}],"name":"mint","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint128"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint128"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_value","type":"uint128"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_owner","type":"address"},{"indexed":true,"name":"_spender","type":"address"},{"indexed":false,"name":"_value","type":"uint128"}],"name":"Approval","type":"event"}];
let rpcNonce = 0;

const log = (s) => {
const now = moment();
console.log("[" + now.format() + "] " + s);
}
module.exports.log = log;

const req = (callForm) => {
// append the id here
callForm.jsonrpc = "2.0";
callForm.id = rpcNonce.toString();
rpcNonce++;

const options = {
url: config.provider,
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
json: callForm
}

return new Promise((resolve, reject) => {
request(options, (err, resp, body) => {
if (err)
reject(err);
resolve(body);
});
});
}

module.exports.compile = async (sourceCode) => {
return await compile(web3, sourceCode);
}

module.exports.getAccounts = async () => {
const resp = await req({
method: "eth_accounts",
params: []
});
return resp.result;
}

module.exports.unlockAccount = async (address, password, duration) => {
const resp = await req({
method: "personal_unlockAccount",
params: [address, password, duration]
});
return resp.result;
}

module.exports.deploy = ({name, abi, binary}, userAddress) => {
const options = {
from: config.userAddress,
gas: 2000000,
gasPrice: 1,
data: binary,
};

return new Promise((resolve, reject) => {
web3.eth.contract(abi).new(options, (err, contract) => {
if (err)
reject(err);

if (contract && contract.address)
resolve(contract);
});
});
}

module.exports.createAt = (contractAddress) => {
return web3.eth.contract(abi).at(contractAddress);
}

const getTransaction = (txHash) => {
return new Promise((resolve, reject) => {
web3.eth.getTransaction(txHash, (err, res) => {
if (err)
reject(err);
resolve(res);
});
});
}

// functions below all use the contract object

module.exports.mint = async ({contract, address, value, userAddress}) => {
const txHash = await new Promise((resolve, reject) => {
log(address);
log(value);
contract.mint(address, value, {
from: userAddress,
gas: 100000,
gasPrice: 1}, (err, res) => {
if (err)
reject(err);
resolve(res);
});
});

return txHash;
// // TODO: verify if this logic is correct
// while (true) {
// let result = null;
// try {
// result = await getTransaction(txHash);
// } catch (e) {
// result = null;
// }

// if (result)
// break;
// }
// return result;
}

module.exports.transfer = async ({contract, to, value, userAddress}) => {
const txHash = await new Promise((resolve, reject) => {
contract.transfer(to, value, {
from: userAddress,
gas: 100000,
gasPrice: 1}, (err, res) => {
if (err)
reject(err);
resolve(res);
});
});
}

module.exports.getBalance = ({contract, address}) => {
return new Promise((resolve, reject) => {
contract.balanceOf(address, (err, res) => {
if (err)
reject(err);
resolve(res);
});
});
}
63 changes: 63 additions & 0 deletions example/token-on-aion/executor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const path = require('path');
const core = require('./core.js');
const tokens = require('./token.js');
const config = require('./config.js');
const fs = require('fs');
const log = require('./core.js').log;

const unlock = async () => {
let unlocked = false;
try {
await core.unlockAccount(config.userAddress, "PLAT4life", 314159);
} catch (e) {
// do nothing here
}
return unlocked;
}

module.exports.execute = async (statement) => {

if (statement.token === tokens.DEPLOY) {
await unlock();
const source = fs.readFileSync(path.resolve(__dirname, 'token.sol'), "utf-8");
const compiled = await core.compile(source);
const tokenInstance = await core.deploy(compiled, config.userAddress);
log("token deployed at: " + tokenInstance.address);
log("please set the contractAddress field in your config to this address");
}

//{contract, address, value, userAddress}
if (statement.token === tokens.MINT) {
await unlock();
const tokenInstance = core.createAt(config.contractAddress);
const response = await core.mint({
contract: tokenInstance,
address: statement.address,
value: statement.value,
userAddress: config.userAddress
});
log("transaction sent: " + response);
}

if (statement.token === tokens.TRANSFER) {
await unlock();
const tokenInstance = core.createAt(config.contractAddress);
//{contract, to, value, userAddress}
const response = await core.transfer({
contract: tokenInstance,
to: statement.address,
value: statement.value,
userAddress: config.userAddress
});
}

if (statement.token === tokens.BALANCE) {
const tokenInstance = core.createAt(config.contractAddress);
log(JSON.stringify(tokenInstance));
const balance = await core.getBalance({
contract: tokenInstance,
address: statement.address
});
log("address: " + statement.address + " has balance: " + balance);
}
}
Loading

0 comments on commit 4545c79

Please sign in to comment.