Skip to content

Commit

Permalink
add new api for payment channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacky.li committed Jul 27, 2024
1 parent c482a58 commit 329e51e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
2 changes: 1 addition & 1 deletion codecs/runtime-codec/src/tapp/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ pub struct EntitySettings {
pub init_amount: Balance,
pub hosting_amount: Balance,
pub cml_id: Option<CmlId>,
pub from_token_id: Option<TokenId>,
// pub from_token_id: Option<TokenId>,
}
13 changes: 11 additions & 2 deletions system-actors/src/payment_channel/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
use tea_codec::serde::TypeId;
use tea_runtime_codec::tapp::{Account, PaymentInfo};
use tea_runtime_codec::tapp::{Account, ChannelId, PaymentInfo};

pub mod error;
pub mod txns;
Expand All @@ -9,10 +9,19 @@ pub const NAME: &[u8] = b"com.tea.payment-channel-actor";

#[doc(hidden)]
#[derive(Debug, Clone, Serialize, Deserialize, TypeId)]
pub struct QueryChannelInfoRequest(pub Account);
pub struct QueryChannelInfoRequest(pub Account, pub Option<u128>);

#[derive(Debug, Clone, Serialize, Deserialize, TypeId)]
pub struct QueryChannelInfoResponse {
pub payer_list: Vec<PaymentInfo>,
pub payee_list: Vec<PaymentInfo>,
}

#[doc(hidden)]
#[derive(Debug, Clone, Serialize, Deserialize, TypeId)]
pub struct QueryChannelWithChannelIdRequest(pub Vec<ChannelId>);

#[derive(Debug, Clone, Serialize, Deserialize, TypeId)]
pub struct QueryChannelWithChannelIdResponse {
pub list: Vec<PaymentInfo>,
}
14 changes: 14 additions & 0 deletions system-actors/src/tokenstate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ pub struct ListPaymentChannelsResponse {
pub struct QueryPaymentChannelListWithAccountRequest {
pub token_id: TokenId,
pub acct: Account,
pub expire_time: Option<Ts>,
}

#[derive(Debug, Clone, Serialize, Deserialize, TypeId)]
Expand All @@ -305,6 +306,19 @@ pub struct QueryPaymentChannelListWithAccountResponse {
pub payee_list: Vec<PaymentInfo>,
}

#[doc(hidden)]
#[derive(Debug, Clone, Serialize, Deserialize, TypeId, Priced)]
#[price(1000000)]
pub struct QueryPaymentChannelListWithChannelIdListRequest {
pub token_id: TokenId,
pub channel_id_list: Vec<ChannelId>,
}

#[derive(Debug, Clone, Serialize, Deserialize, TypeId)]
pub struct QueryPaymentChannelListWithChannelIdListResponse {
pub list: Vec<PaymentInfo>,
}

#[doc(hidden)]
#[derive(Debug, Clone, Serialize, Deserialize, TypeId, Priced)]
#[price(1000000)]
Expand Down
65 changes: 63 additions & 2 deletions utils/wasm-actor-utils/src/client/api/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use prost::Message;
use serde::{Deserialize, Serialize};
use serde_json::json;
use tea_actorx::ActorId;
use tea_runtime_codec::tapp::{Account, Balance, ChannelItem, ChannelItemStatus};
use tea_runtime_codec::tapp::{Account, Balance, ChannelId, ChannelItem, ChannelItemStatus};
use tea_sdk::IntoGlobal;
use tea_system_actors::payment_channel::{
txns::PaymentChannelTxn, QueryChannelInfoRequest, QueryChannelInfoResponse, NAME,
Expand Down Expand Up @@ -88,6 +88,17 @@ pub struct QueryChannelListWithAccountRequest {
pub address: String,
pub tapp_id_b64: String,
pub auth_b64: String,
pub expire_time: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct QueryChannelListWithChannelIdRequest {
pub uuid: String,
pub address: String,
pub tapp_id_b64: String,
pub auth_b64: String,
pub channel_id: Vec<String>,
}

pub async fn open_payment_channel(payload: Vec<u8>, from_actor: String) -> Result<Vec<u8>> {
Expand Down Expand Up @@ -231,8 +242,18 @@ pub async fn query_channel_list_with_account(

let token_id = tapp_payment_channel_token_id()?;
let acct = req.address.parse()?;
let expire_time: Option<u128> = if req.expire_time.is_some() {
let val = u128::from_str_radix(&req.expire_time.unwrap(), 10)?;
Some(val)
} else {
None
};
let res = ActorId::Static(tokenstate::NAME)
.call(tokenstate::QueryPaymentChannelListWithAccountRequest { acct, token_id })
.call(tokenstate::QueryPaymentChannelListWithAccountRequest {
acct,
token_id,
expire_time,
})
.await?;
let latest_tsid = statemachine::query_state_tsid().await?;

Expand All @@ -251,6 +272,46 @@ pub async fn query_channel_list_with_account(
help::result_ok()
}

pub async fn query_channel_list_with_channel_id(
payload: Vec<u8>,
_from_actor: String,
) -> Result<Vec<u8>> {
let req: QueryChannelListWithChannelIdRequest = serde_json::from_slice(&payload)?;
// check_auth(&req.tapp_id_b64, &req.address, &req.auth_b64).await?;

info!("query_channel_list_with_channel_id from local_state ...");
let uuid = req.uuid;

let token_id = tapp_payment_channel_token_id()?;
let mut channel_id_list: Vec<ChannelId> = Vec::new();
for id_str in req.channel_id {
let id = id_str.parse()?;
channel_id_list.push(id);
}
let res = ActorId::Static(tokenstate::NAME)
.call(
tokenstate::QueryPaymentChannelListWithChannelIdListRequest {
token_id,
channel_id_list,
},
)
.await?;
let latest_tsid = statemachine::query_state_tsid().await?;

let x = serde_json::json!({
"list": res.list,
"ts": latest_tsid.ts.to_string(),
});
info!(
"query query_channel_list_with_channel_id from local_state => {:?}",
x
);

help::cache_json_with_uuid(&uuid, x).await?;

help::result_ok()
}

pub async fn payee_update_payment(payload: Vec<u8>, from_actor: String) -> Result<Vec<u8>> {
let req: PayeeUpdatePaymentRequest = serde_json::from_slice(&payload)?;
// check_auth(&req.tapp_id_b64, &req.address, &req.auth_b64).await?;
Expand Down
4 changes: 4 additions & 0 deletions utils/wasm-actor-utils/src/client/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub async fn map_handler(action: &str, arg: Vec<u8>, from_actor: String) -> Resu
"query_channel_list_with_account" => {
api::channel::query_channel_list_with_account(arg, from_actor).await?
}
"query_channel_list_with_channel_id" => {
api::channel::query_channel_list_with_channel_id(arg, from_actor).await?
}
"payee_update_payment" => api::channel::payee_update_payment(arg, from_actor).await?,

_ => vec![],
Expand Down Expand Up @@ -103,6 +106,7 @@ pub fn map_fn_list() -> Vec<&'static str> {
"terminate",
"payer_refill_fund",
"query_channel_list_with_account",
"query_channel_list_with_channel_id",
"payee_update_payment",
]
}

0 comments on commit 329e51e

Please sign in to comment.