From 6f74f008d50b0cb73bec48b7aebc0fff33fd86df Mon Sep 17 00:00:00 2001 From: zksemi <145792744+zksemi@users.noreply.github.com> Date: Fri, 10 May 2024 19:41:01 +0800 Subject: [PATCH] Fix javascript example (#278) * update node example * make exported wasm struct deserializable --- bindings/wasm/src/tx_types/change_pubkey.rs | 2 ++ .../tx_types/contract/contract_matching.rs | 2 +- .../wasm/src/tx_types/contract/funding.rs | 2 +- bindings/wasm/src/tx_types/contract/prices.rs | 4 ++-- .../tx_types/contract/update_global_var.rs | 6 +++--- bindings/wasm/src/tx_types/forced_exit.rs | 1 + bindings/wasm/src/tx_types/order_matching.rs | 2 ++ bindings/wasm/src/tx_types/transfer.rs | 1 + bindings/wasm/src/tx_types/withdraw.rs | 1 + bindings/wasm/src/wallet.rs | 1 + .../node-example/1_change_pubkey.js | 6 +++--- .../node-example/2_auto_deleveraging.js | 18 ++++++++--------- .../node-example/3_update_global_var.js | 14 ++++++------- .../node-example/4_contract_matching.js | 18 ++++++++--------- .../Javascript/node-example/5_liquidation.js | 18 ++++++++--------- examples/Javascript/node-example/6_funding.js | 6 +++--- .../Javascript/node-example/8_sign_rawmsg.js | 4 ++-- .../node-example/9_order_matching.js | 20 +++++++++---------- 18 files changed, 67 insertions(+), 59 deletions(-) diff --git a/bindings/wasm/src/tx_types/change_pubkey.rs b/bindings/wasm/src/tx_types/change_pubkey.rs index fb5685e5..53a0110d 100644 --- a/bindings/wasm/src/tx_types/change_pubkey.rs +++ b/bindings/wasm/src/tx_types/change_pubkey.rs @@ -52,6 +52,7 @@ impl Create2Data { hex::encode(salt_bytes) } + #[wasm_bindgen(js_name=jsValue)] pub fn json_value(&self) -> Result { Ok(serde_wasm_bindgen::to_value(&self.inner)?) } @@ -59,6 +60,7 @@ impl Create2Data { #[wasm_bindgen] impl ChangePubKey { + #[wasm_bindgen(js_name=jsValue)] pub fn json_value(&self) -> Result { Ok(serde_wasm_bindgen::to_value(&self.inner)?) } diff --git a/bindings/wasm/src/tx_types/contract/contract_matching.rs b/bindings/wasm/src/tx_types/contract/contract_matching.rs index e6e25790..514c9605 100644 --- a/bindings/wasm/src/tx_types/contract/contract_matching.rs +++ b/bindings/wasm/src/tx_types/contract/contract_matching.rs @@ -90,7 +90,7 @@ pub struct Contract { #[wasm_bindgen] impl Contract { - #[wasm_bindgen(js_name=jsonValue)] + #[wasm_bindgen(js_name=jsValue)] pub fn json_value(&self) -> Result { Ok(serde_wasm_bindgen::to_value(&self.inner)?) } diff --git a/bindings/wasm/src/tx_types/contract/funding.rs b/bindings/wasm/src/tx_types/contract/funding.rs index 414af80c..870f18bf 100644 --- a/bindings/wasm/src/tx_types/contract/funding.rs +++ b/bindings/wasm/src/tx_types/contract/funding.rs @@ -26,7 +26,7 @@ impl FundingInfo { }) } - #[wasm_bindgen(js_name=jsonValue)] + #[wasm_bindgen(js_name=jsValue)] pub fn json_value(&self) -> Result { Ok(serde_wasm_bindgen::to_value(&self.inner)?) } diff --git a/bindings/wasm/src/tx_types/contract/prices.rs b/bindings/wasm/src/tx_types/contract/prices.rs index 2410d1c7..0b802082 100644 --- a/bindings/wasm/src/tx_types/contract/prices.rs +++ b/bindings/wasm/src/tx_types/contract/prices.rs @@ -25,7 +25,7 @@ impl ContractPrice { }) } - #[wasm_bindgen(js_name=jsonValue)] + #[wasm_bindgen(js_name=jsValue)] pub fn json_value(&self) -> Result { Ok(serde_wasm_bindgen::to_value(&self.inner)?) } @@ -47,7 +47,7 @@ impl SpotPriceInfo { }) } - #[wasm_bindgen(js_name=jsonValue)] + #[wasm_bindgen(js_name=jsValue)] pub fn json_value(&self) -> Result { Ok(serde_wasm_bindgen::to_value(&self.inner)?) } diff --git a/bindings/wasm/src/tx_types/contract/update_global_var.rs b/bindings/wasm/src/tx_types/contract/update_global_var.rs index 8c7b648b..e590a6b9 100644 --- a/bindings/wasm/src/tx_types/contract/update_global_var.rs +++ b/bindings/wasm/src/tx_types/contract/update_global_var.rs @@ -15,7 +15,7 @@ pub struct UpdateGlobalVar { #[wasm_bindgen] impl UpdateGlobalVar { - #[wasm_bindgen(js_name = jsonValue)] + #[wasm_bindgen(js_name=jsValue)] pub fn json_value(&self) -> Result { Ok(serde_wasm_bindgen::to_value(&self.inner)?) } @@ -107,7 +107,7 @@ impl MarginInfo { } } - #[wasm_bindgen(js_name=jsonValue)] + #[wasm_bindgen(js_name=jsValue)] pub fn json_value(&self) -> Result { Ok(serde_wasm_bindgen::to_value(&self)?) } @@ -139,7 +139,7 @@ impl ContractInfo { } } - #[wasm_bindgen(js_name=jsonValue)] + #[wasm_bindgen(js_name=jsValue)] pub fn json_value(&self) -> Result { Ok(serde_wasm_bindgen::to_value(&self)?) } diff --git a/bindings/wasm/src/tx_types/forced_exit.rs b/bindings/wasm/src/tx_types/forced_exit.rs index 355bbec5..396e8d96 100644 --- a/bindings/wasm/src/tx_types/forced_exit.rs +++ b/bindings/wasm/src/tx_types/forced_exit.rs @@ -14,6 +14,7 @@ pub struct ForcedExit { #[wasm_bindgen] impl ForcedExit { + #[wasm_bindgen(js_name=jsValue)] pub fn json_value(&self) -> Result { Ok(serde_wasm_bindgen::to_value(&self.inner)?) } diff --git a/bindings/wasm/src/tx_types/order_matching.rs b/bindings/wasm/src/tx_types/order_matching.rs index 593cc04b..7ff99d52 100644 --- a/bindings/wasm/src/tx_types/order_matching.rs +++ b/bindings/wasm/src/tx_types/order_matching.rs @@ -56,6 +56,7 @@ impl Order { Ok(order) } + #[wasm_bindgen(js_name=jsValue)] pub fn json_value(&self) -> Result { Ok(serde_wasm_bindgen::to_value(&self.inner)?) } @@ -63,6 +64,7 @@ impl Order { #[wasm_bindgen] impl OrderMatching { + #[wasm_bindgen(js_name=jsValue)] pub fn json_value(&self) -> Result { Ok(serde_wasm_bindgen::to_value(&self.inner)?) } diff --git a/bindings/wasm/src/tx_types/transfer.rs b/bindings/wasm/src/tx_types/transfer.rs index 74b2c07f..ab698473 100644 --- a/bindings/wasm/src/tx_types/transfer.rs +++ b/bindings/wasm/src/tx_types/transfer.rs @@ -14,6 +14,7 @@ pub struct Transfer { #[wasm_bindgen] impl Transfer { + #[wasm_bindgen(js_name=jsValue)] pub fn json_value(&self) -> Result { Ok(serde_wasm_bindgen::to_value(&self.inner)?) } diff --git a/bindings/wasm/src/tx_types/withdraw.rs b/bindings/wasm/src/tx_types/withdraw.rs index 0f50c62a..00f7e68a 100644 --- a/bindings/wasm/src/tx_types/withdraw.rs +++ b/bindings/wasm/src/tx_types/withdraw.rs @@ -14,6 +14,7 @@ pub struct Withdraw { #[wasm_bindgen] impl Withdraw { + #[wasm_bindgen(js_name=jsValue)] pub fn json_value(&self) -> Result { Ok(serde_wasm_bindgen::to_value(&self.inner)?) } diff --git a/bindings/wasm/src/wallet.rs b/bindings/wasm/src/wallet.rs index 34ac46bd..5fd35b3c 100644 --- a/bindings/wasm/src/wallet.rs +++ b/bindings/wasm/src/wallet.rs @@ -74,6 +74,7 @@ impl EthTxOption { Ok(EthTxOption { inner }) } + #[wasm_bindgen(js_name=jsValue)] pub fn json_value(&self) -> Result { Ok(serde_wasm_bindgen::to_value(&self.inner)?) } diff --git a/examples/Javascript/node-example/1_change_pubkey.js b/examples/Javascript/node-example/1_change_pubkey.js index 92e33883..ed28c4cf 100644 --- a/examples/Javascript/node-example/1_change_pubkey.js +++ b/examples/Javascript/node-example/1_change_pubkey.js @@ -24,7 +24,7 @@ async function testEcdsaAuth() { 0,"0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001b", ts); let tx = newChangePubkey(tx_builder); - const signer = new Signer(private_key); + const signer = new Signer(private_key, L1Type.Eth); let tx_signature = signer.signChangePubkeyWithEthEcdsaAuth(tx); console.log(tx_signature); @@ -40,7 +40,7 @@ async function testEcdsaAuth() { } async function testOnchainAuth() { - const private_key = "be725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4"; + const private_key = "00f0dfe9e420b857beb5165c4dbe4b21561dc4ca0206ca97f6ee7ad53bc79cab"; const new_pubkey_hash = "0x8255f5a6d0d2b34a19f381e448ed151cc3a59b9e"; const ts = Math.floor(Date.now() / 1000); try { @@ -50,7 +50,7 @@ async function testOnchainAuth() { ts); let tx = newChangePubkey(tx_builder); let addr = "0x04A69b67bcaBfA7D3CCb96e1d25C2e6fC93589fE24A6fD04566B8700ff97a71a"; - const signer = new Signer(private_key,L1Type.Starknet,"SN_GOERLI",addr); + const signer = new Signer(private_key, L1Type.Starknet, "SN_GOERLI", addr); let tx_signature = signer.signChangePubkeyWithOnchain(tx); console.log(tx_signature); diff --git a/examples/Javascript/node-example/2_auto_deleveraging.js b/examples/Javascript/node-example/2_auto_deleveraging.js index 25915d77..f76de887 100644 --- a/examples/Javascript/node-example/2_auto_deleveraging.js +++ b/examples/Javascript/node-example/2_auto_deleveraging.js @@ -1,4 +1,4 @@ -const {AutoDeleveragingBuilder,Signer,newAutoDeleveraging,ContractPrice,SpotPriceInfo,RpcClient } = require('./node-dist/zklink-sdk-node'); +const {AutoDeleveragingBuilder,Signer,L1Type,newAutoDeleveraging,ContractPrice,SpotPriceInfo,RpcClient } = require('./node-dist/zklink-sdk-node'); // CommonJS const fetch = require('node-fetch'); const AbortController = require('abort-controller') @@ -22,22 +22,22 @@ async function testAutoDeleveraging() { const contract_price3 = new ContractPrice(2,"1"); const contract_price4 = new ContractPrice(3,"1"); let contract_prices = []; - contract_prices.push(contract_price1.jsonValue()); - contract_prices.push(contract_price2.jsonValue()); - contract_prices.push(contract_price3.jsonValue()); - contract_prices.push(contract_price4.jsonValue()); + contract_prices.push(contract_price1.jsValue()); + contract_prices.push(contract_price2.jsValue()); + contract_prices.push(contract_price3.jsValue()); + contract_prices.push(contract_price4.jsValue()); let margin_prices = []; const margin_price1 = new SpotPriceInfo(17,"1"); const margin_price2 = new SpotPriceInfo(141,"1"); const margin_price3 = new SpotPriceInfo(142,"1"); - margin_prices.push(margin_price1.jsonValue()); - margin_prices.push(margin_price2.jsonValue()); - margin_prices.push(margin_price3.jsonValue()); + margin_prices.push(margin_price1.jsValue()); + margin_prices.push(margin_price2.jsValue()); + margin_prices.push(margin_price3.jsValue()); let tx_builder = new AutoDeleveragingBuilder(5,1,10,contract_prices,margin_prices,3,2,"33535545","188888","199",17); let tx = newAutoDeleveraging(tx_builder); console.log(tx); - const signer = new Signer(private_key); + const signer = new Signer(private_key, L1Type.Eth); let tx_signature = signer.signAutoDeleveraging(tx); console.log(tx_signature); diff --git a/examples/Javascript/node-example/3_update_global_var.js b/examples/Javascript/node-example/3_update_global_var.js index 4514a083..5063a33d 100644 --- a/examples/Javascript/node-example/3_update_global_var.js +++ b/examples/Javascript/node-example/3_update_global_var.js @@ -17,24 +17,24 @@ global.AbortController = AbortController; async function testUpdGlobalVar() { const private_key = "be725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4"; //funding_infos - const funding_infos = [new FundingInfo(1,2,"100000").jsonValue(), - new FundingInfo(1,3,"3333").jsonValue(), - new FundingInfo(2,5,"456").jsonValue(), - new FundingInfo(1,4,"8980808098").jsonValue()]; + const funding_infos = [new FundingInfo(1,2,"100000").jsValue(), + new FundingInfo(1,3,"3333").jsValue(), + new FundingInfo(2,5,"456").jsValue(), + new FundingInfo(1,4,"8980808098").jsValue()]; const parameter_funding = new Parameter(ParameterType.FundingInfos,funding_infos); // contract_info - const contract_info = new ContractInfo(1,"USDC/USDT",5,10).jsonValue(); + const contract_info = new ContractInfo(1,"USDC/USDT",5,10).jsValue(); 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 margin_info = new MarginInfo(2,17,10).jsValue(); 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()); + console.log(tx.jsValue()); } async function main() { diff --git a/examples/Javascript/node-example/4_contract_matching.js b/examples/Javascript/node-example/4_contract_matching.js index 977e11c8..3a55c703 100644 --- a/examples/Javascript/node-example/4_contract_matching.js +++ b/examples/Javascript/node-example/4_contract_matching.js @@ -1,4 +1,4 @@ -const {ContractMatchingBuilder,Signer,newContractMatching,newContract,ContractBuilder, +const {ContractMatchingBuilder,Signer,L1Type,newContractMatching,newContract,ContractBuilder, RpcClient,ContractPrice,SpotPriceInfo } = require('./node-dist/zklink-sdk-node'); // CommonJS const fetch = require('node-fetch'); @@ -23,20 +23,20 @@ async function testContractMatching() { const contract_price3 = new ContractPrice(2,"1"); const contract_price4 = new ContractPrice(3,"1") let contract_prices = []; - contract_prices.push(contract_price1.jsonValue()); - contract_prices.push(contract_price2.jsonValue()); - contract_prices.push(contract_price3.jsonValue()); - contract_prices.push(contract_price4.jsonValue()); + contract_prices.push(contract_price1.jsValue()); + contract_prices.push(contract_price2.jsValue()); + contract_prices.push(contract_price3.jsValue()); + contract_prices.push(contract_price4.jsValue()); let margin_prices = []; const margin_price1 = new SpotPriceInfo(17,"1"); const margin_price2 = new SpotPriceInfo(141,"1"); const margin_price3 = new SpotPriceInfo(142,"1"); - margin_prices.push(margin_price1.jsonValue()); - margin_prices.push(margin_price2.jsonValue()); - margin_prices.push(margin_price3.jsonValue()); + margin_prices.push(margin_price1.jsValue()); + margin_prices.push(margin_price2.jsValue()); + margin_prices.push(margin_price3.jsValue()); - const signer = new Signer(private_key); + const signer = new Signer(private_key, L1Type.Eth); let taker_contract_builder = new ContractBuilder(5,1,1,3,2, "10","5454545445",true,50,22,false); let unsigned_taker_contract = newContract(taker_contract_builder); diff --git a/examples/Javascript/node-example/5_liquidation.js b/examples/Javascript/node-example/5_liquidation.js index b54be0b9..ecb37bfd 100644 --- a/examples/Javascript/node-example/5_liquidation.js +++ b/examples/Javascript/node-example/5_liquidation.js @@ -1,4 +1,4 @@ -const {LiquidationBuilder,Signer,RpcClient,ContractPrice,newLiquidation,SpotPriceInfo } = require('./node-dist/zklink-sdk-node'); +const {LiquidationBuilder,Signer,L1Type,RpcClient,ContractPrice,newLiquidation,SpotPriceInfo } = require('./node-dist/zklink-sdk-node'); // CommonJS const fetch = require('node-fetch'); const AbortController = require('abort-controller') @@ -22,22 +22,22 @@ async function testLiquidation() { const contract_price3 = new ContractPrice(2,"1"); const contract_price4 = new ContractPrice(3,"1"); let contract_prices = []; - contract_prices.push(contract_price1.jsonValue()); - contract_prices.push(contract_price2.jsonValue()); - contract_prices.push(contract_price3.jsonValue()); - contract_prices.push(contract_price4.jsonValue()); + contract_prices.push(contract_price1.jsValue()); + contract_prices.push(contract_price2.jsValue()); + contract_prices.push(contract_price3.jsValue()); + contract_prices.push(contract_price4.jsValue()); let margin_prices = []; const margin_price1 = new SpotPriceInfo(17,"1"); const margin_price2 = new SpotPriceInfo(141,"1"); const margin_price3 = new SpotPriceInfo(142,"1"); - margin_prices.push(margin_price1.jsonValue()); - margin_prices.push(margin_price2.jsonValue()); - margin_prices.push(margin_price3.jsonValue()); + margin_prices.push(margin_price1.jsValue()); + margin_prices.push(margin_price2.jsValue()); + margin_prices.push(margin_price3.jsValue()); let tx_builder = new LiquidationBuilder(5,1,10,contract_prices,margin_prices,3,"8",17); let tx = newLiquidation(tx_builder); console.log(tx); - const signer = new Signer(private_key); + const signer = new Signer(private_key, L1Type.Eth); let tx_signature = signer.signLiquidation(tx); console.log(tx_signature); diff --git a/examples/Javascript/node-example/6_funding.js b/examples/Javascript/node-example/6_funding.js index 15025f95..86c4638c 100644 --- a/examples/Javascript/node-example/6_funding.js +++ b/examples/Javascript/node-example/6_funding.js @@ -1,4 +1,4 @@ -const {FundingBuilder,Signer,newFunding,RpcClient } = require('./node-dist/zklink-sdk-node'); +const {FundingBuilder,Signer,L1Type,newFunding,RpcClient } = require('./node-dist/zklink-sdk-node'); // CommonJS const fetch = require('node-fetch'); const AbortController = require('abort-controller') @@ -17,8 +17,8 @@ global.AbortController = AbortController; async function testFunding() { const private_key = "be725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4"; try { - const signer = new Signer(private_key); - let tx_builder = new FundingBuilder(5,1,2,[3,4,5],"3",17); + const signer = new Signer(private_key, L1Type.Eth); + let tx_builder = new FundingBuilder(5,1,2,[3],"3",17); let tx = newFunding(tx_builder); console.log(tx); let tx_signature = signer.signFunding(tx); diff --git a/examples/Javascript/node-example/8_sign_rawmsg.js b/examples/Javascript/node-example/8_sign_rawmsg.js index 30018d1d..18b3ac91 100644 --- a/examples/Javascript/node-example/8_sign_rawmsg.js +++ b/examples/Javascript/node-example/8_sign_rawmsg.js @@ -1,4 +1,4 @@ -const {Signer } = require('./node-dist/zklink-sdk-node'); +const {Signer, L1Type } = require('./node-dist/zklink-sdk-node'); // CommonJS const fetch = require('node-fetch'); const AbortController = require('abort-controller') @@ -17,7 +17,7 @@ global.AbortController = AbortController; async function testSignRawmsg() { const private_key = "be725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4"; try { - const signer = new Signer(private_key); + const signer = new Signer(private_key, L1Type.Eth); const zklinkSig = signer.signMusig(new TextEncoder().encode("SIGNED MESSAGE")); console.log("pubkey: " + zklinkSig.pubKey()); console.log("signature: " + zklinkSig.signature()); diff --git a/examples/Javascript/node-example/9_order_matching.js b/examples/Javascript/node-example/9_order_matching.js index 3b8f8d1d..d97fb002 100644 --- a/examples/Javascript/node-example/9_order_matching.js +++ b/examples/Javascript/node-example/9_order_matching.js @@ -1,7 +1,7 @@ -const {OrderMatchingBuilder,Signer,ContractPrice,newOrderMatching,Order,RpcClient,SpotPriceInfo } = require('./node-dist/zklink-sdk-node'); +const {OrderMatchingBuilder,Signer,L1Type,ContractPrice,newOrderMatching,Order,RpcClient,SpotPriceInfo } = require('./node-dist/zklink-sdk-node'); // CommonJS const fetch = require('node-fetch'); -const AbortController = require('abort-controller') +const AbortController = require('abort-controller'); // @ts-ignore global.fetch = fetch; @@ -17,24 +17,24 @@ global.AbortController = AbortController; async function testOrderMatching() { const private_key = "be725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4"; try { - const signer = new Signer(private_key); + const signer = new Signer(private_key, L1Type.Eth); const contract_price1 = new ContractPrice(0,"1"); const contract_price2 = new ContractPrice(1,"1"); const contract_price3 = new ContractPrice(2,"1"); const contract_price4 = new ContractPrice(3,"1") let contract_prices = []; - contract_prices.push(contract_price1.jsonValue()); - contract_prices.push(contract_price2.jsonValue()); - contract_prices.push(contract_price3.jsonValue()); - contract_prices.push(contract_price4.jsonValue()); + contract_prices.push(contract_price1.jsValue()); + contract_prices.push(contract_price2.jsValue()); + contract_prices.push(contract_price3.jsValue()); + contract_prices.push(contract_price4.jsValue()); let margin_prices = []; const margin_price1 = new SpotPriceInfo(17,"1"); const margin_price2 = new SpotPriceInfo(141,"1"); const margin_price3 = new SpotPriceInfo(142,"1"); - margin_prices.push(margin_price1.jsonValue()); - margin_prices.push(margin_price2.jsonValue()); - margin_prices.push(margin_price3.jsonValue()); + margin_prices.push(margin_price1.jsValue()); + margin_prices.push(margin_price2.jsValue()); + margin_prices.push(margin_price3.jsValue()); let maker_order = new Order(5,20,1,1,18,17,"10000000000000","10000000000",true,5,3); let maker = signer.createSignedOrder(maker_order); console.log(maker);