Skip to content

Commit

Permalink
fix examples for contractMaching add oracle prices(fix #147) (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-zkp authored Dec 22, 2023
1 parent c1d4819 commit 10ea3aa
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
35 changes: 27 additions & 8 deletions examples/Golang/6_contract_matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,29 +91,48 @@ func HighLevelContractMatching() {
makers[1] = maker_contract2

contract_price1 := sdk.ContractPrice{
sdk.PairId(1),
*big.NewInt(656566),
sdk.PairId(0),
*big.NewInt(1),
}

contract_price2 := sdk.ContractPrice{
sdk.PairId(1),
*big.NewInt(1),
}

contract_price3 := sdk.ContractPrice{
sdk.PairId(2),
*big.NewInt(1),
}

contract_price4 := sdk.ContractPrice{
sdk.PairId(3),
*big.NewInt(52552131),
*big.NewInt(1),
}
var contract_prices = make([]sdk.ContractPrice,2)

var contract_prices = make([]sdk.ContractPrice,4)
contract_prices[0] = contract_price1
contract_prices[1] = contract_price2
contract_prices[2] = contract_price3
contract_prices[3] = contract_price4

margin_price1 := sdk.SpotPriceInfo {
sdk.TokenId(17),
*big.NewInt(3236653653635635),
*big.NewInt(1),
}
margin_price2 := sdk.SpotPriceInfo {
sdk.TokenId(18),
*big.NewInt(549574875297),
sdk.TokenId(141),
*big.NewInt(1),
}

margin_price3 := sdk.SpotPriceInfo {
sdk.TokenId(142),
*big.NewInt(1),
}
var margin_prices = make([]sdk.SpotPriceInfo,2)
var margin_prices = make([]sdk.SpotPriceInfo,3)
margin_prices[0] = margin_price1
margin_prices[1] = margin_price2
margin_prices[2] = margin_price3

builder := sdk.ContractMatchingBuilder {
sdk.AccountId(10),
Expand Down
2 changes: 1 addition & 1 deletion examples/Javascript/js-example/2_transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function testEvmChains() {
let submitter_signature = signer.submitterSignature(signature.tx);
console.log(submitter_signature);
let rpc_client = new wasm.RpcClient("testnet");
let l1_signature = new wasm.TxLayer1Signature(wasm.L1SignatureType.Eth,signature.eth_signature);
let l1_signature = new wasm.TxLayer1Signature(wasm.L1SignatureType.Eth,signature.layer1_signature.signature);
let tx_hash = await rpc_client.sendTransaction(signature.tx,l1_signature,submitter_signature);
console.log(tx_hash);

Expand Down
2 changes: 1 addition & 1 deletion examples/Javascript/js-example/4_withdraw.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function main() {
let submitter_signature = signer.submitterSignature(signature.tx);
console.log(submitter_signature);
let rpc_client = new wasm.RpcClient("testnet");
let l1_signature = new wasm.TxLayer1Signature(wasm.L1SignatureType.Eth,signature.eth_signature);
let l1_signature = new wasm.TxLayer1Signature(wasm.L1SignatureType.Eth,signature.layer1_signature.signature);
let tx_hash = await rpc_client.sendTransaction(signature.tx,l1_signature,submitter_signature);
console.log(tx_hash);

Expand Down
24 changes: 22 additions & 2 deletions examples/Javascript/node-example/4_contract_matching.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {ContractMatchingBuilder,Signer,newContractMatching,newContract,ContractBuilder,RpcClient } = require('./node-dist/zklink-sdk-node');
const {ContractMatchingBuilder,Signer,newContractMatching,newContract,ContractBuilder,
RpcClient,ContractPrice,SpotPriceInfo } = require('./node-dist/zklink-sdk-node');
// CommonJS
const fetch = require('node-fetch');
const AbortController = require('abort-controller')
Expand All @@ -17,6 +18,24 @@ global.AbortController = AbortController;
async function testContractMatching() {
const private_key = "be725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4";
try {
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());

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());

const signer = new Signer(private_key);
let taker_contract_builder = new ContractBuilder(5,1,1,3,2,
"10","5454545445",true,50,22,false);
Expand All @@ -36,7 +55,8 @@ async function testContractMatching() {
let maker_contract2 = signer.createSignedContract(unsigned_maker_contract2);
console.log(maker_contract2);

let tx_builder = new ContractMatchingBuilder(5,1,taker_contract,[maker_contract1,maker_contract2],"1",17);
let tx_builder = new ContractMatchingBuilder(5,1,taker_contract,
[maker_contract1,maker_contract2],"1",17,contract_prices,margin_prices);
let tx = newContractMatching(tx_builder);
console.log(tx);
let tx_signature = signer.signContractMatching(tx);
Expand Down

0 comments on commit 10ea3aa

Please sign in to comment.