Skip to content

Commit

Permalink
Fix javascript example (#278)
Browse files Browse the repository at this point in the history
* update node example

* make exported wasm struct deserializable
  • Loading branch information
zksemi authored May 10, 2024
1 parent 400f201 commit 6f74f00
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 59 deletions.
2 changes: 2 additions & 0 deletions bindings/wasm/src/tx_types/change_pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ impl Create2Data {
hex::encode(salt_bytes)
}

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

#[wasm_bindgen]
impl ChangePubKey {
#[wasm_bindgen(js_name=jsValue)]
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/src/tx_types/contract/contract_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/src/tx_types/contract/funding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl FundingInfo {
})
}

#[wasm_bindgen(js_name=jsonValue)]
#[wasm_bindgen(js_name=jsValue)]
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
Expand Down
4 changes: 2 additions & 2 deletions bindings/wasm/src/tx_types/contract/prices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl ContractPrice {
})
}

#[wasm_bindgen(js_name=jsonValue)]
#[wasm_bindgen(js_name=jsValue)]
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
Expand All @@ -47,7 +47,7 @@ impl SpotPriceInfo {
})
}

#[wasm_bindgen(js_name=jsonValue)]
#[wasm_bindgen(js_name=jsValue)]
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
Expand Down
6 changes: 3 additions & 3 deletions bindings/wasm/src/tx_types/contract/update_global_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
Expand Down Expand Up @@ -107,7 +107,7 @@ impl MarginInfo {
}
}

#[wasm_bindgen(js_name=jsonValue)]
#[wasm_bindgen(js_name=jsValue)]
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self)?)
}
Expand Down Expand Up @@ -139,7 +139,7 @@ impl ContractInfo {
}
}

#[wasm_bindgen(js_name=jsonValue)]
#[wasm_bindgen(js_name=jsValue)]
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self)?)
}
Expand Down
1 change: 1 addition & 0 deletions bindings/wasm/src/tx_types/forced_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct ForcedExit {

#[wasm_bindgen]
impl ForcedExit {
#[wasm_bindgen(js_name=jsValue)]
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
Expand Down
2 changes: 2 additions & 0 deletions bindings/wasm/src/tx_types/order_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ impl Order {
Ok(order)
}

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

#[wasm_bindgen]
impl OrderMatching {
#[wasm_bindgen(js_name=jsValue)]
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
Expand Down
1 change: 1 addition & 0 deletions bindings/wasm/src/tx_types/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Transfer {

#[wasm_bindgen]
impl Transfer {
#[wasm_bindgen(js_name=jsValue)]
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
Expand Down
1 change: 1 addition & 0 deletions bindings/wasm/src/tx_types/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Withdraw {

#[wasm_bindgen]
impl Withdraw {
#[wasm_bindgen(js_name=jsValue)]
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
Expand Down
1 change: 1 addition & 0 deletions bindings/wasm/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl EthTxOption {
Ok(EthTxOption { inner })
}

#[wasm_bindgen(js_name=jsValue)]
pub fn json_value(&self) -> Result<JsValue, JsValue> {
Ok(serde_wasm_bindgen::to_value(&self.inner)?)
}
Expand Down
6 changes: 3 additions & 3 deletions examples/Javascript/node-example/1_change_pubkey.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 {
Expand All @@ -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);

Expand Down
18 changes: 9 additions & 9 deletions examples/Javascript/node-example/2_auto_deleveraging.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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);

Expand Down
14 changes: 7 additions & 7 deletions examples/Javascript/node-example/3_update_global_var.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
18 changes: 9 additions & 9 deletions examples/Javascript/node-example/4_contract_matching.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions examples/Javascript/node-example/5_liquidation.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions examples/Javascript/node-example/6_funding.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions examples/Javascript/node-example/8_sign_rawmsg.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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());
Expand Down
20 changes: 10 additions & 10 deletions examples/Javascript/node-example/9_order_matching.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 6f74f00

Please sign in to comment.