Skip to content

Commit

Permalink
When setting global parameters, ContractInfo and MarginInfo lack a me…
Browse files Browse the repository at this point in the history
…thod for converting to JSON(fix #108) (#109)

use json parameter of ContractInfo and MarginInfo in wasm(fix #108)
  • Loading branch information
nick-zkp authored Dec 4, 2023
1 parent 39099a9 commit c3d1dda
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
16 changes: 13 additions & 3 deletions bindings/wasm/src/tx_types/contract/update_global_var.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use zklink_sdk_types::tx_builder::UpdateGlobalVarBuilder as TxUpdateGlobalVarBuilder;
Expand Down Expand Up @@ -89,7 +89,7 @@ impl Parameter {
}

#[wasm_bindgen]
#[derive(Deserialize)]
#[derive(Deserialize, Serialize)]
pub struct MarginInfo {
margin_id: u8,
token_id: u32,
Expand All @@ -106,10 +106,15 @@ impl MarginInfo {
ratio,
}
}

#[wasm_bindgen(js_name=jsonValue)]
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self)?)
}
}

#[wasm_bindgen]
#[derive(Deserialize)]
#[derive(Deserialize, Serialize)]
pub struct ContractInfo {
pair_id: u8,
symbol: String,
Expand All @@ -133,6 +138,11 @@ impl ContractInfo {
maintenance_margin_rate,
}
}

#[wasm_bindgen(js_name=jsonValue)]
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self)?)
}
}

impl From<Parameter> for ContractParameter {
Expand Down
19 changes: 14 additions & 5 deletions examples/Javascript/node-example/3_update_global_var.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {UpdateGlobalVarBuilder,Signer,FundingInfo,newUpdateGlobalVar,Parameter,ParameterType,FundingRate,RpcClient } = require('./node-dist/zklink-sdk-node');
const {UpdateGlobalVarBuilder,ContractInfo,Signer,FundingInfo,MarginInfo,newUpdateGlobalVar,Parameter,ParameterType,FundingRate,RpcClient } = require('./node-dist/zklink-sdk-node');
// CommonJS
const fetch = require('node-fetch');
const AbortController = require('abort-controller')
Expand All @@ -21,11 +21,20 @@ async function testUpdGlobalVar() {
new FundingInfo(1,3,"3333").jsonValue(),
new FundingInfo(2,5,"456").jsonValue(),
new FundingInfo(1,4,"8980808098").jsonValue()];
const parameter_infos = new Parameter(ParameterType.FundingInfos,funding_infos);
// fee_account
const parameter = new Parameter(ParameterType.FeeAccount,10)
let tx_builder = new UpdateGlobalVarBuilder(1,2,parameter,1000);
const parameter_funding = new Parameter(ParameterType.FundingInfos,funding_infos);
// contract_info
const contract_info = new ContractInfo(1,"USDC/USDT",5,10).jsonValue();
console.log(contract_info);
const parameter_contract = new Parameter(ParameterType.ContractInfo,contract_info)
// margin_info
const margin_info = new MarginInfo(2,17,10).jsonValue();
const parameter = new Parameter(ParameterType.MarginInfo,margin_info)
console.log(parameter);

let tx_builder = new UpdateGlobalVarBuilder(1,8,parameter,1000);
console.log(tx_builder);
let tx = newUpdateGlobalVar(tx_builder);
console.log(tx.jsonValue());
const signer = new Signer(private_key);
const submitterSignature = await signer.submitterSignature(tx.zklinkTx());
console.log(submitterSignature);
Expand Down

0 comments on commit c3d1dda

Please sign in to comment.