forked from dhiway/cord.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Network Scoring Companion (dhiway#30)
* score companion module * dependency updates * score demos Signed-off-by: "Satish Mohan" <satish@dhiway.com>
- Loading branch information
Showing
21 changed files
with
1,756 additions
and
1,043 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
import * as Cord from '@cord.network/sdk' | ||
import moment from 'moment' | ||
import Keyring from '@polkadot/keyring' | ||
import { ApiPromise, WsProvider } from '@polkadot/api' | ||
import { ScoreType } from '@cord.network/types' | ||
import { UUID } from '@cord.network/utils' | ||
|
||
export const sleep = (ms: number): Promise<void> => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => resolve(), ms) | ||
}) | ||
} | ||
function getRandomFloat(min: number, max: number, decimals: number) { | ||
const str = (Math.random() * (max - min) + min).toFixed(decimals) | ||
|
||
return parseFloat(str) | ||
} | ||
async function main() { | ||
await Cord.init({ address: 'ws://127.0.0.1:9944' }) | ||
const wsProvider = new WsProvider('ws://127.0.0.1:9944') | ||
const api = await ApiPromise.create({ provider: wsProvider }) | ||
|
||
// Step 1: Setup Identities | ||
console.log(`\n❄️ Demo Identities (KeyRing)`) | ||
|
||
const sellerIdentity = Cord.Identity.buildFromURI('//Entity', { | ||
signingKeyPairType: 'sr25519', | ||
}) | ||
console.log( | ||
`🏛 Seller Entity (${sellerIdentity.signingKeyType}): ${sellerIdentity.address}` | ||
) | ||
const deliveryIdentity = Cord.Identity.buildFromURI('//Delivery', { | ||
signingKeyPairType: 'sr25519', | ||
}) | ||
console.log( | ||
`🏛 Delivery Entity (${deliveryIdentity.signingKeyType}): ${deliveryIdentity.address}` | ||
) | ||
const collectorIdentity = Cord.Identity.buildFromURI('//BuyerApp', { | ||
signingKeyPairType: 'ed25519', | ||
}) | ||
console.log( | ||
`📱 Score Collector (${collectorIdentity.signingKeyType}): ${collectorIdentity.address}` | ||
) | ||
const requestorIdentity = Cord.Identity.buildFromURI('//SellerApp', { | ||
signingKeyPairType: 'ed25519', | ||
}) | ||
console.log( | ||
`📱 Score Requestor (${requestorIdentity.signingKeyType}): ${requestorIdentity.address}` | ||
) | ||
const poolTransactionAuthor = Cord.Identity.buildFromURI('//Bob', { | ||
signingKeyPairType: 'sr25519', | ||
}) | ||
console.log( | ||
`🏢 Pool Author (${poolTransactionAuthor.signingKeyType}): ${poolTransactionAuthor.address}` | ||
) | ||
let keyring = new Keyring({ type: 'sr25519' }) | ||
let batchTransactionAuthor = keyring.addFromUri('//Charlie') | ||
console.log( | ||
`🏢 Batch Author (${keyring.type}): ${batchTransactionAuthor.address}` | ||
) | ||
console.log('✅ Identities created!') | ||
|
||
let txPoolBatch: any = [] | ||
|
||
let startTxPrep = moment() | ||
let txPoolCount = 125 | ||
|
||
console.log(`\n❄️ Pool Submission `) | ||
for (let j = 0; j < txPoolCount; j++) { | ||
let tidUid = UUID.generate().toString() | ||
let overallPoolEntryContent = { | ||
entity: sellerIdentity.address, | ||
uid: UUID.generate().toString(), | ||
tid: tidUid, | ||
collector: collectorIdentity.address, | ||
requestor: requestorIdentity.address, | ||
scoreType: ScoreType.overall, | ||
score: getRandomFloat(1.5, 4.5, 2), | ||
} | ||
|
||
let newOverallPoolJournalEntry = Cord.Score.fromJournalProperties( | ||
overallPoolEntryContent, | ||
sellerIdentity | ||
) | ||
|
||
try { | ||
let txOverallPoolEntry = await Cord.Score.entries( | ||
newOverallPoolJournalEntry | ||
) | ||
txPoolBatch.push(txOverallPoolEntry) | ||
} catch (e: any) { | ||
console.log(e.errorCode, '-', e.message) | ||
} | ||
|
||
let deliveryPoolEntryContent = { | ||
entity: deliveryIdentity.address, | ||
uid: UUID.generate().toString(), | ||
tid: tidUid, | ||
collector: collectorIdentity.address, | ||
requestor: requestorIdentity.address, | ||
scoreType: ScoreType.delivery, | ||
score: getRandomFloat(0.5, 3.5, 2), | ||
} | ||
let newDeliveryPoolJournalEntry = Cord.Score.fromJournalProperties( | ||
deliveryPoolEntryContent, | ||
deliveryIdentity | ||
) | ||
try { | ||
let txDeliveryPoolEntry = await Cord.Score.entries( | ||
newDeliveryPoolJournalEntry | ||
) | ||
txPoolBatch.push(txDeliveryPoolEntry) | ||
} catch (e: any) { | ||
console.log(e.errorCode, '-', e.message) | ||
} | ||
} | ||
let ancStartTime = moment() | ||
// console.log('\n') | ||
for (let i = 0; i < txPoolBatch.length; i++) { | ||
process.stdout.write( | ||
' ✨ Creation, Transformation & Submission of ' + | ||
(i + 1) + | ||
' extrinsics to the pool took ' + | ||
moment.duration(moment().diff(ancStartTime)).as('seconds').toFixed(3) + | ||
's\r' | ||
) | ||
|
||
try { | ||
await Cord.Chain.signAndSubmitTx(txPoolBatch[i], poolTransactionAuthor, { | ||
resolveOn: Cord.Chain.IS_READY, | ||
rejectOn: Cord.Chain.IS_ERROR, | ||
}) | ||
} catch (e: any) { | ||
console.log(e.errorCode, '-', e.message) | ||
} | ||
} | ||
|
||
console.log(`\n❄️ Batch Anchoring `) | ||
let txBatch: any = [] | ||
let startBatchPrep = moment() | ||
let txBatchCount = 125 | ||
for (let j = 0; j < txBatchCount; j++) { | ||
let tidUid = UUID.generate().toString() | ||
let overallEntryContent = { | ||
entity: sellerIdentity.address, | ||
uid: UUID.generate().toString(), | ||
tid: tidUid, | ||
collector: collectorIdentity.address, | ||
requestor: requestorIdentity.address, | ||
scoreType: ScoreType.overall, | ||
score: getRandomFloat(1.5, 4.5, 2), | ||
} | ||
|
||
let newOverallJournalEntry = Cord.Score.fromJournalProperties( | ||
overallEntryContent, | ||
sellerIdentity | ||
) | ||
|
||
try { | ||
let txOverallEntry = await Cord.Score.entries(newOverallJournalEntry) | ||
txBatch.push(txOverallEntry) | ||
} catch (e: any) { | ||
console.log(e.errorCode, '-', e.message) | ||
} | ||
|
||
let deliveryEntryContent = { | ||
entity: deliveryIdentity.address, | ||
uid: UUID.generate().toString(), | ||
tid: tidUid, | ||
collector: collectorIdentity.address, | ||
requestor: requestorIdentity.address, | ||
scoreType: ScoreType.delivery, | ||
score: getRandomFloat(0.5, 3.5, 2), | ||
} | ||
let newDeliveryJournalEntry = Cord.Score.fromJournalProperties( | ||
deliveryEntryContent, | ||
deliveryIdentity | ||
) | ||
try { | ||
let txDeliveryEntry = await Cord.Score.entries(newDeliveryJournalEntry) | ||
txBatch.push(txDeliveryEntry) | ||
} catch (e: any) { | ||
console.log(e.errorCode, '-', e.message) | ||
} | ||
} | ||
try { | ||
api.tx.utility.batchAll(txBatch).signAndSend(batchTransactionAuthor) | ||
} catch (e: any) { | ||
console.log(e.errorCode, '-', e.message) | ||
} | ||
process.stdout.write( | ||
' ✨ Creation, Transformation & Submission of ' + | ||
txBatch.length + | ||
' extrinsics to the block took ' + | ||
moment.duration(moment().diff(startBatchPrep)).as('seconds').toFixed(3) + | ||
's\r' | ||
) | ||
console.log('\n\n💤 Waiting for block finalization') | ||
|
||
await sleep(6000) | ||
console.log(`\n❄️ Query Chain Scores `) | ||
const chainDeliveryScore = await Cord.Score.query( | ||
deliveryIdentity.address, | ||
ScoreType.delivery | ||
) | ||
console.dir(chainDeliveryScore, { depth: null, colors: true }) | ||
|
||
const chainDeiveryAvgScore = await Cord.Score.queryAverage( | ||
deliveryIdentity.address, | ||
ScoreType.delivery | ||
) | ||
console.dir(chainDeiveryAvgScore, { depth: null, colors: true }) | ||
|
||
const chainOverallScore = await Cord.Score.query( | ||
sellerIdentity.address, | ||
ScoreType.overall | ||
) | ||
console.dir(chainOverallScore, { depth: null, colors: true }) | ||
|
||
const chainOverallAvgScore = await Cord.Score.queryAverage( | ||
sellerIdentity.address, | ||
ScoreType.overall | ||
) | ||
console.dir(chainOverallAvgScore, { depth: null, colors: true }) | ||
await api.disconnect() | ||
} | ||
|
||
main() | ||
.then(() => console.log('Bye! 👋 👋 👋 \n')) | ||
.finally(Cord.disconnect) | ||
|
||
process.on('SIGINT', async () => { | ||
console.log('Bye! 👋 👋 👋 \n') | ||
Cord.disconnect() | ||
process.exit(0) | ||
}) |
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,101 @@ | ||
import * as Cord from '@cord.network/sdk' | ||
import { ScoreType } from '@cord.network/types' | ||
import { UUID } from '@cord.network/utils' | ||
|
||
async function main() { | ||
await Cord.init({ address: 'ws://127.0.0.1:9944' }) | ||
|
||
// Step 1: Setup Org Identity | ||
console.log(`\n❄️ Demo Identities (KeyRing)`) | ||
//3x4DHc1rxVAEqKWSx1DAAA8wZxLB4VhiRbMV997niBckUwSi | ||
const sellerIdentity = Cord.Identity.buildFromURI('//Entity', { | ||
signingKeyPairType: 'sr25519', | ||
}) | ||
console.log( | ||
`🏛 Seller Entity (${sellerIdentity.signingKeyType}): ${sellerIdentity.address}` | ||
) | ||
const deliveryIdentity = Cord.Identity.buildFromURI('//Delivery', { | ||
signingKeyPairType: 'sr25519', | ||
}) | ||
console.log( | ||
`🏛 Delivery Entity (${deliveryIdentity.signingKeyType}): ${deliveryIdentity.address}` | ||
) | ||
const collectorIdentity = Cord.Identity.buildFromURI('//BuyerApp', { | ||
signingKeyPairType: 'ed25519', | ||
}) | ||
console.log( | ||
`🧑🏻💼 Score Collector (${collectorIdentity.signingKeyType}): ${collectorIdentity.address}` | ||
) | ||
const requestorIdentity = Cord.Identity.buildFromURI('//SellerApp', { | ||
signingKeyPairType: 'ed25519', | ||
}) | ||
console.log( | ||
`👩⚕️ Score Requestor (${requestorIdentity.signingKeyType}): ${requestorIdentity.address}` | ||
) | ||
const transactionAuthor = Cord.Identity.buildFromURI('//Bob', { | ||
signingKeyPairType: 'sr25519', | ||
}) | ||
console.log( | ||
`🏢 Transaction Author (${transactionAuthor.signingKeyType}): ${transactionAuthor.address}` | ||
) | ||
console.log('✅ Identities created!') | ||
|
||
// Step 2: Create a jounal entry | ||
console.log(`\n❄️ Journal Entry `) | ||
let journalContent = { | ||
entity: sellerIdentity.address, | ||
uid: UUID.generate().toString(), | ||
tid: UUID.generate().toString(), | ||
collector: collectorIdentity.address, | ||
requestor: requestorIdentity.address, | ||
scoreType: ScoreType.overall, | ||
score: 3.7, | ||
} | ||
console.dir(journalContent, { depth: null, colors: true }) | ||
|
||
let newJournalEntry = Cord.Score.fromJournalProperties( | ||
journalContent, | ||
sellerIdentity | ||
) | ||
|
||
let journalCreationExtrinsic = await Cord.Score.entries(newJournalEntry) | ||
console.log(`\n❄️ Transformed Journal Entry `) | ||
console.dir(newJournalEntry, { depth: null, colors: true }) | ||
|
||
try { | ||
await Cord.Chain.signAndSubmitTx( | ||
journalCreationExtrinsic, | ||
transactionAuthor, | ||
{ | ||
resolveOn: Cord.Chain.IS_IN_BLOCK, | ||
rejectOn: Cord.Chain.IS_ERROR, | ||
} | ||
) | ||
console.log('✅ Journal Entry added!') | ||
} catch (e: any) { | ||
console.log(e.errorCode, '-', e.message) | ||
} | ||
|
||
console.log(`\n❄️ Query Chain Scores `) | ||
const chainScore = await Cord.Score.query( | ||
journalContent.entity, | ||
journalContent.scoreType | ||
) | ||
console.dir(chainScore, { depth: null, colors: true }) | ||
|
||
const chainAvgScore = await Cord.Score.queryAverage( | ||
journalContent.entity, | ||
journalContent.scoreType | ||
) | ||
console.dir(chainAvgScore, { depth: null, colors: true }) | ||
} | ||
|
||
main() | ||
.then(() => console.log('\nBye! 👋 👋 👋 ')) | ||
.finally(Cord.disconnect) | ||
|
||
process.on('SIGINT', async () => { | ||
console.log('\nBye! 👋 👋 👋 \n') | ||
Cord.disconnect() | ||
process.exit(0) | ||
}) |
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
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
Oops, something went wrong.