diff --git a/.changeset/yellow-schools-glow.md b/.changeset/yellow-schools-glow.md new file mode 100644 index 0000000..cb386c6 --- /dev/null +++ b/.changeset/yellow-schools-glow.md @@ -0,0 +1,5 @@ +--- +"@minima-global/mds": patch +--- + +added getStateVariable util function diff --git a/packages/mds/src/mds.ts b/packages/mds/src/mds.ts index 84a5488..bcabbc8 100644 --- a/packages/mds/src/mds.ts +++ b/packages/mds/src/mds.ts @@ -4,7 +4,7 @@ */ import { createCommandFunction } from './helpers.js'; -import type { MDSObj } from './index.js'; +import type { Coin, MDSObj } from './index.js'; var MDS_MAIN_CALLBACK: any; var API_CALLS: any[] = []; @@ -341,6 +341,21 @@ export const MDS: MDSObj = { } return bytes.buffer; }, + + getStateVariable: (coin, port) => { + //Get the state vars + var statvars = coin.state; + if (Array.isArray(statvars)) { + var len = statvars.length; + for (var i = 0; i < len; i++) { + var state = statvars[i]; + if (state?.port == port) { + return state.data; + } + } + } + return undefined; + }, }, }; diff --git a/packages/mds/src/types.ts b/packages/mds/src/types.ts index efbc4f3..5a67135 100644 --- a/packages/mds/src/types.ts +++ b/packages/mds/src/types.ts @@ -378,5 +378,12 @@ export interface MDSObj { * @param hex The HEX string to convert */ base64ToArrayBuffer: (base64: string) => ArrayBuffer; + + /** + * Get a state variable from a coin + * @param coin The coin to get the state variable from + * @param port The port to get the state variable from + */ + getStateVariable: (coin: Coin, port: number) => string | undefined; }; } diff --git a/tests/test-app-new/src/AppContext.tsx b/tests/test-app-new/src/AppContext.tsx index 615a0ec..2c327c2 100644 --- a/tests/test-app-new/src/AppContext.tsx +++ b/tests/test-app-new/src/AppContext.tsx @@ -24,17 +24,9 @@ const AppProvider: React.FC = ({ children }) => { const coins = await MDS.cmd.coins() - coins.response.forEach((coin) => { - if (coin.state) { - if (Array.isArray(coin.state)) { - coin.state.forEach((state) => { - console.log(state.data) - }) - } else { - console.log(coin.state["0"]) - } - } - }) + console.log(coins.response[0]) + + console.log(MDS.util.getStateVariable(coins.response[0], 2)) } }) } diff --git a/tests/test-app-new/test-app-0.1.0.mds.zip b/tests/test-app-new/test-app-0.1.0.mds.zip index 11fc0dd..791c70a 100644 Binary files a/tests/test-app-new/test-app-0.1.0.mds.zip and b/tests/test-app-new/test-app-0.1.0.mds.zip differ