Skip to content

Commit

Permalink
impl all rpc method in wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-zkp committed Oct 8, 2023
1 parent 84561f8 commit 8cdc06c
Show file tree
Hide file tree
Showing 10 changed files with 535 additions and 142 deletions.
55 changes: 0 additions & 55 deletions bindings/wasm/src/error.rs

This file was deleted.

180 changes: 175 additions & 5 deletions bindings/wasm/src/provider.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
use crate::rpc::{AccountQueryParam, SignedTransaction, TxL1Signature};
use crate::rpc::{AccountQueryParam, L2TxType, SignedTransaction, TxL1Signature};
use std::collections::HashMap;
use std::str::FromStr;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use zklink_sdk_provider::error::RpcError;
use zklink_sdk_provider::network::Network;
use zklink_sdk_provider::response::{AccountSnapshotResp, TokenResp};
use zklink_sdk_provider::response::{
AccountInfoResp, AccountSnapshotResp, BlockNumberResp, BlockOnChainResp, BlockResp, ChainResp,
FastWithdrawTxResp, ForwardTxResp, Page, SubAccountBalances, SubAccountOrders, TokenResp,
TxHashOrDetailResp, TxResp, ZkLinkTxHistory,
};
use zklink_sdk_provider::rpc_wasm::WasmRpcClient;
use zklink_sdk_signers::zklink_signer::ZkLinkSignature;
use zklink_sdk_types::basic_types::bigunit_wrapper::BigUintSerdeWrapper;
use zklink_sdk_types::basic_types::tx_hash::TxHash;
use zklink_sdk_types::basic_types::{BlockNumber, SubAccountId, TokenId};
use zklink_sdk_types::basic_types::{AccountId, BlockNumber, ChainId, SubAccountId, TokenId};
use zklink_sdk_types::prelude::ZkLinkAddress;

#[wasm_bindgen]
pub struct Provider {
Expand All @@ -26,13 +33,13 @@ impl Provider {
}
}

#[wasm_bindgen]
#[wasm_bindgen(js_name=getSupportTokens)]
pub async fn tokens(&self) -> Result<JsValue, JsValue> {
let result: HashMap<TokenId, TokenResp> = self.client.tokens().await?;
Ok(serde_wasm_bindgen::to_value(&result)?)
}

