Skip to content

Commit

Permalink
small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
b3hr4d committed Jul 11, 2023
1 parent 86a8cec commit 5ce838d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 73 deletions.
8 changes: 2 additions & 6 deletions backend/lib/b3_helper_lib/src/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl AccountIdentifier {
data.push(0x0A);
data.extend_from_slice("account-id".as_bytes());
data.extend_from_slice(owner.as_slice());
data.extend_from_slice(subaccount.0.as_ref());
data.extend_from_slice(subaccount.as_slice());

let account_hash = easy_hasher::raw_sha224(data);

Expand Down Expand Up @@ -72,11 +72,7 @@ impl FromStr for AccountIdentifier {

impl fmt::Display for AccountIdentifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut result = String::new();
for byte in self.0.iter() {
result.push_str(&format!("{:02x}", byte));
}
write!(f, "{}", result)
write!(f, "{}", hex::encode(&self.0))
}
}

Expand Down
4 changes: 4 additions & 0 deletions backend/lib/b3_helper_lib/src/subaccount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ impl Subaccount {
self.0 == [0u8; 32]
}

pub fn as_ref(&self) -> &[u8; 32] {
&self.0
}

pub fn as_slice(&self) -> &[u8] {
&self.0
}
Expand Down
21 changes: 20 additions & 1 deletion backend/lib/b3_helper_lib/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl NanoTimeStamp {

/// Converts the timestamp to seconds
pub fn to_secs(&self) -> u64 {
self.0 / 1_000_000_000
self.0 / Self::NS_PER_SECOND
}

/// Converts the timestamp to milliseconds
Expand Down Expand Up @@ -107,6 +107,21 @@ impl NanoTimeStamp {
NanoTimeStamp(self.0 + ns_to_add)
}

/// Get the number of whole seconds represented by the timestamp
pub fn get_secs(&self) -> u64 {
self.0 / Self::NS_PER_SECOND
}

/// Get the number of whole minutes represented by the timestamp
pub fn get_mins(&self) -> u64 {
self.0 / Self::NS_PER_MINUTE
}

/// Get the number of whole hours represented by the timestamp
pub fn get_hours(&self) -> u64 {
self.0 / Self::NS_PER_HOUR
}

/// Get the number of whole days represented by the timestamp
pub fn get_days(&self) -> u64 {
self.0 / Self::NS_PER_DAY
Expand Down Expand Up @@ -136,14 +151,18 @@ mod tests {

let ts = ts.add_secs(1);
assert_eq!(ts.to_secs(), 1);
assert_eq!(ts.get_secs(), 1);

let ts = ts.add_mins(1);
assert_eq!(ts.to_secs(), 61);
assert_eq!(ts.get_mins(), 1);

let ts = ts.add_hours(1);
assert_eq!(ts.to_secs(), 3661);
assert_eq!(ts.get_hours(), 1);

let ts = ts.add_days(1);
assert_eq!(ts.to_secs(), 90061);
assert_eq!(ts.get_days(), 1);
}

Expand Down
6 changes: 3 additions & 3 deletions backend/lib/b3_wallet_lib/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use ic_cdk::export::{candid::CandidType, serde::Deserialize};

#[derive(CandidType, Deserialize, Clone)]
pub struct WalletState {
pub accounts: WalletAccountMap,
pub nonces: AccountsNonce,
pub settings: WalletSettings,
pub accounts: WalletAccountMap,
}

impl Default for WalletState {
Expand Down Expand Up @@ -58,7 +58,7 @@ impl WalletState {

account.rename("Main Account".to_owned());

self.accounts.insert("default".to_owned(), account);
self.accounts.insert("-default".to_owned(), account);

self.nonces.increment(Environment::Production);
}
Expand Down Expand Up @@ -140,7 +140,7 @@ impl WalletState {
}

pub fn remove_account(&mut self, id: &String) -> Result<(), WalletError> {
if id == "default" {
if id == "-default" {
return Err(WalletError::CannotRemoveDefaultAccount);
}

Expand Down
72 changes: 9 additions & 63 deletions frontend/src/components/Wallet/Account/ChainCards/EthCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,10 @@ import {
import Address from "components/Wallet/Address"
import Balance from "components/Wallet/Balance"
import { ChainEnum } from "declarations/b3_wallet/b3_wallet.did"
import { ethers, providers } from "ethers"
import useToastMessage from "hooks/useToastMessage"
import { useCallback, useEffect, useState } from "react"
import { useEffect, useState } from "react"
import { B3BasicWallet, B3Wallet } from "service"
import { AddressesWithChain } from "."
import TransferForm from "../TransferForm"

const provider = new providers.JsonRpcProvider(
"https://data-seed-prebsc-2-s1.binance.org:8545"
)

interface EthCardProps extends AddressesWithChain {
actor: B3Wallet | B3BasicWallet
Expand All @@ -47,7 +41,6 @@ const EthCard: React.FC<EthCardProps> = ({
network,
accountId,
balanceLoading,

networkDetail,
handleBalance,
handleTransfer,
Expand All @@ -60,57 +53,6 @@ const EthCard: React.FC<EthCardProps> = ({
handleBalance(id, chain)
}, [actor, accountId])

const handleEthTransfer = useCallback(
async (from: string, to: string, amount: bigint) => {
console.log(`Transfering ${amount} ETH from ${from} to ${to}`)
setLoading(true)

const nonce = await provider.getTransactionCount(from)
const gasPrice = await provider.getGasPrice().then(s => s.toHexString())
const value = ethers.utils.parseEther(amount.toString()).toHexString()
const data = "0x00"
const gasLimit = ethers.BigNumber.from("24000").toHexString()
const transaction = {
nonce,
gasPrice,
gasLimit,
to,
value,
data
}

try {
const serializeTx = Buffer.from(
ethers.utils.serializeTransaction(transaction).slice(2) + "808080",
"hex"
)

console.log(serializeTx)

setLoading(false)

console.log({ title: "Signing transaction...", variant: "subtle" })

// const res = await actor.request_sign_transaction(
// id,
// [...serializeTx],
// 97n
// )
} catch (error: any) {
errorToast({
title: "Error",
description: error.message,
status: "error",
duration: 5000,
isClosable: true
})

setLoading(false)
}
},
[actor]
)

return (
<Stack
direction="column"
Expand Down Expand Up @@ -152,11 +94,15 @@ const EthCard: React.FC<EthCardProps> = ({
loading={balanceLoading}
/>
</Stack>
<TransferForm
chain={chain}
<Text color="red.500" textAlign="center">
Not available right now, please don't send any tokens to this
address
</Text>
{/* <TransferForm
chain={{ EVM: 80001n }}
title={`Send ${symbol}`}
handleTransfer={handleTransfer}
/>
handleTransfer={handleEthTransfer}
/> */}
</Stack>
</CardBody>
</Stack>
Expand Down

0 comments on commit 5ce838d

Please sign in to comment.