diff --git a/Makefile b/Makefile index 76f71ed8..d065e9e9 100644 --- a/Makefile +++ b/Makefile @@ -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} diff --git a/bindings/sdk/src/ffi.udl b/bindings/sdk/src/ffi.udl index f3178066..ac51407a 100644 --- a/bindings/sdk/src/ffi.udl +++ b/bindings/sdk/src/ffi.udl @@ -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); }; diff --git a/examples/Golang/5_order_matching.go b/examples/Golang/5_order_matching.go index ba7b177f..70c9ddcb 100644 --- a/examples/Golang/5_order_matching.go +++ b/examples/Golang/5_order_matching.go @@ -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 @@ -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( @@ -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{ @@ -83,12 +88,13 @@ 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", @@ -96,7 +102,7 @@ func HighLevelOrderMatching() { Params: []json.RawMessage{ []byte(txSignature.Tx), nil, - ethSignature, + submitterSignature2, }, } JsonTx, err := json.Marshal(rpc_req) diff --git a/examples/Golang/6_contract_matching.go b/examples/Golang/6_contract_matching.go new file mode 100644 index 00000000..a9f616f8 --- /dev/null +++ b/examples/Golang/6_contract_matching.go @@ -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() +} diff --git a/examples/Golang/7_auto_deleveraging.go b/examples/Golang/7_auto_deleveraging.go new file mode 100644 index 00000000..09f6742e --- /dev/null +++ b/examples/Golang/7_auto_deleveraging.go @@ -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() +} diff --git a/examples/Golang/8_funding.go b/examples/Golang/8_funding.go new file mode 100644 index 00000000..724117b6 --- /dev/null +++ b/examples/Golang/8_funding.go @@ -0,0 +1,73 @@ +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"` +} + +func HighLevelFunding() { + privateKey := "0xbe725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4" + + var funding_account_ids = make([]sdk.AccountId,3) + funding_account_ids[0] = sdk.AccountId(55) + funding_account_ids[1] = sdk.AccountId(56) + funding_account_ids[2] = sdk.AccountId(57) + + builder := sdk.FundingBuilder{ + sdk.AccountId(1), + sdk.SubAccountId(99), + sdk.Nonce(23), + funding_account_ids, + *big.NewInt(100000000000), + sdk.TokenId(17), + } + tx := sdk.NewFunding(builder) + signer, err := sdk.NewSigner(privateKey) + if err != nil { + return + } + txSignature, err := signer.SignFunding(tx) + if err != nil { + return + } + fmt.Println("tx signature: %s", txSignature) + + rpc_req := RPCTransaction { + Id: 1, + JsonRpc: "2.0", + Method: "sendTransaction", + Params: []json.RawMessage{ + nil, + nil, + nil, + }, + } + JsonTx, err := json.Marshal(rpc_req) + fmt.Println("Funding 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() { + HighLevelFunding() +} diff --git a/examples/Golang/9_liquidation.go b/examples/Golang/9_liquidation.go new file mode 100644 index 00000000..2a9c3154 --- /dev/null +++ b/examples/Golang/9_liquidation.go @@ -0,0 +1,110 @@ +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 HighLevelLiquidation() { + 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.LiquidationBuilder { + sdk.AccountId(1), + sdk.SubAccountId(1), + sdk.Nonce(9), + contract_prices, + margin_prices, + sdk.AccountId(3), + *big.NewInt(5545), + sdk.TokenId(17), + } + + tx := sdk.NewLiquidation(builder) + signer, err := sdk.NewSigner(privateKey) + if err != nil { + return + } + txSignature, err := signer.SignLiquidation(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("Liquidation 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() { + HighLevelLiquidation() +}