Skip to content

Commit

Permalink
Merge pull request #377 from multiversx/add-empty-lines
Browse files Browse the repository at this point in the history
Add empty lines and ran prettier
  • Loading branch information
popenta authored Feb 13, 2024
2 parents 38d12f1 + 097384d commit 0f243be
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 38 deletions.
96 changes: 75 additions & 21 deletions src/transactionsFactories/delegationTransactionsFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Address } from "../address";
import { DELEGATION_MANAGER_SC_ADDRESS } from "../constants";
import { Err } from "../errors";
import { TransactionNext } from "../transaction";

interface Config {
chainID: string;
minGasLimit: BigNumber.Value;
Expand All @@ -18,14 +19,18 @@ interface Config {
additionalGasLimitPerValidatorNode: BigNumber.Value;
additionalGasLimitForDelegationOperations: BigNumber.Value;
}

interface IValidatorPublicKey {
hex(): string;
}

export class DelegationTransactionsFactory {
private readonly config: Config;

constructor(config: Config) {
this.config = config;
}

createTransactionForNewDelegationContract(options: {
sender: IAddress;
totalDelegationCap: BigNumber.Value;
Expand All @@ -37,9 +42,11 @@ export class DelegationTransactionsFactory {
numberToPaddedHex(options.totalDelegationCap.toString()),
numberToPaddedHex(options.serviceFee.toString()),
];

const executionGasLimit = new BigNumber(this.config.gasLimitCreateDelegationContract).plus(
this.config.additionalGasLimitForDelegationOperations,
);

return new TransactionNextBuilder({
config: this.config,
sender: options.sender,
Expand All @@ -50,6 +57,7 @@ export class DelegationTransactionsFactory {
amount: options.amount,
}).build();
}

createTransactionForAddingNodes(options: {
sender: IAddress;
delegationContract: IAddress;
Expand All @@ -59,11 +67,14 @@ export class DelegationTransactionsFactory {
if (options.publicKeys.length !== options.signedMessages.length) {
throw new Err("The number of public keys should match the number of signed messages");
}

const numNodes = options.publicKeys.length;
const dataParts = ["addNodes"];

for (let i = 0; i < numNodes; i++) {
dataParts.push(...[options.publicKeys[i].hex(), byteArrayToHex(options.signedMessages[i])]);
}

return new TransactionNextBuilder({
config: this.config,
sender: options.sender,
Expand All @@ -73,15 +84,18 @@ export class DelegationTransactionsFactory {
addDataMovementGas: true,
}).build();
}

createTransactionForRemovingNodes(options: {
sender: IAddress;
delegationContract: IAddress;
publicKeys: IValidatorPublicKey[];
}): TransactionNext {
const dataParts = ["removeNodes"];

for (const key of options.publicKeys) {
dataParts.push(key.hex());
}

const numNodes = options.publicKeys.length;
return new TransactionNextBuilder({
config: this.config,
Expand All @@ -92,22 +106,27 @@ export class DelegationTransactionsFactory {
addDataMovementGas: true,
}).build();
}

createTransactionForStakingNodes(options: {
sender: IAddress;
delegationContract: IAddress;
publicKeys: IValidatorPublicKey[];
}): TransactionNext {
let dataParts = ["stakeNodes"];

for (const key of options.publicKeys) {
dataParts = dataParts.concat(key.hex());
}

const numNodes = options.publicKeys.length;
const additionalGasForAllNodes = new BigNumber(numNodes).multipliedBy(
this.config.additionalGasLimitPerValidatorNode,
);

const executionGasLimit = additionalGasForAllNodes
.plus(this.config.gasLimitStake)
.plus(this.config.gasLimitDelegationOperations);

return new TransactionNextBuilder({
config: this.config,
sender: options.sender,
Expand All @@ -117,20 +136,24 @@ export class DelegationTransactionsFactory {
addDataMovementGas: true,
}).build();
}

createTransactionForUnbondingNodes(options: {
sender: IAddress;
delegationContract: IAddress;
publicKeys: IValidatorPublicKey[];
}): TransactionNext {
let dataParts = ["unBondNodes"];

for (const key of options.publicKeys) {
dataParts = dataParts.concat(key.hex());
}

const numNodes = options.publicKeys.length;
const executionGasLimit = new BigNumber(numNodes)
.multipliedBy(this.config.additionalGasLimitPerValidatorNode)
.plus(this.config.gasLimitUnbond)
.plus(this.config.gasLimitDelegationOperations);

return new TransactionNextBuilder({
config: this.config,
sender: options.sender,
Expand All @@ -140,20 +163,24 @@ export class DelegationTransactionsFactory {
addDataMovementGas: true,
}).build();
}

createTransactionForUnstakingNodes(options: {
sender: IAddress;
delegationContract: IAddress;
publicKeys: IValidatorPublicKey[];
}): TransactionNext {
let dataParts = ["unStakeNodes"];

for (const key of options.publicKeys) {
dataParts = dataParts.concat(key.hex());
}

const numNodes = options.publicKeys.length;
const executionGasLimit = new BigNumber(numNodes)
.multipliedBy(this.config.additionalGasLimitPerValidatorNode)
.plus(this.config.gasLimitUnstake)
.plus(this.config.gasLimitDelegationOperations);

return new TransactionNextBuilder({
config: this.config,
sender: options.sender,
Expand All @@ -163,15 +190,18 @@ export class DelegationTransactionsFactory {
addDataMovementGas: true,
}).build();
}

createTransactionForUnjailingNodes(options: {
sender: IAddress;
delegationContract: IAddress;
publicKeys: IValidatorPublicKey[];
}): TransactionNext {
const dataParts = ["unJailNodes"];

for (const key of options.publicKeys) {
dataParts.push(key.hex());
}

const numNodes = options.publicKeys.length;
return new TransactionNextBuilder({
config: this.config,
Expand All @@ -182,104 +212,123 @@ export class DelegationTransactionsFactory {
addDataMovementGas: true,
}).build();
}

createTransactionForChangingServiceFee(options: {
sender: IAddress;
delegationContract: IAddress;
serviceFee: BigNumber.Value;
}): TransactionNext {
const dataParts = ["changeServiceFee", numberToPaddedHex(options.serviceFee)];
const gasLimit = new BigNumber(this.config.gasLimitDelegationOperations).plus(
this.config.additionalGasLimitForDelegationOperations,
);

return new TransactionNextBuilder({
config: this.config,
sender: options.sender,
receiver: options.delegationContract,
dataParts: dataParts,
gasLimit: new BigNumber(this.config.gasLimitDelegationOperations).plus(
this.config.additionalGasLimitForDelegationOperations,
),
gasLimit: gasLimit,
addDataMovementGas: true,
}).build();
}

createTransactionForModifyingDelegationCap(options: {
sender: IAddress;
delegationContract: IAddress;
delegationCap: BigNumber.Value;
}): TransactionNext {
const dataParts = ["modifyTotalDelegationCap", numberToPaddedHex(options.delegationCap)];
const gasLimit = new BigNumber(this.config.gasLimitDelegationOperations).plus(
this.config.additionalGasLimitForDelegationOperations,
);

return new TransactionNextBuilder({
config: this.config,
sender: options.sender,
receiver: options.delegationContract,
dataParts: dataParts,
gasLimit: new BigNumber(this.config.gasLimitDelegationOperations).plus(
this.config.additionalGasLimitForDelegationOperations,
),
gasLimit: gasLimit,
addDataMovementGas: true,
}).build();
}

createTransactionForSettingAutomaticActivation(options: {
sender: IAddress;
delegationContract: IAddress;
}): TransactionNext {
const dataParts = ["setAutomaticActivation", utf8ToHex("true")];
const gasLimit = new BigNumber(this.config.gasLimitDelegationOperations).plus(
this.config.additionalGasLimitForDelegationOperations,
);

return new TransactionNextBuilder({
config: this.config,
sender: options.sender,
receiver: options.delegationContract,
dataParts: dataParts,
gasLimit: new BigNumber(this.config.gasLimitDelegationOperations).plus(
this.config.additionalGasLimitForDelegationOperations,
),
gasLimit: gasLimit,
addDataMovementGas: true,
}).build();
}

createTransactionForUnsettingAutomaticActivation(options: {
sender: IAddress;
delegationContract: IAddress;
}): TransactionNext {
const dataParts = ["setAutomaticActivation", utf8ToHex("false")];
const gasLimit = new BigNumber(this.config.gasLimitDelegationOperations).plus(
this.config.additionalGasLimitForDelegationOperations,
);

return new TransactionNextBuilder({
config: this.config,
sender: options.sender,
receiver: options.delegationContract,
dataParts: dataParts,
gasLimit: new BigNumber(this.config.gasLimitDelegationOperations).plus(
this.config.additionalGasLimitForDelegationOperations,
),
gasLimit: gasLimit,
addDataMovementGas: true,
}).build();
}

createTransactionForSettingCapCheckOnRedelegateRewards(options: {
sender: IAddress;
delegationContract: IAddress;
}): TransactionNext {
const dataParts = ["setCheckCapOnReDelegateRewards", utf8ToHex("true")];
const gasLimit = new BigNumber(this.config.gasLimitDelegationOperations).plus(
this.config.additionalGasLimitForDelegationOperations,
);

return new TransactionNextBuilder({
config: this.config,
sender: options.sender,
receiver: options.delegationContract,
dataParts: dataParts,
gasLimit: new BigNumber(this.config.gasLimitDelegationOperations).plus(
this.config.additionalGasLimitForDelegationOperations,
),
gasLimit: gasLimit,
addDataMovementGas: true,
}).build();
}

createTransactionForUnsettingCapCheckOnRedelegateRewards(options: {
sender: IAddress;
delegationContract: IAddress;
}): TransactionNext {
const dataParts = ["setCheckCapOnReDelegateRewards", utf8ToHex("false")];
const gasLimit = new BigNumber(this.config.gasLimitDelegationOperations).plus(
this.config.additionalGasLimitForDelegationOperations,
);

return new TransactionNextBuilder({
config: this.config,
sender: options.sender,
receiver: options.delegationContract,
dataParts: dataParts,
gasLimit: new BigNumber(this.config.gasLimitDelegationOperations).plus(
this.config.additionalGasLimitForDelegationOperations,
),
gasLimit: gasLimit,
addDataMovementGas: true,
}).build();
}

createTransactionForSettingMetadata(options: {
sender: IAddress;
delegationContract: IAddress;
Expand All @@ -293,21 +342,26 @@ export class DelegationTransactionsFactory {
utf8ToHex(options.website),
utf8ToHex(options.identifier),
];

const gasLimit = new BigNumber(this.config.gasLimitDelegationOperations).plus(
this.config.additionalGasLimitForDelegationOperations,
);

return new TransactionNextBuilder({
config: this.config,
sender: options.sender,
receiver: options.delegationContract,
dataParts: dataParts,
gasLimit: new BigNumber(this.config.gasLimitDelegationOperations).plus(
this.config.additionalGasLimitForDelegationOperations,
),
gasLimit: gasLimit,
addDataMovementGas: true,
}).build();
}

private computeExecutionGasLimitForNodesManagement(numNodes: number): BigNumber.Value {
const additionalGasForAllNodes = new BigNumber(this.config.additionalGasLimitPerValidatorNode).multipliedBy(
numNodes,
);

return new BigNumber(this.config.gasLimitDelegationOperations).plus(additionalGasForAllNodes);
}
}
Loading

0 comments on commit 0f243be

Please sign in to comment.