Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add perp examples for golang sdk(fix #83) #90

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ run_example_js_%: ${ROOT_DIR}/examples/Javascript/node-example/%.js
@cd ${ROOT_DIR}/examples/Javascript/node-example && \
node $< \

GO_FILES = 1_change_pubkey 2_withdraw 3_transfer 4_forced_exit 5_order_matching
GO_FILES = 1_change_pubkey 2_withdraw 3_transfer 4_forced_exit 5_order_matching 6_contract_matching 7_auto_deleveraging 8_funding 9_liquidation
RUN_GO_EXAMPLES = $(patsubst %, run_example_go_%, $(GO_FILES))
run_example_go: ${RUN_GO_EXAMPLES}

Expand Down
9 changes: 9 additions & 0 deletions bindings/sdk/src/ffi.udl
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,15 @@ interface Signer {
TxSignature sign_forced_exit(ForcedExit tx);
[Throws=SignError]
TxSignature sign_order_matching(OrderMatching tx);
[Throws=SignError]
TxSignature sign_contract_matching(ContractMatching tx);
[Throws=SignError]
TxSignature sign_funding(Funding tx);
[Throws=SignError]
TxSignature sign_liquidation(Liquidation tx);
[Throws=SignError]
TxSignature sign_auto_deleveraging(AutoDeleveraging tx);

[Throws=SignError]
ZkLinkSignature submitter_signature([ByRef] ZkLinkTx zklink_tx);
};
Expand Down
28 changes: 17 additions & 11 deletions examples/Golang/5_order_matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ type RPCTransaction struct {
Params []json.RawMessage `json:"params"`
}

type SubmiterSignature struct {
PubKey string `json:"pubKey"`
Signature string `json:"signature"`
}

func HighLevelOrderMatching() {
privateKey := "0xbe725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4"
// create zklink signer
Expand All @@ -34,13 +39,13 @@ func HighLevelOrderMatching() {
*big.NewInt(323289),
*big.NewInt(135),
true,
true,
2,
5,
nil,
)
taker, err = sdk.CreateSignedOrder(
taker, err = taker.CreateSignedOrder(
zklinkSigner,
taker,
)

maker := sdk.NewOrder(
Expand All @@ -53,13 +58,13 @@ func HighLevelOrderMatching() {
*big.NewInt(323355),
*big.NewInt(135),
false,
true,
2,
5,
nil,
)
maker, err = sdk.CreateSignedOrder(
maker, err = maker.CreateSignedOrder(
zklinkSigner,
maker,
)

builder := sdk.OrderMatchingBuilder{
Expand All @@ -83,20 +88,21 @@ func HighLevelOrderMatching() {
}
fmt.Println("tx signature: %s", txSignature)

// get eth signature
var ethSignature []byte = nil;
if txSignature.Layer1Signature != nil {
ethSignature = []byte(*txSignature.Layer1Signature)
}

// get submitter signature
zklinkTx := tx.ToZklinkTx()
submitterSignature, err := signer.SubmitterSignature(zklinkTx)
submitterSignature2, err := json.Marshal(SubmiterSignature {
PubKey: submitterSignature.PubKey,
Signature: submitterSignature.Signature,
})
rpc_req := RPCTransaction {
Id: 1,
JsonRpc: "2.0",
Method: "sendTransaction",
Params: []json.RawMessage{
[]byte(txSignature.Tx),
nil,
ethSignature,
submitterSignature2,
},
}
JsonTx, err := json.Marshal(rpc_req)
Expand Down
133 changes: 133 additions & 0 deletions examples/Golang/6_contract_matching.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package main

import (
"net/http"
"math/big"
"encoding/json"
"fmt"
"bytes"
"io/ioutil"
sdk "github.com/zkLinkProtocol/zklink_sdk/go_example/generated/uniffi/zklink_sdk"
)

type RPCTransaction struct {
Id int64 `json:"id"`
JsonRpc string `json:"jsonrpc"`
Method string `json:"method"`
Params []json.RawMessage `json:"params"`
}

type SubmiterSignature struct {
PubKey string `json:"pubKey"`
Signature string `json:"signature"`
}

func HighLevelContractMatching() {
privateKey := "0xbe725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4"

taker_contract_builder := sdk.ContractBuilder {
sdk.AccountId(1),
sdk.SubAccountId(1),
sdk.SlotId(2),
sdk.Nonce(10),
sdk.PairId(1),
*big.NewInt(45454),
*big.NewInt(113),
true,
5,
3,
false,
}

taker_contract := sdk.NewContract(taker_contract_builder)

maker_contract1_builder := sdk.ContractBuilder {
sdk.AccountId(3),
sdk.SubAccountId(1),
sdk.SlotId(2),
sdk.Nonce(6),
sdk.PairId(1),
*big.NewInt(43434),
*big.NewInt(6767),
true,
1,
2,
false,
}

maker_contract2_builder := sdk.ContractBuilder {
sdk.AccountId(5),
sdk.SubAccountId(1),
sdk.SlotId(2),
sdk.Nonce(100),
sdk.PairId(1),
*big.NewInt(45656),
*big.NewInt(343),
true,
8,
20,
true,
}

maker_contract1 := sdk.NewContract(maker_contract1_builder)
maker_contract2 := sdk.NewContract(maker_contract2_builder)
var makers []*sdk.Contract
makers = make([]*sdk.Contract,2)
makers[0] = maker_contract1
makers[1] = maker_contract2

builder := sdk.ContractMatchingBuilder {
sdk.AccountId(1),
sdk.SubAccountId(1),
taker_contract,
makers,
*big.NewInt(5545),
sdk.TokenId(17),
}

tx := sdk.NewContractMatching(builder)
signer, err := sdk.NewSigner(privateKey)
if err != nil {
return
}
txSignature, err := signer.SignContractMatching(tx)
if err != nil {
return
}
fmt.Println("tx signature: %s", txSignature)

// get submitter signature
zklinkTx := tx.ToZklinkTx()
submitterSignature, err := signer.SubmitterSignature(zklinkTx)
submitterSignature2, err := json.Marshal(SubmiterSignature {
PubKey: submitterSignature.PubKey,
Signature: submitterSignature.Signature,
})

rpc_req := RPCTransaction {
Id: 1,
JsonRpc: "2.0",
Method: "sendTransaction",
Params: []json.RawMessage{
[]byte(txSignature.Tx),
nil,
submitterSignature2,
},
}
JsonTx, err := json.Marshal(rpc_req)
fmt.Println("ContractMatching rpc request:", string(JsonTx))
// get the testnet url or main net url
zklinkUrl := sdk.ZklinkTestNetUrl()
response, err := http.Post(zklinkUrl, "application/json", bytes.NewBuffer(JsonTx))
if err != nil {
fmt.Println(err)
return
}
defer response.Body.Close()
body, _ := ioutil.ReadAll(response.Body)
fmt.Println(string(body))
}

func main() {
HighLevelContractMatching()
}
111 changes: 111 additions & 0 deletions examples/Golang/7_auto_deleveraging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main

import (
"net/http"
"math/big"
"encoding/json"
"fmt"
"bytes"
"io/ioutil"
sdk "github.com/zkLinkProtocol/zklink_sdk/go_example/generated/uniffi/zklink_sdk"
)

type RPCTransaction struct {
Id int64 `json:"id"`
JsonRpc string `json:"jsonrpc"`
Method string `json:"method"`
Params []json.RawMessage `json:"params"`
}

type SubmiterSignature struct {
PubKey string `json:"pubKey"`
Signature string `json:"signature"`
}

func HighLevelAutoDeleveraging() {
privateKey := "0xbe725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4"
contract_price1 := sdk.ContractPrice{
sdk.PairId(1),
*big.NewInt(656566),
}

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

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

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

var margin_prices = make([]sdk.SpotPriceInfo,2)
margin_prices[0] = margin_price1
margin_prices[1] = margin_price2

builder := sdk.AutoDeleveragingBuilder{
sdk.AccountId(3),
sdk.SubAccountId(1),
sdk.Nonce(9),
contract_prices,
margin_prices,
sdk.AccountId(7),
sdk.PairId(3),
*big.NewInt(1000),
*big.NewInt(100000000000),
*big.NewInt(10000),
sdk.TokenId(18),
}
tx := sdk.NewAutoDeleveraging(builder)
signer, err := sdk.NewSigner(privateKey)
if err != nil {
return
}
txSignature, err := signer.SignAutoDeleveraging(tx)
if err != nil {
return
}
fmt.Println("tx signature: %s", txSignature)

// get submitter signature
zklinkTx := tx.ToZklinkTx()
submitterSignature, err := signer.SubmitterSignature(zklinkTx)
submitterSignature2, err := json.Marshal(SubmiterSignature {
PubKey: submitterSignature.PubKey,
Signature: submitterSignature.Signature,
})
rpc_req := RPCTransaction {
Id: 1,
JsonRpc: "2.0",
Method: "sendTransaction",
Params: []json.RawMessage{
[]byte(txSignature.Tx),
nil,
submitterSignature2,
},
}
JsonTx, err := json.Marshal(rpc_req)
fmt.Println("AutoDeleveraging rpc request:", string(JsonTx))
// get the testnet url or main net url
zklinkUrl := sdk.ZklinkTestNetUrl()
response, err := http.Post(zklinkUrl, "application/json", bytes.NewBuffer(JsonTx))
if err != nil {
fmt.Println(err)
return
}
defer response.Body.Close()
body, _ := ioutil.ReadAll(response.Body)
fmt.Println(string(body))
}

func main() {
HighLevelAutoDeleveraging()
}
Loading
Loading