3️Integrate Data Union & Stream
Installation
You will need two node packages:
npm install streamr-client
npm i @dataunions/client
Enable Your Users To Publish Data
const StreamrClient = require('streamr-client')
// get your clients wallet
const WALLET_PROVIDER = // walletProviderOfYourChoice;
const streamr = new StreamrClient({
auth: WALLET_PROVIDER
})
// Publish messages to a stream (Add your Streamr id which can be found in Streamr Core)
streamr.publish('0xd4081fcd7b3d4006515f9daf7c7b6cc13935df13/demo-stream', {
hello: 'world',
})
Member Joins Data Union
Either Add members manually as the joinPart agent...
Make sure this is just a script and not your actual application. You don't want the Data Union client to be configured with the admins private key in your client application.
const PRIVATE_KEY_ADMIN = process.env.PRIVATE_KEY;
// Paste the contract address of your newly created data union
const DU_CONTRACT_ADDRESS = "0x0D483E10612F327FC11965Fc82E90dC19b141641"
const DU = new DataUnionClient({
auth: {
privateKey: PRIVATE_KEY,
},
chain: 'polygon',
});
const dataUnion = await DU.getDataUnion(DU_CONTRACT_ADDRESS);
const tx = await dataUnion.addMembers(['0xabcd', "0x1234"]);
Or let members join themselves via our default join server by providing a shared secret:
const PRIVATE_KEY_ADMIN = process.env.PRIVATE_KEY;
// Paste the contract address of your newly created data union
const DU_CONTRACT_ADDRESS = "0x0D483E10612F327FC11965Fc82E90dC19b141641"
const DU = new DataUnionClient({
auth: {
privateKey: PRIVATE_KEY_ADMIN,
},
chain: 'polygon',
});
const dataUnion = await DU.getDataUnion(
DATA_UNION_CONTRACT_ADDRESS
);
// returns an object with:
// your secret, secret_name, data union contract address and the chain
const sharedSecret = dataUnion.createSecret($custum_secret_name);
// get your clients wallet
const WALLET_PROVIDER = // walletProviderOfYourChoice;
// Paste the contract address of your newly created data union
const DU_CONTRACT_ADDRESS = "0x0D483E10612F327FC11965Fc82E90dC19b141641";
const SHARED_SECRET = process.env.SHARED_SECRET;
const DU = new DataUnionClient({
auth: WALLET_PROVIDER,
chain: 'polygon',
});
const dataUnion = await DU.getDataUnion(
DATA_UNION_CONTRACT_ADDRESS
);
const joinResponse = dataUnion.join(SHARED_SECRET);
Withdraw Earnings
// get your clients wallet
const WALLET_PROVIDER = // walletProviderOfYourChoice;
// Paste the contract address of your newly created data union
const DU_CONTRACT_ADDRESS = "0x0D483E10612F327FC11965Fc82E90dC19b141641";
const DU = new DataUnionClient({
auth: {
WALLET_PROVIDER
},
chain: 'polygon',
});
const earnings = await dataUnion.getWithdrawableEarnings(
'0x008ef9bB0d80829ea82e53fFE40F35B4eaDd1319'
);
const tx = await dataUnion.withdrawAll()
Last updated
Was this helpful?