-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replaced BigNumber with BigInt #384
Changes from all commits
26be467
57a6e19
4680e99
fcc1021
b3ce9ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
import { Query } from "./query"; | ||
import { WasmVirtualMachine } from "./transactionPayloadBuilders"; | ||
import { EndpointDefinition, TypedValue } from "./typesystem"; | ||
const createKeccakHash = require("keccak"); | ||
|
||
interface IAbi { | ||
constructorDefinition: EndpointDefinition; | ||
|
@@ -130,7 +130,7 @@ | |
const nextTx = scNextTransactionFactory.createTransactionForDeploy({ | ||
sender: deployer, | ||
bytecode: bytecode, | ||
gasLimit: gasLimit.valueOf(), | ||
gasLimit: BigInt(gasLimit.valueOf()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Upcasting, because before we had |
||
args: initArguments, | ||
isUpgradeable: metadataAsJson.upgradeable, | ||
isReadable: metadataAsJson.readable, | ||
|
@@ -191,7 +191,7 @@ | |
sender: caller, | ||
contract: this.getAddress(), | ||
bytecode: bytecode, | ||
gasLimit: gasLimit.valueOf(), | ||
gasLimit: BigInt(gasLimit.valueOf()), | ||
args: initArguments, | ||
isUpgradeable: metadataAsJson.upgradeable, | ||
isReadable: metadataAsJson.readable, | ||
|
@@ -229,7 +229,7 @@ | |
sender: caller, | ||
contract: receiver ? receiver : this.getAddress(), | ||
functionName: func.toString(), | ||
gasLimit: gasLimit.valueOf(), | ||
gasLimit: BigInt(gasLimit.valueOf()), | ||
args: args | ||
}) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,8 @@ describe("test transaction construction", async () => { | |
const plainTransactionNextObject = { | ||
sender: "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", | ||
receiver: "erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx", | ||
gasLimit: 56000, | ||
value: "1000000000000000000", | ||
gasLimit: 56000n, | ||
value: 1000000000000000000n, | ||
data: Buffer.from("test"), | ||
chainID: "T" | ||
}; | ||
|
@@ -32,8 +32,8 @@ describe("test transaction construction", async () => { | |
const transaction = Transaction.fromTransactionNext(nextTransaction); | ||
assert.deepEqual(transaction.getSender(), Address.fromBech32(plainTransactionNextObject.sender)); | ||
assert.deepEqual(transaction.getReceiver(), Address.fromBech32(plainTransactionNextObject.receiver)); | ||
assert.equal(transaction.getGasLimit().valueOf(), plainTransactionNextObject.gasLimit); | ||
assert.equal(transaction.getValue().toString(), plainTransactionNextObject.value); | ||
assert.equal(transaction.getGasLimit().valueOf().toFixed(0), plainTransactionNextObject.gasLimit.toString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure why does this work, since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
assert.equal(transaction.getValue().toString(), plainTransactionNextObject.value.toString()); | ||
assert.equal(transaction.getData().toString(), plainTransactionNextObject.data.toString()); | ||
assert.equal(transaction.getChainID().valueOf(), plainTransactionNextObject.chainID); | ||
assert.equal(transaction.getNonce().valueOf(), 0); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -428,9 +428,9 @@ export class Transaction { | |
const tx = new Transaction({ | ||
sender: Address.fromBech32(transaction.sender), | ||
receiver: Address.fromBech32(transaction.receiver), | ||
gasLimit: new BigNumber(transaction.gasLimit).toNumber(), | ||
gasLimit: Number(transaction.gasLimit), | ||
chainID: transaction.chainID, | ||
value: new BigNumber(transaction.value).toFixed(0), | ||
value: new BigNumber(transaction.value.toString()).toFixed(0), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is OK. |
||
data: new TransactionPayload(Buffer.from(transaction.data)), | ||
nonce: Number(transaction.nonce), | ||
gasPrice: Number(transaction.gasPrice), | ||
|
@@ -485,12 +485,12 @@ export class TransactionNext { | |
/** | ||
* The nonce of the transaction (the account sequence number of the sender). | ||
*/ | ||
public nonce: BigNumber.Value; | ||
public nonce: bigint; | ||
|
||
/** | ||
* The value to transfer. | ||
*/ | ||
public value: BigNumber.Value; | ||
public value: bigint; | ||
|
||
/** | ||
* The address of the sender. | ||
|
@@ -515,12 +515,12 @@ export class TransactionNext { | |
/** | ||
* The gas price to be used. | ||
*/ | ||
public gasPrice: BigNumber.Value; | ||
public gasPrice: bigint; | ||
|
||
/** | ||
* The maximum amount of gas to be consumed when processing the transaction. | ||
*/ | ||
public gasLimit: BigNumber.Value; | ||
public gasLimit: bigint; | ||
|
||
/** | ||
* The payload of the transaction. | ||
|
@@ -575,27 +575,27 @@ export class TransactionNext { | |
options, | ||
guardian, | ||
}: { | ||
nonce?: BigNumber.Value; | ||
value?: BigNumber.Value; | ||
nonce?: bigint; | ||
value?: bigint; | ||
sender: string; | ||
receiver: string; | ||
senderUsername?: string; | ||
receiverUsername?: string; | ||
gasPrice?: BigNumber.Value; | ||
gasLimit: BigNumber.Value; | ||
gasPrice?: bigint; | ||
gasLimit: bigint; | ||
data?: Uint8Array; | ||
chainID: string; | ||
version?: number; | ||
options?: number; | ||
guardian?: string; | ||
}) { | ||
this.nonce = nonce || 0; | ||
this.value = value || new BigNumber(0); | ||
this.nonce = nonce || 0n; | ||
this.value = value || 0n; | ||
this.sender = sender; | ||
this.receiver = receiver; | ||
this.senderUsername = senderUsername || ""; | ||
this.receiverUsername = receiverUsername || ""; | ||
this.gasPrice = gasPrice || new BigNumber(TRANSACTION_MIN_GAS_PRICE); | ||
this.gasPrice = gasPrice || BigInt(TRANSACTION_MIN_GAS_PRICE); | ||
this.gasLimit = gasLimit; | ||
this.data = data || new Uint8Array(); | ||
this.chainID = chainID; | ||
|
@@ -614,26 +614,24 @@ export class TransactionNext { | |
export class TransactionComputer { | ||
constructor() { } | ||
|
||
computeTransactionFee(transaction: ITransactionNext, networkConfig: INetworkConfig): BigNumber { | ||
const moveBalanceGas = new BigNumber( | ||
computeTransactionFee(transaction: ITransactionNext, networkConfig: INetworkConfig): bigint { | ||
const moveBalanceGas = BigInt( | ||
networkConfig.MinGasLimit + transaction.data.length * networkConfig.GasPerDataByte); | ||
if (moveBalanceGas > transaction.gasLimit) { | ||
throw new errors.ErrNotEnoughGas(parseInt(transaction.gasLimit.toString(), 10)); | ||
} | ||
|
||
const gasPrice = new BigNumber(transaction.gasPrice); | ||
const feeForMove = moveBalanceGas.multipliedBy(gasPrice); | ||
const gasPrice = transaction.gasPrice; | ||
const feeForMove = moveBalanceGas * gasPrice; | ||
if (moveBalanceGas === transaction.gasLimit) { | ||
return feeForMove; | ||
} | ||
|
||
const diff = new BigNumber(transaction.gasLimit).minus(moveBalanceGas); | ||
const modifiedGasPrice = gasPrice.multipliedBy( | ||
new BigNumber(networkConfig.GasPriceModifier) | ||
); | ||
const processingFee = diff.multipliedBy(modifiedGasPrice); | ||
const diff = transaction.gasLimit - moveBalanceGas; | ||
const modifiedGasPrice = BigInt(new BigNumber(gasPrice.toString()).multipliedBy(new BigNumber(networkConfig.GasPriceModifier)).toFixed(0)); | ||
const processingFee = diff * modifiedGasPrice; | ||
|
||
return feeForMove.plus(processingFee); | ||
return feeForMove + processingFee; | ||
} | ||
|
||
computeBytesForSigning(transaction: ITransactionNext): Uint8Array { | ||
|
@@ -671,7 +669,7 @@ export class TransactionComputer { | |
private toPlainObject(transaction: ITransactionNext) { | ||
return { | ||
nonce: Number(transaction.nonce), | ||
value: new BigNumber(transaction.value).toFixed(0), | ||
value: transaction.value.toString(), | ||
receiver: transaction.receiver, | ||
sender: transaction.sender, | ||
senderUsername: transaction.senderUsername ? Buffer.from(transaction.senderUsername).toString("base64") : undefined, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct ☑️
https://github.com/multiversx/mx-chain-core-go/blob/main/data/transaction/transaction.proto