From 7fea5d32630203b34b3f6023b1b89ec938963525 Mon Sep 17 00:00:00 2001 From: boneyard93501 <4523011+boneyard93501@users.noreply.github.com> Date: Thu, 25 Feb 2021 13:33:01 -0600 Subject: [PATCH] add ETH price getter --- ether-price-getter/.DS_Store | Bin 0 -> 10244 bytes ether-price-getter/.repl_history | 19 ++ ether-price-getter/Config.toml | 12 ++ ether-price-getter/Readme.md | 127 ++++++++++++++ ether-price-getter/Readme.md.bk | 166 ++++++++++++++++++ .../air-scripts/get_eth_price.clj | 10 ++ .../air-scripts/get_eth_price.clj.bk | 10 ++ ether-price-getter/build.sh | 15 ++ ether-price-getter/curl_adapter/.DS_Store | Bin 0 -> 6148 bytes ether-price-getter/curl_adapter/.gitignore | 6 + ether-price-getter/curl_adapter/Cargo.toml | 14 ++ ether-price-getter/curl_adapter/src/main.rs | 55 ++++++ ether-price-getter/curl_cfg.json | 6 + .../ether_price_getter/.DS_Store | Bin 0 -> 6148 bytes .../ether_price_getter/.gitignore | 6 + .../ether_price_getter/Cargo.toml | 15 ++ .../src/eth_price_getter.rs | 29 +++ .../ether_price_getter/src/fce_results.rs | 82 +++++++++ .../ether_price_getter/src/main.rs | 36 ++++ .../ether_price_getter_cfg.json | 3 + 20 files changed, 611 insertions(+) create mode 100644 ether-price-getter/.DS_Store create mode 100644 ether-price-getter/.repl_history create mode 100644 ether-price-getter/Config.toml create mode 100644 ether-price-getter/Readme.md create mode 100644 ether-price-getter/Readme.md.bk create mode 100644 ether-price-getter/air-scripts/get_eth_price.clj create mode 100644 ether-price-getter/air-scripts/get_eth_price.clj.bk create mode 100755 ether-price-getter/build.sh create mode 100644 ether-price-getter/curl_adapter/.DS_Store create mode 100644 ether-price-getter/curl_adapter/.gitignore create mode 100644 ether-price-getter/curl_adapter/Cargo.toml create mode 100644 ether-price-getter/curl_adapter/src/main.rs create mode 100644 ether-price-getter/curl_cfg.json create mode 100644 ether-price-getter/ether_price_getter/.DS_Store create mode 100644 ether-price-getter/ether_price_getter/.gitignore create mode 100644 ether-price-getter/ether_price_getter/Cargo.toml create mode 100644 ether-price-getter/ether_price_getter/src/eth_price_getter.rs create mode 100644 ether-price-getter/ether_price_getter/src/fce_results.rs create mode 100644 ether-price-getter/ether_price_getter/src/main.rs create mode 100644 ether-price-getter/ether_price_getter_cfg.json diff --git a/ether-price-getter/.DS_Store b/ether-price-getter/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..240c6d2ffc9ffd9fbb42dc2ca974783f225f58d9 GIT binary patch literal 10244 zcmeHMU2GIp6uxI$V8#yYw190F=q}a4aPwap^dxOcm$k*#o$$;HDVB)p0yT#gRr_I?gFqC&1MSxGRI3p#WPQ z^+HUXK!j7T^(^37V73Kl?p{U|%-J(!?X>*eHU`77q?Jl@CEv-yk|`@1O&C@JC4}2I zwwk-%xPNeP!)j8@G+`O5QfN^0pBlDU%jT zJ34mklse;bMGlYMm3DN?wD)Qm8);nj+i0qeq-AzzG&entlC_cGRI`vxfTj+g8SDW0S>YIcsZsG@MJ%X;GZf?$>ci zswjhaeo^J}m33>@t>3u0b;2)~S1gv6O8e33rlY^0YngqDozzTKHwSx%b=4W_%jveB zFti?1Nois^d?-%%%jJry>Oi1Bo6wJBb?2B|D+r9cq9zbHsN#~lEtSru%9q@c$7%x7 z0Xou_(XdqoaiT&l_e+7A)uOPE4z#GMrn=1fwW1&^JvMHcbEI&iD9U!vVZ;mL1f-^B zL6p^ALrG|c%X)SbvUc_5xVc?sq(#7O*Y~3rrtpw9X9wE134(n65M8-bv7N58W@@Tf zo5xITeCl|?Oa)n$YH^@!$1Gz(fCU?oN z$>>_J);tVgW- znBcl#gCv(A@5xt8MxhoO5VQ5Cwwm>Vi!#3!G=U@-?xad_7y@$bo6j*Q+Mqm`i z;5fVrufa(~@F_&_X+-c@I0x^*d+Fe+9STyBQenoPpt=?uX&2 zVYFeSeM2=5qXjXavhD@aV&tcX(r&u7f^+9$8Pl-LL6o8XSb^cyAwx9CV$e-vt78z> zus_H~9_H{{)Y+$p;wLT?YcsO|+l@=;D&M8yDhF!-aW!LfeK(FIhW2LhBiwD||xV&*rk%Q$w Z|1)4tUT=B%znA|{UW^x$&;O(R|1Tmu?y&#> literal 0 HcmV?d00001 diff --git a/ether-price-getter/.repl_history b/ether-price-getter/.repl_history new file mode 100644 index 0000000..8d023a7 --- /dev/null +++ b/ether-price-getter/.repl_history @@ -0,0 +1,19 @@ +#V2 +help +l eth_getter artifacts/ether_price_getter.wasm +l curl_adapter artifacts/curl_adapter.wasm +interface +call ether_price_getter ["042baa7d-2e2a-4664-b767-5bdc7988c572", "GBP"] +call ether_price_getter ether_price_getter ["042baa7d-2e2a-4664-b767-5bdc7988c572", "GBP"] +call ether_price_getter ether_price_getter ["42fc4b0e-59cf-4182-8f22-d4095cf0a738", "GBP"] +call ether_price_getter ether_price_getter ["42fc4b0e-59cf-4182-8f22-d4095cf0a738", "USD"] +call ether_price_getter ether_price_getter ["42fc4b0e-59cf-4182-8f22-d4095cf0a738", "GBP"] +call ether_price_getter ether_price_getter ["42fc4b0e-59cf-4182-8f22-d4095cf0a738", "USD"] +call ether_price_getter ether_price_getter ["42fc4b0e-59cf-4182-8f22-d4095cf0a738", "GBP"] +call ether_price_getter ether_price_getter ["42fc4b0e-59cf-4182-8f22-d4095cf0a738", "EUR"] +interface +call ether_price_getter ether_price_getter ["42fc4b0e-59cf-4182-8f22-d4095cf0a738", "EUR"] +inte +interface +call ether_price_getter ether_price_getter ["42fc4b0e-59cf-4182-8f22-d4095cf0a738", "GBP"] +interface diff --git a/ether-price-getter/Config.toml b/ether-price-getter/Config.toml new file mode 100644 index 0000000..c23b324 --- /dev/null +++ b/ether-price-getter/Config.toml @@ -0,0 +1,12 @@ +modules_dir = "artifacts/" + +[[module]] + name = "curl_adapter" + # logger_enabled = false + + [module.mounted_binaries] + curl = "/usr/bin/curl" + +[[module]] + name = "ether_price_getter" + # logger_enabled = true diff --git a/ether-price-getter/Readme.md b/ether-price-getter/Readme.md new file mode 100644 index 0000000..47c1aac --- /dev/null +++ b/ether-price-getter/Readme.md @@ -0,0 +1,127 @@ +# Ether Price Quotation Service + +## Overview +This module utilizes curl and the [Coinmarketcap API](https://coinmarketcap.com/api/) to allow the query of the latest Ether (ETH) price relative to some 120 currencies. The service requires a valid Coinmarketcap API key, which is freely available with [registration](https://pro.coinmarketcap.com/signup/). The query function wraps this API call: https://coinmarketcap.com/api/documentation/v1/#operation/getV2CryptocurrencyQuotesLatest: + +```bash +ether_price_getter(api_key: String, currency_symbol: String) +``` + +which expects the [Coinmarketcap API key] and the three letter currency symbol, e.g.: + +``` +call ether_price_getter ether_price_getter ["32fd4c0f-59cf-4182-8f22-d4083df0a738", "EUR"] +``` + +Please note that you need to supply a valid Coinmarketcap API key and that the free account limits the +number of conversions to one. The result from the Fluence service call is the raw json string, e.g.: + +``` +result: String("{\"status\":{\"timestamp\":\"2021-02-22T20:47:59.960Z\",\"error_code\":0,\"error_message\":null,\"elapsed\":225,\"credit_count\":1,\"notice\":null},\"data\":{\"ETH\":{\"id\":1027,\"name\":\"Ethereum\",\"symbol\":\"ETH\",\"slug\":\"ethereum\",\"num_market_pairs\":5986,\"date_added\":\"2015-08-07T00:00:00.000Z\",\"tags\":[\"mineable\",\"pow\",\"smart-contracts\",\"coinbase-ventures-portfolio\",\"three-arrows-capital-portfolio\",\"polychain-capital-portfolio\"],\"max_supply\":null,\"circulating_supply\":114779907.124,\"total_supply\":114779907.124,\"is_active\":1,\"platform\":null,\"cmc_rank\":2,\"is_fiat\":0,\"last_updated\":\"2021-02-22T20:46:06.000Z\",\"quote\":{\"EUR\":{\"price\":1464.4187545109621,\"volume_24h\":34069036322.326084,\"percent_change_1h\":3.41360427,\"percent_change_24h\":-10.32669439,\"percent_change_7d\":-3.70439114,\"percent_change_30d\":41.39754247,\"market_cap\":168085848633.412,\"last_updated\":\"2021-02-22T20:47:05.000Z\"}}}}}") +``` + +## Where to Find The Service +The service is discoverable at [Fluence](https://dash.fluence.dev/), e.g., https://dash.fluence.dev/blueprint/a7959f53-a70b-4183-83e0-649f4f91160c, and ready for use. + +## How to Build The Service +machine setup instructions + +In the `eth-price-getter` dir, +```bash +# if needed +chmod +x .build.sh + +./build.sh +``` + +That compiles both the curl and the price getter code and places the resulting wasm code into the `artifacts` dir. For local testing run `fce-repl Config.toml` to initiate the repl: + +```bash +fce-repl Config.toml +Welcome to the FCE REPL (version 0.1.33) +app service was created with service id = b0cc4d0d-fb3e-49e1-8532-54d77e03cdaa +elapsed time 82.757883ms + +1> interface +Loaded modules interface: + +curl_adapter: + fn curl_request(url: String) -> String + +ether_price_getter: + fn ether_price_getter(api_key: String, currency_symbol: String) -> String + +2> call ether_price_getter ether_price_getter ["35fd4b0e-56cf-4182-8f22-d4095cf0a738", "EUR"] +INFO: Running "/usr/bin/curl -H X-CMC_PRO_API_KEY: 35fd4b0e-56cf-4182-8f22-d4095cf0a738 -H Accept: application/json -d symbol=ETH&convert=EUR -G https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest" ... + % Total % Received % Xferd Average Speed Time Time Time Current + Dload Upload Total Spent Left Speed +100 868 0 868 0 0 2755 0 --:--:-- --:--:-- --:--:-- 2755 +result: String("{\"status\":{\"timestamp\":\"2021-02-22T21:06:42.598Z\",\"error_code\":0,\"error_message\":null,\"elapsed\":18,\"credit_count\":1,\"notice\":null},\"data\":{\"ETH\":{\"id\":1027,\"name\":\"Ethereum\",\"symbol\":\"ETH\",\"slug\":\"ethereum\",\"num_market_pairs\":5986,\"date_added\":\"2015-08-07T00:00:00.000Z\",\"tags\":[\"mineable\",\"pow\",\"smart-contracts\",\"coinbase-ventures-portfolio\",\"three-arrows-capital-portfolio\",\"polychain-capital-portfolio\"],\"max_supply\":null,\"circulating_supply\":114779907.124,\"total_supply\":114779907.124,\"is_active\":1,\"platform\":null,\"cmc_rank\":2,\"is_fiat\":0,\"last_updated\":\"2021-02-22T21:05:03.000Z\",\"quote\":{\"EUR\":{\"price\":1459.5551152082585,\"volume_24h\":33903071552.817226,\"percent_change_1h\":2.64386327,\"percent_change_24h\":-10.46990835,\"percent_change_7d\":-4.37412016,\"percent_change_30d\":41.26794407,\"market_cap\":167527600565.963,\"last_updated\":\"2021-02-22T21:06:05.000Z\"}}}}}") + elapsed time: 323.584665ms + +3> +``` + +Calling `interface` lists both the curl_adapter and ether_price_getter interfaces. Calling the `ether_price_getter` service function with the API key and conversion currency symbol, `"35fd4b0e-56cf-4182-8f22-d4095cf0a738", "EUR"`, yields the raw json string result expected. + +## How to Deploy The Service +ToDo: fldist installation instructions + + +If you don't have a seed ready to use: + +```bash +fldist create_keypair +{ + id: '12D3KooWQy61BZ4P1DeJzhvsQ76uQAQc7N8tDk6NDz5BFnnhwuPP', + privKey: 'CAESYI9nA79xXH9yYeuU4UDPiaa7C84U0OKnihdSkSdbMOIV4RtA5l9dooR41cO8lCz3okYpEboK6maL7yk1ABgCvrLhG0DmX12ihHjVw7yULPeiRikRugrqZovvKTUAGAK+sg==', + pubKey: 'CAESIOEbQOZfXaKEeNXDvJQs96JGKRG6Cupmi+8pNQAYAr6y', + seed: 'AenQdfKCRrh1sajNFkKKV1iojgVnLWyatdaqwzEMopje' +} +``` + +which yields a fresh keypair and derived seed and allows us to create a new service. The `fldist new_service` is a convenience method that takes care the module uploads to the node, the creation of the blueprint, and finally, the instantiation of the services. + +```bash +fldist new_service --env testnet -n "Ether Price Getter" -s AenQdfKCRrh1sajNFkKKV1iojgVnLWyatdaqwzEMopje --ms artifacts/curl_adapter.wasm:curl_cfg.json artifacts/ether_price_getter.wasm:ether_price_getter_cfg.json +``` + +specifies that we are creating a new service comprised of two modules, the curl_adapter module and the ether_price_getter module, and generates the confirmation with service id: + +```bash +client seed: AenQdfKCRrh1sajNFkKKV1iojgVnLWyatdaqwzEMopje +client peerId: 12D3KooWQy61BZ4P1DeJzhvsQ76uQAQc7N8tDk6NDz5BFnnhwuPP +node peerId: 12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb +uploading blueprint Ether Price Getter to node 12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb via client 12D3KooWQy61BZ4P1DeJzhvsQ76uQAQc7N8tDk6NDz5BFnnhwuPP +creating service a7959f53-a70b-4183-83e0-649f4f91160c +service id: 9840c6be-11c8-47e2-98e0-88d758b5a456 +service created successfully +``` + +We can now discover the service on the [Fluenced dashboard](https://dash.fluence.dev/blueprint/a7959f53-a70b-4183-83e0-649f4f91160c) and put it to use. Whether we want to write a frontend application or use the `fldist` tool to execute the remote service, we need an AIR script. Using the prepared [AIR script](../air-scripts/get_eth_price.js) for which you need to supply your own API key: + +```bash +fldist run_air -p get_eth_price.clj -d '{"service":"9840c6be-11c8-47e2-98e0-88d758b5a456"}' + +client seed: 6w6j5yiPU7pMj86XSoLwesoSxhM2fzNNKF6DDtqm3WCH +client peerId: 12D3KooWAwwuy9mz1w14JhznVo1cA4UKtvVv1Gy4EKRywVRwKQ1w +node peerId: 12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb +Particle id: 9eb6c275-8f1a-4bae-b457-747546b3daaf. Waiting for results... Press Ctrl+C to stop the script. +=================== +[ + "{\"status\":{\"timestamp\":\"2021-02-24T07:11:26.493Z\",\"error_code\":0,\"error_message\":null,\"elapsed\":14,\"credit_count\":1,\"notice\":null},\"data\":{\"ETH\":{\"id\":1027,\"name\":\"Ethereum\",\"symbol\":\"ETH\",\"slug\":\"ethereum\",\"num_market_pairs\":6018,\"date_added\":\"2015-08-07T00:00:00.000Z\",\"tags\":[\"mineable\",\"pow\",\"smart-contracts\",\"coinbase-ventures-portfolio\",\"three-arrows-capital-portfolio\",\"polychain-capital-portfolio\"],\"max_supply\":null,\"circulating_supply\":114799158.0615,\"total_supply\":114799158.0615,\"is_active\":1,\"platform\":null,\"cmc_rank\":2,\"is_fiat\":0,\"last_updated\":\"2021-02-24T07:10:03.000Z\",\"quote\":{\"EUR\":{\"price\":1338.6331723430615,\"volume_24h\":40823704838.16298,\"percent_change_1h\":-1.44088165,\"percent_change_24h\":-0.24837544,\"percent_change_7d\":-8.52364557,\"percent_change_30d\":13.62330192,\"market_cap\":153673961138.17825,\"last_updated\":\"2021-02-24T07:11:06.000Z\"}}}}}" +] +[ + [ + { + peer_pk: '12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb', + service_id: '9840c6be-11c8-47e2-98e0-88d758b5a456', + function_name: 'ether_price_getter', + json_path: '' + } + ] +] +=================== +``` + +yields the expected raw pricing response. Please note that with the free account, only one currency conversion target can be supplied per query. diff --git a/ether-price-getter/Readme.md.bk b/ether-price-getter/Readme.md.bk new file mode 100644 index 0000000..19ccff8 --- /dev/null +++ b/ether-price-getter/Readme.md.bk @@ -0,0 +1,166 @@ +# Ether Price Quotation Service + +## Overview +This module utilizes curl and the Coinbase API to allow the query of the latest Ether (ETH) price relative to some 120 currencies. The service requires a valid Coinmarketcap API key, which is freely available with [registration](). The query function wraps this API call: https://coinmarketcap.com/api/documentation/v1/#operation/getV2CryptocurrencyQuotesLatest: + +```bash +ether_price_getter(api_key: String, currency_symbol: String) +``` + +which expects the Coinmarketcap API key and the three letter currency symbol, e.g.: + +``` +call ether_price_getter ether_price_getter ["32fd4c0f-59cf-4182-8f22-d4083df0a738", "EUR"] +``` + +Please note that you need to supply a valid Coinmarketcap API key and that the free account limits the +number of conversions to one. The result from the Fluence service call is the raw json string, e.g.: + +``` +result: String("{\"status\":{\"timestamp\":\"2021-02-22T20:47:59.960Z\",\"error_code\":0,\"error_message\":null,\"elapsed\":225,\"credit_count\":1,\"notice\":null},\"data\":{\"ETH\":{\"id\":1027,\"name\":\"Ethereum\",\"symbol\":\"ETH\",\"slug\":\"ethereum\",\"num_market_pairs\":5986,\"date_added\":\"2015-08-07T00:00:00.000Z\",\"tags\":[\"mineable\",\"pow\",\"smart-contracts\",\"coinbase-ventures-portfolio\",\"three-arrows-capital-portfolio\",\"polychain-capital-portfolio\"],\"max_supply\":null,\"circulating_supply\":114779907.124,\"total_supply\":114779907.124,\"is_active\":1,\"platform\":null,\"cmc_rank\":2,\"is_fiat\":0,\"last_updated\":\"2021-02-22T20:46:06.000Z\",\"quote\":{\"EUR\":{\"price\":1464.4187545109621,\"volume_24h\":34069036322.326084,\"percent_change_1h\":3.41360427,\"percent_change_24h\":-10.32669439,\"percent_change_7d\":-3.70439114,\"percent_change_30d\":41.39754247,\"market_cap\":168085848633.412,\"last_updated\":\"2021-02-22T20:47:05.000Z\"}}}}}") +``` + +## Where to Find The Service +The service is discoverable at [Fluence](https://dash.fluence.dev/) and ready for use. + +## How to Build The Service +machine setup instructions + +In the `eth-price-getter` dir, +```bash +# if needed +chmod +x .build.sh + +./build.sh +``` + +That compiles both the curl and the price getter code and places the resulting wasm code into the `artifacts` dir. For local testing run `fce-repl Config.toml` to initiate the repl: + +```bash +mbp16~/localdev/ethdenver-hackathon/ether-price-getter(eth-price-getter|…) % fce-repl Config.toml +Welcome to the FCE REPL (version 0.1.33) +app service was created with service id = b0cc4d0d-fb3e-49e1-8532-54d77e03cdaa +elapsed time 82.757883ms + +1> interface +Loaded modules interface: + +curl_adapter: + fn curl_request(url: String) -> String + +ether_price_getter: + fn ether_price_getter(api_key: String, currency_symbol: String) -> String + +2> call ether_price_getter ether_price_getter ["35fd4b0e-56cf-4182-8f22-d4095cf0a738", "EUR"] +INFO: Running "/usr/bin/curl -H X-CMC_PRO_API_KEY: 35fd4b0e-56cf-4182-8f22-d4095cf0a738 -H Accept: application/json -d symbol=ETH&convert=EUR -G https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest" ... + % Total % Received % Xferd Average Speed Time Time Time Current + Dload Upload Total Spent Left Speed +100 868 0 868 0 0 2755 0 --:--:-- --:--:-- --:--:-- 2755 +result: String("{\"status\":{\"timestamp\":\"2021-02-22T21:06:42.598Z\",\"error_code\":0,\"error_message\":null,\"elapsed\":18,\"credit_count\":1,\"notice\":null},\"data\":{\"ETH\":{\"id\":1027,\"name\":\"Ethereum\",\"symbol\":\"ETH\",\"slug\":\"ethereum\",\"num_market_pairs\":5986,\"date_added\":\"2015-08-07T00:00:00.000Z\",\"tags\":[\"mineable\",\"pow\",\"smart-contracts\",\"coinbase-ventures-portfolio\",\"three-arrows-capital-portfolio\",\"polychain-capital-portfolio\"],\"max_supply\":null,\"circulating_supply\":114779907.124,\"total_supply\":114779907.124,\"is_active\":1,\"platform\":null,\"cmc_rank\":2,\"is_fiat\":0,\"last_updated\":\"2021-02-22T21:05:03.000Z\",\"quote\":{\"EUR\":{\"price\":1459.5551152082585,\"volume_24h\":33903071552.817226,\"percent_change_1h\":2.64386327,\"percent_change_24h\":-10.46990835,\"percent_change_7d\":-4.37412016,\"percent_change_30d\":41.26794407,\"market_cap\":167527600565.963,\"last_updated\":\"2021-02-22T21:06:05.000Z\"}}}}}") + elapsed time: 323.584665ms + +3> +``` + +Calling `interface` lists both the curl_adapter and ether_price_getter interfaces. Calling the `ether_price_getter` service function with the API key and conversion currency symbol, `"35fd4b0e-56cf-4182-8f22-d4095cf0a738", "EUR"`, yields the raw json string result expected. + +## How to Deploy The Service +ToDo: fldist installatin instructions + + +If you don't have a seed ready to use: + +```bash +mbp16~/localdev/ethdenver-hackathon/ether-price-getter(eth-price-getter|…) % fldist create_keypair +{ + id: '12D3KooWQy61BZ4P1DeJzhvsQ76uQAQc7N8tDk6NDz5BFnnhwuPP', + privKey: 'CAESYI9nA79xXH9yYeuU4UDPiaa7C84U0OKnihdSkSdbMOIV4RtA5l9dooR41cO8lCz3okYpEboK6maL7yk1ABgCvrLhG0DmX12ihHjVw7yULPeiRikRugrqZovvKTUAGAK+sg==', + pubKey: 'CAESIOEbQOZfXaKEeNXDvJQs96JGKRG6Cupmi+8pNQAYAr6y', + seed: 'AenQdfKCRrh1sajNFkKKV1iojgVnLWyatdaqwzEMopje' +} +``` + +which yields the keypair and seed and allows us to create a new service: + +```bash +fldist new_service --env testnet -n "Ether Price Getter" -s AenQdfKCRrh1sajNFkKKV1iojgVnLWyatdaqwzEMopje --ms artifacts/curl_adapter.wasm:curl_cfg.json artifacts/ether_price_getter.wasm:ether_price_getter_cfg.json +client seed: AenQdfKCRrh1sajNFkKKV1iojgVnLWyatdaqwzEMopje +client peerId: 12D3KooWQy61BZ4P1DeJzhvsQ76uQAQc7N8tDk6NDz5BFnnhwuPP +node peerId: 12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb +uploading blueprint Ether Price Getter to node 12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb via client 12D3KooWQy61BZ4P1DeJzhvsQ76uQAQc7N8tDk6NDz5BFnnhwuPP +creating service c77f4b0f-b9e2-47ca-8358-7add50bab280 + +``` + +``` +mbp16~/localdev/ethdenver-hackathon/ether-price-getter(eth-price-getter|✚1…) % fldist new_service --env testnet -n "Ether Price Getter" -s AenQdfKCRrh1sajNFkKKV1iojgVnLWyatdaqwzEMopje --ms artifacts/curl_adapter.wasm:curl_cfg.json artifacts/ether_price_getter.wasm:ether_price_getter_cfg.json --ttl 120000 +client seed: AenQdfKCRrh1sajNFkKKV1iojgVnLWyatdaqwzEMopje +client peerId: 12D3KooWQy61BZ4P1DeJzhvsQ76uQAQc7N8tDk6NDz5BFnnhwuPP +node peerId: 12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb +uploading blueprint Ether Price Getter to node 12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb via client 12D3KooWQy61BZ4P1DeJzhvsQ76uQAQc7N8tDk6NDz5BFnnhwuPP +creating service a7959f53-a70b-4183-83e0-649f4f91160c +service id: 9840c6be-11c8-47e2-98e0-88d758b5a456 +service created successfully +``` + + + + +## Timeout Error: +``` +fldist new_service --env testnet -n "Ether Price Getter" -s AenQdfKCRrh1sajNFkKKV1iojgVnLWyatdaqwzEMopje --ms artifacts/curl_adapter.wasm:curl_cfg.json artifacts/ether_price_getter.wasm:ether_price_getter_cfg.json +client seed: AenQdfKCRrh1sajNFkKKV1iojgVnLWyatdaqwzEMopje +client peerId: 12D3KooWQy61BZ4P1DeJzhvsQ76uQAQc7N8tDk6NDz5BFnnhwuPP +node peerId: 12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb +uploading blueprint Ether Price Getter to node 12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb via client 12D3KooWQy61BZ4P1DeJzhvsQ76uQAQc7N8tDk6NDz5BFnnhwuPP +creating service c77f4b0f-b9e2-47ca-8358-7add50bab280 +Particle expired. Now: 1614029919574, ttl: 60000, ts: 1614029859570 +Particle expired. Now: 1614029922669, ttl: 60000, ts: 1614029862668 +Particle expired. Now: 1614029925656, ttl: 60000, ts: 1614029865653 +Particle expired. Now: 1614029926178, ttl: 60000, ts: 1614029866176 +Something went wrong! +Error: callback for _callback/createService timed out after 60000 + at Timeout._onTimeout (/Users/bebo/.nvm/versions/node/v12.16.3/lib/node_modules/@fluencelabs/fldist/node_modules/@fluencelabs/fluence/dist/api.js:169:28) + at listOnTimeout (internal/timers.js:549:17) + at processTimers (internal/timers.js:492:7) +``` + +and: + +``` +mbp16~/localdev/ethdenver-hackathon/ether-price-getter(eth-price-getter|✚1…) % fldist new_service --env testnet -n "Ether Price Getter" -s AenQdfKCRrh1sajNFkKKV1iojgVnLWyatdaqwzEMopje --ms artifacts/curl_adapter.wasm:curl_cfg.json artifacts/ether_price_getter.wasm:ether_price_getter_cfg.json --ttl 90000 +client seed: AenQdfKCRrh1sajNFkKKV1iojgVnLWyatdaqwzEMopje +client peerId: 12D3KooWQy61BZ4P1DeJzhvsQ76uQAQc7N8tDk6NDz5BFnnhwuPP +node peerId: 12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb +uploading blueprint Ether Price Getter to node 12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb via client 12D3KooWQy61BZ4P1DeJzhvsQ76uQAQc7N8tDk6NDz5BFnnhwuPP +creating service 95abbf2b-cf4f-4560-b3a5-2717b340ead8 +Particle expired. Now: 1614030097045, ttl: 90000, ts: 1614030007040 +Particle expired. Now: 1614030100739, ttl: 90000, ts: 1614030010735 +Particle expired. Now: 1614030104327, ttl: 90000, ts: 1614030014325 +Particle expired. Now: 1614030104850, ttl: 90000, ts: 1614030014846 +Something went wrong! +Error: callback for _callback/createService timed out after 90000 + at Timeout._onTimeout (/Users/bebo/.nvm/versions/node/v12.16.3/lib/node_modules/@fluencelabs/fldist/node_modules/@fluencelabs/fluence/dist/api.js:169:28) + at listOnTimeout (internal/timers.js:549:17) + at processTimers (internal/timers.js:492:7) +mbp16~/localdev/ethdenver-hackathon/ether-price-getter(eth-price-getter|✚1…) % +``` + +``` +mbp16~/localdev/ethdenver-hackathon/ether-price-getter(eth-price-getter|✚1…) % fldist new_service --env testnet -n "Ether Price Getter" -s AenQdfKCRrh1sajNFkKKV1iojgVnLWyatdaqwzEMopje --ms artifacts/curl_adapter.wasm:curl_cfg.json artifacts/ether_price_getter.wasm:ether_price_getter_cfg.json --ttl 180000 +client seed: AenQdfKCRrh1sajNFkKKV1iojgVnLWyatdaqwzEMopje +client peerId: 12D3KooWQy61BZ4P1DeJzhvsQ76uQAQc7N8tDk6NDz5BFnnhwuPP +node peerId: 12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb +uploading blueprint Ether Price Getter to node 12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb via client 12D3KooWQy61BZ4P1DeJzhvsQ76uQAQc7N8tDk6NDz5BFnnhwuPP +creating service 5f7497e8-1140-44e4-ad93-87d52e7cb359 +Particle expired. Now: 1614030312210, ttl: 180000, ts: 1614030132205 +Particle expired. Now: 1614030315338, ttl: 180000, ts: 1614030135336 +Particle expired. Now: 1614030318206, ttl: 180000, ts: 1614030138203 +Particle expired. Now: 1614030318738, ttl: 180000, ts: 1614030138732 +Something went wrong! +Error: callback for _callback/createService timed out after 180000 + at Timeout._onTimeout (/Users/bebo/.nvm/versions/node/v12.16.3/lib/node_modules/@fluencelabs/fldist/node_modules/@fluencelabs/fluence/dist/api.js:169:28) + at listOnTimeout (internal/timers.js:549:17) + at processTimers (internal/timers.js:492:7) +mbp16~/localdev/ethdenver-hackathon/ether-price-getter(eth-price-getter|✚1…) % +``` \ No newline at end of file diff --git a/ether-price-getter/air-scripts/get_eth_price.clj b/ether-price-getter/air-scripts/get_eth_price.clj new file mode 100644 index 0000000..928d479 --- /dev/null +++ b/ether-price-getter/air-scripts/get_eth_price.clj @@ -0,0 +1,10 @@ +(xor + (seq + (call relay (service "ether_price_getter") ["32fc4c0e-59cf-4245-8f22-d4095de0f738" "EUR"] result) + (call %init_peer_id% (returnService "run") [result]) + ) + (seq + (call relay ("op" "identity") []) + (call %init_peer_id% (returnService "run") ["XOR FAILED" %last_error%]) + ) +) \ No newline at end of file diff --git a/ether-price-getter/air-scripts/get_eth_price.clj.bk b/ether-price-getter/air-scripts/get_eth_price.clj.bk new file mode 100644 index 0000000..a140976 --- /dev/null +++ b/ether-price-getter/air-scripts/get_eth_price.clj.bk @@ -0,0 +1,10 @@ +(xor + (seq + (call relay (service "ether_price_getter") ["42fc4b0e-59cf-4182-8f22-d4095cf0a738" "EUR"] result) + (call %init_peer_id% (returnService "run") [result]) + ) + (seq + (call relay ("op" "identity") []) + (call %init_peer_id% (returnService "run") ["XOR FAILED" %last_error%]) + ) +) \ No newline at end of file diff --git a/ether-price-getter/build.sh b/ether-price-getter/build.sh new file mode 100755 index 0000000..f62c92c --- /dev/null +++ b/ether-price-getter/build.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +mkdir -p artifacts + +cd curl_adapter +fce build --release +cd .. + +cd ether_price_getter +fce build --release +cd .. + +rm -f artifacts/* +cp curl_adapter/target/wasm32-wasi/release/curl_adapter.wasm artifacts/ +cp ether_price_getter/target/wasm32-wasi/release/ether_price_getter.wasm artifacts/ diff --git a/ether-price-getter/curl_adapter/.DS_Store b/ether-price-getter/curl_adapter/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..ce9a318502290467c905ce7ada6ac7060ddcd04e GIT binary patch literal 6148 zcmeHKJ5Iwu5S<|bS)xgqa<7mZn8*CL?$9q!42ivhM`&Bd|($D83Wb!m@U`sZa=6y7clNf4)Q4L z?R}qrb@`pywN10ywkt%cm)G0l^!@FopWU3v?jQQQ`Z+*W}{6$8B$V_~g$+N&!zN4`$%0-cVy(}BDQm@YIb@EZzz E03G!v+5i9m literal 0 HcmV?d00001 diff --git a/ether-price-getter/curl_adapter/.gitignore b/ether-price-getter/curl_adapter/.gitignore new file mode 100644 index 0000000..688c164 --- /dev/null +++ b/ether-price-getter/curl_adapter/.gitignore @@ -0,0 +1,6 @@ +DS_Store +.repl_history +/target +**/**.bak +**/**.bk +Cargo.lock diff --git a/ether-price-getter/curl_adapter/Cargo.toml b/ether-price-getter/curl_adapter/Cargo.toml new file mode 100644 index 0000000..b183740 --- /dev/null +++ b/ether-price-getter/curl_adapter/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "curl_adapter" +version = "0.1.0" +authors = ["Fluence Labs"] +edition = "2018" +publish = false + +[[bin]] +path = "src/main.rs" +name = "curl_adapter" + +[dependencies] +fluence = { version = "=0.2.18", features = ["logger"]} +log = "0.4.8" diff --git a/ether-price-getter/curl_adapter/src/main.rs b/ether-price-getter/curl_adapter/src/main.rs new file mode 100644 index 0000000..65406ba --- /dev/null +++ b/ether-price-getter/curl_adapter/src/main.rs @@ -0,0 +1,55 @@ +/* + * Copyright 2021 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use fluence::fce; +use fluence::WasmLoggerBuilder; + +/// Log level can be changed by `RUST_LOG` env as well. +pub fn main() { + WasmLoggerBuilder::new().build().unwrap(); +} + +#[fce] +pub fn curl_request(url: String) -> String { + // log::info!("get called with url {}", url); + + unsafe { curl(url) } +} + +/// Permissions in `Config.toml` should exist to use host functions. +#[fce] +#[link(wasm_import_module = "host")] +extern "C" { + fn curl(cmd: String) -> String; +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn curl_test() { + let args = r#"-X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x1b4", true],"id":1}'"#; + let url = "https://kovan.infura.io/v3//0cc023286cae4ab886598ecd14e256fd"; + + let cmd = format!("{} {}", args, url); + println!("cmd: {}", cmd); + + let res = curl_request(cmd); + println!("res: {}", res); + assert!(true); + } +} diff --git a/ether-price-getter/curl_cfg.json b/ether-price-getter/curl_cfg.json new file mode 100644 index 0000000..107c94e --- /dev/null +++ b/ether-price-getter/curl_cfg.json @@ -0,0 +1,6 @@ +{ + "name": "curl_adapter", + "mountedBinaries": { + "curl": "/usr/bin/curl" + } +} diff --git a/ether-price-getter/ether_price_getter/.DS_Store b/ether-price-getter/ether_price_getter/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..13d8a5be4989862000e0e1929b4927cb1aeeafa4 GIT binary patch literal 6148 zcmeHKJ5Iwu5S<|@EYXB6ioeQ8O5)_FXf#}`k5V;Naq2mC&`2dbAQc}Ph zY4&+%=IzR_@OVT-=P&D-$V5acxS{NA8JgwI2X>Z`F;MM{*<#ghcZ0f90ppJ3S)OFI zdFb;m?!GhIwrQ4|c8N%J{dRkpzQ11ev+EPt-DCgMOVDUkfC^9nDnJGPwF2nbX4QEh zS1LdSsKA#3_I)UD!#c4G^iKyC9|3^&8}Ei^pCy3B0>C=43q%B_K?Md?bHvb~BVMwu zPV54MF6uYqoV;0cLQ%gR@#5v8bs$$NKn0E!7{zvC{r?93!~B0t;*JVXfj^~yj+;d@ z$17!T9lV_N+5*3ae;R7N9E-PNptoXdtQAjtb;X{MuM@jKrz7rkAb$i*7aA4#4Fx^` D2M;Gl literal 0 HcmV?d00001 diff --git a/ether-price-getter/ether_price_getter/.gitignore b/ether-price-getter/ether_price_getter/.gitignore new file mode 100644 index 0000000..688c164 --- /dev/null +++ b/ether-price-getter/ether_price_getter/.gitignore @@ -0,0 +1,6 @@ +DS_Store +.repl_history +/target +**/**.bak +**/**.bk +Cargo.lock diff --git a/ether-price-getter/ether_price_getter/Cargo.toml b/ether-price-getter/ether_price_getter/Cargo.toml new file mode 100644 index 0000000..39b575b --- /dev/null +++ b/ether-price-getter/ether_price_getter/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "ether_price_getter" +version = "0.1.0" +authors = ["Fluence Labs"] +edition = "2018" +publish = false + +[[bin]] +name = "ether_price_getter" +path = "src/main.rs" + +[dependencies] +fluence = { version = "0.2.18", features = ["logger"]} +log = "0.4.8" +fstrings = { version="0.2.3" } diff --git a/ether-price-getter/ether_price_getter/src/eth_price_getter.rs b/ether-price-getter/ether_price_getter/src/eth_price_getter.rs new file mode 100644 index 0000000..e300558 --- /dev/null +++ b/ether-price-getter/ether_price_getter/src/eth_price_getter.rs @@ -0,0 +1,29 @@ +/* + * Copyright 2021 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use fluence::fce; +use crate::curl_request; + +static URL_LATEST: &'static str = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest"; + + +#[fce] +pub fn ether_price_getter(api_key: String, currency_symbol: String) -> String { + let curl_args = f!(r#"-H "X-CMC_PRO_API_KEY: {api_key}" -H "Accept: application/json" -d "symbol=ETH&convert={currency_symbol}" -G {URL_LATEST}"#); + let response: String = unsafe { curl_request(curl_args) }; + response +} + \ No newline at end of file diff --git a/ether-price-getter/ether_price_getter/src/fce_results.rs b/ether-price-getter/ether_price_getter/src/fce_results.rs new file mode 100644 index 0000000..727aa2a --- /dev/null +++ b/ether-price-getter/ether_price_getter/src/fce_results.rs @@ -0,0 +1,82 @@ +/* + * Copyright 2021 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use crate::jsonrpc_helpers::JSON_RPC; +use crate::Result; +use fluence::fce; +use serde_json::Value; + +#[fce] +#[derive(Debug)] +pub struct JsonRpcResult { + pub jsonrpc: String, + pub result: String, + pub error: String, + pub id: u64, +} + +impl From> for JsonRpcResult { + fn from(result: Result) -> Self { + let jsonrpc = JSON_RPC.into(); + match result { + Ok(res) => { + let result_obj: Value = serde_json::from_str(&res).unwrap(); + let id: u64 = serde_json::from_value(result_obj["id"].clone()).unwrap(); + let result: String = serde_json::from_value(result_obj["result"].clone()).unwrap(); + + Self { + jsonrpc, + id, + result, + error: "".to_string(), + } + } + Err(err) => { + let result_obj: Value = serde_json::from_str(&err).unwrap(); + let id: u64 = serde_json::from_value(result_obj["id"].clone()).unwrap(); + + Self { + jsonrpc, + id, + result: "".to_string(), + error: err, + } + } + } + } +} + +#[fce] +#[derive(Debug)] +pub struct TestResult { + pub test_passed: bool, + pub error: String, +} + +impl From> for TestResult { + fn from(result: Result) -> Self { + match result { + Ok(res) => Self { + test_passed: true, + error: res, + }, + Err(err) => Self { + test_passed: false, + error: err, + }, + } + } +} diff --git a/ether-price-getter/ether_price_getter/src/main.rs b/ether-price-getter/ether_price_getter/src/main.rs new file mode 100644 index 0000000..59f7442 --- /dev/null +++ b/ether-price-getter/ether_price_getter/src/main.rs @@ -0,0 +1,36 @@ +/* + * Copyright 2021 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#![allow(non_snake_case, unused_variables, unused_imports, unused_parens, unused_mut)] + +#[macro_use] +extern crate fstrings; + +use fluence::fce; +use fluence::WasmLoggerBuilder; + +mod eth_price_getter; + +pub(crate) type Result = std::result::Result; + +pub fn main() { + WasmLoggerBuilder::new().build().ok(); +} + +#[fce] +#[link(wasm_import_module = "curl_adapter")] +extern "C" { + pub fn curl_request(url: String) -> String; +} diff --git a/ether-price-getter/ether_price_getter_cfg.json b/ether-price-getter/ether_price_getter_cfg.json new file mode 100644 index 0000000..b929aa4 --- /dev/null +++ b/ether-price-getter/ether_price_getter_cfg.json @@ -0,0 +1,3 @@ +{ + "name": "Ether-Price-Getter" +} \ No newline at end of file