Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pawelpisarek/eth-observable
observable ethereum wraper
https://github.com/pawelpisarek/eth-observable
angular4 blockchain dapp ethereum ethereum-contract observable solidity truffle-framework web3js
Last synced: about 1 month ago
JSON representation
observable ethereum wraper
- Host: GitHub
- URL: https://github.com/pawelpisarek/eth-observable
- Owner: PawelPisarek
- Created: 2017-12-02T12:50:15.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-12-27T20:44:01.000Z (about 7 years ago)
- Last Synced: 2024-12-15T15:48:16.837Z (about 1 month ago)
- Topics: angular4, blockchain, dapp, ethereum, ethereum-contract, observable, solidity, truffle-framework, web3js
- Language: TypeScript
- Size: 152 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# eth-observable
Wraper to _web3_ to instatly reload data from _smart contract_ :tada:
# Installation
`npm install eth-observable --save`
# Example:
https://github.com/PawelPisarek/truffle-angular-starter## 1. Create enum to represent smartcontract data. Every smartcontract service has own enum
```
export enum ContractEnum {
PING, PONG
}
```## 2 Create staticData, deployed interface and ContractValues class
### eg PongStaticData, PongDeployed, PongContract
```
export class PongStaticData {
constructor(public pongval: string, public creator: string) {
}
}
export interface PongDeployed extends TruffleContract {
pongval
getAddress;
getPongvalTransactional()
getPongvalConstant()
setPongval(_pongval, from)
getPongvalTxRetrievalAttempted()
kill()
}export class PongContract implements ContractValues {
constructor(public deployed: PongDeployed, public contract: PongStaticData) {
}getDeployed(): PongDeployed {
return this.deployed;
}getStaticData(): PongStaticData {
return this.contract;
}
}
```## 3. Create service to manage smart contract
```
import {default as Web3} from "web3";
import {InitializeContract, DeployedAndStaticData, ValuesContract, EthObservable, initializeContractHelper,
hideValuesHelper,
AppState,
ContractValues, TruffleContract} from "eth-observable";
import * as pong_artifacts from "../../../build/contracts/Pong.json";@Injectable()
export class PongService implements InitializeContract, ValuesContract {
private pongSource = new Subject>();
pong$ = this.pongSource.asObservable();
public dependsOnModifier = [true, true];constructor() {
}getUniqueName(): ContractEnum {
return ContractEnum.PONG;
}initialize(contractFactoryService: EthObservable, contractsEnum: InitializeContract, app: AppState): Observable {
return initializeContractHelper(contractFactoryService, this, this, pong_artifacts, this.pongSource, app);
}hideValues(array: any[]): any[] {
return hideValuesHelper(array, this.dependsOnModifier);
}getContractValuesPromise(deployed: PongDeployed, web3: Web3, hideVal: InitializeContract): Promise> {
const values = [deployed.pongval, deployed.getAddress.call()];
return Promise.all(hideVal.hideValues(values))
.then(data => {
return new PongContract(deployed, new PongStaticData(data[0], data[1]));
});
}
}
```## 4. Enjoy observable data from smart contract
```
export class ContractSenderComponent implements OnInit {
pong: DeployedAndStaticData;
amount: number;constructor(public _ethObservable: EthObservable, private _pongService: PongService) {
}ngOnInit() {
this._pongService.pong$.subscribe((data) => {
this.pong = data;
});
}onSubmit() {
this.pong.deployed.setPongval(this.amount, {from: this.pong.yoursAccounts});
this._ethObservable.refresh(this._pongService);
}initialize(appState: AppState) {
return this._pongService.initialize(this._ethObservable, this._pongService, appState);
}
}```
## 5. Initialize all data
```
export class AppComponent {
this._ethObservable.createConnection(new Web3(new Web3.providers.HttpProvider('http://localhost:8545')));
new Promise(res => {
this._ethObservable.web3.eth.getAccounts((err, accs) => {
if (err != null) {
alert("There was an error fetching your accounts.");
return;
}
if (accs.length === 0) {
alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
return;
}
this.accounts = accs;
this.account = this.accounts[0];
console.dir(this.accounts);
return res(this.account);
});
}).then((k: string) => {
const appState = new AppState(new Map(), new Map(), new Map(), k);
this._ethObservable.getAccounts()
.map(contractEnum => {
this._ethObservable.getContract(appState.mapAllContractFunction.get(contractEnum), appState);
return contractEnum;
})
.subscribe();
this.ping.initialize(appState).subscribe();
this.pong.initialize(appState).subscribe();
});
}
}
``````
{{ ping?.staticData.address }}
```