Skip to content

Commit

Permalink
Merge pull request #7 from mybucks-online/feat/trx-api-calls
Browse files Browse the repository at this point in the history
Refactor trx api calls
  • Loading branch information
koko37 authored Nov 25, 2024
2 parents ea1d44a + 939ae1b commit 031be89
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 333 deletions.
12 changes: 5 additions & 7 deletions src/components/NetworkSelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ const NetworkSelector = ({ network, chainId, updateNetwork }) => {

return (
<Select onChange={onChange} value={network + "." + chainId}>
{Object.values(EVM_NETWORKS)
.sort(({ order: a }, { order: b }) => a - b)
.map(({ chainId: cid, label }) => (
<option key={cid} value={NETWORK.EVM + "." + cid}>
{label}
</option>
))}
{EVM_NETWORKS.map(({ chainId: cid, label }) => (
<option key={cid} value={NETWORK.EVM + "." + cid}>
{label}
</option>
))}

<option value={NETWORK.TRON + ".1"}>Tron</option>
</Select>
Expand Down
7 changes: 1 addition & 6 deletions src/contexts/Store.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const StoreContext = createContext({
connectivity: true,
password: "",
passcode: "",
salt: "",
hash: "",
setup: (p, pc, s, h) => {},
reset: () => {},
Expand Down Expand Up @@ -49,7 +48,6 @@ const StoreProvider = ({ children }) => {
// key parts
const [password, setPassword] = useState("");
const [passcode, setPasscode] = useState("");
const [salt, setSalt] = useState("");
const [hash, setHash] = useState("");

// network related
Expand Down Expand Up @@ -118,7 +116,6 @@ const StoreProvider = ({ children }) => {
const reset = () => {
setPassword("");
setPasscode("");
setSalt("");
setHash("");

setNetwork(DEFAULT_NETWORK);
Expand All @@ -136,10 +133,9 @@ const StoreProvider = ({ children }) => {
selectToken("");
};

const setup = (pw, pc, s, h) => {
const setup = (pw, pc, h) => {
setPassword(pw);
setPasscode(pc);
setSalt(s);
setHash(h);
};

Expand Down Expand Up @@ -172,7 +168,6 @@ const StoreProvider = ({ children }) => {
connectivity,
password,
passcode,
salt,
hash,
reset,
setup,
Expand Down
29 changes: 6 additions & 23 deletions src/lib/account/evm.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import IERC20 from "@mybucks/lib/erc20.json";
class EvmAccount {
network = NETWORK.EVM;
chainId = null;
networkInfo = null;

signer = null;
account = null;
Expand All @@ -26,7 +27,8 @@ class EvmAccount {

constructor(hashKey, chainId) {
this.chainId = chainId;
this.provider = new ethers.JsonRpcProvider(EVM_NETWORKS[chainId].provider);
this.networkInfo = EVM_NETWORKS.find((n) => n.chainId === chainId);
this.provider = new ethers.JsonRpcProvider(this.networkInfo.provider);

this.signer = getEvmPrivateKey(hashKey);
this.account = new ethers.Wallet(this.signer, this.provider);
Expand All @@ -42,15 +44,15 @@ class EvmAccount {
}

linkOfAddress(address) {
return EVM_NETWORKS[this.chainId].scanner + "/address/" + address;
return this.networkInfo.scanner + "/address/" + address;
}

linkOfContract(address) {
return EVM_NETWORKS[this.chainId].scanner + "/address/" + address + "#code";
return this.networkInfo.scanner + "/address/" + address + "#code";
}

linkOfTransaction(txn) {
return EVM_NETWORKS[this.chainId].scanner + "/tx/" + txn;
return this.networkInfo.scanner + "/tx/" + txn;
}

async getNetworkStatus() {
Expand Down Expand Up @@ -147,25 +149,6 @@ class EvmAccount {
}
}

/*
async nativeCurrency() {
const balance = await this.provider.getBalance(this.address);
return ethers.formatEther(balance);
}
async balanceOfErc20(token) {
const erc20 = new Contract(token, IERC20.abi, this.provider);
const result = await erc20.balanceOf(this.account.address);
return result;
}
async transferErc20(token, to, amount, gasPrice = null, gasLimit = null) {
const erc20 = new Contract(token, IERC20.abi, this.provider);
const tx = await erc20.connect(this.account).transfer(to, amount);
await tx.wait();
}
*/

/**
*
* @param {*} token contract address or null(for native currency)
Expand Down
20 changes: 10 additions & 10 deletions src/lib/account/tron.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@ class TronAccount {
// [TODO] Now it only returns balance of TRX and USDT
async queryBalances() {
const nativeTokenName = "TRX";
// get TRX balance
const trxRawBalance = await this.tronweb.trx.getBalance(this.address);
const nativeTokenBalance = this.tronweb.fromSun(trxRawBalance);
const nativeTokenPrice = await queryPrice(nativeTokenName);

// balance of TRC20 USDT
const usdtContract = await this.tronweb.contract().at(TRC20_USDT_ADDRESS);
const usdtRawBalance = await usdtContract.methods
.balanceOf(this.address)
.call();

const [trxRawBalance, nativeTokenPrice, usdtRawBalance, usdtPrice] =
await Promise.all([
this.tronweb.trx.getBalance(this.address),
queryPrice(nativeTokenName),
usdtContract.methods.balanceOf(this.address).call(),
queryPrice("USDT"),
]);

const nativeTokenBalance = this.tronweb.fromSun(trxRawBalance);
const usdtBalance = this.tronweb.fromSun(usdtRawBalance);
const usdtPrice = await queryPrice("USDT");

return [
nativeTokenName,
Expand Down
75 changes: 38 additions & 37 deletions src/lib/conf.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { Buffer } from "buffer";
import { ethers } from "ethers";
import { scrypt } from "scrypt-js";

const abi = new ethers.AbiCoder();

export const PASSWORD_MIN_LENGTH = 12;
export const PASSWORD_MAX_LENGTH = 128;
export const PASSCODE_MIN_LENGTH = 6;
export const PASSCODE_MAX_LENGTH = 16;

export const PASSCODE_MAX_TRY = 3;

/**
* [CRITICAL] DON'T CHANGE FOREVER!!!
* Reference:
Expand All @@ -12,15 +22,26 @@ export const HASH_OPTIONS = {
p: import.meta.env.DEV ? 1 : 5, // parallelization parameter
keyLen: 64,
};
export const PASSWORD_MIN_LENGTH = 12;
export const PASSWORD_MAX_LENGTH = 128;
export const PASSCODE_MIN_LENGTH = 6;
export const PASSCODE_MAX_LENGTH = 16;

export const PASSCODE_MAX_TRY = 3;
export const generateHash = async (password, passcode, cb = () => {}) => {
const salt = `${password.slice(-4)}${passcode}`;

const passwordBuffer = Buffer.from(password);
const saltBuffer = Buffer.from(salt);

const hashBuffer = await scrypt(
passwordBuffer,
saltBuffer,
HASH_OPTIONS.N,
HASH_OPTIONS.r,
HASH_OPTIONS.p,
HASH_OPTIONS.keyLen,
cb
);

return Buffer.from(hashBuffer).toString("hex");
};

export const generateSalt = (password, passcode) =>
`${password.slice(-4)}${passcode}`;
export const getEvmPrivateKey = (h) =>
ethers.keccak256(abi.encode(["string"], [h]));

Expand All @@ -32,8 +53,8 @@ export const NETWORK = Object.freeze({
export const DEFAULT_NETWORK = NETWORK.EVM;
export const DEFAULT_CHAIN_ID = 1;

export const EVM_NETWORKS = {
1: {
export const EVM_NETWORKS = [
{
chainId: 1,
name: "ethereum",
label: "Ethereum",
Expand All @@ -42,7 +63,7 @@ export const EVM_NETWORKS = {
scanner: "https://etherscan.io",
order: 1,
},
137: {
{
chainId: 137,
name: "polygon",
label: "Polygon",
Expand All @@ -52,7 +73,7 @@ export const EVM_NETWORKS = {
scanner: "https://polygonscan.com",
order: 2,
},
42161: {
{
chainId: 42161,
name: "arbitrum",
label: "Arbitrum",
Expand All @@ -62,7 +83,7 @@ export const EVM_NETWORKS = {
scanner: "https://arbiscan.io",
order: 3,
},
10: {
{
chainId: 10,
name: "optimism",
label: "Optimism",
Expand All @@ -72,15 +93,15 @@ export const EVM_NETWORKS = {
scanner: "https://optimistic.etherscan.io",
order: 4,
},
56: {
{
chainId: 56,
name: "bsc",
label: "BNB Chain",
provider: "https://bsc-dataseed.binance.org/",
scanner: "https://bscscan.com",
order: 5,
},
43114: {
{
chainId: 43114,
name: "avalanche",
label: "Avalanche",
Expand All @@ -90,27 +111,7 @@ export const EVM_NETWORKS = {
scanner: "https://snowtrace.io",
order: 6,
},
42220: {
chainId: 42220,
name: "celo",
label: "Celo",
provider:
"https://celo-mainnet.infura.io/v3/" +
import.meta.env.VITE_INFURA_API_KEY,
scanner: "https://celoscan.io",
order: 7,
},
59144: {
chainId: 59144,
name: "linea",
label: "Linea",
provider:
"https://linea-mainnet.infura.io/v3/" +
import.meta.env.VITE_INFURA_API_KEY,
scanner: "https://lineascan.build",
order: 8,
},
};
];

export const GAS_PRICE = Object.freeze({
HIGH: "high",
Expand All @@ -129,8 +130,8 @@ export const gasMultiplier = (option) =>
// 15 minutes, after this period, wallet will be locked.
export const IDLE_DURATION = 900_000;

// in every 15 seconds, it refreshes gas price or network status
export const REFRESH_STATUS_DURATION = 15_000;
// in every 30 seconds, it refreshes gas price or network status
export const REFRESH_STATUS_DURATION = 30_000;

// The hidden balances will be displayed as shown below
export const BALANCE_PLACEHOLDER = "*****";
Expand Down
Loading

0 comments on commit 031be89

Please sign in to comment.