bridge: reverse truncate when withdraw the token

This commit is contained in:
ghassmo
2021-10-14 10:24:31 +03:00
parent 55491c2d0c
commit 9e03e020fd
3 changed files with 33 additions and 9 deletions

View File

@@ -53,7 +53,7 @@ pub struct TokenNotification {
pub token_id: jubjub::Fr,
pub drk_pub_key: jubjub::SubgroupPoint,
pub received_balance: u64,
pub decimals: u16
pub decimals: u16,
}
pub struct Bridge {
@@ -178,7 +178,7 @@ impl Bridge {
}
},
BridgeRequestsPayload::Send(addr, amount) => {
client.send(addr, amount).await?;
client.send(addr, mint_address, amount).await?;
let res = BridgeResponse {
error: BridgeResponseError::NoError,
payload: BridgeResponsePayload::Send,
@@ -210,5 +210,10 @@ pub trait NetworkClient {
async fn get_notifier(self: Arc<Self>) -> Result<async_channel::Receiver<TokenNotification>>;
async fn send(self: Arc<Self>, address: Vec<u8>, amount: u64) -> Result<()>;
async fn send(
self: Arc<Self>,
address: Vec<u8>,
mint: Option<String>,
amount: u64,
) -> Result<()>;
}

View File

@@ -392,7 +392,7 @@ impl NetworkClient for BtcClient {
async fn get_notifier(self: Arc<Self>) -> Result<async_channel::Receiver<TokenNotification>> {
Ok(self.notify_channel.1.clone())
}
async fn send(self: Arc<Self>, address: Vec<u8>, amount: u64) -> Result<()> {
async fn send(self: Arc<Self>, address: Vec<u8>, _mint: Option<String>, amount: u64) -> Result<()> {
// address is not a btc address, so derive the btc address
let client = &self.client;
let public_key = deserialize(&address)?;

View File

@@ -26,7 +26,7 @@ use tungstenite::Message;
use super::bridge::{NetworkClient, TokenNotification, TokenSubscribtion};
use crate::rpc::{jsonrpc, jsonrpc::JsonResult, websockets, websockets::WsStream};
use crate::serial::{deserialize, serialize, Decodable, Encodable};
use crate::util::{generate_id, NetworkName};
use crate::util::{generate_id, NetworkName, parse::truncate};
use crate::{Error, Result};
pub const SOL_NATIVE_TOKEN_ID: &str = "So11111111111111111111111111111111111111112";
@@ -235,7 +235,7 @@ impl SolClient {
token_id: generate_id(&mint.unwrap().to_string(), &NetworkName::Solana)?,
drk_pub_key,
received_balance: amnt,
decimals: decimals as u16
decimals: decimals as u16,
})
.await
.map_err(Error::from)?;
@@ -252,7 +252,7 @@ impl SolClient {
token_id: generate_id(SOL_NATIVE_TOKEN_ID, &NetworkName::Solana)?,
drk_pub_key,
received_balance: amnt,
decimals: decimals as u16
decimals: decimals as u16,
})
.await
.map_err(Error::from)?;
@@ -297,7 +297,7 @@ impl SolClient {
keypair: &Keypair,
) -> SolResult<Signature> {
debug!(target: "SOL BRIDGE", "Sending {} {:?} tokens to main wallet",
amount / u64::pow(10, decimals as u32), mint);
amount / u64::pow(10, decimals as u32), mint);
// The token account from our main wallet
let main_tok_pk = get_associated_token_address(&self.main_keypair.pubkey(), mint);
@@ -464,9 +464,28 @@ impl NetworkClient for SolClient {
Ok(self.notify_channel.1.clone())
}
async fn send(self: Arc<Self>, address: Vec<u8>, amount: u64) -> Result<()> {
async fn send(
self: Arc<Self>,
address: Vec<u8>,
mint: Option<String>,
amount: u64,
) -> Result<()> {
let rpc = RpcClient::new(self.rpc_server.to_string());
let address: Pubkey = deserialize(&address)?;
let mut decimals = 9;
if mint.is_some() {
let mint_address: Option<Pubkey> = self.check_mint_address(mint)?;
if let Some(mint_addr) = mint_address {
let tkn = rpc.get_token_supply(&mint_addr).map_err(SolFailed::from)?;
decimals = tkn.decimals;
};
}
// reverse truncate
truncate(amount, decimals as u16, 8)?;
let instruction =
system_instruction::transfer(&self.main_keypair.pubkey(), &address, amount);