From e0c7db1159bb58019e9fea47cc7a7b432c4a7198 Mon Sep 17 00:00:00 2001 From: danielailie Date: Thu, 10 Oct 2024 16:32:31 +0300 Subject: [PATCH] Remove primitives file --- src/networkProviders/accounts.ts | 6 +- src/networkProviders/apiNetworkProvider.ts | 6 +- src/networkProviders/constants.ts | 6 +- src/networkProviders/contractResults.ts | 10 +-- src/networkProviders/pairs.ts | 6 +- src/networkProviders/primitives.spec.ts | 17 ----- src/networkProviders/primitives.ts | 66 ------------------- .../providers.dev.net.spec.ts | 4 +- src/networkProviders/tokenDefinitions.ts | 19 +++--- src/networkProviders/tokens.ts | 7 +- src/networkProviders/transactionEvents.ts | 18 ++--- src/networkProviders/transactionLogs.ts | 4 +- src/networkProviders/transactionReceipt.ts | 12 ++-- src/networkProviders/transactions.ts | 33 ++++++---- src/testutils/dummyQuery.ts | 4 +- src/tokenTransferBuilders.ts | 4 +- 16 files changed, 77 insertions(+), 145 deletions(-) delete mode 100644 src/networkProviders/primitives.spec.ts delete mode 100644 src/networkProviders/primitives.ts diff --git a/src/networkProviders/accounts.ts b/src/networkProviders/accounts.ts index 7c2a29dc0..cee89aea6 100644 --- a/src/networkProviders/accounts.ts +++ b/src/networkProviders/accounts.ts @@ -1,12 +1,12 @@ import BigNumber from "bignumber.js"; +import { Address } from "../address"; import { IAddress } from "./interface"; -import { Address } from "./primitives"; /** * A plain view of an account, as queried from the Network. */ export class AccountOnNetwork { - address: IAddress = new Address(""); + address: IAddress = Address.empty(); nonce: number = 0; balance: BigNumber = new BigNumber(0); code: string = ""; @@ -65,7 +65,7 @@ export class GuardianData { class Guardian { activationEpoch: number = 0; - address: IAddress = new Address(""); + address: IAddress = Address.empty(); serviceUID: string = ""; static fromHttpResponse(responsePart: any): Guardian { diff --git a/src/networkProviders/apiNetworkProvider.ts b/src/networkProviders/apiNetworkProvider.ts index 0b777c35c..985a4b50e 100644 --- a/src/networkProviders/apiNetworkProvider.ts +++ b/src/networkProviders/apiNetworkProvider.ts @@ -1,5 +1,6 @@ import axios from "axios"; import { ErrContractQuery, ErrNetworkProvider } from "../errors"; +import { numberToPaddedHex } from "../utils.codec"; import { AccountOnNetwork, GuardianData } from "./accounts"; import { defaultAxiosConfig, defaultPagination } from "./config"; import { BaseUserAgent } from "./constants"; @@ -12,7 +13,6 @@ import { NetworkProviderConfig } from "./networkProviderConfig"; import { NetworkStake } from "./networkStake"; import { NetworkStatus } from "./networkStatus"; import { PairOnNetwork } from "./pairs"; -import { Nonce } from "./primitives"; import { ProxyNetworkProvider } from "./proxyNetworkProvider"; import { DefinitionOfFungibleTokenOnNetwork, DefinitionOfTokenCollectionOnNetwork } from "./tokenDefinitions"; import { FungibleTokenOfAccountOnNetwork, NonFungibleTokenOfAccountOnNetwork } from "./tokens"; @@ -115,7 +115,7 @@ export class ApiNetworkProvider implements INetworkProvider { collection: string, nonce: number, ): Promise { - const nonceAsHex = new Nonce(nonce).hex(); + const nonceAsHex = numberToPaddedHex(nonce); const response = await this.doGetGeneric(`accounts/${address.bech32()}/nfts/${collection}-${nonceAsHex}`); const tokenData = NonFungibleTokenOfAccountOnNetwork.fromApiHttpResponse(response); return tokenData; @@ -181,7 +181,7 @@ export class ApiNetworkProvider implements INetworkProvider { } async getNonFungibleToken(collection: string, nonce: number): Promise { - const nonceAsHex = new Nonce(nonce).hex(); + const nonceAsHex = numberToPaddedHex(nonce); const response = await this.doGetGeneric(`nfts/${collection}-${nonceAsHex}`); const token = NonFungibleTokenOfAccountOnNetwork.fromApiHttpResponse(response); return token; diff --git a/src/networkProviders/constants.ts b/src/networkProviders/constants.ts index ea134f203..d85d175b4 100644 --- a/src/networkProviders/constants.ts +++ b/src/networkProviders/constants.ts @@ -1,7 +1,7 @@ import BigNumber from "bignumber.js"; -import { Address } from "./primitives"; +import { Address } from "../address"; export const MaxUint64AsBigNumber = new BigNumber("18446744073709551615"); export const EsdtContractAddress = new Address("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u"); -export const BaseUserAgent = "multiversx-sdk" -export const UnknownClientName = "unknown" +export const BaseUserAgent = "multiversx-sdk"; +export const UnknownClientName = "unknown"; diff --git a/src/networkProviders/contractResults.ts b/src/networkProviders/contractResults.ts index 37046fb0d..a7f2a3674 100644 --- a/src/networkProviders/contractResults.ts +++ b/src/networkProviders/contractResults.ts @@ -1,6 +1,6 @@ +import { Address } from "../address"; import { IAddress } from "./interface"; import { TransactionLogs } from "./transactionLogs"; -import { Address } from "./primitives"; export class ContractResults { readonly items: ContractResultItem[]; @@ -14,12 +14,12 @@ export class ContractResults { } static fromProxyHttpResponse(results: any[]): ContractResults { - let items = results.map(item => ContractResultItem.fromProxyHttpResponse(item)); + let items = results.map((item) => ContractResultItem.fromProxyHttpResponse(item)); return new ContractResults(items); } static fromApiHttpResponse(results: any[]): ContractResults { - let items = results.map(item => ContractResultItem.fromApiHttpResponse(item)); + let items = results.map((item) => ContractResultItem.fromApiHttpResponse(item)); return new ContractResults(items); } } @@ -28,8 +28,8 @@ export class ContractResultItem { hash: string = ""; nonce: number = 0; value: string = ""; - receiver: IAddress = new Address(""); - sender: IAddress = new Address(""); + receiver: IAddress = Address.empty(); + sender: IAddress = Address.empty(); data: string = ""; previousHash: string = ""; originalHash: string = ""; diff --git a/src/networkProviders/pairs.ts b/src/networkProviders/pairs.ts index 723c25b7b..31a92bf12 100644 --- a/src/networkProviders/pairs.ts +++ b/src/networkProviders/pairs.ts @@ -1,9 +1,9 @@ -import {Address} from "./primitives"; -import {IAddress} from "./interface"; import BigNumber from "bignumber.js"; +import { Address } from "../address"; +import { IAddress } from "./interface"; export class PairOnNetwork { - address: IAddress = new Address(""); + address: IAddress = Address.empty(); id: string = ""; symbol: string = ""; name: string = ""; diff --git a/src/networkProviders/primitives.spec.ts b/src/networkProviders/primitives.spec.ts deleted file mode 100644 index 202a9dfee..000000000 --- a/src/networkProviders/primitives.spec.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { assert } from "chai"; -import { Address } from "./primitives"; - -describe("test primitives", function () { - it("should create address from bech32 and from pubkey", async function () { - let aliceBech32 = "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"; - let bobBech32 = "erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"; - let alicePubkey = Buffer.from("0139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1", "hex"); - let bobPubkey = Buffer.from("8049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f8", "hex"); - - assert.equal(new Address(aliceBech32).bech32(), Address.fromPubkey(alicePubkey).bech32()); - assert.equal(new Address(bobBech32).bech32(), Address.fromPubkey(bobPubkey).bech32()); - assert.equal(new Address(aliceBech32).toString(), aliceBech32); - assert.equal(new Address(bobBech32).toString(), bobBech32); - }); -}); - diff --git a/src/networkProviders/primitives.ts b/src/networkProviders/primitives.ts deleted file mode 100644 index ed50b4efa..000000000 --- a/src/networkProviders/primitives.ts +++ /dev/null @@ -1,66 +0,0 @@ -import * as bech32 from "bech32"; -import { IAddress } from "./interface"; - -/** - * The human-readable-part of the bech32 addresses. - */ -const HRP = "erd"; - -export class Address implements IAddress { - private readonly value: string; - - constructor(value: string) { - this.value = value; - } - - static fromPubkey(pubkey: Buffer): IAddress { - let words = bech32.toWords(pubkey); - let address = bech32.encode(HRP, words); - return new Address(address); - } - - bech32(): string { - return this.value; - } - - toString() { - return this.bech32(); - } -} - -export class Nonce { - private readonly value: number; - - constructor(value: number) { - this.value = value; - } - - valueOf(): number { - return this.value; - } - - hex(): string { - return numberToPaddedHex(this.value); - } -} - -export function numberToPaddedHex(value: number) { - let hex = value.toString(16); - return zeroPadStringIfOddLength(hex); -} - -export function isPaddedHex(input: string) { - input = input || ""; - let decodedThenEncoded = Buffer.from(input, "hex").toString("hex"); - return input.toUpperCase() == decodedThenEncoded.toUpperCase(); -} - -export function zeroPadStringIfOddLength(input: string): string { - input = input || ""; - - if (input.length % 2 == 1) { - return "0" + input; - } - - return input; -} diff --git a/src/networkProviders/providers.dev.net.spec.ts b/src/networkProviders/providers.dev.net.spec.ts index 354cc67ae..d08e5b4f7 100644 --- a/src/networkProviders/providers.dev.net.spec.ts +++ b/src/networkProviders/providers.dev.net.spec.ts @@ -1,13 +1,13 @@ import { AxiosHeaders } from "axios"; import { assert } from "chai"; +import { Address } from "../address"; +import { MockQuery } from "../testutils/dummyQuery"; import { ApiNetworkProvider } from "./apiNetworkProvider"; import { INetworkProvider, ITransactionNext } from "./interface"; -import { Address } from "./primitives"; import { ProxyNetworkProvider } from "./proxyNetworkProvider"; import { NonFungibleTokenOfAccountOnNetwork } from "./tokens"; import { TransactionEventData } from "./transactionEvents"; import { TransactionOnNetwork } from "./transactions"; -import { MockQuery } from "../testutils/dummyQuery"; describe("test network providers on devnet: Proxy and API", function () { let alice = new Address("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"); diff --git a/src/networkProviders/tokenDefinitions.ts b/src/networkProviders/tokenDefinitions.ts index 63bfa523d..fbaccf2ac 100644 --- a/src/networkProviders/tokenDefinitions.ts +++ b/src/networkProviders/tokenDefinitions.ts @@ -1,12 +1,12 @@ import BigNumber from "bignumber.js"; +import { Address } from "../address"; import { IAddress } from "./interface"; -import { Address } from "./primitives"; export class DefinitionOfFungibleTokenOnNetwork { identifier: string = ""; name: string = ""; ticker: string = ""; - owner: IAddress = new Address(""); + owner: IAddress = Address.empty(); decimals: number = 0; supply: BigNumber = new BigNumber(0); isPaused: boolean = false; @@ -55,7 +55,7 @@ export class DefinitionOfFungibleTokenOnNetwork { result.identifier = identifier; result.name = tokenName.toString(); result.ticker = identifier; - result.owner = Address.fromPubkey(owner); + result.owner = new Address(owner); result.decimals = properties.NumDecimals.toNumber(); result.supply = new BigNumber(supply.toString()).shiftedBy(-result.decimals); result.isPaused = properties.IsPaused; @@ -76,7 +76,7 @@ export class DefinitionOfTokenCollectionOnNetwork { type: string = ""; name: string = ""; ticker: string = ""; - owner: IAddress = new Address(""); + owner: IAddress = Address.empty(); decimals: number = 0; canPause: boolean = false; canFreeze: boolean = false; @@ -120,7 +120,7 @@ export class DefinitionOfTokenCollectionOnNetwork { result.type = tokenType.toString(); result.name = tokenName.toString(); result.ticker = collection; - result.owner = Address.fromPubkey(owner); + result.owner = new Address(owner); result.decimals = properties.NumDecimals.toNumber() ?? 0; result.canPause = properties.CanPause || false; result.canFreeze = properties.CanFreeze || false; @@ -150,8 +150,11 @@ function parseTokenProperties(propertiesBuffers: Buffer[]): Record // This only handles booleans and numbers. function parseValueOfTokenProperty(value: string): any { switch (value) { - case "true": return true; - case "false": return false; - default: return new BigNumber(value); + case "true": + return true; + case "false": + return false; + default: + return new BigNumber(value); } } diff --git a/src/networkProviders/tokens.ts b/src/networkProviders/tokens.ts index 855d2428e..1c838f070 100644 --- a/src/networkProviders/tokens.ts +++ b/src/networkProviders/tokens.ts @@ -1,5 +1,6 @@ import { BigNumber } from "bignumber.js"; -import { Address, Nonce } from "./primitives"; +import { Address } from "../address"; +import { numberToPaddedHex } from "../utils.codec"; import { IAddress } from "./interface"; export class FungibleTokenOfAccountOnNetwork { @@ -26,7 +27,7 @@ export class NonFungibleTokenOfAccountOnNetwork { nonce: number = 0; type: string = ""; name: string = ""; - creator: IAddress = new Address(""); + creator: IAddress = Address.empty(); supply: BigNumber = new BigNumber(0); decimals: number = 0; royalties: BigNumber = new BigNumber(0); @@ -49,7 +50,7 @@ export class NonFungibleTokenOfAccountOnNetwork { static fromProxyHttpResponseByNonce(payload: any): NonFungibleTokenOfAccountOnNetwork { let result = NonFungibleTokenOfAccountOnNetwork.fromHttpResponse(payload); - let nonceAsHex = new Nonce(result.nonce).hex(); + let nonceAsHex = numberToPaddedHex(result.nonce); result.identifier = `${payload.tokenIdentifier}-${nonceAsHex}`; result.collection = payload.tokenIdentifier || ""; diff --git a/src/networkProviders/transactionEvents.ts b/src/networkProviders/transactionEvents.ts index b37d47abc..ab11c5892 100644 --- a/src/networkProviders/transactionEvents.ts +++ b/src/networkProviders/transactionEvents.ts @@ -1,8 +1,8 @@ +import { Address } from "../address"; import { IAddress } from "./interface"; -import { Address } from "./primitives"; export class TransactionEvent { - address: IAddress = new Address(""); + address: IAddress = Address.empty(); identifier: string = ""; topics: TransactionEventTopic[] = []; @@ -18,16 +18,16 @@ export class TransactionEvent { } static fromHttpResponse(responsePart: { - address: string, - identifier: string, - topics: string[], - data: string, - additionalData?: string[] + address: string; + identifier: string; + topics: string[]; + data: string; + additionalData?: string[]; }): TransactionEvent { let result = new TransactionEvent(); result.address = new Address(responsePart.address); result.identifier = responsePart.identifier || ""; - result.topics = (responsePart.topics || []).map(topic => new TransactionEventTopic(topic)); + result.topics = (responsePart.topics || []).map((topic) => new TransactionEventTopic(topic)); result.dataPayload = TransactionEventData.fromBase64(responsePart.data); result.additionalData = (responsePart.additionalData || []).map(TransactionEventData.fromBase64); @@ -37,7 +37,7 @@ export class TransactionEvent { } findFirstOrNoneTopic(predicate: (topic: TransactionEventTopic) => boolean): TransactionEventTopic | undefined { - return this.topics.filter(topic => predicate(topic))[0]; + return this.topics.filter((topic) => predicate(topic))[0]; } getLastTopic(): TransactionEventTopic { diff --git a/src/networkProviders/transactionLogs.ts b/src/networkProviders/transactionLogs.ts index 36f5d021d..36c10690d 100644 --- a/src/networkProviders/transactionLogs.ts +++ b/src/networkProviders/transactionLogs.ts @@ -1,10 +1,10 @@ +import { Address } from "../address"; import { ErrUnexpectedCondition } from "./../errors"; import { IAddress } from "./interface"; -import { Address } from "./primitives"; import { TransactionEvent } from "./transactionEvents"; export class TransactionLogs { - address: IAddress = new Address(""); + address: IAddress = Address.empty(); events: TransactionEvent[] = []; constructor(init?: Partial) { diff --git a/src/networkProviders/transactionReceipt.ts b/src/networkProviders/transactionReceipt.ts index 07e72fb56..49d14d62e 100644 --- a/src/networkProviders/transactionReceipt.ts +++ b/src/networkProviders/transactionReceipt.ts @@ -1,17 +1,17 @@ +import { Address } from "../address"; import { IAddress } from "./interface"; -import { Address } from "./primitives"; export class TransactionReceipt { value: string = ""; - sender: IAddress = new Address(""); + sender: IAddress = Address.empty(); data: string = ""; hash: string = ""; static fromHttpResponse(response: { - value: string, - sender: string, - data: string, - txHash: string + value: string; + sender: string; + data: string; + txHash: string; }): TransactionReceipt { let receipt = new TransactionReceipt(); diff --git a/src/networkProviders/transactions.ts b/src/networkProviders/transactions.ts index beaecc0a8..232691315 100644 --- a/src/networkProviders/transactions.ts +++ b/src/networkProviders/transactions.ts @@ -1,12 +1,12 @@ -import { TransactionStatus } from "./transactionStatus"; +import { Address } from "../address"; import { ContractResults } from "./contractResults"; -import { Address } from "./primitives"; import { IAddress, ITransaction, ITransactionNext } from "./interface"; import { TransactionLogs } from "./transactionLogs"; import { TransactionReceipt } from "./transactionReceipt"; +import { TransactionStatus } from "./transactionStatus"; export function prepareTransactionForBroadcasting(transaction: ITransaction | ITransactionNext): any { - if ("toSendable" in transaction){ + if ("toSendable" in transaction) { return transaction.toSendable(); } @@ -15,8 +15,12 @@ export function prepareTransactionForBroadcasting(transaction: ITransaction | IT value: transaction.value.toString(), receiver: transaction.receiver, sender: transaction.sender, - senderUsername: transaction.senderUsername ? Buffer.from(transaction.senderUsername).toString("base64") : undefined, - receiverUsername: transaction.receiverUsername ? Buffer.from(transaction.receiverUsername).toString("base64") : undefined, + senderUsername: transaction.senderUsername + ? Buffer.from(transaction.senderUsername).toString("base64") + : undefined, + receiverUsername: transaction.receiverUsername + ? Buffer.from(transaction.receiverUsername).toString("base64") + : undefined, gasPrice: Number(transaction.gasPrice), gasLimit: Number(transaction.gasLimit), data: transaction.data.length === 0 ? undefined : Buffer.from(transaction.data).toString("base64"), @@ -25,8 +29,11 @@ export function prepareTransactionForBroadcasting(transaction: ITransaction | IT options: transaction.options, guardian: transaction.guardian || undefined, signature: Buffer.from(transaction.signature).toString("hex"), - guardianSignature: transaction.guardianSignature.length === 0 ? undefined : Buffer.from(transaction.guardianSignature).toString("hex"), - } + guardianSignature: + transaction.guardianSignature.length === 0 + ? undefined + : Buffer.from(transaction.guardianSignature).toString("hex"), + }; } export class TransactionOnNetwork { @@ -37,8 +44,8 @@ export class TransactionOnNetwork { round: number = 0; epoch: number = 0; value: string = ""; - receiver: IAddress = new Address(""); - sender: IAddress = new Address(""); + receiver: IAddress = Address.empty(); + sender: IAddress = Address.empty(); gasLimit: number = 0; gasPrice: number = 0; function: string = ""; @@ -59,13 +66,17 @@ export class TransactionOnNetwork { Object.assign(this, init); } - static fromProxyHttpResponse(txHash: string, response: any, processStatus?: TransactionStatus | undefined): TransactionOnNetwork { + static fromProxyHttpResponse( + txHash: string, + response: any, + processStatus?: TransactionStatus | undefined, + ): TransactionOnNetwork { let result = TransactionOnNetwork.fromHttpResponse(txHash, response); result.contractResults = ContractResults.fromProxyHttpResponse(response.smartContractResults || []); if (processStatus) { result.status = processStatus; - result.isCompleted = result.status.isSuccessful() || result.status.isFailed() + result.isCompleted = result.status.isSuccessful() || result.status.isFailed(); } return result; diff --git a/src/testutils/dummyQuery.ts b/src/testutils/dummyQuery.ts index 525eeaa0c..076369df3 100644 --- a/src/testutils/dummyQuery.ts +++ b/src/testutils/dummyQuery.ts @@ -3,8 +3,8 @@ import { IAddress } from "../interface"; import { IContractQuery } from "../networkProviders/interface"; export class MockQuery implements IContractQuery { - caller: IAddress = new Address(""); - address: IAddress = new Address(""); + caller: IAddress = Address.empty(); + address: IAddress = Address.empty(); func: string = ""; args: string[] = []; value: string = ""; diff --git a/src/tokenTransferBuilders.ts b/src/tokenTransferBuilders.ts index 7760ca7d9..a557ca363 100644 --- a/src/tokenTransferBuilders.ts +++ b/src/tokenTransferBuilders.ts @@ -35,7 +35,7 @@ export class ESDTTransferPayloadBuilder { */ export class ESDTNFTTransferPayloadBuilder { payment: ITokenTransfer = TokenTransfer.nonFungible("", 0); - destination: IAddress = new Address(""); + destination: IAddress = Address.empty(); setPayment(payment: ITokenTransfer): ESDTNFTTransferPayloadBuilder { this.payment = payment; @@ -70,7 +70,7 @@ export class ESDTNFTTransferPayloadBuilder { */ export class MultiESDTNFTTransferPayloadBuilder { payments: ITokenTransfer[] = []; - destination: IAddress = new Address(""); + destination: IAddress = Address.empty(); setPayments(payments: ITokenTransfer[]): MultiESDTNFTTransferPayloadBuilder { this.payments = payments;