Skip to content

Commit

Permalink
feat: add data request examples
Browse files Browse the repository at this point in the history
  • Loading branch information
gabaldon committed Jul 1, 2021
1 parent e48b227 commit e9ab63d
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"vue-observe-visibility": "^0.4.6",
"vue-router": "^3.0.3",
"vuex": "^3.0.1",
"witnet-radon-js": "0.8.4"
"witnet-radon-js": "0.8.4",
"witnet-requests": "git+https://github.com/witnet/witnet-requests-js"
},
"devDependencies": {
"@babel/compat-data": "7.9.0",
Expand Down
9 changes: 6 additions & 3 deletions src/components/EditorToolBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,17 @@ export default {
: 'data_request.js'
},
autoTryDataRequest: {
set() {
this.toggleTryDataRequest()
},
get() {
return this.autoTry
},
set() {
this.toggleTryDataRequest()
},
},
},
beforeDestroy() {
this.clear()
},
methods: {
...mapMutations({
undo: EDITOR_UNDO,
Expand Down
7 changes: 6 additions & 1 deletion src/components/steps/Loading.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</template>

<script>
import { mapState } from 'vuex'
import { mapState, mapMutations } from 'vuex'
import Card from '@/components/card/Card.vue'
import Spinner from '@/components/Spinner.vue'
Expand All @@ -21,6 +21,7 @@ export default {
},
computed: {
...mapState({
locale: state => state.wallet.locale,
sessionId: state => state.wallet.sessionId,
error: state =>
state.wallet.errors.unlockWallet || state.wallet.errors.createWallet,
Expand All @@ -30,6 +31,7 @@ export default {
sessionId(value) {
if (value) {
this.$router.push(`/wallet/transactions?session_id=${this.sessionId}`)
this.setDefaultTemplates({ locale: this.locale })
}
},
error(value) {
Expand All @@ -39,6 +41,9 @@ export default {
},
},
methods: {
...mapMutations({
setDefaultTemplates: 'setDefaultTemplates',
}),
goToFirstStep() {
this.$router.push('/ftu/welcome')
},
Expand Down
15 changes: 15 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { es as fnsEs, enGB } from 'date-fns/locale'
import en from 'element-ui/lib/locale/lang/en'
import es from 'element-ui/lib/locale/lang/es'
import bitcoinPrice from '@/radExamples/bitcoinPrice.js'
import ethPrice from '@/radExamples/ethPrice.js'

export const EDITOR_ALLOWED_PROTOCOLS = ['http', 'https']

Expand Down Expand Up @@ -163,3 +165,16 @@ export const BIRTH_DATE_DELAY_DAYS = 30
export const GENESIS_TIMESTAMP = 1602666000000

export const EPOCH_PERIOD = 45 * 1000

export const RAD_EXAMPLES = [
{
name: 'Bitcoin price',
description: 'Bitcoin price in USD',
radRequest: bitcoinPrice,
},
{
name: 'Eth price',
description: 'Eth price in USD',
radRequest: ethPrice,
},
]
49 changes: 49 additions & 0 deletions src/radExamples/bitcoinPrice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as Witnet from 'witnet-requests'

// Retrieves USD price of a bitcoin from the BitStamp API
const bitstamp = new Witnet.Source('https://www.bitstamp.net/api/ticker/')
.parseJSONMap() // Parse a `Map` from the retrieved `String`
.getFloat('last') // Get the `Float` value associated to the `last` key
.multiply(1000)
.round()

// Retrieves USD price of a bitcoin from CoinDesk's "bitcoin price index" API
// The JSON here is a bit more complex, thus more operators are needed
const coindesk = new Witnet.Source(
'https://api.coindesk.com/v1/bpi/currentprice.json',
)
.parseJSONMap() // Parse a `Map` from the retrieved `String`
.getMap('bpi') // Get the `Map` value associated to the `bpi` key
.getMap('USD') // Get the `Map` value associated to the `USD` key
.getFloat('rate_float') // Get the `Float` value associated to the `rate_float` key
.multiply(1000)
.round()

// Filters out any value that is more than 1.5 times the standard
// deviationaway from the average, then computes the average mean of the
// values that pass the filter.
const aggregator = new Witnet.Aggregator({
filters: [[Witnet.Types.FILTERS.deviationStandard, 1.5]],
reducer: Witnet.Types.REDUCERS.averageMean,
})

// Filters out any value that is more than 1.5 times the standard
// deviationaway from the average, then computes the average mean of the
// values that pass the filter.
const tally = new Witnet.Tally({
filters: [[Witnet.Types.FILTERS.deviationStandard, 1.5]],
reducer: Witnet.Types.REDUCERS.averageMean,
})

// This is the Witnet.Request object that needs to be exported
const request = new Witnet.Request()
.addSource(bitstamp) // Use source 1
.addSource(coindesk) // Use source 2
.setAggregator(aggregator) // Set the aggregator function
.setTally(tally) // Set the tally function
.setQuorum(100, 70) // Set witness count
.setFees(10, 1) // Set economic incentives
.schedule(0) // Make this request immediately solvable

// Do not forget to export the request object
export { request as default }
60 changes: 60 additions & 0 deletions src/radExamples/ethPrice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as Witnet from 'witnet-requests'

// Retrieves USD price of eth from the BitStamp API
const bitstamp = new Witnet.Source(
'https://www.bitstamp.net/api/v2/ticker/ethusd/',
)
.parseJSONMap() // Parse a `Map` from the retrieved `String`
.getFloat('last') // Get the `Float` value associated to the `last` key
.multiply(1000)
.round()

// Retrieves USD price of eth from the coincap API
const coincap = new Witnet.Source('https://api.coincap.io/v2/assets')
.parseJSONMap()
.getArray('data')
.getMap(1)
.getFloat('priceUsd')
.multiply(1000)
.round()

// Retrieves USD price of eth from the coinpaprika API
const coinpaprika = new Witnet.Source(
'https://api.coinpaprika.com/v1/tickers/eth-ethereum',
)
.parseJSONMap()
.getMap('quotes')
.getMap('USD')
.getFloat('price')
.multiply(1000)
.round()

// Filters out any value that is more than 1.5 times the standard
// deviationaway from the average, then computes the average mean of the
// values that pass the filter.
const aggregator = new Witnet.Aggregator({
filters: [[Witnet.Types.FILTERS.deviationStandard, 1.5]],
reducer: Witnet.Types.REDUCERS.averageMean,
})

// Filters out any value that is more than 1.0 times the standard
// deviationaway from the average, then computes the average mean of the
// values that pass the filter.
const tally = new Witnet.Tally({
filters: [[Witnet.Types.FILTERS.deviationStandard, 1.5]],
reducer: Witnet.Types.REDUCERS.averageMean,
})

// This is the Witnet.Request object that needs to be exported
const request = new Witnet.Request()
.addSource(bitstamp) // Use source 1
.addSource(coincap) // Use source 2
.addSource(coinpaprika) // Use source 3
.setAggregator(aggregator) // Set the aggregator function
.setTally(tally) // Set the tally function
.setQuorum(100, 70) // Set witness count
.setFees(10, 1) // Set economic incentives
.schedule(0) // Make this request immediately solvable

// Do not forget to export the request object
export { request as default }
24 changes: 22 additions & 2 deletions src/store/rad.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@/utils'
import i18n from '@/plugins/i18n'
import { Radon } from 'witnet-radon-js'
import { EDITOR_STAGES, HISTORY_UPDATE_TYPE } from '@/constants'
import { EDITOR_STAGES, HISTORY_UPDATE_TYPE, RAD_EXAMPLES } from '@/constants'
import {
UPDATE_HISTORY,
UPDATE_TEMPLATE,
Expand Down Expand Up @@ -63,6 +63,23 @@ export default {
},
},
mutations: {
setDefaultTemplates: function(state, { locale }) {
RAD_EXAMPLES.forEach(example => {
const radRequest = {
retrieve: example.radRequest.data.data_request.retrieve,
aggregate: example.radRequest.data.data_request.aggregate,
tally: example.radRequest.data.data_request.tally,
timelock: example.radRequest.data.data_request.time_lock,
}
this.dispatch('saveTemplate', {
template: {
name: example.name,
description: example.description,
radRequest: new Radon(radRequest, locale).getMir(),
},
})
})
},
setSubscriptId: function(state, { id }) {
state.subscriptIds.push(id)
},
Expand Down Expand Up @@ -434,7 +451,6 @@ export default {
const isImportingTemplate = args ? !!args.template : null
const date = Date.now()
const templates = context.state.templates

if (isImportingTemplate && !isValidRadRequest(args.template.radRequest)) {
// data request is invalid
createNotification({
Expand Down Expand Up @@ -489,6 +505,10 @@ export default {
session_id: context.rootState.wallet.sessionId,
key: 'templates',
})
console.log(
`getTemplates--${context.rootState.wallet.walletId}-${context.rootState.wallet.sessionId}--`,
request,
)
if (request.result) {
context.commit(SET_TEMPLATES, { templates: request.result.value })
} else {
Expand Down
Loading

0 comments on commit e9ab63d

Please sign in to comment.