Skip to content

Commit

Permalink
Merge pull request #464 from casper-ecosystem/feat-utils-and-fixes
Browse files Browse the repository at this point in the history
Feat utils and fixes
  • Loading branch information
Comp0te authored Dec 16, 2024
2 parents 016ee84 + 4efa3ff commit 7122398
Show file tree
Hide file tree
Showing 19 changed files with 600 additions and 225 deletions.
33 changes: 33 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,39 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Removed
-->

## [5.0.0-rc8] - 2024-12-16

### Added

- `makeCep18TransferDeploy` and `makeNftTransferDeploy` utils
- `PrivateKey.toBytes` method
- Rename Key's TypeID to KeyTypeID
- Add initial list values to `CLValueList.newCLList`
- `Args.getByName` getter
- Remove unused `Effects` class
- Improvements for `TransformKind` transformation parsing

### Fixed

- Issue with `ed25519` private key length

## [5.0.0-rc7] - 2024-12-13

### Added

- Checksummed `PublicKey` hex
- Improvements in `Transaction` creation from JSON
- Ability to send `Deploy` with the `RPC.putTransaction` method
- Renamed `StoredValue.prepaid` to `StoredValue.prepayment`
- Improvements in RPC client error processing

### Fixed

- Issue with implicit `axios` dependency
- Issue with `secp256k1` bytes
- Issue with `StepPayload.executionEffects` annotations
- Issue with `ExecutionResult` parsing from JSON

## [5.0.0-rc6] - 2024-12-08

