Skip to content

Commit 1d3c3a9

Browse files
committed
Integrated Rust & Wasm
* Created library in /wasm-lib with all wasm content * Added script in package.json "build-wasm" which runs "cd wasm-lib && wasm-pack build --target web --out-dir pkg" * Need to first import init from wasm-lib in index.js and run "index().then()" before any rendering due to async nature of Rust imported functions
1 parent 0789758 commit 1d3c3a9

File tree

330 files changed

+758
-27
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

330 files changed

+758
-27
lines changed

.DS_Store

0 Bytes
Binary file not shown.

package-lock.json

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"homepage": "https://Sebastian-git.github.io/BinomialBeacon",
55
"private": true,
66
"dependencies": {
7-
"@alpacahq/alpaca-trade-api": "^3.0.1",
87
"@fortawesome/free-brands-svg-icons": "^6.4.2",
98
"@fortawesome/free-solid-svg-icons": "^6.4.2",
109
"@fortawesome/react-fontawesome": "^0.2.0",
@@ -23,11 +22,13 @@
2322
"react-tree-graph": "^8.0.1",
2423
"recharts": "^2.7.2",
2524
"typescript": "^4.9.5",
25+
"wasm-lib": "file:wasm-lib/pkg",
2626
"web-vitals": "^2.1.4"
2727
},
2828
"scripts": {
2929
"start": "react-scripts start",
3030
"build": "react-scripts build",
31+
"build-wasm": "cd wasm-lib && wasm-pack build --target web --out-dir pkg",
3132
"test": "react-scripts test",
3233
"eject": "react-scripts eject",
3334
"predeploy": "npm run build",

src/App.js

+9-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { up_size, down_size, call_buy_payoff, put_buy_payoff, get_rnp } from "wasm-lib";
12
import { BrowserRouter as Router, Route, Routes, useNavigate, useLocation } from 'react-router-dom';import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
23
import { faArrowsToEye } from '@fortawesome/free-solid-svg-icons';
34
import React, { useEffect, useState, useRef } from 'react';
@@ -11,16 +12,6 @@ function round(value, decimals) {
1112
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
1213
}
1314

14-
const upSize = (stdDev, deltaT) => { return Math.exp((stdDev * 0.01) * Math.sqrt(deltaT)) };
15-
16-
const downSize = (stdDev, deltaT) => { return 1.0/(Math.exp((stdDev * 0.01) * Math.sqrt(deltaT))) };
17-
18-
const callBuyPayoff = (curPrice, strikePrice) => { return Math.max(curPrice - strikePrice, 0) };
19-
20-
const putBuyPayoff = (curPrice, strikePrice) => { return Math.max(strikePrice - curPrice, 0) };
21-
22-
const getRNP = (rfr, deltaT, downMove, upMove) => { return (Math.exp(rfr * deltaT) - downMove) / (upMove - downMove) };
23-
2415
let rfr = 0.0375;
2516
let totalTime = 20;
2617

@@ -129,8 +120,8 @@ function Dashboard({
129120
const [steps, setSteps] = useState(1);
130121
const [deltaT, setDeltaT] = useState(steps / steps);
131122
const [centerGraphTooltip, setCenterGraphTooltip] = useState(false);
132-
const [upMove, setUpMove] = useState(() => upSize(stdDev, deltaT));
133-
const [downMove, setDownMove] = useState(() => downSize(stdDev, deltaT));
123+
const [upMove, setUpMove] = useState(() => up_size(stdDev, deltaT));
124+
const [downMove, setDownMove] = useState(() => down_size(stdDev, deltaT));
134125
const [riskNeutralProbability, setRiskNeutralProbability] = useState(0);
135126
const [resetCount, setResetCount] = useState(0);
136127
const echartRef = useRef(null);
@@ -153,7 +144,7 @@ function Dashboard({
153144

154145

155146
useEffect(() => {
156-
let RNP = getRNP(rfr, deltaT, downMove, upMove)
147+
let RNP = get_rnp(rfr, deltaT, downMove, upMove)
157148
setRiskNeutralProbability(round(RNP, 4));
158149
}, [downMove, upMove, deltaT])
159150

@@ -168,22 +159,22 @@ function Dashboard({
168159
// Just need to figure out if we want a fixed time till expiration or just deltaT always = 1 year vs 1 month, weigh options later
169160

170161
// Size of up/down moves (functions return a %, stdDev whole number)
171-
let newUpMove = upSize(stdDev, newDeltaT);
172-
let newDownMove = downSize(stdDev, newDeltaT);
162+
let newUpMove = up_size(stdDev, newDeltaT);
163+
let newDownMove = down_size(stdDev, newDeltaT);
173164
setUpMove(newUpMove);
174165
setDownMove(newDownMove);
175166

176-
let RNP = getRNP(rfr, deltaT, downMove, upMove)
167+
let RNP = get_rnp(rfr, deltaT, downMove, upMove)
177168
setRiskNeutralProbability(round(RNP, 4));
178169
}
179170

180171
const createGraphData = (deltaT, curSteps, price, id = 0, parentId = null, optionType) => {
181172
let payoff;
182173
if (optionType === "call") {
183-
payoff = callBuyPayoff(price, strikePrice);
174+
payoff = call_buy_payoff(price, strikePrice);
184175
}
185176
else if (optionType === "put") {
186-
payoff = putBuyPayoff(price, strikePrice);
177+
payoff = put_buy_payoff(price, strikePrice);
187178
}
188179

189180
let x = (steps - curSteps) * 100 * steps;

src/index.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import React from 'react';
1+
import init from "wasm-lib"
22
import ReactDOM from 'react-dom/client';
3-
import './index.css';
3+
import React from 'react';
44
import App from './App';
5+
import './index.css';
56

6-
const root = ReactDOM.createRoot(document.getElementById('root'));
7-
root.render(
8-
<React.StrictMode>
9-
<App />
10-
</React.StrictMode>
11-
);
7+
init().then(() => {
8+
const root = ReactDOM.createRoot(document.getElementById('root'));
9+
root.render(
10+
<React.StrictMode>
11+
<App />
12+
</React.StrictMode>
13+
);
14+
});

wasm-lib/Cargo.lock

+123
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wasm-lib/Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "wasm-lib"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[lib]
9+
crate-type = ["cdylib"]
10+
11+
[dependencies]
12+
wasm-bindgen = "0.2.87"

wasm-lib/src/lib.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use wasm_bindgen::prelude::*;
2+
3+
#[wasm_bindgen]
4+
pub fn up_size(std_dev: f64, delta_t: f64) -> f64 {
5+
(std_dev * 0.01 * delta_t.sqrt()).exp()
6+
}
7+
8+
#[wasm_bindgen]
9+
pub fn down_size(std_dev: f64, delta_t: f64) -> f64 {
10+
1.0 / (std_dev * 0.01 * delta_t.sqrt()).exp()
11+
}
12+
13+
#[wasm_bindgen]
14+
pub fn call_buy_payoff(cur_price: f64, strike_price: f64) -> f64 {
15+
f64::max(cur_price - strike_price, 0.0)
16+
}
17+
18+
#[wasm_bindgen]
19+
pub fn put_buy_payoff(cur_price: f64, strike_price: f64) -> f64 {
20+
f64::max(strike_price - cur_price, 0.0)
21+
}
22+
23+
#[wasm_bindgen]
24+
pub fn get_rnp(rfr: f64, delta_t: f64, up_move: f64, down_move: f64) -> f64 {
25+
(((rfr * delta_t) - down_move) / (up_move - down_move)).exp()
26+
}

wasm-lib/target/.rustc_info.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc_fingerprint":18004062239059203955,"outputs":{"14908675589988555557":{"success":true,"status":"","code":0,"stdout":"___.wasm\nlib___.rlib\n___.wasm\nlib___.a\n/Users/sebastiancevallos/.rustup/toolchains/stable-x86_64-apple-darwin\noff\n___\ndebug_assertions\npanic=\"abort\"\nproc_macro\ntarget_arch=\"wasm32\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"wasm\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"unknown\"\ntarget_pointer_width=\"32\"\ntarget_vendor=\"unknown\"\n","stderr":"warning: dropping unsupported crate type `dylib` for target `wasm32-unknown-unknown`\n\nwarning: dropping unsupported crate type `proc-macro` for target `wasm32-unknown-unknown`\n\nwarning: 2 warnings emitted\n\n"},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/sebastiancevallos/.rustup/toolchains/stable-x86_64-apple-darwin\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.71.1 (eb26296b5 2023-08-03)\nbinary: rustc\ncommit-hash: eb26296b556cef10fb713a38f3d16b9886080f26\ncommit-date: 2023-08-03\nhost: x86_64-apple-darwin\nrelease: 1.71.1\nLLVM version: 16.0.5\n","stderr":""}},"successes":{}}

wasm-lib/target/CACHEDIR.TAG

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Signature: 8a477f597d28d172789f06886806bc55
2+
# This file is a cache directory tag created by cargo.
3+
# For information about cache directory tags see https://bford.info/cachedir/

wasm-lib/target/debug/.cargo-lock

Whitespace-only changes.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3b9f57917b0f92cb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc":17101774351297483375,"features":"[\"default\"]","target":1151118578467687688,"profile":13345265645950336421,"path":16942254729369446916,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/bumpalo-48f3aff246bb733b/dep-lib-bumpalo"}}],"rustflags":[],"metadata":10871386354195723922,"config":2202906307356721367,"compile_kind":0}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
30b1272f8321df92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc":17101774351297483375,"features":"[]","target":10623512480563079566,"profile":2258518674978727886,"path":12115073575712318311,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/cfg-if-e712001a848d95f7/dep-lib-cfg-if"}}],"rustflags":[],"metadata":8462187951337715540,"config":2202906307356721367,"compile_kind":0}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8ae25a1ea3a6bd41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc":17101774351297483375,"features":"[]","target":4487324886529943577,"profile":13345265645950336421,"path":15822162212426524846,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/log-b56018c3939b976a/dep-lib-log"}}],"rustflags":[],"metadata":179143468214550567,"config":2202906307356721367,"compile_kind":0}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c68e332ec246ddb7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc":17101774351297483375,"features":"[\"alloc\", \"default\", \"race\", \"std\"]","target":14856186769647684053,"profile":13345265645950336421,"path":1910896929141093066,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/once_cell-906b0e4d05c79817/dep-lib-once_cell"}}],"rustflags":[],"metadata":14177539708254521827,"config":2202906307356721367,"compile_kind":0}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8a6962e8d63b09ec
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc":17101774351297483375,"features":"[\"default\", \"proc-macro\"]","target":427768481117760528,"profile":13345265645950336421,"path":15801991002327282864,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/proc-macro2-08c0c464f6f413b2/dep-build-script-build-script-build"}}],"rustflags":[],"metadata":7635439851376710101,"config":2202906307356721367,"compile_kind":0}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2623bfcb1f069ca9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc":17101774351297483375,"features":"","target":0,"profile":0,"path":0,"deps":[[2275467556785573469,"build_script_build",false,17008191261949192586]],"local":[{"RerunIfChanged":{"output":"debug/build/proc-macro2-cf8a1e767002d49d/output","paths":["build.rs"]}}],"rustflags":[],"metadata":0,"config":0,"compile_kind":0}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3c190e7d6244de8c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc":17101774351297483375,"features":"[\"default\", \"proc-macro\"]","target":16714894217519287322,"profile":13345265645950336421,"path":2363726862177389386,"deps":[[2275467556785573469,"build_script_build",false,12221650222408737574],[10721203361745528474,"unicode_ident",false,5233511978115821712]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/proc-macro2-d4e60d1531b077bd/dep-lib-proc-macro2"}}],"rustflags":[],"metadata":7635439851376710101,"config":2202906307356721367,"compile_kind":0}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
103b861835ce7b12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc":17101774351297483375,"features":"[\"default\", \"proc-macro\"]","target":10824007166531090010,"profile":13345265645950336421,"path":17238910176999022440,"deps":[[2275467556785573469,"proc_macro2",false,10150625799935236412]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/quote-652c0814cfe6295b/dep-lib-quote"}}],"rustflags":[],"metadata":2717943770976187624,"config":2202906307356721367,"compile_kind":0}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.

0 commit comments

Comments
 (0)