#[wasm_bindgen(js_name=accountQuery)]
#[wasm_bindgen(js_name=getAccountSnapshot)]
pub async fn account_query(
&self,
account_query: AccountQueryParam,
Expand Down Expand Up @@ -67,4 +74,167 @@ impl Provider {
.await?;
Ok(result.as_hex())
}

#[wasm_bindgen(js_name=getSupportChains)]
pub async fn get_support_chains(&self) -> Result<JsValue, JsValue> {
let result: Vec<ChainResp> = self.client.get_support_chains().await?;
Ok(serde_wasm_bindgen::to_value(&result)?)
}

#[wasm_bindgen(js_name=getLatestBlockNumber)]
pub async fn block_info(&self) -> Result<JsValue, JsValue> {
let result: BlockNumberResp = self.client.block_info().await?;
Ok(serde_wasm_bindgen::to_value(&result)?)
}

#[wasm_bindgen(js_name=getBlockByNumber)]
pub async fn block_detail(
&self,
block_number: Option<u32>,
include_tx: bool,
include_update: bool,
) -> Result<JsValue, JsValue> {
let result: BlockResp = self
.client
.block_detail(
block_number.map(|b| BlockNumber(b)),
include_tx,
include_update,
)
.await?;
Ok(serde_wasm_bindgen::to_value(&result)?)
}

#[wasm_bindgen(js_name=getPendingBlock)]
pub async fn pending_block_detail(
&self,
last_tx_timestamp_micro: u64,
include_tx: bool,
include_update: bool,
limit: Option<usize>,
) -> Result<JsValue, JsValue> {
let result: Vec<TxHashOrDetailResp> = self
.client
.pending_block_detail(last_tx_timestamp_micro, include_tx, include_update, limit)
.await?;
Ok(serde_wasm_bindgen::to_value(&result)?)
}

#[wasm_bindgen(js_name=getBlockOnChainByNumber)]
pub async fn block_onchain_detail(&self, block_number: u32) -> Result<JsValue, JsValue> {
let result: BlockOnChainResp = self
.client
.block_onchain_detail(BlockNumber(block_number))
.await?;
Ok(serde_wasm_bindgen::to_value(&result)?)
}

#[wasm_bindgen(js_name=getAccount)]
pub async fn account_info(&self, account_query: AccountQueryParam) -> Result<JsValue, JsValue> {
let result: AccountInfoResp = self.client.account_info(account_query.into()).await?;
Ok(serde_wasm_bindgen::to_value(&result)?)
}

#[wasm_bindgen(js_name=getAccountBalances)]
pub async fn account_balances(
&self,
account_id: u32,
sub_account_id: Option<u8>,
) -> Result<JsValue, JsValue> {
let result: SubAccountBalances = self
.client
.account_balances(
AccountId(account_id),
sub_account_id.map(|id| SubAccountId(id)),
)
.await?;
Ok(serde_wasm_bindgen::to_value(&result)?)
}

#[wasm_bindgen(js_name=getAccountOrderSlots)]
pub async fn account_order_slots(
&self,
account_id: u32,
sub_account_id: Option<u8>,
) -> Result<JsValue, JsValue> {
let result: SubAccountOrders = self
.client
.account_order_slots(
AccountId(account_id),
sub_account_id.map(|id| SubAccountId(id)),
)
.await?;
Ok(serde_wasm_bindgen::to_value(&result)?)
}

#[wasm_bindgen(js_name=getTokenReserve)]
pub async fn token_remain(&self, token_id: u32, mapping: bool) -> Result<JsValue, JsValue> {
let result: HashMap<ChainId, BigUintSerdeWrapper> =
self.client.token_remain(TokenId(token_id), mapping).await?;
Ok(serde_wasm_bindgen::to_value(&result)?)
}

#[wasm_bindgen(js_name=getTransactionByHash)]
pub async fn tx_info(&self, hash: String, include_update: bool) -> Result<JsValue, JsValue> {
let hash = TxHash::from_hex(&hash).map_err(|_e| RpcError::InvalidInputParameter)?;
let result: TxResp = self.client.tx_info(hash, include_update).await?;
Ok(serde_wasm_bindgen::to_value(&result)?)
}

#[wasm_bindgen(js_name=getAccountTransactionHistory)]
pub async fn tx_history(
&self,
tx_type: L2TxType,
address: String,
page_index: u64,
page_size: u32,
) -> Result<JsValue, JsValue> {
let address =
ZkLinkAddress::from_hex(&address).map_err(|_e| RpcError::InvalidInputParameter)?;
let result: Page<ZkLinkTxHistory> = self
.client
.tx_history(tx_type.into(), address, page_index, page_size)
.await?;
Ok(serde_wasm_bindgen::to_value(&result)?)
}

#[wasm_bindgen(js_name=getFastWithdrawTxs)]
pub async fn tx_fast_withdraw(
&self,
last_tx_timestamp: u64,
max_txs: u32,
) -> Result<JsValue, JsValue> {
let result: Vec<FastWithdrawTxResp> = self
.client
.tx_fast_withdraw(last_tx_timestamp, max_txs)
.await?;
Ok(serde_wasm_bindgen::to_value(&result)?)
}

#[wasm_bindgen(js_name=pullForwardTxs)]
pub async fn pull_forward_txs(
&self,
sub_account_id: u8,
offset_id: i64,
limit: i64,
) -> Result<JsValue, JsValue> {
let result: Vec<ForwardTxResp> = self
.client
.pull_forward_txs(SubAccountId(sub_account_id), offset_id, limit)
.await?;
Ok(serde_wasm_bindgen::to_value(&result)?)
}

#[wasm_bindgen(js_name=confirmFullExit)]
pub async fn confirm_full_exit(
&self,
tx_hash: String,
submitter_signature: String,
) -> Result<bool, JsValue> {
let hash = TxHash::from_hex(&tx_hash).map_err(|_e| RpcError::InvalidInputParameter)?;
let signature = ZkLinkSignature::from_hex(&submitter_signature)
.map_err(|_e| RpcError::InvalidInputParameter)?;
let result: bool = self.client.confirm_full_exit(hash, signature).await?;
Ok(result)
}
}
28 changes: 27 additions & 1 deletion bindings/wasm/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use zklink_sdk_types::prelude::ZkLinkAddress;
use zklink_sdk_types::signatures::TxLayer1Signature;
use zklink_sdk_types::tx_type::change_pubkey::ChangePubKey;
use zklink_sdk_types::tx_type::transfer::Transfer;
use zklink_sdk_types::tx_type::zklink_tx::ZkLinkTx;
use zklink_sdk_types::tx_type::zklink_tx::{ZkLinkTx, ZkLinkTxType};

#[wasm_bindgen]
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -44,6 +44,17 @@ pub struct SignedTransaction {
tx: JsValue,
}

#[wasm_bindgen]
pub enum L2TxType {
Deposit,
FullExit,
ChangePubKey,
Transfer,
Withdraw,
ForcedExit,
OrderMatching,
}

#[wasm_bindgen]
impl AccountQueryParam {
#[wasm_bindgen(constructor)]
Expand Down Expand Up @@ -102,6 +113,7 @@ impl SignedTransaction {
SignedTransaction { tx_type, tx }
}
}

impl From<SignedTransaction> for ZkLinkTx {
fn from(tx: SignedTransaction) -> ZkLinkTx {
match tx.tx_type {
Expand All @@ -119,3 +131,17 @@ impl From<SignedTransaction> for ZkLinkTx {
}
}
}

impl From<L2TxType> for ZkLinkTxType {
fn from(tx_type: L2TxType) -> ZkLinkTxType {
match tx_type {
L2TxType::Deposit => ZkLinkTxType::Deposit,
L2TxType::Transfer => ZkLinkTxType::Transfer,
L2TxType::ChangePubKey => ZkLinkTxType::ChangePubKey,
L2TxType::OrderMatching => ZkLinkTxType::OrderMatching,
L2TxType::FullExit => ZkLinkTxType::FullExit,
L2TxType::ForcedExit => ZkLinkTxType::ForcedExit,
L2TxType::Withdraw => ZkLinkTxType::Withdraw,
}
}
}
3 changes: 2 additions & 1 deletion examples/Javascript/js-example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
</head>
<body>
<noscript>This page contains webassembly and javascript content, please enable javascript in your browser.</noscript>
<script type = "module" src = "2_transfer.js">
<!-- <script type = "module" src = "2_transfer.js">-->
<script type = "module" src = "rpc.js">
</script>
</body>
</html>
17 changes: 0 additions & 17 deletions examples/Javascript/js-example/index.js

This file was deleted.

Loading

0 comments on commit 8cdc06c

Please sign in to comment.