You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.0 KiB

4 years ago
import Web3 from 'web3';
import Contract from 'web3-eth-contract';
import IPFS from 'ipfs';
4 years ago
import { forumContract } from 'concordia-contracts';
import { createOrbitInstance, getPeerDatabases, openKVDBs } from './utils/orbitUtils';
import ipfsOptions from './options/ipfsOptions';
4 years ago
import { WEB3_PROVIDER_URL } from './constants';
import { startAPI } from './app';
process.on('unhandledRejection', error => {
// This happens when attempting to initialize without any available Swarm addresses (e.g. Rendezvous)
if(error.code === 'ERR_NO_VALID_ADDRESSES'){
console.error('unhandledRejection', error.message);
process.exit(1);
}
});
4 years ago
async function main () {
4 years ago
console.log('Initializing...');
const web3 = new Web3(new Web3.providers.WebsocketProvider(WEB3_PROVIDER_URL));
4 years ago
const networkId = await web3.eth.net.getId();
const contractAddress = forumContract.networks[networkId].address;
4 years ago
Contract.setProvider(WEB3_PROVIDER_URL);
4 years ago
const contract = new Contract(forumContract.abi, contractAddress);
4 years ago
const ipfs = await IPFS.create(ipfsOptions);
const orbit = await createOrbitInstance(ipfs, contractAddress);
4 years ago
// Open & replicate databases of existing users
const userAddresses = await contract.methods.getUserAddresses().call();
const peerDBs = await getPeerDatabases(orbit, userAddresses);
await openKVDBs(orbit, peerDBs);
// Listen for new users and subscribe to their databases
const eventJsonInterface = web3.utils._.find(
contract._jsonInterface,
obj => obj.name === "UserSignedUp" && obj.type === 'event'
);
web3.eth.subscribe('logs', {
address: contractAddress,
topics: [eventJsonInterface.signature]
}, function(error, result){
if (!error) {
const eventObj = web3.eth.abi.decodeLog(
eventJsonInterface.inputs,
result.data,
result.topics.slice(1)
)
4 years ago
const userAddress = eventObj[1];
console.log(`User signed up:`, userAddress);
getPeerDatabases(orbit, [userAddress]).then(peerDBs => openKVDBs(orbit, peerDBs));
4 years ago
}
});
4 years ago
startAPI(orbit);
4 years ago
}
main();