Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gagarin55 committed Sep 24, 2020
1 parent 87a253b commit d33688e
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 142 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"copy-webpack-plugin": "5.1.1",
"cross-env": "7.0.2",
"css-loader": "^3.5.3",
"electron": "10.1.1",
"electron-builder": "^22.7.0",
"electron": "10.1.2",
"electron-builder": "22.8.1",
"eslint": "^7.0.0",
"eslint-plugin-react": "^7.20.0",
"file-loader": "^6.0.0",
Expand All @@ -69,7 +69,6 @@
"bignumber.js": "^9.0.0",
"bip32": "^2.0.5",
"bip39": "^3.0.2",
"bn.js": "^5.1.2",
"clipboard-copy": "3.1.0",
"lodash": "4.17.15",
"node-fetch": "2.6.1",
Expand Down
70 changes: 70 additions & 0 deletions src/common/ErgoBoxSet.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {ErgoBoxSet} from "./ErgoBoxSet";

describe("ErgoBoxSet", () => {
it("calc ERG balance", () => {
const boxes = [
{
"boxId": "5ddeb5b1e7c62ff522e7a776c15d066cbaa2e03379509c87b443f38ade7c5808",
"transactionId": "33b588717c538f9f85a7fbea8eb3df112f013bd6e15343aaac5ad42088a4c8d0",
"value": "15126500000",
"index": 0,
"creationHeight": BigInt(324574),
"ergoTree": "100604000580df9825043c0e20d595c4a6575a58b394644979225cbf1564a73eded8cb10386f41d02d4f6b9e910428040ad806d601b2a5730000d602c67201060ed603e4c6a70504d604c1a7d6059272047301d6069aa3730295e67202d801d607ed93e47202cbc2a793cbc272017303eb02d1ededededededed8fa3720391a39972037304720593e4c672010405e4c6a7040593e4c672010504720393db63087201db6308a792c1720172047207d1ededededededed92a37203720593e4c672010405e4c6a7040592e4c672010504720690e4c6720105049a7206730593db63087201db6308a792c1720172047207d1edededed93e4c672010405e4c6a7040593e4c672010504720393c27201c2a793db63087201db6308a791c172017204",
"address": "Gxd4hMRT6Lbs5atqVxppyRNa4pKntcdMiAzAnmobNoncQKGBzo7pXBnx2u2D9R9nSfFvtoePzrjxZReT3mx5S4Mg1yjZysXrZHXT2Fb8DJ171ipa5FUbN6skZduVHnjgcpHKymxp9PBMb7j7bXirPRdVdx27qBzUcHyyaHunXRDC7qXRHxESSY3rdzLAX61XLci3SF1PdgBYhSwDgwtuDCDtKBFVvEWXg5eWdJ7xEv8rYYK6Nrrhu8mnRUoiMN1PGxo1e4fzz44qHnVFYnLFp64TNLH6nffmYxEWBWkjLBvkhYR3xBpA8FaF7TosmjsoMu3nEzpNUMrL8iQgFhp6FmHxUWswajtpyNwrCQDKa4oKqWyk88SjxVXX94Eussf9BJvuneiKXx2rgZ",
"assets": [{"tokenId": "08b59b14e4fdd60e5952314adbaa8b4e00bc0f0b676872a5224d3bf8591074cd", "amount": "1"}],
"additionalRegisters": {"R4": "05fc8ba1ea0c", "R5": "04f8cf27"},
"spentTransactionId": null
},
{
"boxId": "257be6fe5b5e09746da9d121a1a6a081c7c2b03c33bdc9982926a8e4ebab6818",
"transactionId": "33b588717c538f9f85a7fbea8eb3df112f013bd6e15343aaac5ad42088a4c8d0",
"value": "2500000",
"index": 1,
"creationHeight": BigInt(324574),
"ergoTree": "0008cd0327e65711a59378c59359c3e1d0f7abe906479eccb76094e50fe79d743ccc15e6",
"address": "9gmNsqrqdSppLUBqg2UzREmmivgqh1r3jmNcLAc53hk3YCvAGWE",
"assets": [],
"additionalRegisters": {},
"spentTransactionId": null
},
{
"boxId": "5e6584933860f5dcabe535dc555185f9336ae762d79f28d493bac28a596dc5f3",
"transactionId": "33b588717c538f9f85a7fbea8eb3df112f013bd6e15343aaac5ad42088a4c8d0",
"value": "2500000",
"index": 2,
"creationHeight": BigInt(324574),
"ergoTree": "0008cd0327e65711a59378c59359c3e1d0f7abe906479eccb76094e50fe79d743ccc15e6",
"address": "9gmNsqrqdSppLUBqg2UzREmmivgqh1r3jmNcLAc53hk3YCvAGWE",
"assets": [], "additionalRegisters": {},
"spentTransactionId": null
},
{
"boxId": "7e61ea89c721b91a8cf3f2337fb1002c58e859a825243fa5012444ce2d591f99",
"transactionId": "33b588717c538f9f85a7fbea8eb3df112f013bd6e15343aaac5ad42088a4c8d0",
"value": "2000000",
"index": 3,
"creationHeight": BigInt(324574),
"ergoTree": "1005040004000e36100204a00b08cd0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ea02d192a39a8cc7a701730073011001020402d19683030193a38cc7b2a57300000193c2b2a57301007473027303830108cdeeac93b1a57304",
"address": "2iHkR7CWvD1R4j1yZg5bkeDRQavjAaVPeTDFGGLZduHyfWMuYpmhHocX8GJoaieTx78FntzJbCBVL6rf96ocJoZdmWBL2fci7NqWgAirppPQmZ7fN9V6z13Ay6brPriBKYqLp1bT2Fk4FkFLCfdPpe",
"assets": [],
"additionalRegisters": {},
"spentTransactionId": "9d879d915986cfe4233b4dcef98fca5ae6b19d9498818d9d25256dce8a4ba2ad"
},
{
"boxId": "c983c53075a703455aa53c3d93836d50cb4ecc7ab47390895b019ce53d9bdb49",
"transactionId": "33b588717c538f9f85a7fbea8eb3df112f013bd6e15343aaac5ad42088a4c8d0",
"value": "42362500000",
"index": 4,
"creationHeight": BigInt(324574),
"ergoTree": "0008cd0327e65711a59378c59359c3e1d0f7abe906479eccb76094e50fe79d743ccc15e6",
"address": "9gmNsqrqdSppLUBqg2UzREmmivgqh1r3jmNcLAc53hk3YCvAGWE",
"assets": [],
"additionalRegisters": {},
"spentTransactionId": null
}];
const set = new ErgoBoxSet(boxes);
console.log(set.assetsIds());
expect(set.balance('ERG').toString()).toEqual("57496000000");
expect(set.balance('08b59b14e4fdd60e5952314adbaa8b4e00bc0f0b676872a5224d3bf8591074cd').toString()).toEqual("1");
});
});
29 changes: 29 additions & 0 deletions src/common/ErgoBoxSet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {ErgoBox, ErgoTokenAmount} from "./backend-types";

