Skip to content

Commit

Permalink
Add makeCsprTransferDeploy and makeAuctionManagerDeploy utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Comp0te committed Nov 24, 2024
1 parent 671f634 commit deb37a0
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 222 deletions.
118 changes: 0 additions & 118 deletions src/@types/casperWallet.d.ts

This file was deleted.

104 changes: 0 additions & 104 deletions src/@types/casperlabsSigner.d.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/@types/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export enum CasperNetworkName {
Mainnet = 'casper',
Testnet = 'casper-test'
}

export enum AuctionManagerEntryPoint {
delegate = 'delegate',
undelegate = 'undelegate',
redelegate = 'redelegate'
}
1 change: 1 addition & 0 deletions src/@types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './common'
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './types';
export * from './rpc';
export * from './sse';
export * from './utils';
export * from './@types';
100 changes: 100 additions & 0 deletions src/utils/auction-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {
Args,
CLValue,
CLValueUInt512,
ContractHash,
Deploy,
DeployHeader,
ExecutableDeployItem,
PublicKey,
StoredContractByHash
} from "../types";
import { AuctionManagerEntryPoint, CasperNetworkName } from "../@types";
import { AuctionManagerContractHashMap } from "./constants";

export interface IMakeAuctionManagerDeployParams {
contractEntryPoint: AuctionManagerEntryPoint,
delegatorPublicKeyHex: string,
validatorPublicKeyHex: string,
newValidatorPublicKeyHex?: string,
amount: string,
paymentAmount?: string,
chainName?: CasperNetworkName;
}

/**
* Creates a deploy for the Auction Manager contract.
*
* This function generates a deploy that interacts with the Auction Manager
* contract on the Casper network. It supports operations such as delegation,
* un-delegation, and validator change management.
*
* @param params - The parameters required to create the deploy.
* @param params.contractEntryPoint - The entry point to invoke in the Auction Manager contract.
* @param params.delegatorPublicKeyHex - The delegator's public key in hexadecimal format.
* @param params.validatorPublicKeyHex - The validator's public key in hexadecimal format.
* @param params.newValidatorPublicKeyHex - (Optional) The new validator's public key in hexadecimal format (used in validator change operations).
* @param params.amount - The amount of CSPR to delegate/undelegate/redelegate.
* This value must be represented in its smallest unit (motes).
* For example, to transfer 2.5 CSPR, provide the value `2500000000` (2.5 * 10^9 motes).
* @param params.paymentAmount - The amount of CSPR to pay a network fee.
* This value must be represented in its smallest unit (motes).
* For example, to transfer 2.5 CSPR, provide the value `2500000000` (2.5 * 10^9 motes).
* @param params.chainName - (Optional) The name of the Casper network chain - {CasperNetworkName}.
* Must be either `'casper'` (mainnet) or `'casper-test'` (testnet).
* Defaults to `'CasperNetworkName.Mainnet'` if not specified.
*
* @returns A deploy object that can be signed and sent to the network.
*
* @throws {Error} Throws an error if required parameters are missing or invalid.
*
* @example
* ```ts
* import { makeAuctionManagerDeploy, AuctionManagerEntryPoint } from 'casper-js-sdk';
*
* const deploy = makeAuctionManagerDeploy({
* contractEntryPoint: AuctionManagerEntryPoint.delegate,
* delegatorPublicKeyHex: "0123456789abcdef...",
* validatorPublicKeyHex: "0123456789awedef...",
* amount: "500000000000",
* });
* ```
*/
export const makeAuctionManagerDeploy = ({
delegatorPublicKeyHex,
validatorPublicKeyHex,
contractEntryPoint,
amount,
paymentAmount = '2500000000',
chainName = CasperNetworkName.Mainnet,
newValidatorPublicKeyHex,
}: IMakeAuctionManagerDeployParams
) => {
const delegatorPublicKey = PublicKey.newPublicKey(delegatorPublicKeyHex);
const validatorPublicKey = PublicKey.newPublicKey(validatorPublicKeyHex);
const newValidatorValidatorPublicKey = newValidatorPublicKeyHex
? PublicKey.newPublicKey(newValidatorPublicKeyHex)
: null;

const session = new ExecutableDeployItem();
session.storedContractByHash = new StoredContractByHash(
ContractHash.newContract(AuctionManagerContractHashMap[chainName]),
contractEntryPoint,
Args.fromMap({
validator: CLValue.newCLPublicKey(validatorPublicKey),
delegator: CLValue.newCLPublicKey(delegatorPublicKey),
amount: CLValueUInt512.newCLUInt512(amount),
...(newValidatorValidatorPublicKey ? {
new_validator: CLValue.newCLPublicKey(newValidatorValidatorPublicKey)
} : {})
})
);

const payment = ExecutableDeployItem.standardPayment(paymentAmount);

const deployHeader = DeployHeader.default();
deployHeader.account = delegatorPublicKey;
deployHeader.chainName = chainName;

return Deploy.makeDeploy(deployHeader, payment, session);
};
6 changes: 6 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { CasperNetworkName } from "../@types";

export const AuctionManagerContractHashMap: Record<CasperNetworkName, string> = {
[CasperNetworkName.Mainnet] : 'ccb576d6ce6dec84a551e48f0d0b7af89ddba44c7390b690036257a04a3ae9ea',
[CasperNetworkName.Testnet] : '93d923e336b20a4c4ca14d592b60e5bd3fe330775618290104f9beb326db7ae2'
}
Loading

0 comments on commit deb37a0

Please sign in to comment.