-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial import based on Chris's porting from web3.
- Loading branch information
Showing
73 changed files
with
13,470 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ""; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.