export class ErgoBoxSet {
private boxes: Array<ErgoBox>;
private balances: Map<string, bigint> = new Map();

constructor(boxes: Array<ErgoBox>) {
this.boxes = Array.from(boxes);
// Calculate balances balances
let ergValue = BigInt(0);
this.boxes.forEach((box: ErgoBox) => {
ergValue += BigInt(box.value);
box.assets.forEach((a: ErgoTokenAmount) => {
let amount = this.balances.get(a.tokenId);
amount = (amount||BigInt(0)) + BigInt(a.amount);
this.balances.set(a.tokenId, amount);
});
});
this.balances.set('ERG', ergValue);
}

public assetsIds(): Array<string> {
return Array.from(this.balances.keys());
}

public balance(tokenId: string): bigint {
return this.balances.get(tokenId);
}
}
8 changes: 7 additions & 1 deletion src/common/backend-types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import {WalletBox} from "../main/application/services/wallet/Wallet";
import {TokenValue} from "../main/ergoplatform/connector/types";

export interface AddressInfo {
address: string;
}
export type ErgoBox = WalletBox;
export type ErgoTokenAmount = Omit<TokenValue, 'amount'> & { amount: string };
export type ErgoBox = Omit<WalletBox, 'value' | 'assets'> & {
value: string;
assets: Array<ErgoTokenAmount>;
};


