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 Aug 9, 2021
1 parent e89d319 commit 8b75b99
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 6 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
3 changes: 3 additions & 0 deletions src/components/EditorToolBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ export default {
},
},
},
beforeDestroy() {
this.clear()
},
methods: {
...mapMutations({
undo: EDITOR_UNDO,
Expand Down
9 changes: 7 additions & 2 deletions src/components/steps/Loading.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
</template>

<script>
import { mapState } from 'vuex'
import { mapState, mapMutations } from 'vuex'
import Card from '@/components/card/Card.vue'
import Spinner from '@/components/Spinner.vue'
// TODO(#1714): Remove logic from this component
export default {
name: 'WalletDisclaimer',
components: {
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 }
20 changes: 18 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
83 changes: 82 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3109,6 +3109,11 @@ bignumber.js@^9.0.0:
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-9.0.0.tgz#805880f84a329b5eac6e7cb6f8274b6d82bdf075"
integrity sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==

bignumber.js@^9.0.1:
version "9.0.1"
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-9.0.1.tgz#8d7ba124c882bfd8e43260c67475518d0689e4e5"
integrity sha512-IdZR9mh6ahOBv/hYGiXyVuyCetmGJhtYkqLBpTStdhEGjegpPlUawydyaF3pbIOFynJTpllEs+NP+CS9jKFLjA==

binary-extensions@^1.0.0:
version "1.13.1"
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-1.13.1.tgz#598afe54755b2868a5330d2aff9d4ebb53209b65"
Expand Down Expand Up @@ -3733,6 +3738,14 @@ cbor@^4.1.5:
json-text-sequence "^0.1"
nofilter "^1.0.3"

cbor@^5.0.1:
version "5.2.0"
resolved "https://registry.yarnpkg.com/cbor/-/cbor-5.2.0.tgz#4cca67783ccd6de7b50ab4ed62636712f287a67c"
integrity sha512-5IMhi9e1QU76ppa5/ajP1BmMWZ2FHkhAhjeVKQ/EFCgYSEaeVaoGtL7cxJskf9oCCk+XjzaIdc3IuU/dbA/o2A==
dependencies:
bignumber.js "^9.0.1"
nofilter "^1.0.4"

ccount@^1.0.0:
version "1.0.5"
resolved "https://registry.yarnpkg.com/ccount/-/ccount-1.0.5.tgz#ac82a944905a65ce204eb03023157edf29425c17"
Expand Down Expand Up @@ -6674,6 +6687,20 @@ functional-red-black-tree@^1.0.1:
resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"
integrity sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=

generate-function@^2.0.0:
version "2.3.1"
resolved "https://registry.yarnpkg.com/generate-function/-/generate-function-2.3.1.tgz#f069617690c10c868e73b8465746764f97c3479f"
integrity sha512-eeB5GfMNeevm/GRYq20ShmsaGcmI81kIX2K9XQx5miC8KdHaC6Jm0qQ8ZNeGOi7wYB8OsdxKs+Y2oVuTFuVwKQ==
dependencies:
is-property "^1.0.2"

generate-object-property@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/generate-object-property/-/generate-object-property-1.2.0.tgz#9c0e1c40308ce804f4783618b937fa88f99d50d0"
integrity sha1-nA4cQDCM6AT0eDYYuTf6iPmdUNA=
dependencies:
is-property "^1.0.0"

generic-names@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/generic-names/-/generic-names-2.0.1.tgz#f8a378ead2ccaa7a34f0317b05554832ae41b872"
Expand Down Expand Up @@ -8063,6 +8090,11 @@ is-promise@^2.0.0:
resolved "https://registry.yarnpkg.com/is-promise/-/is-promise-2.2.2.tgz#39ab959ccbf9a774cf079f7b40c7a26f763135f1"
integrity sha512-+lP4/6lKUBfQjZ2pdxThZvLUAafmZb8OAxFb8XXtiQmS35INgr85hdOGoEs124ez1FCnZJt6jau/T+alh58QFQ==

is-property@^1.0.0, is-property@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/is-property/-/is-property-1.0.2.tgz#57fe1c4e48474edd65b09911f26b1cd4095dda84"
integrity sha1-V/4cTkhHTt1lsJkR8msc1Ald2oQ=

is-regex@^1.0.3, is-regex@^1.0.4, is-regex@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/is-regex/-/is-regex-1.1.1.tgz#c6f98aacc546f6cec5468a07b7b153ab564a57b9"
Expand Down Expand Up @@ -10119,7 +10151,7 @@ node-releases@^1.1.29, node-releases@^1.1.3, node-releases@^1.1.60:
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-1.1.61.tgz#707b0fca9ce4e11783612ba4a2fcba09047af16e"
integrity sha512-DD5vebQLg8jLCOzwupn954fbIiZht05DAZs0k2u8NStSe6h9XdsuIQL8hSRKYiU8WUQRznmSDrKGbv3ObOmC7g==

nofilter@^1.0.3:
nofilter@^1.0.3, nofilter@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/nofilter/-/nofilter-1.0.4.tgz#78d6f4b6a613e7ced8b015cec534625f7667006e"
integrity sha512-N8lidFp+fCz+TD51+haYdbDGrcBWwuHX40F5+z0qkUjMJ5Tp+rdSuAkMJ9N9eoolDlEVTf6u5icM+cNKkKW2mA==
Expand Down Expand Up @@ -11596,6 +11628,31 @@ proto-list@~1.2.1:
resolved "https://registry.yarnpkg.com/proto-list/-/proto-list-1.2.4.tgz#212d5bfe1318306a420f6402b8e26ff39647a849"
integrity sha1-IS1b/hMYMGpCD2QCuOJv85ZHqEk=

protocol-buffers-encodings@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/protocol-buffers-encodings/-/protocol-buffers-encodings-1.1.1.tgz#f1e4a386711823137330171d2c82b49d062e75d3"
integrity sha512-5aFshI9SbhtcMiDiZZu3g2tMlZeS5lhni//AGJ7V34PQLU5JA91Cva7TIs6inZhYikS3OpnUzAUuL6YtS0CyDA==
dependencies:
signed-varint "^2.0.1"
varint "5.0.0"

protocol-buffers-schema@^3.1.1:
version "3.5.1"
resolved "https://registry.yarnpkg.com/protocol-buffers-schema/-/protocol-buffers-schema-3.5.1.tgz#8388e768d383ac8cbea23e1280dfadb79f4122ad"
integrity sha512-YVCvdhxWNDP8/nJDyXLuM+UFsuPk4+1PB7WGPVDzm3HTHbzFLxQYeW2iZpS4mmnXrQJGBzt230t/BbEb7PrQaw==

protocol-buffers@^4.1.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/protocol-buffers/-/protocol-buffers-4.2.0.tgz#e25c64c36bf9b0c77b5546f147344d900c495001"
integrity sha512-hNp56d5uuREVde7UqP+dmBkwzxrhJwYU5nL/mdivyFfkRZdgAgojkyBeU3jKo7ZHrjdSx6Q1CwUmYJI6INt20g==
dependencies:
generate-function "^2.0.0"
generate-object-property "^1.2.0"
protocol-buffers-encodings "^1.1.0"
protocol-buffers-schema "^3.1.1"
signed-varint "^2.0.0"
varint "^5.0.0"

proxy-addr@~2.0.5:
version "2.0.6"
resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.6.tgz#fdc2336505447d3f2f2c638ed272caf614bbb2bf"
Expand Down Expand Up @@ -13125,6 +13182,13 @@ signal-exit@^3.0.0, signal-exit@^3.0.2:
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.3.tgz#a1410c2edd8f077b08b4e253c8eacfcaf057461c"
integrity sha512-VUJ49FC8U1OxwZLxIbTTrDvLnf/6TDgxZcK8wxR8zs13xpx7xbG60ndBlhNrFi2EMuFRoeDoJO7wthSLq42EjA==

signed-varint@^2.0.0, signed-varint@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/signed-varint/-/signed-varint-2.0.1.tgz#50a9989da7c98c2c61dad119bc97470ef8528129"
integrity sha1-UKmYnafJjCxh2tEZvJdHDvhSgSk=
dependencies:
varint "~5.0.0"

simple-swizzle@^0.2.2:
version "0.2.2"
resolved "https://registry.yarnpkg.com/simple-swizzle/-/simple-swizzle-0.2.2.tgz#a4da6b635ffcccca33f70d17cb92592de95e557a"
Expand Down Expand Up @@ -14855,6 +14919,16 @@ validate-npm-package-license@^3.0.1:
spdx-correct "^3.0.0"
spdx-expression-parse "^3.0.0"

[email protected]:
version "5.0.0"
resolved "https://registry.yarnpkg.com/varint/-/varint-5.0.0.tgz#d826b89f7490732fabc0c0ed693ed475dcb29ebf"
integrity sha1-2Ca4n3SQcy+rwMDtaT7Uddyynr8=

varint@^5.0.0, varint@~5.0.0:
version "5.0.2"
resolved "https://registry.yarnpkg.com/varint/-/varint-5.0.2.tgz#5b47f8a947eb668b848e034dcfa87d0ff8a7f7a4"
integrity sha512-lKxKYG6H03yCZUpAGOPOsMcGxd1RHCu1iKvEHYDPmTyq2HueGhD73ssNBqqQWfvYs04G9iUFRvmAVLW20Jw6ow==

vary@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
Expand Down Expand Up @@ -15494,6 +15568,13 @@ [email protected]:
prettier "^2.0.5"
rosetta "^1.1.0"

"witnet-requests@git+https://github.com/witnet/witnet-requests-js":
version "0.3.0"
resolved "git+https://github.com/witnet/witnet-requests-js#4ed30f2e3c8551b1a8a61265df7ed03e08e38e6f"
dependencies:
cbor "^5.0.1"
protocol-buffers "^4.1.0"

word-wrap@~1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c"
Expand Down

0 comments on commit 8b75b99

Please sign in to comment.