Skip to content

Commit

Permalink
fix integration test bug fix(fix #149) (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-zkp authored Dec 22, 2023
1 parent b3307b7 commit 7e3c65c
Show file tree
Hide file tree
Showing 31 changed files with 247 additions and 62 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ lint:
cargo machete
cargo test --all
make test_go
make build_wasm

lint-check:
cargo fmt -- --check
Expand Down
2 changes: 1 addition & 1 deletion binding_tests/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func TestSignWithdraw(t *testing.T) {
fmt.Printf("zklink tx: %s\n", zklinkTx)

// test signer
signer, err := sdk.NewSigner(s, sdk.L1TypeEth);
signer, err := sdk.NewSigner(s, sdk.L1SignerTypeEth{});
assert.Nil(t, err)
tx_signature, err := signer.SignWithdraw(tx, l2SourceTokenSymbol,nil,nil)
assert.Nil(t, err)
Expand Down
8 changes: 7 additions & 1 deletion bindings/sdk/src/ffi.udl
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,15 @@ enum L1Type {
"Starknet",
};

[Enum]
interface L1SignerType {
Eth();
Starknet(string chain_id,string address);
};

interface Signer {
[Throws=SignError]
constructor([ByRef] string private_key, L1Type l1_type);
constructor([ByRef] string private_key, L1SignerType l1_type);
[Throws=SignError]
TxSignature sign_change_pubkey_with_create2data_auth(ChangePubKey tx, Create2Data crate2data);
[Throws=SignError]
Expand Down
1 change: 1 addition & 0 deletions bindings/sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use zklink_sdk_interface::error::SignError;
use zklink_sdk_interface::sign_change_pubkey::{
create_signed_change_pubkey, eth_signature_of_change_pubkey,
};
use zklink_sdk_interface::signer::L1SignerType;
use zklink_sdk_interface::signer::{L1Type, Signer};
use zklink_sdk_interface::ChangePubKeyAuthRequest;

Expand Down
33 changes: 23 additions & 10 deletions bindings/wasm/src/json_rpc_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ pub struct JsonRpcSigner {
inner: InterfaceJsonRpcSigner,
}

//#[wasm_bindgen(constructor)]
#[wasm_bindgen(js_name=newRpcSignerWithProvider)]
pub fn new_with_provider(provider: Provider) -> Result<JsonRpcSigner, JsValue> {
let inner = InterfaceJsonRpcSigner::new(JsonRpcProvider::Provider(provider), None, None)?;
Ok(JsonRpcSigner { inner })
}

//#[wasm_bindgen(constructor)]
#[wasm_bindgen(js_name=newRpcSignerWithSigner)]
pub fn new_with_signer(
signer: Signer,
Expand All @@ -54,15 +52,18 @@ impl JsonRpcSigner {
Ok(self.inner.init_zklink_signer(signature).await?)
}

#[wasm_bindgen(js_name = signTransfer)]
pub async fn sign_transfer(
&self,
tx: Transfer,
token_symbol: &str,
) -> Result<JsValue, JsValue> {
#[wasm_bindgen(js_name = pubkeyHash)]
pub fn pub_key_hash(&self) -> String {
self.inner.pub_key_hash()
}

#[wasm_bindgen(js_name=signChangePubkeyWithOnchain)]
pub fn sign_change_pubkey_with_onchain(&self, tx: ChangePubKey) -> Result<JsValue, JsValue> {
let inner_tx = tx.json_value()?;
let transfer: TxTransfer = serde_wasm_bindgen::from_value(inner_tx)?;
let signature = self.inner.sign_transfer(transfer, token_symbol).await?;
let change_pubkey: TxChangePubKey = serde_wasm_bindgen::from_value(inner_tx)?;
let signature = self
.inner
.sign_change_pubkey_with_onchain_auth_data(change_pubkey)?;
Ok(serde_wasm_bindgen::to_value(&signature)?)
}

Expand Down Expand Up @@ -96,6 +97,18 @@ impl JsonRpcSigner {
Ok(serde_wasm_bindgen::to_value(&signature)?)
}

#[wasm_bindgen(js_name = signTransfer)]
pub async fn sign_transfer(
&self,
tx: Transfer,
token_symbol: &str,
) -> Result<JsValue, JsValue> {
let inner_tx = tx.json_value()?;
let transfer: TxTransfer = serde_wasm_bindgen::from_value(inner_tx)?;
let signature = self.inner.sign_transfer(transfer, token_symbol).await?;
Ok(serde_wasm_bindgen::to_value(&signature)?)
}

#[wasm_bindgen(js_name=createSignedOrder)]
pub fn create_signed_order(&self, order: Order) -> Result<JsValue, JsValue> {
let inner_order = order.json_value()?;
Expand Down
27 changes: 15 additions & 12 deletions bindings/wasm/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::tx_types::transfer::Transfer;
use crate::tx_types::withdraw::Withdraw;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use zklink_sdk_interface::signer::L1Type as InnerL1Type;
use zklink_sdk_interface::signer::L1SignerType;
use zklink_sdk_interface::signer::Signer as InterfaceSigner;
use zklink_sdk_types::tx_type::change_pubkey::ChangePubKey as TxChangePubKey;
use zklink_sdk_types::tx_type::change_pubkey::Create2Data as ChangePubKeyCreate2Data;
Expand Down Expand Up @@ -38,20 +38,23 @@ pub enum L1Type {
Starknet,
}

impl From<L1Type> for InnerL1Type {
fn from(value: L1Type) -> InnerL1Type {
match value {
L1Type::Eth => InnerL1Type::Eth,
L1Type::Starknet => InnerL1Type::Starknet,
}
}
}

#[wasm_bindgen]
impl Signer {
#[wasm_bindgen(constructor)]
pub fn new(private_key: &str, l1_type: L1Type) -> Result<Signer, JsValue> {
let inner = InterfaceSigner::new(private_key, l1_type.into())?;
pub fn new(
private_key: &str,
l1_type: L1Type,
starknet_chain_id: Option<String>,
starknet_addr: Option<String>,
) -> Result<Signer, JsValue> {
let l1_signer_type = match l1_type {
L1Type::Eth => L1SignerType::Eth,
L1Type::Starknet => L1SignerType::Starknet {
chain_id: starknet_chain_id.unwrap(),
address: starknet_addr.unwrap(),
},
};
let inner = InterfaceSigner::new(private_key, l1_signer_type)?;
Ok(Signer { inner })
}

Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/10_update_global_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func HighLevelUpdateGlobalVar() {
}

tx := sdk.NewUpdateGlobalVar(builder)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
signer, err := sdk.NewSigner(privateKey, sdk.L1SignerTypeEth{})
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/1_change_pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func HighLevelChangePubkeyEcdsa() {
timeStamp,
}
tx := sdk.NewChangePubKey(builder)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
signer, err := sdk.NewSigner(privateKey, sdk.L1SignerTypeEth{})
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/2_withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func HighLevelWithdraw() {
Timestamp: timestamp,
}
tx := sdk.NewWithdraw(builder)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
signer, err := sdk.NewSigner(privateKey, sdk.L1SignerTypeEth{})
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/3_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func HighLevelTransfer() {
}
tokenSymbol := "DAI"
tx := sdk.NewTransfer(builder)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
signer, err := sdk.NewSigner(privateKey, sdk.L1SignerTypeEth{})
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/4_forced_exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func HighLevelForcedExit() {
Timestamp: sdk.TimeStamp(1693472232),
}
tx := sdk.NewForcedExit(builder)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
signer, err := sdk.NewSigner(privateKey, sdk.L1SignerTypeEth{})
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/5_order_matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func HighLevelOrderMatching() {
*big.NewInt(5479779),
}
tx := sdk.NewOrderMatching(builder)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
signer, err := sdk.NewSigner(privateKey, sdk.L1SignerTypeEth{})
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/6_contract_matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func HighLevelContractMatching() {
}

tx := sdk.NewContractMatching(builder)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
signer, err := sdk.NewSigner(privateKey, sdk.L1SignerTypeEth{})
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/7_auto_deleveraging.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func HighLevelAutoDeleveraging() {
sdk.TokenId(18),
}
tx := sdk.NewAutoDeleveraging(builder)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
signer, err := sdk.NewSigner(privateKey, sdk.L1SignerTypeEth{})
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/8_funding.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func HighLevelFunding() {
sdk.TokenId(17),
}
tx := sdk.NewFunding(builder)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
signer, err := sdk.NewSigner(privateKey, sdk.L1SignerTypeEth{})
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Golang/9_liquidation.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func HighLevelLiquidation() {
}

tx := sdk.NewLiquidation(builder)
signer, err := sdk.NewSigner(privateKey, sdk.L1TypeEth)
signer, err := sdk.NewSigner(privateKey, sdk.L1SignerTypeEth{})
if err != nil {
return
}
Expand Down
5 changes: 2 additions & 3 deletions examples/Javascript/js-example/1_change_pubkey.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import init, * as wasm from "./web-dist/zklink-sdk-web.js";
import { ethers } from "https://cdn-cors.ethers.io/lib/ethers-5.5.4.esm.min.js";

async function testEcdsaAuth() {
await init();
Expand All @@ -19,7 +18,7 @@ async function testEcdsaAuth() {
const provider = window.bitkeep && window.bitkeep.ethereum;
await provider.request({ method: 'eth_requestAccounts' });

const signer = new wasm.newW(provider);
const signer = new wasm.newRpcSignerWithProvider(provider);

// use cached ethereum signature to init zklink signer
//const signature = "0x1111111111";
Expand Down Expand Up @@ -56,7 +55,7 @@ async function testCreate2() {
let tx = wasm.newChangePubkey(tx_builder);
const provider = window.bitkeep && window.bitkeep.ethereum;
await provider.request({ method: 'eth_requestAccounts' });
const signer = new wasm.JsonRpcSigner(provider);
const signer = new wasm.newRpcSignerWithProvider(provider);
await signer.initZklinkSigner(null);
console.log(signer);

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 @@ -14,7 +14,7 @@ async function testEvmChains() {
let transfer = wasm.newTransfer(tx_builder);
const provider = window.bitkeep && window.bitkeep.ethereum;
await provider.request({ method: 'eth_requestAccounts' });
const signer = new wasm.JsonRpcSigner(provider);
const signer = new wasm.newRpcSignerWithProvider(provider);
await signer.initZklinkSigner(null);
console.log(signer);

Expand Down
2 changes: 1 addition & 1 deletion examples/Javascript/js-example/3_order_matching.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ async function main() {
try {
const provider = window.bitkeep && window.bitkeep.ethereum;
await provider.request({ method: 'eth_requestAccounts' });
const signer = new wasm.JsonRpcSigner(provider);
const signer = new wasm.newRpcSignerWithProvider(provider);
await signer.initZklinkSigner(null);
console.log(signer);

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 @@ -10,7 +10,7 @@ async function main() {
let withdraw = wasm.newWithdraw(tx_builder);
const provider = window.bitkeep && window.bitkeep.ethereum;
await provider.request({ method: 'eth_requestAccounts' });
const signer = new wasm.JsonRpcSigner(provider);
const signer = new wasm.newRpcSignerWithProvider(provider);
await signer.initZklinkSigner(null);
console.log(signer);

Expand Down
2 changes: 1 addition & 1 deletion examples/Javascript/js-example/5_forced_exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async function main() {
let forced_exit = wasm.newForcedExit(tx_builder);
const provider = window.bitkeep && window.bitkeep.ethereum;
await provider.request({ method: 'eth_requestAccounts' });
const signer = new wasm.JsonRpcSigner(provider);
const signer = new wasm.newRpcSignerWithProvider(provider);
await signer.initZklinkSigner(null);
console.log(signer);
let signature = signer.signForcedExit(forced_exit)
Expand Down
46 changes: 46 additions & 0 deletions examples/Javascript/js-example/7_deposit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { connect,disconnect } from "starknetkit";
import Contract from "starknet";

async function starknetDeposit() {
await disconnect();
const starknet = await connect();

if (!starknet) {
throw Error("User rejected wallet selection or silent connect found nothing")
}

// (optional) connect the wallet
await starknet.enable();
if (starknet.isConnected === false) {
console.log("not connected");
return;
}
console.log(starknet);

let provider = starknet.provider;
let token_address = "0x05dea2a666a1542d6b39bb6f6cd914c6c1383e47bd183141b1ded0f341aa733f";
const zklinkAddress = "0x0346dd9e1bd173fc59cda8f2b9bb5dc10d7ecb8ca838ed08847ff3acbef21484";
//1.approve
const erc20_abi = await starknet.provider.getClassAt(token_address);
const erc20_contract = new Contract(erc20_abi.abi, token_address, provider);
erc20_contract.connect(starknet.account);
const call_data_approve = await erc20_contract.populate("approve",[zklinkAddress,100000000]);
const res_approve = await erc20_contract.approve(call_data_approve.calldata);
console.log(res_approve.transaction_hash);
await provider.waitForTransaction(res_approve.transaction_hash);
//let bal = await erc20_contract.balanceOf(starknet.selectedAddress);
//console.log(bal);


//2.deposit
const { abi } = await starknet.provider.getClassAt(zklinkAddress);
const contract = new Contract(abi, zklinkAddress, provider);
// Connect account with the contract
contract.connect(starknet.account);
const res = await contract.depositERC20(token_address,1,starknet.selectedAddress,0,false);
console.log(res.transaction_hash);
await provider.waitForTransaction(res.transaction_hash);

}

await starknetDeposit();
Loading

0 comments on commit 7e3c65c

Please sign in to comment.