Commit b553d26c authored by Quentin Bramas's avatar Quentin Bramas
Browse files

simple example

parent 65e8a449
import fs from 'fs';
import web3 from '../common/web3';
let account = web3.eth.accounts.privateKeyToAccount(
fs.readFileSync('.accountSSS', 'utf8')
);
web3.eth.defaultAccount = account.address;
web3.eth.accounts.wallet.add(account);
console.log('current account', account.address);
const abi = JSON.parse(fs.readFileSync('contract/firstContract_sol_TestValue.abi', 'utf8'));
async function deployContract()
{
const compiledSource = fs.readFileSync('contract/firstContract_sol_TestValue.bin', 'utf8');
const contract = new web3.eth.Contract(abi, {
gasPrice: web3.utils.toWei('50', 'gwei'),
from: web3.eth.defaultAccount
});
// Smart contract EVM bytecode as hex
contract.options.data = '0x' + compiledSource;
console.log('deploying contract');
const result = await contract
.deploy({
arguments: []
})
.send({
gas: 1500000
})
console.log('contract deployed');
console.log(result);
}
async function interact() {
const contract = new web3.eth.Contract(abi, {
gasPrice: web3.utils.toWei('50', 'gwei'),
from: web3.eth.defaultAccount
});
contract.options.address = '0xBcD41F2B2E895D6521C414276b8103936b778bd6'
console.log('sending transaction');
/*
const result = await contract.methods.donate().send({
value: web3.utils.toWei('1', 'ether'),
gas: 100000
});*/
/*const result = await contract.methods.changeWinnerString("I love SSS").send({
gas: 100000
});*/
/*const result = await contract.methods.bestDonator().call();*/
const gasEstimate = await contract.methods.withdraw().estimateGas();
const result = await contract.methods.withdraw().send({
gas: gasEstimate
});
console.log(result)
}
//deployContract();
interact();
import fs from 'fs';
import web3 from '../common/web3';
import createOrLoadAccount from './createOrLoadAccount';
import * as commands from '../common/commands';
import createContractCommand from './createContractCommand';
let account = web3.eth.accounts.privateKeyToAccount(
fs.readFileSync('.accountSSS', 'utf8')
);
web3.eth.defaultAccount = account.address;
web3.eth.accounts.wallet.add(account);
console.log('current account', account.address);
commands['createContract'] = createContractCommand;
const abi = JSON.parse(fs.readFileSync('contract/firstContract_sol_TestValue.abi', 'utf8'));
if(process.argv.length < 4)
async function deployContract()
{
console.log('Usage:\nnode index.js PRIVATE_KEY_FILE COMMAND ARGS');
process.exit(1);
const compiledSource = fs.readFileSync('contract/firstContract_sol_TestValue.bin', 'utf8');
const contract = new web3.eth.Contract(abi, {
gasPrice: web3.utils.toWei('50', 'gwei'),
from: web3.eth.defaultAccount
});
// Smart contract EVM bytecode as hex
contract.options.data = '0x' + compiledSource;
console.log('deploying contract');
const result = await contract
.deploy({
arguments: []
})
.send({
gas: 1500000
})
console.log('contract deployed');
console.log(result);
}
async function interact() {
const contract = new web3.eth.Contract(abi, {
gasPrice: web3.utils.toWei('50', 'gwei'),
from: web3.eth.defaultAccount
});
let account = createOrLoadAccount(process.argv[2]);
web3.eth.defaultAccount = account.address;
web3.eth.accounts.wallet.add(account);
console.log('account: '+account.address)
contract.options.address = '0xBcD41F2B2E895D6521C414276b8103936b778bd6'
console.log('sending transaction');
/*
const result = await contract.methods.donate().send({
value: web3.utils.toWei('1', 'ether'),
gas: 100000
});*/
/*const result = await contract.methods.changeWinnerString("I love SSS").send({
gas: 100000
});*/
/*const result = await contract.methods.bestDonator().call();*/
const command = process.argv[3];
if(!commands[command])
{
console.log('Available commands:');
Object.keys(commands).forEach(c => console.log(' -',c));
process.exit(1);
}
console.log('running command', command);
try{
commands[command].apply(null, process.argv.slice(4)).then(console.log);
}
catch (e)
{
console.error('error while running command', command, e);
const gasEstimate = await contract.methods.withdraw().estimateGas();
const result = await contract.methods.withdraw().send({
gas: gasEstimate
});
console.log(result)
}
//deployContract();
interact();
import web3 from '../common/web3';
import createOrLoadAccount from './createOrLoadAccount';
import * as commands from '../common/commands';
import createContractCommand from './createContractCommand';
commands['createContract'] = createContractCommand;
if(process.argv.length < 4)
{
console.log('Usage:\nnode index.js PRIVATE_KEY_FILE COMMAND ARGS');
process.exit(1);
}
let account = createOrLoadAccount(process.argv[2]);
web3.eth.defaultAccount = account.address;
web3.eth.accounts.wallet.add(account);
console.log('account: '+account.address)
const command = process.argv[3];
if(!commands[command])
{
console.log('Available commands:');
Object.keys(commands).forEach(c => console.log(' -',c));
process.exit(1);
}
console.log('running command', command);
try{
commands[command].apply(null, process.argv.slice(4)).then(console.log);
}
catch (e)
{
console.error('error while running command', command, e);
}
import { h, app } from "hyperapp";
import { preventDefault, targetValue } from "@hyperapp/events"
import * as commands from '../common/commands';
import web3 from '../common/web3';
import PrivateKeyForm from './PrivateKeyForm'
import Reader from './Reader'
import Editor from './Editor'
import Reviewer from './Reviewer'
import Author from './Author'
import { dispatchAsync } from "./dispatchAsync"
import account from './account'
import prettyAddr from './prettyAddr'
import { fetchJournalPaper } from './JournalPaper';
const setActiveTab = (activeTab) => (state) => ({...state, activeTab});
const tabs = [
{title:'Reader', component: Reader},
{title:'Author', component: Author},
{title:'Reviewer', component: Reviewer},
{title:'Editor', component: Editor},
];
import web3 from '../common/web3';
import PrivateKeyForm from './PrivateKeyForm';
const Layout = (state) => {
return <div class="container">
......@@ -30,106 +13,15 @@ const Layout = (state) => {
<small>full address: {account(state).address}</small>
</div>
}
<div>The journal: {prettyAddr(state.journalAdr)}</div>
<div>
{state.fetchingJournal && 'Loading...'}
{state.numPapers === null ? '' : state.numPapers+' papers'}
{' '}
<button
class="btn btn-info"
onclick={preventDefault(dispatchAsync((state) => async (dispatch) => {
console.log('fetching Journal')
dispatch({...state, fetchingJournal:true});
const r = await commands.journal(state.journalAdr);
dispatch({...state, numPapers:r.numPapers, fetchingJournal:false});
console.log('fetched journal', r);
}))}>
Fetch journal information
</button>
</div><br/>
<ul class="nav nav-tabs">
{
tabs.map(t => <li class="nav-item">
<a class={'nav-link'+(state.activeTab === t.title?' active':'')}
href="#"
onclick={preventDefault(setActiveTab(t.title))}>
{t.title}
</a>
</li>)
}
</ul>
<div class="tab-content" id="myTabContent">
{
tabs
.filter(t => state.activeTab === t.title)
.map(t => <div
title={t.title}
class={'tab-pane show'+(state.activeTab === t.title?' active':'')}
role="tabpanel">
<t.component state={state} />
</div>)
}
</div>
Hello
</div>
}
let defaultKey = localStorage.getItem('defaultPrivateKey');
if(defaultKey)
{
web3.eth.defaultAccount = account(defaultKey).address;
commands.setFrom(account(defaultKey).address);
web3.eth.accounts.wallet.add(account(defaultKey));
}
let paperMatch = window.location.search.match(/(\?|\&)paper=([^&]*)$/)
let defaultPaper = {
paperForm:{}
};
if(paperMatch)
{
try{
const paperForm = JSON.parse(decodeURIComponent(paperMatch[2]));
const paperFormJson = JSON.stringify(paperForm);
const paperFormHash = web3.utils.sha3(paperFormJson);
defaultPaper = {
paperForm,
paperFormJson,
paperFormHash
}
}
catch (e) { }//ignore errors
}
let journalPaperMatch = window.location.search.match(/(\?|\&)journalPaper=([^&]*)/)
console.log(journalPaperMatch)
app({
init: {
journalAdr: '0x5353531074558f4b7fcf0d52fc3b11be1f07b6c1',
privateKey: defaultKey,
numPapers: null,
activeTab: 'Reader',
...defaultPaper
privateKey: '',
winnerString: ''
},
view: Layout,
node: document.getElementById("app"),
subscriptions: journalPaperMatch && ((action, props) => [
(dispatch, props) => {
if(!journalPaperMatch) return (() => {});
const idx = journalPaperMatch[2]
journalPaperMatch = false
setTimeout(() => {
dispatch((state) => ({
...state,
paperIdxInput: idx
}));
console.log('dispatch paperIdxInput', idx)
dispatch(fetchJournalPaper)
}, 1000);
return (() => {});
},
{}
])
})
import { h, app } from "hyperapp";
import { preventDefault, targetValue } from "@hyperapp/events"
import * as commands from '../common/commands';
import web3 from '../common/web3';
import PrivateKeyForm from './PrivateKeyForm'
import Reader from './Reader'
import Editor from './Editor'
import Reviewer from './Reviewer'
import Author from './Author'
import { dispatchAsync } from "./dispatchAsync"
import account from './account'
import prettyAddr from './prettyAddr'
import { fetchJournalPaper } from './JournalPaper';
const setActiveTab = (activeTab) => (state) => ({...state, activeTab});
const tabs = [
{title:'Reader', component: Reader},
{title:'Author', component: Author},
{title:'Reviewer', component: Reviewer},
{title:'Editor', component: Editor},
];
const Layout = (state) => {
return <div class="container">
{ !state.privateKey
? <div class="container"><PrivateKeyForm /></div>
: <div><h2>Your account: {prettyAddr(account(state).address)} <small><a href="#" onclick={preventDefault((state) => ({...state, privateKey:null}))}>change</a></small></h2>
<small>full address: {account(state).address}</small>
</div>
}
<div>The journal: {prettyAddr(state.journalAdr)}</div>
<div>
{state.fetchingJournal && 'Loading...'}
{state.numPapers === null ? '' : state.numPapers+' papers'}
{' '}
<button
class="btn btn-info"
onclick={preventDefault(dispatchAsync((state) => async (dispatch) => {
console.log('fetching Journal')
dispatch({...state, fetchingJournal:true});
const r = await commands.journal(state.journalAdr);
dispatch({...state, numPapers:r.numPapers, fetchingJournal:false});
console.log('fetched journal', r);
}))}>
Fetch journal information
</button>
</div><br/>
<ul class="nav nav-tabs">
{
tabs.map(t => <li class="nav-item">
<a class={'nav-link'+(state.activeTab === t.title?' active':'')}
href="#"
onclick={preventDefault(setActiveTab(t.title))}>
{t.title}
</a>
</li>)
}
</ul>
<div class="tab-content" id="myTabContent">
{
tabs
.filter(t => state.activeTab === t.title)
.map(t => <div
title={t.title}
class={'tab-pane show'+(state.activeTab === t.title?' active':'')}
role="tabpanel">
<t.component state={state} />
</div>)
}
</div>
</div>
}
let defaultKey = localStorage.getItem('defaultPrivateKey');
if(defaultKey)
{
web3.eth.defaultAccount = account(defaultKey).address;
commands.setFrom(account(defaultKey).address);
web3.eth.accounts.wallet.add(account(defaultKey));
}
let paperMatch = window.location.search.match(/(\?|\&)paper=([^&]*)$/)
let defaultPaper = {
paperForm:{}
};
if(paperMatch)
{
try{
const paperForm = JSON.parse(decodeURIComponent(paperMatch[2]));
const paperFormJson = JSON.stringify(paperForm);
const paperFormHash = web3.utils.sha3(paperFormJson);
defaultPaper = {
paperForm,
paperFormJson,
paperFormHash
}
}
catch (e) { }//ignore errors
}
let journalPaperMatch = window.location.search.match(/(\?|\&)journalPaper=([^&]*)/)
console.log(journalPaperMatch)
app({
init: {
journalAdr: '0x5353531074558f4b7fcf0d52fc3b11be1f07b6c1',
privateKey: defaultKey,
numPapers: null,
activeTab: 'Reader',
...defaultPaper
},
view: Layout,
node: document.getElementById("app"),
subscriptions: journalPaperMatch && ((action, props) => [
(dispatch, props) => {
if(!journalPaperMatch) return (() => {});
const idx = journalPaperMatch[2]
journalPaperMatch = false
setTimeout(() => {
dispatch((state) => ({
...state,
paperIdxInput: idx
}));
console.log('dispatch paperIdxInput', idx)
dispatch(fetchJournalPaper)
}, 1000);
return (() => {});
},
{}
])
})
const network = 'rinkeby';
//const network = 'rinkeby';
//const network = 'ropsten';
//const url = 'https://'+network+'.infura.io/v3/KEY';
const url = 'http://127.0.0.1:8501';
const Web3 = require('web3');
import Web3 from 'web3';
export default new Web3(url);
pragma solidity >=0.4.26 <0.5.0;
contract TestValue {
string public winnerString = "";
struct Person {
string favoriteString;
uint256 donated;
}
mapping (address => Person) public donators;
uint256 public maxDonation = 0;
address public bestDonator;
function donate() public payable
{
Person storage person = donators[msg.sender];
person.donated += msg.value;
if(person.donated > maxDonation)
{
maxDonation = person.donated;
bestDonator = msg.sender;
}
}
function changeWinnerString(string memory _s) public
{
require(msg.sender == bestDonator);
winnerString = _s;
}
function donatedBy() public view returns (uint256)
{
return donators[msg.sender].donated;
}
function withdraw() public
{
require(donators[msg.sender].donated > 0);
require(msg.sender != bestDonator);
donators[msg.sender].donated = 0;
msg.sender.transfer(donators[msg.sender].donated);
}
function () private
{
}
}
[{"constant":false,"inputs":[],"name":"withdraw","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"winnerString","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"bestDonator","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_s","type":"string"}],"name":"changeWinnerString","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"maxDonation","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"donatedBy","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"donators","outputs":[{"name":"favoriteString","type":"string"},{"name":"donated","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"donate","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"payable":false,"stateMutability":"nonpayable","type":"fallback"}]
\ No newline at end of file
608060405260206040519081016040528060008152506000908051906020019061002a929190610042565b50600060025534801561003c57600080fd5b506100e7565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061008357805160ff19168380011785556100b1565b828001600101855582156100b1579182015b828111156100b0578251825591602001919060010190610095565b5b5090506100be91906100c2565b5090565b6100e491905b808211156100e05760008160009055506001016100c8565b5090565b90565b610872806100f66000396000f30060806040526004361061008e576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633ccfd60b1461009d5780635be097e2146100b457806382958ec914610144578063a78ae3d41461019b578063a8681fd914610204578063ccf3aa551461022f578063d24f8cdd1461025a578063ed88c68e1461031d575b34801561009a57600080fd5b50005b3480156100a957600080fd5b506100b2610327565b005b3480156100c057600080fd5b506100c96104a8565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101095780820151818401526020810190506100ee565b50505050905090810190601f1680156101365780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561015057600080fd5b50610159610546565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b3480156101a757600080fd5b50610202600480360381019080803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919291929050505061056c565b005b34801561021057600080fd5b506102196105e2565b6040518082815260200191505060405180910390f35b34801561023b57600080fd5b506102446105e8565b6040518082815260200191505060405180910390f35b34801561026657600080fd5b5061029b600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610632565b6040518080602001838152602001828103825284818151815260200191508051906020019080838360005b838110156102e15780820151818401526020810190506102c6565b50505050905090810190601f16801561030e5780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b6103256106ee565b005b6000600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206001015411151561037857600080fd5b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141515156103d557600080fd5b6000600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600101819055503373ffffffffffffffffffffffffffffffffffffffff166108fc600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600101549081150290604051600060405180830381858888f193505050501580156104a5573d6000803e3d6000fd5b50565b60008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561053e5780601f106105135761010080835404028352916020019161053e565b820191906000526020600020905b81548152906001019060200180831161052157829003601f168201915b505050505081565b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156105c857600080fd5b80600090805190602001906105de9291906107a1565b5050565b60025481565b6000600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010154905090565b6001602052806000526040600020600091509050806000018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156106de5780601f106106b3576101008083540402835291602001916106de565b820191906000526020600020905b8154815290600101906020018083116106c157829003601f168201915b5050505050908060010154905082565b6000600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002090503481600101600082825401925050819055506002548160010154111561079e57806001015460028190555033600360006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505b50565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106107e257805160ff1916838001178555610810565b82800160010185558215610810579182015b8281111561080f5782518255916020019190600101906107f4565b5b50905061081d9190610821565b5090565b61084391905b8082111561083f576000816000905550600101610827565b5090565b905600a165627a7a72305820bd837d474ba2a3d63ba4d4d7fe2c127a2c7c406396cddd3cbf75db4e1255c9d10029
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment