Skip to content

Commit

Permalink
Merge pull request #175 from thepower/feat/eth-transactions
Browse files Browse the repository at this point in the history
fix: added getBlockByHeight, removed prettifyTx, getBlock from wallet lib
  • Loading branch information
jackkru69 authored Oct 30, 2024
2 parents 16a0da5 + bf74ea2 commit 520070c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 72 deletions.
1 change: 1 addition & 0 deletions packages/tssdk/src/helpers/network.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export enum ChainAction {
GET_CHAIN_NODES = 'GET_CHAIN_NODES',
GET_BLOCK_HASH = 'GET_BLOCK_HASH',
GET_BLOCK = 'GET_BLOCK',
GET_BLOCK_BY_HEIGHT = 'GET_BLOCK_BY_HEIGHT',
GET_BLOCK_INFO = 'GET_BLOCK_INFO',
GET_WALLET = 'GET_WALLET',
GET_WALLET_SEQUENCE = 'GET_SEQUENCE',
Expand Down
3 changes: 3 additions & 0 deletions packages/tssdk/src/helpers/network.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export const transformResponse = async (response: any, kind: ChainAction, reques
case ChainAction.GET_BLOCK:
return response.block;

case ChainAction.GET_BLOCK_BY_HEIGHT:
return response.block;

case ChainAction.GET_BLOCK_INFO:
return response.block;

Expand Down
10 changes: 10 additions & 0 deletions packages/tssdk/src/libs/network/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ export class NetworkApi {
hash,
});

public getBlockByHeight = async (height: string) => this.askBlockchainTo(ChainAction.GET_BLOCK_BY_HEIGHT, {
chain: this.currentChain,
height,
});

public getBlockInfo = async (hash = 'last') => this.askBlockchainTo(ChainAction.GET_BLOCK_INFO, {
chain: this.currentChain,
hash,
Expand Down Expand Up @@ -474,6 +479,11 @@ export class NetworkApi {
requestParams.url = parameters.hash;
break;

case ChainAction.GET_BLOCK_BY_HEIGHT:
actionUrl = '/blockn';
requestParams.url = parameters.height;
break;

case ChainAction.GET_BLOCK_INFO:
actionUrl = '/blockinfo';
requestParams.url = parameters.hash;
Expand Down
73 changes: 1 addition & 72 deletions packages/tssdk/src/libs/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AddressApi } from './address/address';
import { NetworkApi } from './network/network';
import { TransactionsApi } from './transactions';
import { COIN, CryptoApi, DERIVATION_PATH_BASE } from './crypto/crypto';
import { Maybe, RegisteredAccount } from '../typings';
import { RegisteredAccount } from '../typings';
import { NetworkEnum } from '../config/network.enum';

export class WalletApi {
Expand Down Expand Up @@ -142,77 +142,6 @@ export class WalletApi {
return this.networkApi.sendPreparedTX(transmission);
}

private prettifyTx(inputTx: any, block: any) {
const tx = { ...inputTx };
if (tx.ver >= 2) {
tx.timestamp = tx.t;

if (tx.payload) {
const payment =
tx.payload.find((elem: any) => elem.purpose === 'transfer') ||
tx.payload.find(
(elem: any) => elem.purpose === 'srcfee' ||
tx.payload.find((elem: any) => elem.purpose === 'srcfeehint'),
);
if (payment) {
tx.cur = payment.cur;
tx.amount = payment.amount;
}
}
if (!tx.cur || !tx.amount) {
tx.cur = '---';
tx.amount = 0;
}

tx.sig = Array.isArray(tx.sig)
? tx.sig.reduce(
(acc: any, item: any) => Object.assign(acc, { [item.extra.pubkey]: item.signature }),
{},
)
: [];
}

if (tx.address) {
tx.address = AddressApi.hexToTextAddress(tx.address);
}

if (tx.to) {
tx.to = AddressApi.hexToTextAddress(tx.to);
}

if (tx.from) {
tx.from = AddressApi.hexToTextAddress(tx.from);
}

tx.inBlock = block.hash;
tx.blockNumber = block.header.height;

return tx;
}

public async getBlock(inputHash: string, address: Maybe<string> = null) {
let hash = inputHash;
if (address !== null) {
hash = `${hash}?addr=${address}`;
}

const block = await this.networkApi.getBlock(hash);
// Correct the sums and addresses: we bring the addresses to text form, and the sums to the required number of characters after the decimal point
block.bals = Object.keys(block.bals).reduce(
(acc, key) => Object.assign(acc, {
[AddressApi.hexToTextAddress(key)]: block.bals[key],
}),
{},
);

block.txs = Object.keys(block.txs).reduce(
(acc, key) => Object.assign(acc, { [key]: this.prettifyTx(block.txs[key], block) }),
{},
);

return block;
}

public async loadBalance(address: string) {
const walletData = await this.networkApi.getWallet(address);

Expand Down

0 comments on commit 520070c

Please sign in to comment.