Skip to content

Commit

Permalink
Merge pull request #477 from casper-ecosystem/utils-enhancement
Browse files Browse the repository at this point in the history
Aligned casing for fromJSON and toJSON, updated type for chain name for utils
  • Loading branch information
Comp0te authored Dec 23, 2024
2 parents 11b671b + ef81a1e commit 7664c82
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/sse/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ export class RawEvent {
parseAsTransactionProcessedEvent(): TransactionProcessedEvent {
return this.parseEvent(
TransactionProcessedEvent,
TransactionProcessedEvent.fromJson
TransactionProcessedEvent.fromJSON
);
}

parseAsTransactionAcceptedEvent(): TransactionAcceptedEvent {
return this.parseEvent(
TransactionAcceptedEvent,
TransactionAcceptedEvent.fromJson
TransactionAcceptedEvent.fromJSON
);
}

Expand Down Expand Up @@ -340,7 +340,7 @@ export class TransactionAcceptedEvent {
})
transactionAcceptedPayload: TransactionAcceptedPayload;

public static fromJson(data: unknown): TransactionAcceptedEvent | Error {
public static fromJSON(data: unknown): TransactionAcceptedEvent | Error {
try {
const transactionEvent = TypedJSON.parse(data, TransactionAcceptedEvent);
if (!transactionEvent) throw new Error('TransactionAcceptedEvent is nil');
Expand Down Expand Up @@ -475,7 +475,7 @@ export class TransactionProcessedEvent {
})
transactionProcessedPayload: TransactionProcessedPayload;

public static fromJson(data: any): TransactionProcessedEvent | Error {
public static fromJSON(data: any): TransactionProcessedEvent | Error {
try {
const transactionEvent = TypedJSON.parse(data, TransactionProcessedEvent);
if (!transactionEvent)
Expand Down
2 changes: 1 addition & 1 deletion src/types/Deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('Deploy', () => {
await deploy.sign(senderKey);
await deploy.sign(recipientKey);

const json = Deploy.toJson(deploy);
const json = Deploy.toJSON(deploy);

deploy = Deploy.fromJSON(json);

Expand Down
2 changes: 1 addition & 1 deletion src/types/Deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ export class Deploy {
* @param deploy The deploy object to convert to JSON.
* @returns A JSON representation of the deploy.
*/
public static toJson = (deploy: Deploy) => {
public static toJSON = (deploy: Deploy) => {
const serializer = new TypedJSON(Deploy);
return serializer.toPlainJson(deploy);
};
Expand Down
4 changes: 2 additions & 2 deletions src/types/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class TransactionV1 {
* @param transaction The `TransactionV1` object.
* @returns A JSON version of the `TransactionV1`.
*/
public static toJson = (transaction: TransactionV1) => {
public static toJSON = (transaction: TransactionV1) => {
const serializer = new TypedJSON(TransactionV1);

return serializer.toPlainJson(transaction);
Expand Down Expand Up @@ -550,7 +550,7 @@ export class Transaction {
return Deploy.newTransactionFromDeploy(deploy);
}

static fromJson(json: any): Transaction {
static fromJSON(json: any): Transaction {
try {
const txV1 = TransactionV1.fromJSON(json);

Expand Down
23 changes: 15 additions & 8 deletions src/utils/cep-18-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ import {
Deploy,
DeployHeader,
Duration,
ExecutableDeployItem, Key, KeyTypeID,
ExecutableDeployItem,
Key,
KeyTypeID,
PublicKey,
StoredContractByHash
} from '../types';
import { CasperNetworkName } from '../@types';

export interface IMakeCep18TransferDeployParams {
contractHash: string,
contractHash: string;
senderPublicKeyHex: string;
recipientPublicKeyHex: string;
transferAmount: string;
paymentAmount: string,
chainName?: CasperNetworkName;
paymentAmount: string;
chainName?: string;
ttl?: number;
}

Expand All @@ -36,8 +38,7 @@ export interface IMakeCep18TransferDeployParams {
* 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).
* @param params.chainName - (Optional) The name of the Casper network chain - {CasperNetworkName}.
* Must be either `'casper'` (mainnet) or `'casper-test'` (testnet).
* @param params.chainName - (Optional) The name of the Casper network chain.
* Defaults to `'CasperNetworkName.Mainnet'` if not specified.
* @param params.ttl - (Optional) The time-to-live (TTL) for the `Deploy` in milliseconds.
* Specifies how long the `Deploy` is valid before it expires.
Expand All @@ -59,14 +60,15 @@ export interface IMakeCep18TransferDeployParams {
* console.log('Created Deploy:', deploy);
* ```
*/

export const makeCep18TransferDeploy = ({
contractHash,
senderPublicKeyHex,
recipientPublicKeyHex,
transferAmount,
paymentAmount,
chainName = CasperNetworkName.Mainnet,
ttl = DEFAULT_DEPLOY_TTL,
ttl = DEFAULT_DEPLOY_TTL
}: IMakeCep18TransferDeployParams): Deploy => {
const senderPublicKey = PublicKey.newPublicKey(senderPublicKeyHex);
const recipientPublicKey = PublicKey.newPublicKey(recipientPublicKeyHex);
Expand All @@ -77,7 +79,12 @@ export const makeCep18TransferDeploy = ({
ContractHash.newContract(contractHash),
'transfer',
Args.fromMap({
recipient: CLValue.newCLKey(Key.createByType(recipientPublicKey.accountHash().toPrefixedString(), KeyTypeID.Account)),
recipient: CLValue.newCLKey(
Key.createByType(
recipientPublicKey.accountHash().toPrefixedString(),
KeyTypeID.Account
)
),
amount: CLValueUInt256.newCLUInt256(transferAmount)
})
);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/cep-nft-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface IMakeNftTransferDeployParams {
senderPublicKeyHex: string;
recipientPublicKeyHex: string;
paymentAmount: string;
chainName?: CasperNetworkName;
chainName?: string;
ttl?: number;
tokenId?: string;
tokenHash?: string;
Expand Down
5 changes: 2 additions & 3 deletions src/utils/cspr-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface IMakeCsprTransferDeployParams {
senderPublicKeyHex: string;
recipientPublicKeyHex: string;
transferAmount: string;
chainName?: CasperNetworkName;
chainName?: string;
memo?: string;
ttl?: number;
}
Expand All @@ -29,8 +29,7 @@ export interface IMakeCsprTransferDeployParams {
* @param params.transferAmount - The amount of CSPR to transfer.
* 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).
* @param params.chainName - (Optional) The name of the Casper network chain.
* Defaults to `'CasperNetworkName.Mainnet'` if not specified.
* @param params.memo - (Optional) Tag/Memo (Comment/Note/Remark)
* Most exchanges require a Tag/Memo for CSPR deposits to be credited correctly.
Expand Down

0 comments on commit 7664c82

Please sign in to comment.