export interface Event {
type: string;
Expand Down
8 changes: 7 additions & 1 deletion src/main/application/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ export default class Application extends EventEmitter {
if (this.currentWallet == null) {
return [];
}
return this.currentWallet.getUnspentBoxes();
// map result to response format because of BigInt
return this.currentWallet.getUnspentBoxes()
.map((item) => ({
...item,
value: item.value.toString(),
assets: item.assets.map((a) => ({...a, amount: a.amount.toString()}))
}));
}

public getTransactions(): Array<any> {
Expand Down
8 changes: 4 additions & 4 deletions src/renderer/modules/wallet/Boxes/Outputs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import TokensValues from "../TokensValues";
import KeyboardArrowDownIcon from '@material-ui/icons/KeyboardArrowDown';
import KeyboardArrowUpIcon from '@material-ui/icons/KeyboardArrowUp';
import {makeStyles} from "@material-ui/core/styles";
import {WalletBox} from "../../../../main/application/services/wallet/Wallet";
import {ErgoBox} from "../../../../common/backend-types";

const useRowStyles = makeStyles({
root: {
Expand All @@ -26,7 +26,7 @@ const useRowStyles = makeStyles({
},
});

function Row(props: {box: WalletBox; selected: boolean; onSelect: any}): React.ReactElement {
function Row(props: {box: ErgoBox; selected: boolean; onSelect: any}): React.ReactElement {
const { box, selected, onSelect } = props;
const [open, setOpen] = React.useState(false);
const classes = useRowStyles();
Expand Down Expand Up @@ -84,11 +84,11 @@ function Outputs(): React.ReactElement {
dispatch(fetchUnspentBoxes());
}, []);

function isItemSelected(box: WalletBox): boolean {
function isItemSelected(box: ErgoBox): boolean {
return selected.indexOf(box.boxId) !== -1;
}

const handleClick = (event: React.MouseEvent<unknown>, box: WalletBox) => {
const handleClick = (event: React.MouseEvent<unknown>, box: ErgoBox) => {
const selectedIndex = selected.indexOf(box.boxId);
let newSelected: string[] = [];

Expand Down
15 changes: 7 additions & 8 deletions src/renderer/modules/wallet/Boxes/Send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {makeStyles} from "@material-ui/core/styles";
import {MoneyUnits} from "../../../../common/MoneyUnits";
import SendOutlinedIcon from '@material-ui/icons/SendOutlined';
import TransferDialog from "../transfer/TransferDialog";
import {WalletBox} from "../../../../main/application/services/wallet/Wallet";
import {ErgoBox} from "../../../../common/backend-types";
import {ErgoBoxSet} from "../../../../common/ErgoBoxSet";

const useStyles = makeStyles(() => ({
container: {
Expand All @@ -25,16 +26,14 @@ const useStyles = makeStyles(() => ({
}));


function Send (props: {fromBoxes: Array<WalletBox>}): React.ReactElement {
function Send (props: {fromBoxes: Array<ErgoBox>}): React.ReactElement {
const { fromBoxes } = props;
const classes = useStyles();
const [asset, setAsset] = React.useState('ERG');
const [transferDlgOpen, setTransferDlgOpen ] = React.useState(false);

const { fromBoxes } = props;

const total: MoneyUnits = fromBoxes.reduce((total: MoneyUnits, box: any) => {
return total.plus(new MoneyUnits(box.value, 9));
}, new MoneyUnits(0, 9));
const boxSet = new ErgoBoxSet(fromBoxes);
const total = new MoneyUnits(boxSet.balance('ERG'), 9);

function handleChange(event: React.ChangeEvent<{ value: string }>): void {
setAsset(event.target.value);
Expand Down Expand Up @@ -79,7 +78,7 @@ function Send (props: {fromBoxes: Array<WalletBox>}): React.ReactElement {
disabled={!canSend}
onClick={handleSendClick}
variant="contained"
color={'secondary'}
color="secondary"
endIcon={<SendOutlinedIcon />}
>
Send
Expand Down
3 changes: 1 addition & 2 deletions src/renderer/modules/wallet/TokensValues.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Box} from "@material-ui/core";
import Chip from "@material-ui/core/Chip";
import {Box, Chip} from "@material-ui/core";
import AssetValue from "../../components/AssetValue";
import * as React from "react";

Expand Down
39 changes: 22 additions & 17 deletions src/renderer/modules/wallet/Transactions/TxDetailsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,32 @@ function TxDetailsDialog(props: TxDetailsProps): React.ReactElement {
</Typography>
</Grid>
{/* Tx Schema - Inputs -> Outputs */}
<Grid container direction={"column"} wrap="nowrap">
<Grid container direction="column" wrap="nowrap">
{/* inputs list */}
<Grid item>

<Grid container direction={'column'}>
{
tx.inputs.map((i: any) => (
<Grid item key={i.id}>
<Grid container wrap={"nowrap"} spacing={1} alignItems="baseline" justify={'space-between'}>
<Grid item>
{(i.address.length > 60) ? (
<Address shortened={true} value={i.address} />
) : (
<Address shortened={false} value={i.address} />
)}
</Grid>
<Grid item>
<AssetValue amount={i.value} decimals={9} symbol={"ERG"}/>
</Grid>
</Grid>
</Grid>
<Box key={i.id} display="flex">
<Box flexBasis={0} flexGrow={2}>
{(i.address.length > 60) ? (
<Address shortened={true} value={i.address} />
) : (
<Address shortened={false} value={i.address} />
)}
</Box>
<Box
flexBasis={0}
flexGrow={1}
display="flex"
justifyContent="flex-end"
alignItems="center"
>
<TokensValues assets={i.assets} />
<AssetValue amount={i.value} decimals={9} symbol="ERG"/>
</Box>
</Box>
))
}
</Grid>
Expand All @@ -111,10 +116,10 @@ function TxDetailsDialog(props: TxDetailsProps): React.ReactElement {

{/* outputs list */}
<Grid item>
<Grid container direction={'column'}>
<Grid container direction="column">
{
tx.outputs.map((i: any) => (
<Box key={i.id} display={"flex"}>
<Box key={i.id} display="flex">
<Box flexBasis={0} flexGrow={2}>
{(i.address.length > 60) ? (
<Address shortened={true} value={i.address} />
Expand Down
12 changes: 6 additions & 6 deletions src/renderer/modules/wallet/transfer/InitialStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import {MoneyUnits} from "../../../../common/MoneyUnits";
import {fromErg} from "../../../../common/utils";
import TokensValues from "../TokensValues";
import {minBoxValue} from "../../../../common/constants";
import {WalletBox} from "../../../../main/application/services/wallet/Wallet";
import {ErgoBox} from "../../../../common/backend-types";
import {ErgoBoxSet} from "../../../../common/ErgoBoxSet";

interface InitialStepProps {
fromBoxes: Array<WalletBox>;
fromBoxes: Array<ErgoBox>;
onContinue?: any;
backendApi?: any;
}
Expand All @@ -23,9 +24,8 @@ function InitialStep(props: InitialStepProps): React.ReactElement {
const amountRef = React.createRef<any>();

// Total nanoERG available
const totalMoney: MoneyUnits = fromBoxes.reduce((total: MoneyUnits, box: any) => {
return total.plus(new MoneyUnits(box.value, 9));
}, new MoneyUnits(0, 9));
const boxes = new ErgoBoxSet(fromBoxes);
const totalMoney = new MoneyUnits(boxes.balance('ERG'), 9);

const [recipient, setRecipient] = React.useState('');
const [recipientError, setRecipientError] = React.useState('');
Expand Down Expand Up @@ -114,7 +114,7 @@ function InitialStep(props: InitialStepProps): React.ReactElement {

return (
<>
<div>Spend boxes</div>
<Box mb={1}>Spend boxes</Box>
<Grid container direction="column">
{fromBoxes.map((box) => {
return (
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/modules/wallet/transfer/TransferDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import InitialStep from "./InitialStep";
import ConfirmationStep from "./ConfirmationStep";
import {UnsignedTransaction} from "../../../../main/application/services/wallet/TransactionBuilder";
import FinalStep from "./FinalStep";
import {WalletBox} from "../../../../main/application/services/wallet/Wallet";
import {ErgoBox} from "../../../../common/backend-types";

enum Page {
INITIAL,
Expand All @@ -18,7 +18,7 @@ enum Page {
interface TransferProps {
open: boolean;
onClose: () => void;
fromBoxes: Array<WalletBox>;
fromBoxes: Array<ErgoBox>;
}

const useStyles = makeStyles((theme) => ({
Expand Down
Loading

0 comments on commit d33688e

Please sign in to comment.