Skip to content

Commit

Permalink
Merge pull request #104 from zkLinkProtocol/add-perp-js-examples
Browse files Browse the repository at this point in the history
add perp js examples
  • Loading branch information
zkbenny authored Dec 1, 2023
2 parents d601595 + 87a1957 commit 1ddafb7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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}
56 changes: 56 additions & 0 deletions examples/Javascript/node-example/5_liquidation.js
Original file line number Diff line number Diff line change
@@ -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();
45 changes: 45 additions & 0 deletions examples/Javascript/node-example/6_funding.js
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 1ddafb7

Please sign in to comment.