### Added
Expand Down
78 changes: 78 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ npm install casper-js-sdk --save
- [Creating a legacy deploy](#creating-a-legacy-deploy)
- [Creating and sending CSPR transfer deploy](#creating-and-sending-cspr-transfer-deploy)
- [Creating and sending Auction manager deploy](#creating-and-sending-auction-manager-deploy)
- [Creating and sending CEP-18 transfer deploy](#creating-and-sending-cep-18-transfer-deploy)
- [Creating and sending NFT transfer deploy](#creating-and-sending-nft-transfer-deploy)

## Migration guides

Expand Down Expand Up @@ -310,3 +312,79 @@ const result = await rpcClient.putDeploy(deploy);

console.log(`Deploy Hash: ${result.deployHash}`);
```

### Creating and sending CEP-18 transfer deploy

Example of how to construct a CEP-18 transfer deploy and push it to the network:

```ts
import {
HttpHandler,
RpcClient,
KeyAlgorithm,
PrivateKey,
makeCep18TransferDeploy
} from 'casper-js-sdk';

// get private key fromHex, fromPem or generate it
const privateKey = await PrivateKey.fromHex(
'privateKeyHex',
KeyAlgorithm.SECP256K1 // or KeyAlgorithm.ED25519, depends on your private key
);

const deploy = await makeCep18TransferDeploy({
contractHash: '0123456789asdfbcdef...',
senderPublicKeyHex: '0123456789asdfbcdef...',
recipientPublicKeyHex: '0123456789abcdef...',
transferAmount: '25000000000', // 25 CEP-18 with 9 decimals
paymentAmount: '3000000000' // 3 CSPR
});

await deploy.sign(privateKey);

const rpcHandler = new HttpHandler('http://<Node Address>:7777/rpc');
const rpcClient = new RpcClient(rpcHandler);

const result = await rpcClient.putDeploy(deploy);

console.log(`Deploy Hash: ${result.deployHash}`);
```

### Creating and sending NFT transfer deploy

Example of how to construct a NFT transfer deploy and push it to the network:

```ts
import {
HttpHandler,
RpcClient,
KeyAlgorithm,
PrivateKey,
makeNftTransferDeploy,
NFTTokenStandard
} from 'casper-js-sdk';

// get private key fromHex, fromPem or generate it
const privateKey = await PrivateKey.fromHex(
'privateKeyHex',
KeyAlgorithm.SECP256K1 // or KeyAlgorithm.ED25519, depends on your private key
);

const deploy = await makeNftTransferDeploy({
nftStandard: NFTTokenStandard.CEP47,
contractPackageHash: '0123456789asdfbcdef...',
senderPublicKeyHex: '0123456789asdfbcdef...',
recipientPublicKeyHex: '0123456789abcdef...',
paymentAmount: '3000000000', // 3 CSPR
tokenId: 234
});

await deploy.sign(privateKey);

const rpcHandler = new HttpHandler('http://<Node Address>:7777/rpc');
const rpcClient = new RpcClient(rpcHandler);

const result = await rpcClient.putDeploy(deploy);

console.log(`Deploy Hash: ${result.deployHash}`);
```
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "casper-js-sdk",
"version": "5.0.0-rc7",
"version": "5.0.0-rc8",
"license": "Apache 2.0",
"description": "SDK to interact with the Casper blockchain",
"homepage": "https://github.com/casper-ecosystem/casper-js-sdk#README.md",
Expand Down
5 changes: 5 additions & 0 deletions src/@types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ export enum AuctionManagerEntryPoint {
undelegate = 'undelegate',
redelegate = 'redelegate'
}

export enum NFTTokenStandard {
CEP47 = 'CEP47',
CEP78 = 'CEP78'
}
4 changes: 4 additions & 0 deletions src/types/Args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ export class Args {
})
public args: Map<string, CLValue>;

public getByName(argName: string): CLValue | undefined {
return this.args.get(argName);
}

/**
* Creates an instance of `Args` from a map of arguments.
* @param args - A map containing argument names as keys and `CLValue` instances as values.
Expand Down
7 changes: 0 additions & 7 deletions src/types/ExecutionResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ export class Operation {
public kind: string;
}

/**
* A collection of transformations applied during a transaction.
* A log of all transforms produced during execution, used only in 2.0+ Network
*/
@jsonObject
export class Effects extends Array<Transform> {}

/**
* Represents the effect of a transaction, including the operations and transformations.
*/
Expand Down
46 changes: 31 additions & 15 deletions src/types/Transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class TransformKind {
* @returns `true` if the transformation is a WriteTransfer, otherwise `false`.
*/
public isWriteTransfer(): boolean {
return this.data.includes?.('WriteTransfer') ?? false;
return this.isTransformation('WriteTransfer');
}

/**
Expand All @@ -81,7 +81,7 @@ export class TransformKind {
* @returns `true` if the transformation is a WriteAccount, otherwise `false`.
*/
public isWriteAccount(): boolean {
return this.data.includes?.('WriteAccount') ?? false;
return this.isTransformation('WriteAccount');
}

/**
Expand All @@ -90,7 +90,7 @@ export class TransformKind {
* @returns `true` if the transformation is a WriteContract, otherwise `false`.
*/
public isWriteContract(): boolean {
return this.data === '"WriteContract"';
return this.isTransformation('WriteContract');
}

/**
Expand All @@ -99,7 +99,7 @@ export class TransformKind {
* @returns `true` if the transformation is a WriteWithdraw, otherwise `false`.
*/
public isWriteWithdraw(): boolean {
return this.data.includes?.('WriteWithdraw') ?? false;
return this.isTransformation('WriteWithdraw');
}

/**
Expand All @@ -108,7 +108,7 @@ export class TransformKind {
* @returns `true` if the transformation is a WriteUnbonding, otherwise `false`.
*/
public isWriteUnbonding(): boolean {
return this.data.includes?.('WriteUnbonding') ?? false;
return this.isTransformation('WriteUnbonding');
}

/**
Expand All @@ -117,7 +117,7 @@ export class TransformKind {
* @returns `true` if the transformation is a WriteCLValue, otherwise `false`.
*/
public isWriteCLValue(): boolean {
return this.data.includes?.('CLValue') ?? false;
return this.isTransformation('CLValue');
}

/**
Expand All @@ -126,7 +126,7 @@ export class TransformKind {
* @returns `true` if the transformation is a WritePackage, otherwise `false`.
*/
public isWritePackage(): boolean {
return this.data.includes?.('"Package"') ?? false;
return this.isTransformation('Package');
}

/**
Expand All @@ -135,7 +135,7 @@ export class TransformKind {
* @returns `true` if the transformation is a WriteAddressableEntity, otherwise `false`.
*/
public isWriteAddressableEntity(): boolean {
return this.data.includes?.('"AddressableEntity"') ?? false;
return this.isTransformation('AddressableEntity');
}

/**
Expand All @@ -144,7 +144,7 @@ export class TransformKind {
* @returns `true` if the transformation is a WriteBidKind, otherwise `false`.
*/
public isWriteBidKind(): boolean {
return this.data.includes?.('"BidKind"') ?? false;
return this.isTransformation('BidKind');
}

/**
Expand All @@ -153,7 +153,7 @@ export class TransformKind {
* @returns `true` if the transformation is a WriteNamedKey, otherwise `false`.
*/
public isWriteNamedKey(): boolean {
return this.data.includes?.('"NamedKey"') ?? false;
return this.isTransformation('NamedKey');
}

/**
Expand All @@ -162,7 +162,7 @@ export class TransformKind {
* @returns `true` if the transformation is a WriteMessage, otherwise `false`.
*/
public isWriteMessage(): boolean {
return this.data.includes?.('"Message"') ?? false;
return this.isTransformation('Message');
}

/**
Expand All @@ -171,7 +171,7 @@ export class TransformKind {
* @returns `true` if the transformation is a WriteMessageTopic, otherwise `false`.
*/
public isWriteMessageTopic(): boolean {
return this.data.includes?.('"MessageTopic"') ?? false;
return this.isTransformation('MessageTopic');
}

/**
Expand All @@ -180,7 +180,7 @@ export class TransformKind {
* @returns `true` if the transformation is a WriteBid, otherwise `false`.
*/
public isWriteBid(): boolean {
return this.data.includes?.('WriteBid') ?? false;
return this.isTransformation('WriteBid');
}

/**
Expand All @@ -189,7 +189,7 @@ export class TransformKind {
* @returns `true` if the transformation is AddUInt512, otherwise `false`.
*/
public isAddUint512(): boolean {
return this.data.includes?.('AddUInt512') ?? false;
return this.isTransformation('AddUInt512');
}

/**
Expand All @@ -198,7 +198,7 @@ export class TransformKind {
* @returns `true` if the transformation is a WriteDeployInfo, otherwise `false`.
*/
public isWriteDeployInfo(): boolean {
return this.data.includes?.('WriteDeployInfo') ?? false;
return this.isTransformation('WriteDeployInfo');
}

/**
Expand Down Expand Up @@ -398,6 +398,22 @@ export class TransformKind {

return jsonRes2.Write?.CLValue;
}

/**
* Checks if `TransformKind` has the transformation specified by name.
*
* @param `name` - transformation name (aka WriteTransfer)
* @returns `true` if the transformation is a WriteTransfer, otherwise `false`.
*/
public isTransformation(name: string): boolean {
if (typeof this.data === 'string') {
return this.data.includes(name);
} else if (typeof this.data === 'object') {
return Object.keys(this.data).some(key => key.includes(name));
}

return false;
}
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/types/clvalue/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,13 @@ export class CLValueList {
/**
* Creates a new CLValue instance with a List value.
* @param elementType - The CLType for the elements of the list.
* @param elements - Optional array of CLValues to initialize the list with.
* @returns A new CLValue instance containing CLTypeList and a CLValueList.
*/
public static newCLList(elementType: CLType): CLValue {
public static newCLList(elementType: CLType, elements: CLValue[] = []): CLValue {
const listType = new CLTypeList(elementType);
const clValue = new CLValue(listType);
clValue.list = new CLValueList(listType);
clValue.list = new CLValueList(listType, elements);
return clValue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/clvalue/Tuple3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class CLValueTuple3 {
* Retrieves the values of the tuple as an array.
* @returns An array containing the three CLValues of the tuple.
*/
public getValue(): [CLValue, CLValue, CLValue] {
public value(): [CLValue, CLValue, CLValue] {
return [this.inner1, this.inner2, this.inner3];
}

Expand Down
Loading

0 comments on commit 7122398

Please sign in to comment.