Skip to content

Commit

Permalink
Add tip6 helpers into wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Sep 26, 2023
1 parent 074c062 commit b329f9c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 29 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
resolver = "2"
members = ["abi-parser", "shared", "core", "debugger"]

[profile.release]
Expand Down
49 changes: 49 additions & 0 deletions core/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,52 @@ pub fn check_abi(abi: &str) -> Result<(), JsValue> {
ton_abi::Contract::try_from(contract).handle_error()?;
Ok(())
}

#[wasm_bindgen(typescript_custom_section)]
const FUNCTION_ENTRY: &str = r#"
export type FunctionEntry = {
name: string,
id: number,
};
"#;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "FunctionEntry")]
pub type FunctionEntry;
}

#[wasm_bindgen(js_name = "getContractFunctions")]
pub fn get_contract_functions(abi: &str) -> Result<Vec<FunctionEntry>, JsValue> {
let contract = serde_json::from_str::<ton_abi::contract::SerdeContract>(abi).handle_error()?;
let sorted_function_names = contract
.functions
.iter()
.map(|item| item.name.clone())
.collect::<Vec<_>>();

let contract = ton_abi::Contract::try_from(contract).handle_error()?;
let mut result = Vec::with_capacity(sorted_function_names.len());
for name in sorted_function_names {
if let Some(function) = contract.functions.get(&name) {
result.push(
ObjectBuilder::new()
.set("name", name)
.set("id", function.input_id)
.build()
.unchecked_into(),
);
}
}

Ok(result)
}

#[wasm_bindgen(js_name = "computeTip6InterfaceId")]
pub fn compute_tip6_interface_id(method_ids: &[u32]) -> u32 {
let mut result = 0;
for id in method_ids {
result ^= id;
}
result
}
45 changes: 17 additions & 28 deletions src/components/Tip6Workspace.vue
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<script setup lang="ts">
import { ref, shallowRef, watchEffect } from 'vue';
import { computed, ref, shallowRef, watchEffect } from 'vue';
import * as core from '@core';
const input = ref<string>('');
const selectedMethods = ref<string[]>([])
const selectedMethods = ref<number[]>([])
const state = shallowRef<{
error?: undefined;
methods: string[];
methods: core.FunctionEntry[];
}>({
methods: [],
});
watchEffect(() => {
try {
const parsed = JSON.parse(input.value || '{}');
const methods = parsed.functions?.map(item => item.name) ?? []
const methods = input.value.trim() != '' ? core.getContractFunctions(input.value) : [];
selectedMethods.value = []
state.value = {
error: undefined,
methods,
}
} catch (e: any) {
Expand All @@ -27,9 +28,11 @@ watchEffect(() => {
}
});
const interfaceId = computed(() => core.computeTip6InterfaceId(new Uint32Array(selectedMethods.value)));
function toggleAll() {
selectedMethods.value = selectedMethods.value.length !== state.value.methods.length
? state.value.methods
? state.value.methods.map(item => item.id)
: []
}
</script>
Expand All @@ -42,12 +45,8 @@ function toggleAll() {
<div class="field">
<label class="label">Enter JSON ABI:</label>
<div class="control mb-3">
<textarea
spellcheck="false"
rows="5"
:class="['textarea', { 'is-danger': state.error != null }]"
v-model="input"
></textarea>
<textarea spellcheck="false" rows="5" :class="['textarea', { 'is-danger': state.error != null }]"
v-model="input"></textarea>
</div>

<pre v-if="state.error != null" class="help is-danger">{{ state.error }}</pre>
Expand All @@ -57,35 +56,25 @@ function toggleAll() {
<div class="label is-flex is-justify-content-space-between is-align-content-center">
Methods:

<button
class="button is-small"
:disabled="state.methods.length === 0"
@click="toggleAll()"
>
<button class="button is-small" :disabled="state.methods.length === 0" @click="toggleAll()">
Toggle all
</button>
</div>

<div
class="mb-3"
v-for="method in state.methods"
>
<div class="mb-3" v-for="method in state.methods">
<label class="button is-fullwidth is-justify-content-flex-start">
<div class="checkbox">
<input
type="checkbox"
:id="method"
:value="method"
v-model="selectedMethods"
/> {{ method }}
<input type="checkbox" :id="method.name" :value="method.id" v-model="selectedMethods" />
<code class="m-3">0x{{ method.id.toString(16).padStart(8, '0') }}</code>
<span>{{ method.name }}</span>
</div>
</label>
</div>
</div>
</div>
<div class="column">
<label class="label">Interface ID:</label>
<pre class="encoded-data">Interface Id</pre>
<pre class="encoded-data">0x{{ interfaceId.toString(16).padStart(8, '0') }}</pre>
</div>
</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion src/providers/useMicrowave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,6 @@ const deployMicrowave = (setStatus: (status?: string) => void) => {
state_init: MICROWAVE_BOC
})
.sendExternal({

Check failure on line 417 in src/providers/useMicrowave.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type '{ withoutSignature: true; stateInit: string; }' is not assignable to parameter of type 'SendExternalParams'.
publicKey: '',
withoutSignature: true,
stateInit: FACTORY_BOC
});
Expand Down

0 comments on commit b329f9c

Please sign in to comment.