From 87a1957913890cc03919a68b565d41e5dc0daaa2 Mon Sep 17 00:00:00 2001 From: nickwest Date: Thu, 30 Nov 2023 04:03:59 -0800 Subject: [PATCH] add perp js examples --- Makefile | 2 +- .../Javascript/node-example/5_liquidation.js | 56 +++++++++++++++++++ examples/Javascript/node-example/6_funding.js | 45 +++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 examples/Javascript/node-example/5_liquidation.js create mode 100644 examples/Javascript/node-example/6_funding.js diff --git a/Makefile b/Makefile index d065e9e9..14b7f3ca 100644 --- a/Makefile +++ b/Makefile @@ -104,6 +104,6 @@ GO_FILES = 1_change_pubkey 2_withdraw 3_transfer 4_forced_exit 5_order_matching RUN_GO_EXAMPLES = $(patsubst %, run_example_go_%, $(GO_FILES)) run_example_go: ${RUN_GO_EXAMPLES} -JS_FILES = 1_change_pubkey 2_auto_deleveraging 3_update_global_var 4_contract_matching +JS_FILES = 1_change_pubkey 2_auto_deleveraging 3_update_global_var 4_contract_matching 5_liquidation 6_funding RUN_JS_EXAMPLES = $(patsubst %, run_example_js_%, $(JS_FILES)) run_example_js: ${RUN_JS_EXAMPLES} \ No newline at end of file diff --git a/examples/Javascript/node-example/5_liquidation.js b/examples/Javascript/node-example/5_liquidation.js new file mode 100644 index 00000000..1f50008c --- /dev/null +++ b/examples/Javascript/node-example/5_liquidation.js @@ -0,0 +1,56 @@ +const {LiquidationBuilder,Signer,RpcClient,ContractPrice,newLiquidation,SpotPriceInfo } = require('./node-dist/zklink-sdk-node'); +// CommonJS +const fetch = require('node-fetch'); +const AbortController = require('abort-controller') + +// @ts-ignore +global.fetch = fetch; +// @ts-ignore +global.Headers = fetch.Headers; +// @ts-ignore +global.Request = fetch.Request; +// @ts-ignore +global.Response = fetch.Response; +// @ts-ignore +global.AbortController = AbortController; + +async function testLiquidation() { + const private_key = "be725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4"; + try { + const contract_price1 = new ContractPrice(1,"10000000000000"); + const contract_price2 = new ContractPrice(1,"2000000000000"); + let contract_prices = []; + contract_prices.push(contract_price1.jsonValue()); + contract_prices.push(contract_price2.jsonValue()); + + let margin_prices = []; + const margin_price1 = new SpotPriceInfo(17,"3236653653635635"); + const margin_price2 = new SpotPriceInfo(18,"549574875297"); + margin_prices.push(margin_price1.jsonValue()); + margin_prices.push(margin_price2.jsonValue()); + let tx_builder = new LiquidationBuilder(5,1,10,contract_prices,margin_prices,3,"188888",17); + let tx = newLiquidation(tx_builder); + console.log(tx); + const signer = new Signer(private_key); + let tx_signature = signer.signLiquidation(tx); + console.log(tx_signature); + + let submitter_signature = signer.submitterSignature(tx_signature.tx); + console.log(submitter_signature); + //send to zklink + let rpc_client = new RpcClient("testnet"); + let tx_hash = await rpc_client.sendTransaction(tx_signature.tx,null,submitter_signature); + console.log(tx_hash); + + } catch (error) { + console.error(error); + } + +} + +async function main() { + console.log(global); + await testLiquidation(); +} + +main(); diff --git a/examples/Javascript/node-example/6_funding.js b/examples/Javascript/node-example/6_funding.js new file mode 100644 index 00000000..9f5eb0a5 --- /dev/null +++ b/examples/Javascript/node-example/6_funding.js @@ -0,0 +1,45 @@ +const {FundingBuilder,Signer,newFunding,RpcClient } = require('./node-dist/zklink-sdk-node'); +// CommonJS +const fetch = require('node-fetch'); +const AbortController = require('abort-controller') + +// @ts-ignore +global.fetch = fetch; +// @ts-ignore +global.Headers = fetch.Headers; +// @ts-ignore +global.Request = fetch.Request; +// @ts-ignore +global.Response = fetch.Response; +// @ts-ignore +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],"34343",17); + let tx = newFunding(tx_builder); + console.log(tx); + let tx_signature = signer.signFunding(tx); + console.log(tx_signature); + + let submitter_signature = signer.submitterSignature(tx_signature.tx); + console.log(submitter_signature); + //send to zklink + let rpc_client = new RpcClient("testnet"); + let tx_hash = await rpc_client.sendTransaction(tx_signature.tx,null,submitter_signature); + console.log(tx_hash); + + } catch (error) { + console.error(error); + } + +} + +async function main() { + console.log(global); + await testFunding(); +} + +main();