Skip to content

Commit

Permalink
feat: merge base
Browse files Browse the repository at this point in the history
  • Loading branch information
way2ex committed Aug 14, 2023
2 parents 56405e5 + 37df1e6 commit d4a0e03
Show file tree
Hide file tree
Showing 24 changed files with 4,845 additions and 337 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ example/
test/lib/dist/
/lib/
!src/lib
!test/lib

42 changes: 22 additions & 20 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
printWidth: 130
tabWidth: 4
useTabs: false
semi: true
singleQuote: true
quoteProps: 'as-needed'
jsxSingleQuote: false
trailingComma: 'es5'
bracketSpacing: true
bracketSameLine: false
jsxBracketSameLine: false
arrowParens: 'always'
requirePragma: false
insertPragma: false
proseWrap: 'preserve'
htmlWhitespaceSensitivity: 'css'
vueIndentScriptAndStyle: false
endOfLine: 'lf'
embeddedLanguageFormatting: 'auto'
singleAttributePerLine: false
{
"printWidth": 130,
"tabWidth": 4,
"useTabs": false,
"semi": true,
"singleQuote": true,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"trailingComma": "es5",
"bracketSpacing": true,
"bracketSameLine": false,
"jsxBracketSameLine": false,
"arrowParens": "always",
"requirePragma": false,
"insertPragma": false,
"proseWrap": "preserve",
"htmlWhitespaceSensitivity": "css",
"vueIndentScriptAndStyle": false,
"endOfLine": "lf",
"embeddedLanguageFormatting": "auto",
"singleAttributePerLine": false
}
204 changes: 110 additions & 94 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"copy-protocol": "cp -R ./src/protocol ./lib/commonjs/src",
"prepare": "npm run build",
"build": "npm run clean && webpack --config webpack.config.js --progress --color",
"build:es": "npm run clean && webpack --config webpack.es.config.js --progress --color",
"build:dev": "NODE_ENV=development npm run build",
"clean:all": "rimraf dist lib",
"clean": "rimraf dist",
Expand Down Expand Up @@ -62,10 +61,9 @@
"eventemitter3": "^3.1.0",
"events": "^3.3.0",
"injectpromise": "^1.0.0",
"lodash": "^4.17.21",
"querystring-es3": "^0.2.1",
"semver": "^5.6.0",
"typescript": "^5.0.4",
"typescript": "^5.1.6",
"validator": "^13.7.0"
},
"devDependencies": {
Expand All @@ -82,6 +80,7 @@
"@types/json-schema": "^7.0.12",
"@types/lodash": "^4.14.197",
"@types/mocha": "^10.0.1",
"@types/node": "^20.4.2",
"@types/validator": "^13.7.17",
"@typescript-eslint/eslint-plugin": "^5.61.0",
"@typescript-eslint/parser": "^5.61.0",
Expand Down
42 changes: 30 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import providers from './lib/providers/index.js';
import type { Providers } from './lib/providers/index.js';
Expand All @@ -8,8 +9,8 @@ import EventEmitter from 'eventemitter3';
import { version } from '../package.json';
import semver from 'semver';

import TransactionBuilder from './lib/TransactionBuilder';
// import Trx from 'lib/trx';
import TransactionBuilder from './lib/TransactionBuilder/TransactionBuilder.js';
import Trx from './lib/trx.js';
// import Contract from 'lib/contract';
// import Plugin from 'lib/plugin';
// import Event from 'lib/event';
Expand Down Expand Up @@ -39,7 +40,7 @@ export default class TronWeb extends EventEmitter {
static providers = providers;
static BigNumber = BigNumber;
static TransactionBuilder = TransactionBuilder;
// static Trx = Trx;
static Trx = Trx;
// static Contract = Contract;
// static Plugin = Plugin;
// static Event = Event;
Expand All @@ -48,6 +49,7 @@ export default class TronWeb extends EventEmitter {
static utils = utils;

transactionBuilder: TransactionBuilder;
trx: Trx;
providers: Providers;
BigNumber: typeof BigNumber;
defaultBlock: number | false;
Expand Down Expand Up @@ -95,8 +97,8 @@ export default class TronWeb extends EventEmitter {
if (utils.isString(eventServer)) eventServer = new providers.HttpProvider(eventServer);

// this.event = new Event(this);
// this.transactionBuilder = new TransactionBuilder(this);
// this.trx = new Trx(this);
this.transactionBuilder = new TransactionBuilder(this);
this.trx = new Trx(this);
// this.plugin = new Plugin(this, options);
this.utils = utils;

Expand Down Expand Up @@ -140,6 +142,13 @@ export default class TronWeb extends EventEmitter {
this.sha3 = TronWeb.sha3;
this.fromUtf8 = TronWeb.fromUtf8;
this.address = TronWeb.address;
this.toUtf8 = TronWeb.toUtf8;
this.isAddress = TronWeb.isAddress;
this.fromAscii = TronWeb.fromAscii;
this.toHex = TronWeb.toHex;
this.toBigNumber = TronWeb.toBigNumber;
this.fromDecimal = TronWeb.fromDecimal;
this.createAccount = TronWeb.createAccount;
// for sidechain
// if (typeof sideOptions === 'object' && (sideOptions.fullNode || sideOptions.fullHost)) {
// this.sidechain = new SideChain(sideOptions, TronWeb, this, privateKey);
Expand Down Expand Up @@ -330,7 +339,8 @@ export default class TronWeb extends EventEmitter {
return (prefix ? '0x' : '') + keccak256(Buffer.from(string, 'utf-8')).toString().substring(2);
}

static toHex(val) {
toHex: typeof TronWeb.toHex;
static toHex(val: any) {
if (utils.isBoolean(val)) return TronWeb.fromDecimal(+val);

if (utils.isBigNumber(val)) return TronWeb.fromDecimal(val);
Expand All @@ -340,6 +350,8 @@ export default class TronWeb extends EventEmitter {
if (utils.isString(val)) {
if (/^(-|)0x/.test(val)) return val;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (!isFinite(val) || /^\s*$/.test(val)) return TronWeb.fromUtf8(val);
}

Expand All @@ -351,7 +363,8 @@ export default class TronWeb extends EventEmitter {
}
}

static toUtf8(hex) {
toUtf8: typeof TronWeb.toUtf8;
static toUtf8(hex: string) {
if (utils.isHex(hex)) {
hex = hex.replace(/^0x/, '');
return Buffer.from(hex, 'hex').toString('utf8');
Expand Down Expand Up @@ -386,18 +399,20 @@ export default class TronWeb extends EventEmitter {
}
}

static fromAscii(string, padding = 0) {
fromAscii: typeof TronWeb.fromAscii;
static fromAscii(string: string, padding?: number) {
if (!utils.isString(string)) {
throw new Error('The passed value is not a valid utf-8 string');
}
return '0x' + Buffer.from(string, 'ascii').toString('hex').padEnd(padding, '0');
return '0x' + Buffer.from(string, 'ascii').toString('hex').padEnd(padding!, '0');
}

static toDecimal(value) {
return TronWeb.toBigNumber(value).toNumber();
}

static fromDecimal(value) {
fromDecimal: typeof TronWeb.fromDecimal;
static fromDecimal(value: number | BigNumber) {
const number = TronWeb.toBigNumber(value);
const result = number.toString(16);

Expand All @@ -414,18 +429,21 @@ export default class TronWeb extends EventEmitter {
return utils.isBigNumber(trx) ? sun : sun.toString(10);
}

static toBigNumber(amount = 0) {
toBigNumber: typeof TronWeb.toBigNumber;
static toBigNumber(amount: string | number | BigNumber = 0) {
if (utils.isBigNumber(amount)) return amount;

if (utils.isString(amount) && /^(-|)0x/.test(amount)) return new BigNumber(amount.replace('0x', ''), 16);

return new BigNumber(amount.toString(10), 10);
}

static isAddress(address: unknown = ''): boolean {
isAddress: typeof TronWeb.isAddress;
static isAddress(address = ''): boolean {
return isAddress(address);
}

createAccount: typeof TronWeb.createAccount;
static async createAccount() {
const account = utils.accounts.generateAccount();

Expand Down
57 changes: 28 additions & 29 deletions src/lib/TransactionBuilder/TransactionBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import TronWeb from '../../index';
import { AbiCoder } from '../../utils/ethersUtils';
import { ADDRESS_PREFIX_REGEX, toHex } from '../../utils/address';
import { ABIType, encodeParamsV2ByABI } from '../../utils/abi';
import { CreateSmartContractTransaction, SignedTransaction, Transaction, TransactionCapsule } from '../../types/Transaction';
import { keccak256 } from '../../utils/ethersUtils';
import Validator from '../../paramValidator/index';

import { GetSignWeightResponse } from '../../types/APIResponse';
import { isArray, isInteger, isNotNullOrUndefined, isObject, isString } from '../../utils/validations';
import TronWeb from '../../index.js';
import { AbiCoder, keccak256 } from '../../utils/ethersUtils.js';
import { ADDRESS_PREFIX_REGEX, toHex } from '../../utils/address.js';
import { encodeParamsV2ByABI } from '../../utils/abi.js';
import { CreateSmartContractTransaction, SignedTransaction, Transaction, TransactionCapsule } from '../../types/Transaction.js';
import Validator from '../../paramValidator/index.js';
import { GetSignWeightResponse } from '../../types/APIResponse.js';
import { isArray, isInteger, isNotNullOrUndefined, isObject, isString } from '../../utils/validations.js';
import {
AccountPermissionUpdateContract,
AssetIssueContract,
Expand All @@ -21,7 +19,7 @@ import {
UnDelegateResourceContract,
UnfreezeBalanceV2Contract,
UpdateAssetContract,
} from '../../types/Contract';
} from '../../types/Contract.js';
import {
AlterTransactionOptions,
CreateSmartContractOptions,
Expand All @@ -41,7 +39,8 @@ import {
TxLocal,
UpdateTokenOptions,
VoteInfo,
} from './helper';
} from './helper.js';
import { Address } from '../../types/Trx.js';

export default class TransactionBuilder {
tronWeb: TronWeb;
Expand Down Expand Up @@ -192,7 +191,7 @@ export default class TransactionBuilder {
async freezeBalance(
amount: NumberLike = 0,
duration: NumberLike = 3,
resource = 'BANDWIDTH',
resource: Resource = 'BANDWIDTH',
ownerAddress: string = this.tronWeb.defaultAddress.hex as string,
receiverAddress?: string,
options: PermissionId = {}
Expand Down Expand Up @@ -245,7 +244,7 @@ export default class TransactionBuilder {
}

async unfreezeBalance(
resource = 'BANDWIDTH',
resource: Resource = 'BANDWIDTH',
address: string = this.tronWeb.defaultAddress.hex as string,
receiverAddress?: string,
options: PermissionId = {}
Expand Down Expand Up @@ -597,7 +596,7 @@ export default class TransactionBuilder {
// @ts-ignore
const tokenId = options.tokenId || options.token_id;

let { abi = false, parameters = [] } = options;
let { abi, parameters = [] } = options;
const { bytecode = false, name = '' } = options;
if (abi && isString(abi)) {
try {
Expand All @@ -607,15 +606,15 @@ export default class TransactionBuilder {
}
}

const newAbi = abi as unknown as Record<string, unknown>;
let entries: ABIType[] | null = null;
if (newAbi.entrys) {
entries = newAbi.entrys as ABIType[];
const newAbi = abi as { entrys: unknown[] } | unknown[];
let entries: unknown[] | null = newAbi as unknown[];
if ((newAbi as { entrys: unknown[] }).entrys) {
entries = (newAbi as { entrys: unknown[] }).entrys;
}

if (!isArray(entries)) throw new Error('Invalid options.abi provided');

const payable = entries.some((func) => {
const payable = entries.some((func: any) => {
return func.type === 'constructor' && 'payable' === func.stateMutability.toLowerCase();
});

Expand Down Expand Up @@ -688,7 +687,7 @@ export default class TransactionBuilder {
} else if (funcABIV2) {
parameters = encodeParamsV2ByABI(funcABIV2, parametersV2).replace(/^(0x)/, '');
} else {
let constructorParams: any = entries.find((it) => {
let constructorParams: any = entries.find((it: any) => {
return it.type === 'constructor';
});

Expand Down Expand Up @@ -1491,7 +1490,7 @@ export default class TransactionBuilder {
* Can only be created by a current Super Representative.
*/
async createProposal(
parameters: Record<string, string>[],
parameters: Record<string, string | number> | Record<string, string | number>[],
issuerAddress: string = this.tronWeb.defaultAddress.hex as string,
options: PermissionId = {}
) {
Expand All @@ -1513,7 +1512,7 @@ export default class TransactionBuilder {

const data = {
owner_address: toHex(issuerAddress as string),
parameters: parameters,
parameters: newParams,
};
return createTransaction(this.tronWeb, ContractType.ProposalCreateContract, data, options?.permissionId);
}
Expand Down Expand Up @@ -1934,22 +1933,22 @@ export default class TransactionBuilder {
async updateAccountPermissions(
ownerAddress = this.tronWeb.defaultAddress.hex,
ownerPermission: Permission,
witnessPermission: Permission,
activesPermissions: Permission | Permission[],
witnessPermission?: Permission,
activesPermissions?: Permission | Permission[],
options: PermissionId = {}
) {
if (!TronWeb.isAddress(ownerAddress)) throw new Error('Invalid ownerAddress provided');
if (!TronWeb.isAddress(ownerAddress as Address)) throw new Error('Invalid ownerAddress provided');

if (!this.checkPermissions(ownerPermission, 0)) {
throw new Error('Invalid ownerPermissions provided');
}

if (!this.checkPermissions(witnessPermission, 1)) {
if (!this.checkPermissions(witnessPermission!, 1)) {
throw new Error('Invalid witnessPermissions provided');
}

if (!Array.isArray(activesPermissions)) {
activesPermissions = [activesPermissions];
activesPermissions = [activesPermissions!];
}

for (const activesPermission of activesPermissions) {
Expand Down Expand Up @@ -2041,7 +2040,7 @@ export default class TransactionBuilder {

if (options.data) {
if (options.dataFormat !== 'hex') options.data = TronWeb.toHex(options.data);
options.data = options.data.replace(/^0x/, '');
options.data = options.data!.replace(/^0x/, '');
if (options.data.length === 0) throw new Error('Invalid data provided');
transaction.raw_data.data = options.data;
}
Expand Down
Loading

0 comments on commit d4a0e03

Please sign in to comment.