diff --git a/resources/migration-guide-v2-v5.md b/resources/migration-guide-v2-v5.md index 206f56aec..6c491e28b 100644 --- a/resources/migration-guide-v2-v5.md +++ b/resources/migration-guide-v2-v5.md @@ -78,18 +78,14 @@ const bool = CLValueBuilder.bool(false); ##### New Syntax (5.x) ```typescript -import { - CLValueList, - CLValueUInt32, - CLValueBool -} from 'casper-js-sdk'; +import { CLValue, CLTypeUInt32 } from 'casper-js-sdk'; -const list = CLValueList.newCLList(CLTypeUInt32, [ - CLValueUInt32.newCLUInt32(1), - CLValueUInt32.newCLUInt32(2), - CLValueUInt32.newCLUInt32(3) +const list = CLValue.newCLList(CLTypeUInt32, [ + CLValue.newCLUInt32(1), + CLValue.newCLUInt32(2), + CLValue.newCLUInt32(3) ]); -const bool = CLValueBool.newCLValueBool(false); +const bool = CLValue.newCLValueBool(false); ``` More examples are available in the [unit tests](https://github.com/casper-ecosystem/casper-js-sdk/tree/feat-5.0.0/src/types/clvalue). diff --git a/resources/TransactionBuilder.md b/resources/transaction-builder.md similarity index 100% rename from resources/TransactionBuilder.md rename to resources/transaction-builder.md diff --git a/src/@types/common.ts b/src/@types/common.ts index c7c6c3aa2..5ec1dea6c 100644 --- a/src/@types/common.ts +++ b/src/@types/common.ts @@ -1,7 +1,8 @@ export enum CasperNetworkName { Mainnet = 'casper', Testnet = 'casper-test', - Integration = 'integration-test' + Integration = 'integration-test', + DevNet = 'dev-net' } export enum AuctionManagerEntryPoint { @@ -10,7 +11,7 @@ export enum AuctionManagerEntryPoint { redelegate = 'redelegate', addBid = 'add_bid', withdrawBid = 'withdraw_bid', - activateBid = 'activate_bid', + activateBid = 'activate_bid' } export enum NFTTokenStandard { diff --git a/src/types/Deploy.ts b/src/types/Deploy.ts index 9cde94540..bc72609c3 100644 --- a/src/types/Deploy.ts +++ b/src/types/Deploy.ts @@ -399,6 +399,7 @@ export class Deploy { paymentLimitedMode.gasPriceTolerance = 1; paymentLimitedMode.paymentAmount = paymentAmount; paymentLimitedMode.standardPayment = standardPayment; + pricingMode.paymentLimited = paymentLimitedMode; return new Transaction( deploy.hash, diff --git a/src/types/ExecutableDeployItem.ts b/src/types/ExecutableDeployItem.ts index 2ef78288f..e3cf19f5d 100644 --- a/src/types/ExecutableDeployItem.ts +++ b/src/types/ExecutableDeployItem.ts @@ -3,17 +3,7 @@ import { BigNumber, BigNumberish } from '@ethersproject/bignumber'; import { concat } from '@ethersproject/bytes'; import { Args } from './Args'; -import { - CLTypeOption, - CLTypeUInt64, - CLValue, - CLValueByteArray, - CLValueOption, - CLValueString, - CLValueUInt32, - CLValueUInt512, - CLValueUInt64 -} from './clvalue'; +import { CLTypeOption, CLTypeUInt64, CLValue, CLValueOption } from './clvalue'; import { ContractHash, URef } from './key'; import { byteArrayJsonDeserializer, @@ -75,12 +65,10 @@ export class ModuleBytes { * @returns The serialized byte array. */ bytes(): Uint8Array { - const lengthBytes = CLValueUInt32.newCLUInt32( + const lengthBytes = CLValue.newCLUInt32( BigNumber.from(this.moduleBytes.length) ).bytes(); - const bytesArrayBytes = CLValueByteArray.newCLByteArray( - this.moduleBytes - ).bytes(); + const bytesArrayBytes = CLValue.newCLByteArray(this.moduleBytes).bytes(); let result = concat([lengthBytes, bytesArrayBytes]); @@ -142,7 +130,7 @@ export class StoredContractByHash { */ bytes(): Uint8Array { const hashBytes = this.hash.hash.toBytes(); - const entryPointBytes = CLValueString.newCLString(this.entryPoint).bytes(); + const entryPointBytes = CLValue.newCLString(this.entryPoint).bytes(); const argBytes = this.args.toBytes(); return concat([hashBytes, entryPointBytes, argBytes]); @@ -192,8 +180,8 @@ export class StoredContractByName { * @returns The serialized byte array. */ bytes(): Uint8Array { - const nameBytes = CLValueString.newCLString(this.name).bytes(); - const entryPointBytes = CLValueString.newCLString(this.entryPoint).bytes(); + const nameBytes = CLValue.newCLString(this.name).bytes(); + const entryPointBytes = CLValue.newCLString(this.entryPoint).bytes(); const argBytes = this.args.toBytes(); return concat([nameBytes, entryPointBytes, argBytes]); @@ -263,11 +251,9 @@ export class StoredVersionedContractByHash { bytes(): Uint8Array { const hashBytes = this.hash.hash.toBytes(); const optionBytes = new CLValueOption( - this.version - ? CLValueUInt32.newCLUInt32(BigNumber.from(this.version)) - : null + this.version ? CLValue.newCLUInt32(BigNumber.from(this.version)) : null ).bytes(); - const entryPointBytes = CLValueString.newCLString(this.entryPoint).bytes(); + const entryPointBytes = CLValue.newCLString(this.entryPoint).bytes(); const argBytes = this.args?.toBytes() || new Uint8Array(); return concat([hashBytes, optionBytes, entryPointBytes, argBytes]); @@ -325,13 +311,11 @@ export class StoredVersionedContractByName { * @returns The serialized byte array. */ bytes(): Uint8Array { - const nameBytes = CLValueString.newCLString(this.name).bytes(); + const nameBytes = CLValue.newCLString(this.name).bytes(); const optionBytes = new CLValueOption( - this.version - ? CLValueUInt32.newCLUInt32(BigNumber.from(this.version)) - : null + this.version ? CLValue.newCLUInt32(BigNumber.from(this.version)) : null ).bytes(); - const entryPointBytes = CLValueString.newCLString(this.entryPoint).bytes(); + const entryPointBytes = CLValue.newCLString(this.entryPoint).bytes(); const argBytes = this.args?.toBytes() || new Uint8Array(); return concat([nameBytes, optionBytes, entryPointBytes, argBytes]); @@ -376,7 +360,7 @@ export class TransferDeployItem { id?: BigNumberish ): TransferDeployItem { const runtimeArgs = Args.fromMap({}); - runtimeArgs.insert('amount', CLValueUInt512.newCLUInt512(amount)); + runtimeArgs.insert('amount', CLValue.newCLUInt512(amount)); if (sourcePurse) { runtimeArgs.insert('source', CLValue.newCLUref(sourcePurse)); } @@ -394,9 +378,7 @@ export class TransferDeployItem { runtimeArgs.insert( 'id', - id - ? CLValueOption.newCLOption(CLValueUInt64.newCLUint64(id)) - : defaultClValue + id ? CLValue.newCLOption(CLValue.newCLUint64(id)) : defaultClValue ); return new TransferDeployItem(runtimeArgs); @@ -556,7 +538,7 @@ export class ExecutableDeployItem { const executableDeployItem = new ExecutableDeployItem(); executableDeployItem.moduleBytes = new ModuleBytes( Uint8Array.from([]), - Args.fromMap({ amount: CLValueUInt512.newCLUInt512(amount) }) + Args.fromMap({ amount: CLValue.newCLUInt512(amount) }) ); return executableDeployItem; } diff --git a/src/types/PricingMode.ts b/src/types/PricingMode.ts index a2adbc0d9..120ab6304 100644 --- a/src/types/PricingMode.ts +++ b/src/types/PricingMode.ts @@ -1,6 +1,6 @@ import { jsonObject, jsonMember } from 'typedjson'; import { Hash } from './key'; -import { CLValueBool, CLValueUInt64, CLValueUInt8 } from './clvalue'; +import { CLValue } from './clvalue'; import { CalltableSerialization } from './CalltableSerialization'; /** @@ -29,18 +29,18 @@ export class PaymentLimitedMode { public toBytes(): Uint8Array { const calltableSerializer = new CalltableSerialization(); - calltableSerializer.addField(0, CLValueUInt8.newCLUint8(0).bytes()); + calltableSerializer.addField(0, CLValue.newCLUint8(0).bytes()); calltableSerializer.addField( 1, - CLValueUInt64.newCLUint64(this.paymentAmount).bytes() + CLValue.newCLUint64(this.paymentAmount).bytes() ); calltableSerializer.addField( 2, - CLValueUInt8.newCLUint8(this.gasPriceTolerance).bytes() + CLValue.newCLUint8(this.gasPriceTolerance).bytes() ); calltableSerializer.addField( 3, - CLValueBool.newCLValueBool(this.standardPayment).bytes() + CLValue.newCLValueBool(this.standardPayment).bytes() ); return calltableSerializer.toBytes(); @@ -75,14 +75,14 @@ export class FixedMode { public toBytes(): Uint8Array { const calltableSerializer = new CalltableSerialization(); - calltableSerializer.addField(0, CLValueUInt8.newCLUint8(1).bytes()); + calltableSerializer.addField(0, CLValue.newCLUint8(1).bytes()); calltableSerializer.addField( 1, - CLValueUInt8.newCLUint8(this.gasPriceTolerance).bytes() + CLValue.newCLUint8(this.gasPriceTolerance).bytes() ); calltableSerializer.addField( 2, - CLValueUInt8.newCLUint8(this.additionalComputationFactor).bytes() + CLValue.newCLUint8(this.additionalComputationFactor).bytes() ); return calltableSerializer.toBytes(); @@ -107,7 +107,7 @@ export class PrepaidMode { public toBytes(): Uint8Array { const calltableSerializer = new CalltableSerialization(); - calltableSerializer.addField(0, CLValueUInt8.newCLUint8(2).bytes()); + calltableSerializer.addField(0, CLValue.newCLUint8(2).bytes()); calltableSerializer.addField(1, this.receipt.toBytes()); return calltableSerializer.toBytes(); diff --git a/src/types/Transaction.test.ts b/src/types/Transaction.test.ts index 255c40b02..74dfb6215 100644 --- a/src/types/Transaction.test.ts +++ b/src/types/Transaction.test.ts @@ -17,12 +17,7 @@ import { } from './TransactionEntryPoint'; import { TransactionScheduling } from './TransactionScheduling'; import { Args } from './Args'; -import { - CLValue, - CLValueOption, - CLValueUInt512, - CLValueUInt64 -} from './clvalue'; +import { CLValue } from './clvalue'; import { TransactionV1Payload } from './TransactionV1Payload'; import { NativeTransferBuilder } from './TransactionBuilder'; @@ -43,8 +38,8 @@ describe('Test Transaction', () => { '0202f5a92ab6da536e7b1a351406f3744224bec85d7acbab1497b65de48a1a707b64' ) ), - amount: CLValueUInt512.newCLUInt512(BigNumber.from(paymentAmount)), - id: CLValueOption.newCLOption(CLValueUInt64.newCLUint64(3)) + amount: CLValue.newCLUInt512(BigNumber.from(paymentAmount)), + id: CLValue.newCLOption(CLValue.newCLUint64(3)) }); const sessionTarget = new SessionTarget(); @@ -122,62 +117,69 @@ describe('Test Transaction', () => { it('should parse deploy json', async () => { const json = { - "deploy": { - "approvals": [], - "hash": "076E77DE17De7F262c5531017c214afd664D9702D4b5b771996aE4dcAf9C01f9", - "header": { - "account": "02024570ae3c361650d5b1Bcd1724a1aF09ffe067d7F69ebd75B567c10c8379a7719", - "timestamp": "2025-01-21T18:07:52.214Z", - "ttl": "30m", - "dependencies": [], - "gas_price": 1, - "body_hash": "D52E4B46542D9C83BA6EF894dBA82e475011166A4C5F434b0d99248cfDC9Abc5", - "chain_name": "casper-test" + deploy: { + approvals: [], + hash: + '076E77DE17De7F262c5531017c214afd664D9702D4b5b771996aE4dcAf9C01f9', + header: { + account: + '02024570ae3c361650d5b1Bcd1724a1aF09ffe067d7F69ebd75B567c10c8379a7719', + timestamp: '2025-01-21T18:07:52.214Z', + ttl: '30m', + dependencies: [], + gas_price: 1, + body_hash: + 'D52E4B46542D9C83BA6EF894dBA82e475011166A4C5F434b0d99248cfDC9Abc5', + chain_name: 'casper-test' }, - "payment": { - "ModuleBytes": { - "module_bytes": "", - "args": [ + payment: { + ModuleBytes: { + module_bytes: '', + args: [ [ - "amount", + 'amount', { - "cl_type": "U512", - "bytes": "0400943577", - "parsed": "2000000000" + cl_type: 'U512', + bytes: '0400943577', + parsed: '2000000000' } ] ] } }, - "session": { - "StoredVersionedContractByHash": { - "hash": "6497c59f1bcfBBBC468Dc889dd73dCd542827fb966a24Eb33e4140Ab5BB4aE28", - "version": null, - "entry_point": "mint", - "args": [ + session: { + StoredVersionedContractByHash: { + hash: + '6497c59f1bcfBBBC468Dc889dd73dCd542827fb966a24Eb33e4140Ab5BB4aE28', + version: null, + entry_point: 'mint', + args: [ [ - "recipient", + 'recipient', { - "cl_type": "Key", - "bytes": "00ABbc44715BA77Ea117f9379A3f5b62879Be71D105b7508b610a676AEf4c3C26a", - "parsed": { - "Account": "account-hash-ABBC44715ba77ea117f9379a3f5b62879Be71D105B7508B610a676AeF4C3C26A" + cl_type: 'Key', + bytes: + '00ABbc44715BA77Ea117f9379A3f5b62879Be71D105b7508b610a676AEf4c3C26a', + parsed: { + Account: + 'account-hash-ABBC44715ba77ea117f9379a3f5b62879Be71D105B7508B610a676AeF4C3C26A' } } ], [ - "token_metas", + 'token_metas', { - "cl_type": { - "List": { - "Map": { - "key": "String", - "value": "String" + cl_type: { + List: { + Map: { + key: 'String', + value: 'String' } } }, - "bytes": "0100000003000000040000006e616d65080000004e6f6f6f6f6f6f6f0b0000006465736372697074696f6e04000000747275650500000061737365745400000068747470733a2f2f697066732d676174657761792e637370722e73747564696f2f697066732f516d5a62714d767a5036706a45415554753135376d5470736e38526b4d637a585a48767a56784454647854436572", - "parsed": "" + bytes: + '0100000003000000040000006e616d65080000004e6f6f6f6f6f6f6f0b0000006465736372697074696f6e04000000747275650500000061737365745400000068747470733a2f2f697066732d676174657761792e637370722e73747564696f2f697066732f516d5a62714d767a5036706a45415554753135376d5470736e38526b4d637a585a48767a56784454647854436572', + parsed: '' } ] ] @@ -187,6 +189,8 @@ describe('Test Transaction', () => { }; const tx = Transaction.fromJSON(json); - expect(tx.hash.toHex()).to.equal('076e77de17de7f262c5531017c214afd664d9702d4b5b771996ae4dcaf9c01f9') + expect(tx.hash.toHex()).to.equal( + '076e77de17de7f262c5531017c214afd664d9702d4b5b771996ae4dcaf9c01f9' + ); }); }); diff --git a/src/types/TransactionBuilder.test.ts b/src/types/TransactionBuilder.test.ts index dbf156670..1ff0254db 100644 --- a/src/types/TransactionBuilder.test.ts +++ b/src/types/TransactionBuilder.test.ts @@ -12,7 +12,7 @@ import { makeCsprTransferDeploy } from '../utils'; import { Args } from './Args'; -import { CLValue, CLValueUInt256 } from './clvalue'; +import { CLValue } from './clvalue'; import { Key, KeyTypeID } from './key'; import { AuctionManagerEntryPoint, CasperNetworkName } from '../@types'; @@ -74,7 +74,7 @@ describe('Test TransactionBuilder', () => { KeyTypeID.Account ) ), - amount: CLValueUInt256.newCLUInt256('1000000000') + amount: CLValue.newCLUInt256('1000000000') }) ) .payment(2_000000000) diff --git a/src/types/TransactionBuilder.ts b/src/types/TransactionBuilder.ts index 72fbae2f1..3f8ed0565 100644 --- a/src/types/TransactionBuilder.ts +++ b/src/types/TransactionBuilder.ts @@ -22,15 +22,7 @@ import { AccountHash, ContractHash, Hash } from './key'; import { Transaction, TransactionV1 } from './Transaction'; import { TransactionV1Payload } from './TransactionV1Payload'; import { Duration, Timestamp } from './Time'; -import { - CLValue, - CLValueByteArray, - CLValueOption, - CLValueUInt32, - CLValueUInt512, - CLValueUInt64, - CLValueUInt8 -} from './clvalue'; +import { CLValue } from './clvalue'; import { ExecutableDeployItem, StoredContractByHash, @@ -56,6 +48,7 @@ abstract class TransactionBuilder> { protected _entryPoint!: TransactionEntryPoint; protected _scheduling: TransactionScheduling = new TransactionScheduling({}); // Standard protected _runtimeArgs: Args; + protected _contractHash: string; /** * Sets the initiator address using a public key. @@ -81,6 +74,14 @@ abstract class TransactionBuilder> { return (this as unknown) as T; } + /** + * Sets the contract hash for the transaction. + */ + public contractHash(contractHash: string): T { + this._contractHash = contractHash; + return (this as unknown) as T; + } + /** * Sets the timestamp for the transaction. */ @@ -160,7 +161,7 @@ export class NativeTransferBuilder extends TransactionBuilder< > { private _target!: CLValue; private _publicKey: PublicKey; - private _amount: CLValue = CLValueUInt512.newCLUInt512('0'); + private _amount: CLValue = CLValue.newCLUInt512('0'); private _amountRow: BigNumber | string = '0'; private _idTransfer?: number; @@ -185,7 +186,7 @@ export class NativeTransferBuilder extends TransactionBuilder< * Sets the target account hash for the transfer. */ public targetAccountHash(accountHashKey: AccountHash): NativeTransferBuilder { - this._target = CLValueByteArray.newCLByteArray(accountHashKey.toBytes()); + this._target = CLValue.newCLByteArray(accountHashKey.toBytes()); return this; } @@ -194,7 +195,7 @@ export class NativeTransferBuilder extends TransactionBuilder< */ public amount(amount: BigNumber | string): NativeTransferBuilder { this._amountRow = amount; - this._amount = CLValueUInt512.newCLUInt512(amount); + this._amount = CLValue.newCLUInt512(amount); return this; } @@ -218,7 +219,7 @@ export class NativeTransferBuilder extends TransactionBuilder< if (this._idTransfer) { runtimeArgs.insert( 'id', - CLValueOption.newCLOption(CLValueUInt64.newCLUint64(this._idTransfer)) + CLValue.newCLOption(CLValue.newCLUint64(this._idTransfer)) ); } @@ -274,19 +275,19 @@ export class NativeAddBidBuilder extends TransactionBuilder< } public amount(amount: BigNumber | string): NativeAddBidBuilder { - this._amount = CLValueUInt512.newCLUInt512(amount); + this._amount = CLValue.newCLUInt512(amount); return this; } public delegationRate(delegationRate: number): NativeAddBidBuilder { - this._delegationRate = CLValueUInt8.newCLUint8(delegationRate); + this._delegationRate = CLValue.newCLUint8(delegationRate); return this; } public minimumDelegationAmount( minimumDelegationAmount: BigNumberish ): NativeAddBidBuilder { - this._minimumDelegationAmount = CLValueUInt64.newCLUint64( + this._minimumDelegationAmount = CLValue.newCLUint64( minimumDelegationAmount ); return this; @@ -295,14 +296,14 @@ export class NativeAddBidBuilder extends TransactionBuilder< public maximumDelegationAmount( maximumDelegationAmount: BigNumberish ): NativeAddBidBuilder { - this._maximumDelegationAmount = CLValueUInt64.newCLUint64( + this._maximumDelegationAmount = CLValue.newCLUint64( maximumDelegationAmount ); return this; } public reservedSlots(reservedSlots: BigNumber): NativeAddBidBuilder { - this._reservedSlots = CLValueUInt32.newCLUInt32(reservedSlots); + this._reservedSlots = CLValue.newCLUInt32(reservedSlots); return this; } @@ -368,9 +369,16 @@ export class NativeAddBidBuilder extends TransactionBuilder< this._runtimeArgs = runtimeArgs; const contractHash = + this._contractHash ?? AuctionManagerContractHashMap[this._chainName as CasperNetworkName] ?? AuctionManagerContractHashMap.casper; + if (!contractHash) { + throw new Error( + `Contract hash is undefined. Check _contractHash or _chainName: ${this._chainName}` + ); + } + const session = new ExecutableDeployItem(); session.storedContractByHash = new StoredContractByHash( ContractHash.newContract(contractHash), @@ -392,7 +400,7 @@ export class NativeWithdrawBidBuilder extends TransactionBuilder< NativeWithdrawBidBuilder > { private _validator!: CLValue; - private _amount: CLValue = CLValueUInt512.newCLUInt512('0'); + private _amount: CLValue = CLValue.newCLUInt512('0'); constructor() { super(); @@ -408,7 +416,7 @@ export class NativeWithdrawBidBuilder extends TransactionBuilder< } public amount(amount: BigNumber | string): NativeWithdrawBidBuilder { - this._amount = CLValueUInt512.newCLUInt512(amount); + this._amount = CLValue.newCLUInt512(amount); return this; } @@ -428,9 +436,16 @@ export class NativeWithdrawBidBuilder extends TransactionBuilder< }); const contractHash = + this._contractHash ?? AuctionManagerContractHashMap[this._chainName as CasperNetworkName] ?? AuctionManagerContractHashMap.casper; + if (!contractHash) { + throw new Error( + `Contract hash is undefined. Check _contractHash or _chainName: ${this._chainName}` + ); + } + const session = new ExecutableDeployItem(); session.storedContractByHash = new StoredContractByHash( ContractHash.newContract(contractHash), @@ -452,7 +467,7 @@ export class NativeDelegateBuilder extends TransactionBuilder< NativeDelegateBuilder > { private _validator!: CLValue; - private _amount: CLValue = CLValueUInt512.newCLUInt512('0'); + private _amount: CLValue = CLValue.newCLUInt512('0'); constructor() { super(); @@ -468,7 +483,7 @@ export class NativeDelegateBuilder extends TransactionBuilder< } public amount(amount: BigNumber | string): NativeDelegateBuilder { - this._amount = CLValueUInt512.newCLUInt512(amount); + this._amount = CLValue.newCLUInt512(amount); return this; } @@ -492,9 +507,16 @@ export class NativeDelegateBuilder extends TransactionBuilder< } const contractHash = + this._contractHash ?? AuctionManagerContractHashMap[this._chainName as CasperNetworkName] ?? AuctionManagerContractHashMap.casper; + if (!contractHash) { + throw new Error( + `Contract hash is undefined. Check _contractHash or _chainName: ${this._chainName}` + ); + } + const session = new ExecutableDeployItem(); session.storedContractByHash = new StoredContractByHash( ContractHash.newContract(contractHash), @@ -520,7 +542,7 @@ export class NativeUndelegateBuilder extends TransactionBuilder< NativeUndelegateBuilder > { private _validator!: CLValue; - private _amount: CLValue = CLValueUInt512.newCLUInt512('0'); + private _amount: CLValue = CLValue.newCLUInt512('0'); constructor() { super(); @@ -536,7 +558,7 @@ export class NativeUndelegateBuilder extends TransactionBuilder< } public amount(amount: BigNumber | string): NativeUndelegateBuilder { - this._amount = CLValueUInt512.newCLUInt512(amount); + this._amount = CLValue.newCLUInt512(amount); return this; } @@ -560,9 +582,16 @@ export class NativeUndelegateBuilder extends TransactionBuilder< } const contractHash = + this._contractHash ?? AuctionManagerContractHashMap[this._chainName as CasperNetworkName] ?? AuctionManagerContractHashMap.casper; + if (!contractHash) { + throw new Error( + `Contract hash is undefined. Check _contractHash or _chainName: ${this._chainName}` + ); + } + const session = new ExecutableDeployItem(); session.storedContractByHash = new StoredContractByHash( ContractHash.newContract(contractHash), @@ -589,7 +618,7 @@ export class NativeRedelegateBuilder extends TransactionBuilder< > { private _validator!: CLValue; private _newValidator!: CLValue; - private _amount: CLValue = CLValueUInt512.newCLUInt512('0'); + private _amount: CLValue = CLValue.newCLUInt512('0'); constructor() { super(); @@ -610,7 +639,7 @@ export class NativeRedelegateBuilder extends TransactionBuilder< } public amount(amount: BigNumber | string): NativeRedelegateBuilder { - this._amount = CLValueUInt512.newCLUInt512(amount); + this._amount = CLValue.newCLUInt512(amount); return this; } @@ -635,9 +664,16 @@ export class NativeRedelegateBuilder extends TransactionBuilder< } const contractHash = + this._contractHash ?? AuctionManagerContractHashMap[this._chainName as CasperNetworkName] ?? AuctionManagerContractHashMap.casper; + if (!contractHash) { + throw new Error( + `Contract hash is undefined. Check _contractHash or _chainName: ${this._chainName}` + ); + } + const session = new ExecutableDeployItem(); session.storedContractByHash = new StoredContractByHash( ContractHash.newContract(contractHash), @@ -692,9 +728,16 @@ export class NativeActivateBidBuilder extends TransactionBuilder< }); const contractHash = + this._contractHash ?? AuctionManagerContractHashMap[this._chainName as CasperNetworkName] ?? AuctionManagerContractHashMap.casper; + if (!contractHash) { + throw new Error( + `Contract hash is undefined. Check _contractHash or _chainName: ${this._chainName}` + ); + } + const session = new ExecutableDeployItem(); session.storedContractByHash = new StoredContractByHash( ContractHash.newContract(contractHash), diff --git a/src/types/TransactionEntryPoint.ts b/src/types/TransactionEntryPoint.ts index a49dcc49d..0be12b5fc 100644 --- a/src/types/TransactionEntryPoint.ts +++ b/src/types/TransactionEntryPoint.ts @@ -1,5 +1,5 @@ import { jsonObject, jsonMember } from 'typedjson'; -import { CLValueString } from './clvalue'; +import { CLValue, CLValueString } from './clvalue'; import { CalltableSerialization } from './CalltableSerialization'; /** @@ -125,7 +125,7 @@ export class TransactionEntryPoint { this.type === TransactionEntryPointEnum.Custom && this.customEntryPoint ) { - const entryPointBytes = CLValueString.newCLString( + const entryPointBytes = CLValue.newCLString( this.customEntryPoint ).bytes(); calltableSerialization.addField(1, entryPointBytes); diff --git a/src/types/TransactionScheduling.ts b/src/types/TransactionScheduling.ts index 570efe10e..173be326f 100644 --- a/src/types/TransactionScheduling.ts +++ b/src/types/TransactionScheduling.ts @@ -1,7 +1,7 @@ import { jsonObject, jsonMember } from 'typedjson'; import { Timestamp } from './Time'; -import { CLValueUInt64 } from './clvalue'; +import { CLValue } from './clvalue'; import { CalltableSerialization } from './CalltableSerialization'; import { fromBytesU64, toBytesU64 } from './ByteConverters'; @@ -40,10 +40,7 @@ export class FutureEraScheduling { public toBytes(): Uint8Array { const calltableSerialization = new CalltableSerialization(); calltableSerialization.addField(0, Uint8Array.of(1)); - calltableSerialization.addField( - 1, - CLValueUInt64.newCLUint64(this.eraID).bytes() - ); + calltableSerialization.addField(1, CLValue.newCLUint64(this.eraID).bytes()); return calltableSerialization.toBytes(); } diff --git a/src/types/TransactionTarget.ts b/src/types/TransactionTarget.ts index 12b097b8c..2f5542ed0 100644 --- a/src/types/TransactionTarget.ts +++ b/src/types/TransactionTarget.ts @@ -7,9 +7,9 @@ import { Hash } from './key'; import { CLTypeOption, CLTypeUInt32, + CLValue, CLValueOption, - CLValueString, - CLValueUInt32 + CLValueString } from './clvalue'; import { ExecutableDeployItem } from './ExecutableDeployItem'; import { CalltableSerialization } from './CalltableSerialization'; @@ -139,8 +139,8 @@ export class ByPackageHashInvocationTarget { const calltableSerialization = new CalltableSerialization(); const versionBytes = this.version - ? CLValueOption.newCLOption( - CLValueUInt32.newCLUInt32(BigNumber.from(this.version)) + ? CLValue.newCLOption( + CLValue.newCLUInt32(BigNumber.from(this.version)) ).bytes() : new CLValueOption(null, new CLTypeOption(CLTypeUInt32)).bytes(); @@ -173,16 +173,13 @@ export class ByPackageNameInvocationTarget { const calltableSerialization = new CalltableSerialization(); const versionBytes = this.version - ? CLValueOption.newCLOption( - CLValueUInt32.newCLUInt32(BigNumber.from(this.version)) + ? CLValue.newCLOption( + CLValue.newCLUInt32(BigNumber.from(this.version)) ).bytes() : new CLValueOption(null, new CLTypeOption(CLTypeUInt32)).bytes(); calltableSerialization.addField(0, Uint8Array.of(3)); - calltableSerialization.addField( - 1, - CLValueString.newCLString(this.name).bytes() - ); + calltableSerialization.addField(1, CLValue.newCLString(this.name).bytes()); calltableSerialization.addField(2, versionBytes); return calltableSerialization.toBytes(); @@ -251,10 +248,7 @@ export class TransactionInvocationTarget { } else if (this.byName) { const calltableSerializer = new CalltableSerialization(); calltableSerializer.addField(0, Uint8Array.of(1)); - calltableSerializer.addField( - 1, - CLValueString.newCLString(this.byName).bytes() - ); + calltableSerializer.addField(1, CLValue.newCLString(this.byName).bytes()); return calltableSerializer.toBytes(); } else if (this.byPackageHash) { return this.byPackageHash.toBytes(); diff --git a/src/types/TransactionV1Payload.ts b/src/types/TransactionV1Payload.ts index cc8b493ca..45c88f204 100644 --- a/src/types/TransactionV1Payload.ts +++ b/src/types/TransactionV1Payload.ts @@ -9,7 +9,7 @@ import { TransactionEntryPoint } from './TransactionEntryPoint'; import { TransactionScheduling } from './TransactionScheduling'; import { CalltableSerialization } from './CalltableSerialization'; import { deserializeArgs, serializeArgs } from './SerializationUtils'; -import { CLValueString, CLValueUInt64 } from './clvalue'; +import { CLValue } from './clvalue'; import { expandBuffer, writeBytes, @@ -356,12 +356,9 @@ export class TransactionV1Payload { return new CalltableSerialization() .addField(0, this.initiatorAddr.toBytes()) - .addField( - 1, - CLValueUInt64.newCLUint64(this.timestamp.toMilliseconds()).bytes() - ) - .addField(2, CLValueUInt64.newCLUint64(this.ttl.duration).bytes()) - .addField(3, CLValueString.newCLString(this.chainName).bytes()) + .addField(1, CLValue.newCLUint64(this.timestamp.toMilliseconds()).bytes()) + .addField(2, CLValue.newCLUint64(this.ttl.duration).bytes()) + .addField(3, CLValue.newCLString(this.chainName).bytes()) .addField(4, this.pricingMode.toBytes()) .addField(5, fields.toBytes()) .toBytes(); diff --git a/src/types/clvalue/Any.ts b/src/types/clvalue/Any.ts index 916972a72..c2cb3b540 100644 --- a/src/types/clvalue/Any.ts +++ b/src/types/clvalue/Any.ts @@ -1,6 +1,3 @@ -import { CLTypeAny } from './cltype'; -import { CLValue } from './CLValue'; - /** * Represents an 'Any' value in the Casper type system. * This type can store any arbitrary data as a byte array, making it highly flexible. @@ -41,15 +38,4 @@ export class CLValueAny { public toJSON(): Uint8Array { return this.bytes(); } - - /** - * Creates a new CLValue instance containing an 'Any' value. - * @param data - The Uint8Array to be stored within the CLValue. - * @returns A new CLValue instance encapsulating the 'Any' value. - */ - public static newCLAny(data: Uint8Array): CLValue { - const res = new CLValue(CLTypeAny); - res.any = new CLValueAny(data); - return res; - } } diff --git a/src/types/clvalue/Bool.test.ts b/src/types/clvalue/Bool.test.ts index c4c2706a1..3e8c61688 100644 --- a/src/types/clvalue/Bool.test.ts +++ b/src/types/clvalue/Bool.test.ts @@ -2,6 +2,7 @@ import { expect } from 'chai'; import { CLValueBool } from './Bool'; import { CLValueParser } from './Parser'; import { CLTypeBool } from './cltype'; +import { CLValue } from './CLValue'; describe('CLBool', () => { it('Should be able to return proper value by calling .value()', () => { @@ -31,7 +32,7 @@ describe('CLBool', () => { }); it('toJSON() / fromJSON() do proper bytes serialization', () => { - const myBool = CLValueBool.newCLValueBool(false); + const myBool = CLValue.newCLValueBool(false); const json = CLValueParser.toJSON(myBool); const expectedJson = JSON.parse('{"bytes":"00","cl_type":"Bool"}'); diff --git a/src/types/clvalue/Bool.ts b/src/types/clvalue/Bool.ts index f35fababb..dd3dd4308 100644 --- a/src/types/clvalue/Bool.ts +++ b/src/types/clvalue/Bool.ts @@ -1,5 +1,4 @@ -import { CLTypeBool } from './cltype'; -import { CLValue, IResultWithBytes } from './CLValue'; +import { IResultWithBytes } from './CLValue'; /** * Represents a boolean value in the Casper type system. @@ -49,17 +48,6 @@ export class CLValueBool { return this.value; } - /** - * Creates a new CLValue instance containing a boolean value. - * @param val - The boolean value to be stored in the CLValue. - * @returns A new CLValue instance encapsulating the boolean value. - */ - public static newCLValueBool(val: boolean): CLValue { - const res = new CLValue(CLTypeBool); - res.bool = new CLValueBool(val); - return res; - } - /** * Creates a CLValueBool instance from a Uint8Array. * Parses the first byte in the array to determine the boolean value. diff --git a/src/types/clvalue/ByteArray.test.ts b/src/types/clvalue/ByteArray.test.ts index 15cb2059e..a4625a9e3 100644 --- a/src/types/clvalue/ByteArray.test.ts +++ b/src/types/clvalue/ByteArray.test.ts @@ -2,6 +2,7 @@ import { expect } from 'chai'; import { CLValueByteArray } from './ByteArray'; import { CLValueParser } from './Parser'; import { CLTypeByteArray } from './cltype'; +import { CLValue } from './CLValue'; describe('CLByteArray', () => { it('Should be able to return proper value by calling .value()', () => { @@ -13,7 +14,7 @@ describe('CLByteArray', () => { it('Should be able to return proper byte array by calling toBytes() / fromBytes()', () => { const expectedBytes = Uint8Array.from(Array(32).fill(42)); - const hash = CLValueByteArray.newCLByteArray(expectedBytes); + const hash = CLValue.newCLByteArray(expectedBytes); const bytes = hash.bytes(); expect(bytes).to.deep.eq(expectedBytes); @@ -24,7 +25,7 @@ describe('CLByteArray', () => { it('toJson() / fromJson()', () => { const bytes = Uint8Array.from(Array(32).fill(42)); - const hash = CLValueByteArray.newCLByteArray(bytes); + const hash = CLValue.newCLByteArray(bytes); const json = CLValueParser.toJSON(hash); const expectedJson = JSON.parse( '{"bytes":"2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a","cl_type":{"ByteArray":32}}' diff --git a/src/types/clvalue/ByteArray.ts b/src/types/clvalue/ByteArray.ts index 4c5109698..cccd4299a 100644 --- a/src/types/clvalue/ByteArray.ts +++ b/src/types/clvalue/ByteArray.ts @@ -1,5 +1,5 @@ import { CLTypeByteArray } from './cltype'; -import { CLValue, IResultWithBytes } from './CLValue'; +import { IResultWithBytes } from './CLValue'; /** * Represents a byte array value in the Casper type system. @@ -44,19 +44,6 @@ export class CLValueByteArray { ).join(''); } - /** - * Creates a new CLValue instance containing a ByteArray value. - * @param val - The Uint8Array to be stored within the CLValue. - * @returns A new CLValue instance encapsulating the ByteArray. - */ - public static newCLByteArray(val: Uint8Array): CLValue { - const byteArrayType = new CLTypeByteArray(val.length); - const clValueByteArray = new CLValue(byteArrayType); - - clValueByteArray.byteArray = new CLValueByteArray(val); - return clValueByteArray; - } - /** * Creates a CLValueByteArray instance from a Uint8Array. * Extracts the bytes specified by the given CLTypeByteArray size. diff --git a/src/types/clvalue/CLValue.ts b/src/types/clvalue/CLValue.ts index 70b640c16..524422d50 100644 --- a/src/types/clvalue/CLValue.ts +++ b/src/types/clvalue/CLValue.ts @@ -1,8 +1,30 @@ +import { BigNumberish } from '@ethersproject/bignumber'; + import { CLType, + CLTypeAny, + CLTypeBool, + CLTypeByteArray, CLTypeDynamic, + CLTypeInt32, + CLTypeInt64, CLTypeKey, + CLTypeList, + CLTypeMap, + CLTypeOption, CLTypePublicKey, + CLTypeResult, + CLTypeString, + CLTypeTuple1, + CLTypeTuple2, + CLTypeTuple3, + CLTypeUInt128, + CLTypeUInt256, + CLTypeUInt32, + CLTypeUInt512, + CLTypeUInt64, + CLTypeUInt8, + CLTypeUnit, CLTypeUref, TypeID } from './cltype'; @@ -217,4 +239,281 @@ export class CLValue { clValue.publicKey = data; return clValue; } + + /** + * Creates a new CLValue instance containing an 'Any' value. + * @param data - The Uint8Array to be stored within the CLValue. + * @returns A new CLValue instance encapsulating the 'Any' value. + */ + public static newCLAny(data: Uint8Array): CLValue { + const res = new CLValue(CLTypeAny); + res.any = new CLValueAny(data); + return res; + } + + /** + * Creates a new CLValue instance containing a boolean value. + * @param val - The boolean value to be stored in the CLValue. + * @returns A new CLValue instance encapsulating the boolean value. + */ + public static newCLValueBool(val: boolean): CLValue { + const res = new CLValue(CLTypeBool); + res.bool = new CLValueBool(val); + return res; + } + + /** + * Creates a new CLValue instance containing a ByteArray value. + * @param val - The Uint8Array to be stored within the CLValue. + * @returns A new CLValue instance encapsulating the ByteArray. + */ + public static newCLByteArray(val: Uint8Array): CLValue { + const byteArrayType = new CLTypeByteArray(val.length); + const clValueByteArray = new CLValue(byteArrayType); + + clValueByteArray.byteArray = new CLValueByteArray(val); + return clValueByteArray; + } + + /** + * 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, + elements: CLValue[] = [] + ): CLValue { + const listType = new CLTypeList(elementType); + const clValue = new CLValue(listType); + clValue.list = new CLValueList(listType, elements); + return clValue; + } + + /** + * Creates a new CLValue instance with a Map value. + * @param keyType - The CLType for the map keys. + * @param valType - The CLType for the map values. + * @returns A new CLValue instance with CLTypeMap and a CLValueMap. + */ + public static newCLMap(keyType: CLType, valType: CLType): CLValue { + const mapType = new CLTypeMap(keyType, valType); + const clValue = new CLValue(mapType); + clValue.map = new CLValueMap(mapType); + return clValue; + } + + /** + * Creates a new `CLValue` instance that represents an optional value. + * + * This method allows you to wrap a given `CLValue` as an optional type (`Option`), + * which can either contain a value or be `null`. This is useful for scenarios where + * a value may or may not be present. + * + * If `inner` is `null`, the method will use the provided `clType` as the type of the option. + * If `clType` is not provided, it defaults to `CLTypeAny`. If `inner` is not `null`, + * its type is used instead of `clType`. + * + * @param inner - The `CLValue` to be wrapped in the option. Pass `null` if no value is present. + * @param clType - (Optional) The `CLType` representing the type of the value contained in the option. + * This is required if `inner` is `null` to properly define the option type. + * + * @returns A new `CLValue` instance containing: + * - A `CLTypeOption` representing the type of the optional value. + * - A `CLValueOption` that holds the inner value or `null`. + * + * @example + * ```typescript + * // Example of an option containing a value + * const innerValue = CLValue.fromU32(42); + * const optionValue = CLValue.newCLOption(innerValue); + * + * // Example of an empty option with an explicitly defined type + * const emptyOptionValue = CLValue.newCLOption(null, CLType.U32); + * + * // Example of an empty option with no type provided (defaults to CLTypeAny) + * const emptyOptionValueDefault = CLValue.newCLOption(null); + * ``` + */ + public static newCLOption(inner: CLValue | null, clType?: CLType): CLValue { + const clTypeForOption = inner === null ? clType || CLTypeAny : inner.type; + const optionType = new CLTypeOption(clTypeForOption); + const clValue = new CLValue(optionType); + clValue.option = new CLValueOption(inner, optionType); + return clValue; + } + + /** + * Creates a new CLValue instance with a Result value. + * @param innerOk - The CLType for the success case. + * @param innerErr - The CLType for the error case. + * @param value - The CLValue to be contained in the Result. + * @param isSuccess - A boolean indicating whether the Result is a success (true) or an error (false). + * @returns A new CLValue instance containing CLTypeResult and a CLValueResult. + */ + public static newCLResult( + innerOk: CLType, + innerErr: CLType, + value: CLValue, + isSuccess: boolean + ): CLValue { + const resultType = new CLTypeResult(innerOk, innerErr); + const clValue = new CLValue(resultType); + clValue.result = new CLValueResult(resultType, isSuccess, value); + return clValue; + } + + /** + * Creates a new CLValue instance with a string value. + * @param val - The string value to be represented. + * @returns A new CLValue instance containing CLTypeString and a CLValueString. + */ + public static newCLString(val: string): CLValue { + const res = new CLValue(CLTypeString); + res.stringVal = new CLValueString(val); + return res; + } + + /** + * Creates a new CLValue instance with a Tuple1 value. + * @param val - The CLValue to be contained in the tuple. + * @returns A new CLValue instance containing CLTypeTuple1 and a CLValueTuple1. + */ + public static newCLTuple1(val: CLValue): CLValue { + const tupleType = new CLTypeTuple1(val.type); + const clValue = new CLValue(tupleType); + clValue.tuple1 = new CLValueTuple1(tupleType, val); + return clValue; + } + + /** + * Creates a new CLValue instance with a Tuple2 value. + * @param val1 - The first CLValue in the tuple. + * @param val2 - The second CLValue in the tuple. + * @returns A new CLValue instance containing CLTypeTuple2 and a CLValueTuple2. + */ + public static newCLTuple2(val1: CLValue, val2: CLValue): CLValue { + const tupleType = new CLTypeTuple2(val1.type, val2.type); + const clValue = new CLValue(tupleType); + clValue.tuple2 = new CLValueTuple2(tupleType, val1, val2); + return clValue; + } + + /** + * Creates a new CLValue instance with a Tuple3 value. + * @param val1 - The first CLValue in the tuple. + * @param val2 - The second CLValue in the tuple. + * @param val3 - The third CLValue in the tuple. + * @returns A new CLValue instance containing CLTypeTuple3 and a CLValueTuple3. + */ + public static newCLTuple3( + val1: CLValue, + val2: CLValue, + val3: CLValue + ): CLValue { + const tupleType = new CLTypeTuple3(val1.type, val2.type, val3.type); + const clValue = new CLValue(tupleType); + clValue.tuple3 = new CLValueTuple3(tupleType, val1, val2, val3); + return clValue; + } + + /** + * Creates a new CLValue instance with a Unit value. + * + * @returns A new CLValue instance with CLTypeUnit and a CLValueUnit. + */ + public static newCLUnit(): CLValue { + const res = new CLValue(CLTypeUnit); + res.unit = new CLValueUnit(); + return res; + } + + /** + * Creates a new CLValue instance with an Int32 value. + * @param val - The 32-bit integer to be encapsulated in a CLValue. + * @returns A new CLValue instance containing CLTypeInt32 and a CLValueInt32. + */ + public static newCLInt32(val: BigNumberish): CLValue { + const res = new CLValue(CLTypeInt32); + res.i32 = new CLValueInt32(val); + return res; + } + + /** + * Creates a new CLValue instance with an Int64 value. + * @param val - The value to be stored in the Int64. Accepts any BigNumberish type. + * @returns A new CLValue instance containing CLTypeInt64 and a CLValueInt64. + */ + public static newCLInt64(val: BigNumberish): CLValue { + const res = new CLValue(CLTypeInt64); + res.i64 = new CLValueInt64(val); + return res; + } + + /** + * Creates a new CLValue instance with a UInt8 value. + * @param value - The value to initialize the UInt8 with. Must be an integer between 0 and 255. + * @returns A new CLValue instance containing CLTypeUInt8 and a CLValueUInt8. + */ + public static newCLUint8(value: BigNumberish): CLValue { + const res = new CLValue(CLTypeUInt8); + res.ui8 = new CLValueUInt8(value); + return res; + } + + /** + * Creates a new CLValue instance with a UInt32 value. + * @param value - The value to initialize the UInt32 with. + * @returns A new CLValue instance containing CLTypeUInt32 and a CLValueUInt32. + */ + public static newCLUInt32(value: BigNumberish): CLValue { + const res = new CLValue(CLTypeUInt32); + res.ui32 = new CLValueUInt32(value); + return res; + } + + /** + * Creates a new CLValue instance with a UInt64 value. + * @param val - The value to initialize the UInt64 with. Can be any BigNumberish type. + * @returns A new CLValue instance containing CLTypeUInt64 and a CLValueUInt64. + */ + public static newCLUint64(val: BigNumberish): CLValue { + const res = new CLValue(CLTypeUInt64); + res.ui64 = new CLValueUInt64(val); + return res; + } + + /** + * Creates a new CLValue instance with a UInt128 value. + * @param value - The value to initialize the UInt128 with. + * @returns A new CLValue instance containing CLTypeUInt128 and a CLValueUInt128. + */ + public static newCLUInt128(value: BigNumberish): CLValue { + const res = new CLValue(CLTypeUInt128); + res.ui128 = new CLValueUInt128(value); + return res; + } + + /** + * Creates a new CLValue instance with a UInt256 value. + * @param value - The value to initialize the UInt256 with. + * @returns A new CLValue instance containing CLTypeUInt256 and a CLValueUInt256. + */ + public static newCLUInt256(value: BigNumberish): CLValue { + const res = new CLValue(CLTypeUInt256); + res.ui256 = new CLValueUInt256(value); + return res; + } + + /** + * Creates a new CLValue instance with a UInt512 value. + * @param value - The value to initialize the UInt512 with. + * @returns A new CLValue instance containing CLTypeUInt512 and a CLValueUInt512. + */ + public static newCLUInt512(value: BigNumberish): CLValue { + const res = new CLValue(CLTypeUInt512); + res.ui512 = new CLValueUInt512(value); + return res; + } } diff --git a/src/types/clvalue/List.test.ts b/src/types/clvalue/List.test.ts index 46cb7d2a5..356dec02c 100644 --- a/src/types/clvalue/List.test.ts +++ b/src/types/clvalue/List.test.ts @@ -1,12 +1,11 @@ import { expect } from 'chai'; -import { CLTypeBool, CLTypeList, CLTypeUInt32 } from './cltype'; +import { BigNumber } from '@ethersproject/bignumber'; import { concat } from '@ethersproject/bytes'; + +import { CLTypeBool, CLTypeList, CLTypeUInt32 } from './cltype'; import { toBytesU32 } from '../ByteConverters'; -import { CLValueUInt32 } from './Numeric/Uint32'; import { CLValueList } from './List'; -import { BigNumber } from '@ethersproject/bignumber'; import { CLValue } from './CLValue'; -import { CLValueBool } from './Bool'; describe('CLValueList', () => { let typeList: CLTypeList; @@ -16,8 +15,8 @@ describe('CLValueList', () => { beforeEach(() => { typeList = new CLTypeList(new CLTypeList(CLTypeUInt32)); - element1 = CLValueUInt32.newCLUInt32(BigNumber.from(1)); - element2 = CLValueUInt32.newCLUInt32(BigNumber.from(2)); + element1 = CLValue.newCLUInt32(BigNumber.from(1)); + element2 = CLValue.newCLUInt32(BigNumber.from(2)); clValueList = new CLValueList(typeList, [element1, element2]); }); @@ -49,7 +48,7 @@ describe('CLValueList', () => { }); it('should add an element to the list when calling append()', () => { - const element3 = CLValueUInt32.newCLUInt32(BigNumber.from(3)); + const element3 = CLValue.newCLUInt32(BigNumber.from(3)); clValueList.append(element3); expect(clValueList.elements).to.deep.equal([element1, element2, element3]); }); @@ -70,13 +69,13 @@ describe('CLValueList', () => { }); it('should set the element at specified index when calling set()', () => { - const element3 = CLValueUInt32.newCLUInt32(BigNumber.from(3)); + const element3 = CLValue.newCLUInt32(BigNumber.from(3)); clValueList.set(1, element3); expect(clValueList.elements[1]).to.equal(element3); }); it('should throw error if index is out of bounds in set()', () => { - const element3 = CLValueUInt32.newCLUInt32(BigNumber.from(3)); + const element3 = CLValue.newCLUInt32(BigNumber.from(3)); expect(() => clValueList.set(2, element3)).to.throw( 'List index out of bounds.' ); @@ -95,7 +94,7 @@ describe('CLValueList', () => { }); it('should create a CLValue instance with a List value when calling newCLList()', () => { - const newList = CLValueList.newCLList(CLTypeUInt32); + const newList = CLValue.newCLList(CLTypeUInt32); expect(newList.list).to.be.instanceOf(CLValueList); }); }); @@ -108,8 +107,8 @@ describe('CLValueList with boolean values', () => { beforeEach(() => { boolTypeList = new CLTypeList(CLTypeBool); - trueElement = CLValueBool.newCLValueBool(true); - falseElement = CLValueBool.newCLValueBool(false); + trueElement = CLValue.newCLValueBool(true); + falseElement = CLValue.newCLValueBool(false); clValueBoolList = new CLValueList(boolTypeList, [ trueElement, falseElement @@ -135,7 +134,7 @@ describe('CLValueList with boolean values', () => { }); it('should add a boolean element to the list when calling append()', () => { - const newElement = CLValueBool.newCLValueBool(true); + const newElement = CLValue.newCLValueBool(true); clValueBoolList.append(newElement); expect(clValueBoolList.elements).to.deep.equal([ trueElement, @@ -151,7 +150,7 @@ describe('CLValueList with boolean values', () => { }); it('should throw error if index is out of bounds in set() for boolean list', () => { - const newElement = CLValueBool.newCLValueBool(false); + const newElement = CLValue.newCLValueBool(false); expect(() => clValueBoolList.set(2, newElement)).to.throw( 'List index out of bounds.' ); diff --git a/src/types/clvalue/List.ts b/src/types/clvalue/List.ts index 901cacdf4..f205df9e6 100644 --- a/src/types/clvalue/List.ts +++ b/src/types/clvalue/List.ts @@ -1,6 +1,6 @@ import { concat } from '@ethersproject/bytes'; -import { CLType, CLTypeList } from './cltype'; +import { CLTypeList } from './cltype'; import { CLValue, IResultWithBytes } from './CLValue'; import { CLValueUInt32 } from './Numeric'; import { CLValueParser } from './Parser'; @@ -124,22 +124,6 @@ export class CLValueList { return this.elements.map(d => d.toJSON()); } - /** - * 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, - elements: CLValue[] = [] - ): CLValue { - const listType = new CLTypeList(elementType); - const clValue = new CLValue(listType); - clValue.list = new CLValueList(listType, elements); - return clValue; - } - /** * Creates a CLValueList instance from a Uint8Array. * Parses the byte array to interpret the length of the list and each element in the list. diff --git a/src/types/clvalue/Map.test.ts b/src/types/clvalue/Map.test.ts index efb274c0a..8e8f90212 100644 --- a/src/types/clvalue/Map.test.ts +++ b/src/types/clvalue/Map.test.ts @@ -2,26 +2,21 @@ import { expect } from 'chai'; import { CLValueMap } from './Map'; import { CLTypeBool, CLTypeInt32, CLTypeMap, CLTypeString } from './cltype'; -import { CLValueBool } from './Bool'; -import { CLValueString } from './String'; -import { CLValueInt32 } from './Numeric'; import { CLValueParser } from './Parser'; +import { CLValue } from './CLValue'; describe('CLValue CLMap implementation', () => { it('Maps should return proper clType', () => { const mapType = new CLTypeMap(CLTypeBool, CLTypeBool); const testMap = new CLValueMap(mapType); - testMap.append( - CLValueBool.newCLValueBool(true), - CLValueBool.newCLValueBool(false) - ); + testMap.append(CLValue.newCLValueBool(true), CLValue.newCLValueBool(false)); expect(testMap.toString()).to.be.eq('(true="false")'); }); it('Should be able to create Map with proper values - correct by construction', () => { - const myKey = CLValueString.newCLString('ABC'); - const myVal = CLValueInt32.newCLInt32(123); + const myKey = CLValue.newCLString('ABC'); + const myVal = CLValue.newCLInt32(123); const mapType = new CLTypeMap(CLTypeString, CLTypeInt32); const testMap = new CLValueMap(mapType); testMap.append(myKey, myVal); @@ -30,8 +25,8 @@ describe('CLValue CLMap implementation', () => { }); it('Should be able to return proper values by calling .get() on Map', () => { - const myKey = CLValueString.newCLString('ABC'); - const myVal = CLValueInt32.newCLInt32(10); + const myKey = CLValue.newCLString('ABC'); + const myVal = CLValue.newCLInt32(10); const mapType = new CLTypeMap(CLTypeString, CLTypeInt32); const testMap = new CLValueMap(mapType); testMap.append(myKey, myVal); @@ -58,9 +53,9 @@ describe('CLValue CLMap implementation', () => { }); it('fromBytes() / toBytes()', () => { - const myKey = CLValueString.newCLString('ABC'); - const myVal = CLValueInt32.newCLInt32(10); - const clValueMap = CLValueMap.newCLMap(CLTypeString, CLTypeInt32); + const myKey = CLValue.newCLString('ABC'); + const myVal = CLValue.newCLInt32(10); + const clValueMap = CLValue.newCLMap(CLTypeString, CLTypeInt32); clValueMap.map?.append(myKey, myVal); const bytes = clValueMap.bytes(); @@ -71,9 +66,9 @@ describe('CLValue CLMap implementation', () => { }); it('fromJSON() / toJSON()', () => { - const myKey = CLValueString.newCLString('ABC'); - const myVal = CLValueInt32.newCLInt32(10); - const clValueMap = CLValueMap.newCLMap(CLTypeString, CLTypeInt32); + const myKey = CLValue.newCLString('ABC'); + const myVal = CLValue.newCLInt32(10); + const clValueMap = CLValue.newCLMap(CLTypeString, CLTypeInt32); clValueMap.map?.append(myKey, myVal); const json = CLValueParser.toJSON(clValueMap); diff --git a/src/types/clvalue/Map.ts b/src/types/clvalue/Map.ts index 800fe7226..2834dc333 100644 --- a/src/types/clvalue/Map.ts +++ b/src/types/clvalue/Map.ts @@ -1,6 +1,6 @@ import { concat } from '@ethersproject/bytes'; -import { CLType, CLTypeMap } from './cltype'; +import { CLTypeMap } from './cltype'; import { CLValue, IResultWithBytes } from './CLValue'; import { CLValueTuple2 } from './Tuple2'; import { CLValueUInt32 } from './Numeric'; @@ -169,7 +169,7 @@ export class CLValueMap { return new Error('map key already exists'); } - const tuple = CLValueTuple2.newCLTuple2(key, val).tuple2; + const tuple = CLValue.newCLTuple2(key, val).tuple2; if (!tuple) { return new Error('no tuple'); @@ -219,17 +219,4 @@ export class CLValueMap { return { result: mapResult, bytes: remainder }; } - - /** - * Creates a new CLValue instance with a Map value. - * @param keyType - The CLType for the map keys. - * @param valType - The CLType for the map values. - * @returns A new CLValue instance with CLTypeMap and a CLValueMap. - */ - public static newCLMap(keyType: CLType, valType: CLType): CLValue { - const mapType = new CLTypeMap(keyType, valType); - const clValue = new CLValue(mapType); - clValue.map = new CLValueMap(mapType); - return clValue; - } } diff --git a/src/types/clvalue/Numeric/Int32.ts b/src/types/clvalue/Numeric/Int32.ts index f1f3af162..d9d6cef90 100644 --- a/src/types/clvalue/Numeric/Int32.ts +++ b/src/types/clvalue/Numeric/Int32.ts @@ -1,7 +1,7 @@ import { BigNumber, BigNumberish } from '@ethersproject/bignumber'; -import { CLTypeInt32, Int32ByteSize } from '../cltype'; -import { CLValue, IResultWithBytes } from '../CLValue'; +import { Int32ByteSize } from '../cltype'; +import { IResultWithBytes } from '../CLValue'; import { toBytesI32 } from '../../ByteConverters'; import { CLValueNumeric } from './Abstract'; @@ -22,17 +22,6 @@ export class CLValueInt32 extends CLValueNumeric { return toBytesI32(this.value); } - /** - * Creates a new CLValue instance with an Int32 value. - * @param val - The 32-bit integer to be encapsulated in a CLValue. - * @returns A new CLValue instance containing CLTypeInt32 and a CLValueInt32. - */ - public static newCLInt32(val: BigNumberish): CLValue { - const res = new CLValue(CLTypeInt32); - res.i32 = new CLValueInt32(val); - return res; - } - /** * Creates a CLValueInt32 instance from a Uint8Array. * Interprets the first 4 bytes of the array as a 32-bit integer in little-endian format. diff --git a/src/types/clvalue/Numeric/Int64.ts b/src/types/clvalue/Numeric/Int64.ts index a201b299b..9611445b8 100644 --- a/src/types/clvalue/Numeric/Int64.ts +++ b/src/types/clvalue/Numeric/Int64.ts @@ -1,7 +1,7 @@ import { BigNumber, BigNumberish } from '@ethersproject/bignumber'; -import { CLTypeInt64, Int64ByteSize } from '../cltype'; -import { CLValue, IResultWithBytes } from '../CLValue'; +import { Int64ByteSize } from '../cltype'; +import { IResultWithBytes } from '../CLValue'; import { toBytesI64 } from '../../ByteConverters'; import { CLValueNumeric } from './Abstract'; @@ -22,17 +22,6 @@ export class CLValueInt64 extends CLValueNumeric { return toBytesI64(this.value); } - /** - * Creates a new CLValue instance with an Int64 value. - * @param val - The value to be stored in the Int64. Accepts any BigNumberish type. - * @returns A new CLValue instance containing CLTypeInt64 and a CLValueInt64. - */ - public static newCLInt64(val: BigNumberish): CLValue { - const res = new CLValue(CLTypeInt64); - res.i64 = new CLValueInt64(val); - return res; - } - /** * Creates a CLValueInt64 instance from a Uint8Array. * Interprets the first 8 bytes of the array as a 64-bit integer in little-endian format. diff --git a/src/types/clvalue/Numeric/Numeric.test.ts b/src/types/clvalue/Numeric/Numeric.test.ts index 37fb271f9..d42719657 100644 --- a/src/types/clvalue/Numeric/Numeric.test.ts +++ b/src/types/clvalue/Numeric/Numeric.test.ts @@ -1,6 +1,5 @@ import { expect } from 'chai'; import { CLValueUInt32 } from './Uint32'; -import { CLValueUInt128 } from './Uint128'; import { CLValueParser } from '../Parser'; import { CLTypeInt64, @@ -8,9 +7,7 @@ import { CLTypeUInt64, CLTypeUInt8 } from '../cltype'; -import { CLValueUInt64 } from './Uint64'; -import { CLValueUInt8 } from './Uint8'; -import { CLValueInt64 } from './Int64'; +import { CLValue } from '../CLValue'; const MAX_I64 = '9223372036854775807'; const MAX_U8 = 255; @@ -24,15 +21,15 @@ describe('Numeric implementation tests', () => { }); it('Numeric clType() should return proper type', () => { - const num = CLValueUInt128.newCLUInt128(20000); + const num = CLValue.newCLUInt128(20000); expect(num.getType().toString()).to.be.eq('U128'); }); it('CLI32 do proper toBytes()/fromBytes()', () => { - const num1 = CLValueUInt32.newCLUInt32(10); + const num1 = CLValue.newCLUInt32(10); const num1bytes = num1.bytes(); - const num2 = CLValueUInt32.newCLUInt32(1); + const num2 = CLValue.newCLUInt32(1); const num2bytes = num2.bytes(); expect( @@ -44,10 +41,10 @@ describe('Numeric implementation tests', () => { }); it('CLI64 do proper toBytes()/fromBytes()', () => { - const num1 = CLValueInt64.newCLInt64(10); + const num1 = CLValue.newCLInt64(10); const num1bytes = num1.bytes(); - const num2 = CLValueInt64.newCLInt64(MAX_I64); + const num2 = CLValue.newCLInt64(MAX_I64); const num2bytes = num2.bytes(); expect( @@ -59,7 +56,7 @@ describe('Numeric implementation tests', () => { }); it('CLU8 do proper toBytes()/fromBytes()', () => { - const num1 = CLValueUInt8.newCLUint8(MAX_U8); + const num1 = CLValue.newCLUint8(MAX_U8); const num1bytes = num1.bytes(); expect( @@ -68,7 +65,7 @@ describe('Numeric implementation tests', () => { }); it('CLU32 do proper toBytes()/fromBytes()', () => { - const num1 = CLValueUInt32.newCLUInt32(MAX_U32); + const num1 = CLValue.newCLUInt32(MAX_U32); const num1bytes = num1.bytes(); expect( @@ -77,7 +74,7 @@ describe('Numeric implementation tests', () => { }); it('CLU64 do proper toBytes()/fromBytes()', () => { - const num1 = CLValueUInt64.newCLUint64(MAX_U64); + const num1 = CLValue.newCLUint64(MAX_U64); const num1bytes = num1.bytes(); expect( @@ -86,7 +83,7 @@ describe('Numeric implementation tests', () => { }); it('CLU64 toJSON() / fromJSON()', () => { - const num1 = CLValueUInt64.newCLUint64(MAX_U64); + const num1 = CLValue.newCLUint64(MAX_U64); const num1JSON = CLValueParser.toJSON(num1); const expectedJson = JSON.parse( '{"bytes":"ffffffffffffffff","cl_type":"U64"}' diff --git a/src/types/clvalue/Numeric/Uint128.ts b/src/types/clvalue/Numeric/Uint128.ts index ba81fc6f5..819b0a905 100644 --- a/src/types/clvalue/Numeric/Uint128.ts +++ b/src/types/clvalue/Numeric/Uint128.ts @@ -1,8 +1,7 @@ import { BigNumberish } from '@ethersproject/bignumber'; -import { CLTypeUInt128 } from '../cltype'; import { fromBytesUInt128 } from '../UintBig'; -import { CLValue, IResultWithBytes } from '../CLValue'; +import { IResultWithBytes } from '../CLValue'; import { toBytesU128 } from '../../ByteConverters'; import { CLValueNumeric } from './Abstract'; @@ -22,17 +21,6 @@ export class CLValueUInt128 extends CLValueNumeric { return toBytesU128(this.value); } - /** - * Creates a new CLValue instance with a UInt128 value. - * @param value - The value to initialize the UInt128 with. - * @returns A new CLValue instance containing CLTypeUInt128 and a CLValueUInt128. - */ - public static newCLUInt128(value: BigNumberish): CLValue { - const res = new CLValue(CLTypeUInt128); - res.ui128 = new CLValueUInt128(value); - return res; - } - /** * Creates a CLValueUInt128 instance from a Uint8Array. * Parses the byte array to retrieve the UInt128 value. diff --git a/src/types/clvalue/Numeric/Uint256.ts b/src/types/clvalue/Numeric/Uint256.ts index 23491a5f9..f9096a64d 100644 --- a/src/types/clvalue/Numeric/Uint256.ts +++ b/src/types/clvalue/Numeric/Uint256.ts @@ -1,8 +1,7 @@ import { BigNumberish } from '@ethersproject/bignumber'; import { fromBytesUInt256 } from '../UintBig'; -import { CLValue, IResultWithBytes } from '../CLValue'; -import { CLTypeUInt256 } from '../cltype'; +import { IResultWithBytes } from '../CLValue'; import { toBytesU256 } from '../../ByteConverters'; import { CLValueNumeric } from './Abstract'; @@ -22,17 +21,6 @@ export class CLValueUInt256 extends CLValueNumeric { return toBytesU256(this.value); } - /** - * Creates a new CLValue instance with a UInt256 value. - * @param value - The value to initialize the UInt256 with. - * @returns A new CLValue instance containing CLTypeUInt256 and a CLValueUInt256. - */ - public static newCLUInt256(value: BigNumberish): CLValue { - const res = new CLValue(CLTypeUInt256); - res.ui256 = new CLValueUInt256(value); - return res; - } - /** * Creates a CLValueUInt256 instance from a Uint8Array. * Parses the byte array to retrieve the UInt256 value. diff --git a/src/types/clvalue/Numeric/Uint32.ts b/src/types/clvalue/Numeric/Uint32.ts index bf782d9a8..89896bc9a 100644 --- a/src/types/clvalue/Numeric/Uint32.ts +++ b/src/types/clvalue/Numeric/Uint32.ts @@ -1,7 +1,7 @@ import { BigNumber, BigNumberish } from '@ethersproject/bignumber'; -import { CLTypeUInt32, Int32ByteSize } from '../cltype'; -import { CLValue, IResultWithBytes } from '../CLValue'; +import { Int32ByteSize } from '../cltype'; +import { IResultWithBytes } from '../CLValue'; import { toBytesU32 } from '../../ByteConverters'; import { CLValueNumeric } from './Abstract'; @@ -21,17 +21,6 @@ export class CLValueUInt32 extends CLValueNumeric { return toBytesU32(this.value); } - /** - * Creates a new CLValue instance with a UInt32 value. - * @param value - The value to initialize the UInt32 with. - * @returns A new CLValue instance containing CLTypeUInt32 and a CLValueUInt32. - */ - public static newCLUInt32(value: BigNumberish): CLValue { - const res = new CLValue(CLTypeUInt32); - res.ui32 = new CLValueUInt32(value); - return res; - } - /** * Creates a CLValueUInt32 instance from a Uint8Array. * Parses the byte array to retrieve the UInt32 value. diff --git a/src/types/clvalue/Numeric/Uint512.ts b/src/types/clvalue/Numeric/Uint512.ts index 836ae5973..17c7d04fc 100644 --- a/src/types/clvalue/Numeric/Uint512.ts +++ b/src/types/clvalue/Numeric/Uint512.ts @@ -1,8 +1,7 @@ import { BigNumber, BigNumberish } from '@ethersproject/bignumber'; import { fromBytesUInt512 } from '../UintBig'; -import { CLValue, IResultWithBytes } from '../CLValue'; -import { CLTypeUInt512 } from '../cltype'; +import { IResultWithBytes } from '../CLValue'; import { toBytesU512 } from '../../ByteConverters'; import { CLValueNumeric } from './Abstract'; @@ -49,15 +48,4 @@ export class CLValueUInt512 extends CLValueNumeric { ): IResultWithBytes { return fromBytesUInt512(source); } - - /** - * Creates a new CLValue instance with a UInt512 value. - * @param value - The value to initialize the UInt512 with. - * @returns A new CLValue instance containing CLTypeUInt512 and a CLValueUInt512. - */ - public static newCLUInt512(value: BigNumberish): CLValue { - const res = new CLValue(CLTypeUInt512); - res.ui512 = new CLValueUInt512(value); - return res; - } } diff --git a/src/types/clvalue/Numeric/Uint64.ts b/src/types/clvalue/Numeric/Uint64.ts index 5d557d515..50d7df093 100644 --- a/src/types/clvalue/Numeric/Uint64.ts +++ b/src/types/clvalue/Numeric/Uint64.ts @@ -1,7 +1,7 @@ import { BigNumber, BigNumberish } from '@ethersproject/bignumber'; -import { CLTypeUInt64, Int64ByteSize } from '../cltype'; -import { CLValue, IResultWithBytes } from '../CLValue'; +import { Int64ByteSize } from '../cltype'; +import { IResultWithBytes } from '../CLValue'; import { toBytesU64 } from '../../ByteConverters'; import { CLValueNumeric } from './Abstract'; @@ -21,17 +21,6 @@ export class CLValueUInt64 extends CLValueNumeric { return toBytesU64(this.value); } - /** - * Creates a new CLValue instance with a UInt64 value. - * @param val - The value to initialize the UInt64 with. Can be any BigNumberish type. - * @returns A new CLValue instance containing CLTypeUInt64 and a CLValueUInt64. - */ - public static newCLUint64(val: BigNumberish): CLValue { - const res = new CLValue(CLTypeUInt64); - res.ui64 = new CLValueUInt64(val); - return res; - } - /** * Creates a CLValueUInt64 instance from a Uint8Array. * Parses the byte array to retrieve the UInt64 value. diff --git a/src/types/clvalue/Numeric/Uint8.ts b/src/types/clvalue/Numeric/Uint8.ts index 7d97e5a78..78f388eb3 100644 --- a/src/types/clvalue/Numeric/Uint8.ts +++ b/src/types/clvalue/Numeric/Uint8.ts @@ -1,7 +1,6 @@ import { BigNumberish } from '@ethersproject/bignumber'; -import { CLValue, IResultWithBytes } from '../CLValue'; -import { CLTypeUInt8 } from '../cltype'; +import { IResultWithBytes } from '../CLValue'; import { toBytesU8 } from '../../ByteConverters'; import { CLValueNumeric } from './Abstract'; @@ -21,17 +20,6 @@ export class CLValueUInt8 extends CLValueNumeric { return toBytesU8(this.value); } - /** - * Creates a new CLValue instance with a UInt8 value. - * @param value - The value to initialize the UInt8 with. Must be an integer between 0 and 255. - * @returns A new CLValue instance containing CLTypeUInt8 and a CLValueUInt8. - */ - public static newCLUint8(value: BigNumberish): CLValue { - const res = new CLValue(CLTypeUInt8); - res.ui8 = new CLValueUInt8(value); - return res; - } - /** * Creates a CLValueUInt8 instance from a Uint8Array. * Parses the first byte to retrieve the UInt8 value. diff --git a/src/types/clvalue/Option.ts b/src/types/clvalue/Option.ts index cb65d767f..c80db06ac 100644 --- a/src/types/clvalue/Option.ts +++ b/src/types/clvalue/Option.ts @@ -1,6 +1,6 @@ import { concat } from '@ethersproject/bytes'; -import { CLType, CLTypeAny, CLTypeOption } from './cltype'; +import { CLTypeOption } from './cltype'; import { CLValue, IResultWithBytes } from './CLValue'; import { CLValueParser } from './Parser'; import { CLValueUInt8 } from './Numeric'; @@ -73,46 +73,6 @@ export class CLValueOption { return this.inner; } - /** - * Creates a new `CLValue` instance that represents an optional value. - * - * This method allows you to wrap a given `CLValue` as an optional type (`Option`), - * which can either contain a value or be `null`. This is useful for scenarios where - * a value may or may not be present. - * - * If `inner` is `null`, the method will use the provided `clType` as the type of the option. - * If `clType` is not provided, it defaults to `CLTypeAny`. If `inner` is not `null`, - * its type is used instead of `clType`. - * - * @param inner - The `CLValue` to be wrapped in the option. Pass `null` if no value is present. - * @param clType - (Optional) The `CLType` representing the type of the value contained in the option. - * This is required if `inner` is `null` to properly define the option type. - * - * @returns A new `CLValue` instance containing: - * - A `CLTypeOption` representing the type of the optional value. - * - A `CLValueOption` that holds the inner value or `null`. - * - * @example - * ```typescript - * // Example of an option containing a value - * const innerValue = CLValue.fromU32(42); - * const optionValue = CLValue.newCLOption(innerValue); - * - * // Example of an empty option with an explicitly defined type - * const emptyOptionValue = CLValue.newCLOption(null, CLType.U32); - * - * // Example of an empty option with no type provided (defaults to CLTypeAny) - * const emptyOptionValueDefault = CLValue.newCLOption(null); - * ``` - */ - public static newCLOption(inner: CLValue | null, clType?: CLType): CLValue { - const clTypeForOption = inner === null ? clType || CLTypeAny : inner.type; - const optionType = new CLTypeOption(clTypeForOption); - const clValue = new CLValue(optionType); - clValue.option = new CLValueOption(inner, optionType); - return clValue; - } - /** * Creates a CLValueOption instance from a Uint8Array. * Parses the byte array to determine if the option is empty or contains a value. diff --git a/src/types/clvalue/Result.ts b/src/types/clvalue/Result.ts index 16bf495bb..42fc9583b 100644 --- a/src/types/clvalue/Result.ts +++ b/src/types/clvalue/Result.ts @@ -1,6 +1,6 @@ import { concat } from '@ethersproject/bytes'; -import { CLType, CLTypeResult } from './cltype'; +import { CLTypeResult } from './cltype'; import { CLValue, IResultWithBytes } from './CLValue'; import { CLValueParser } from './Parser'; import { CLValueUInt8 } from './Numeric'; @@ -66,26 +66,6 @@ export class CLValueResult { return this.inner; } - /** - * Creates a new CLValue instance with a Result value. - * @param innerOk - The CLType for the success case. - * @param innerErr - The CLType for the error case. - * @param value - The CLValue to be contained in the Result. - * @param isSuccess - A boolean indicating whether the Result is a success (true) or an error (false). - * @returns A new CLValue instance containing CLTypeResult and a CLValueResult. - */ - public static newCLResult( - innerOk: CLType, - innerErr: CLType, - value: CLValue, - isSuccess: boolean - ): CLValue { - const resultType = new CLTypeResult(innerOk, innerErr); - const clValue = new CLValue(resultType); - clValue.result = new CLValueResult(resultType, isSuccess, value); - return clValue; - } - /** * Creates a CLValueResult instance from a Uint8Array. * Parses the byte array to interpret the success flag and the inner value. diff --git a/src/types/clvalue/String.test.ts b/src/types/clvalue/String.test.ts index c280146f5..122129992 100644 --- a/src/types/clvalue/String.test.ts +++ b/src/types/clvalue/String.test.ts @@ -1,6 +1,7 @@ import { expect } from 'chai'; import { CLValueString } from './String'; import { CLValueParser } from './Parser'; +import { CLValue } from './CLValue'; describe('CLString', () => { it('CLString value() should return proper value', () => { @@ -9,14 +10,14 @@ describe('CLString', () => { }); it('toBytes() / fromBytes()', () => { - const str = CLValueString.newCLString('ABC'); + const str = CLValue.newCLString('ABC'); const bytes = str.bytes(); const parsed = CLValueParser.fromBytesByType(bytes, str.type); expect(parsed.result).to.be.deep.eq(str); }); it('toJSON() / fromJSON()', () => { - const str = CLValueString.newCLString('ABC-DEF'); + const str = CLValue.newCLString('ABC-DEF'); const json = CLValueParser.toJSON(str); const fromJSON = CLValueParser.fromJSON(json); const expectedJson = JSON.parse( diff --git a/src/types/clvalue/String.ts b/src/types/clvalue/String.ts index 4ea250d87..7176aaca8 100644 --- a/src/types/clvalue/String.ts +++ b/src/types/clvalue/String.ts @@ -1,7 +1,6 @@ import { concat } from '@ethersproject/bytes'; -import { CLTypeString } from './cltype'; -import { CLValue, IResultWithBytes } from './CLValue'; +import { IResultWithBytes } from './CLValue'; import { CLValueUInt32 } from './Numeric'; import { fromBytesString } from '../ByteConverters'; @@ -59,17 +58,6 @@ export class CLValueString { return this.value; } - /** - * Creates a new CLValue instance with a string value. - * @param val - The string value to be represented. - * @returns A new CLValue instance containing CLTypeString and a CLValueString. - */ - public static newCLString(val: string): CLValue { - const res = new CLValue(CLTypeString); - res.stringVal = new CLValueString(val); - return res; - } - /** * Creates a CLValueString instance from a Uint8Array. * Parses the byte array to retrieve the string value, interpreting the first 4 bytes as the string length. diff --git a/src/types/clvalue/Tuple.test.ts b/src/types/clvalue/Tuple.test.ts index 24f666c38..b0891d2d2 100644 --- a/src/types/clvalue/Tuple.test.ts +++ b/src/types/clvalue/Tuple.test.ts @@ -1,10 +1,5 @@ import { expect } from 'chai'; -import { CLValueBool } from './Bool'; import { CLValueTuple2 } from './Tuple2'; -import { CLValueString } from './String'; -import { CLValueTuple1 } from './Tuple1'; -import { CLValueInt32 } from './Numeric'; -import { CLValueTuple3 } from './Tuple3'; import { CLValueParser } from './Parser'; import { CLTypeBool, @@ -14,43 +9,43 @@ import { CLTypeTuple2, CLTypeTuple3 } from './cltype'; -import { CLValueByteArray } from './ByteArray'; +import { CLValue } from './CLValue'; describe('CLTuple', () => { it('Tuple2 should return proper clType', () => { - const myBool = CLValueBool.newCLValueBool(false); - const myStr = CLValueString.newCLString('ABC'); - const myTup = CLValueTuple2.newCLTuple2(myBool, myStr); + const myBool = CLValue.newCLValueBool(false); + const myStr = CLValue.newCLString('ABC'); + const myTup = CLValue.newCLTuple2(myBool, myStr); expect(myTup.getType().toString()).to.be.eq('Tuple2 (Bool, String)'); }); it('Should be able to create tuple with proper values - correct by construction', () => { - const myTup2 = CLValueTuple2.newCLTuple2( - CLValueBool.newCLValueBool(true), - CLValueBool.newCLValueBool(false) + const myTup2 = CLValue.newCLTuple2( + CLValue.newCLValueBool(true), + CLValue.newCLValueBool(false) ); expect(myTup2.tuple2).to.be.an.instanceof(CLValueTuple2); }); it('Should be able to return proper values by calling .value() on Tuple', () => { - const myBool = CLValueBool.newCLValueBool(false); - const myTuple = CLValueTuple1.newCLTuple1(myBool); + const myBool = CLValue.newCLValueBool(false); + const myTuple = CLValue.newCLTuple1(myBool); expect(myTuple.tuple1?.value()).to.be.deep.eq(myBool); }); it('Should run toBytes() / fromBytes()', () => { - const myTup1 = CLValueTuple1.newCLTuple1(CLValueBool.newCLValueBool(true)); - const myTup2 = CLValueTuple2.newCLTuple2( - CLValueBool.newCLValueBool(false), - CLValueInt32.newCLInt32(555) + const myTup1 = CLValue.newCLTuple1(CLValue.newCLValueBool(true)); + const myTup2 = CLValue.newCLTuple2( + CLValue.newCLValueBool(false), + CLValue.newCLInt32(555) ); - const myTup3 = CLValueTuple3.newCLTuple3( - CLValueInt32.newCLInt32(555), - CLValueString.newCLString('ABC'), - CLValueString.newCLString('XYZ') + const myTup3 = CLValue.newCLTuple3( + CLValue.newCLInt32(555), + CLValue.newCLString('ABC'), + CLValue.newCLString('XYZ') ); const myTup1Bytes = myTup1.bytes(); @@ -78,8 +73,8 @@ describe('CLTuple', () => { }); it('fromJSON() / toJSON()', () => { - const arr = CLValueByteArray.newCLByteArray(Uint8Array.from([1, 2, 3])); - const arr2 = CLValueByteArray.newCLByteArray( + const arr = CLValue.newCLByteArray(Uint8Array.from([1, 2, 3])); + const arr2 = CLValue.newCLByteArray( Uint8Array.from([ 1, 2, @@ -118,13 +113,9 @@ describe('CLTuple', () => { ]) ); - const myTup1 = CLValueTuple1.newCLTuple1(arr); - const myTup2 = CLValueTuple2.newCLTuple2(arr, arr2); - const myTup3 = CLValueTuple3.newCLTuple3( - arr, - arr2, - CLValueString.newCLString('ABC') - ); + const myTup1 = CLValue.newCLTuple1(arr); + const myTup2 = CLValue.newCLTuple2(arr, arr2); + const myTup3 = CLValue.newCLTuple3(arr, arr2, CLValue.newCLString('ABC')); const myTup1JSON = CLValueParser.toJSON(myTup1); const expectedMyTup1JSON = JSON.parse( @@ -152,15 +143,15 @@ describe('CLTuple', () => { }); it('fromJSON() / toJSON()', () => { - const myTup1 = CLValueTuple1.newCLTuple1(CLValueBool.newCLValueBool(true)); - const myTup2 = CLValueTuple2.newCLTuple2( - CLValueBool.newCLValueBool(false), - CLValueInt32.newCLInt32(555) + const myTup1 = CLValue.newCLTuple1(CLValue.newCLValueBool(true)); + const myTup2 = CLValue.newCLTuple2( + CLValue.newCLValueBool(false), + CLValue.newCLInt32(555) ); - const myTup3 = CLValueTuple3.newCLTuple3( - CLValueInt32.newCLInt32(555), - CLValueString.newCLString('ABC'), - CLValueString.newCLString('XYZ') + const myTup3 = CLValue.newCLTuple3( + CLValue.newCLInt32(555), + CLValue.newCLString('ABC'), + CLValue.newCLString('XYZ') ); const myTup1JSON = CLValueParser.toJSON(myTup1); diff --git a/src/types/clvalue/Tuple1.ts b/src/types/clvalue/Tuple1.ts index 9f8c56443..158400f9e 100644 --- a/src/types/clvalue/Tuple1.ts +++ b/src/types/clvalue/Tuple1.ts @@ -52,18 +52,6 @@ export class CLValueTuple1 { return [this.innerVal.toJSON()]; } - /** - * Creates a new CLValue instance with a Tuple1 value. - * @param val - The CLValue to be contained in the tuple. - * @returns A new CLValue instance containing CLTypeTuple1 and a CLValueTuple1. - */ - public static newCLTuple1(val: CLValue): CLValue { - const tupleType = new CLTypeTuple1(val.type); - const clValue = new CLValue(tupleType); - clValue.tuple1 = new CLValueTuple1(tupleType, val); - return clValue; - } - /** * Creates a CLValueTuple1 instance from a Uint8Array. * Parses the byte array to retrieve the inner value of the tuple. diff --git a/src/types/clvalue/Tuple2.ts b/src/types/clvalue/Tuple2.ts index b8d4942d8..d96076f38 100644 --- a/src/types/clvalue/Tuple2.ts +++ b/src/types/clvalue/Tuple2.ts @@ -59,19 +59,6 @@ export class CLValueTuple2 { return [this.inner1, this.inner2]; } - /** - * Creates a new CLValue instance with a Tuple2 value. - * @param val1 - The first CLValue in the tuple. - * @param val2 - The second CLValue in the tuple. - * @returns A new CLValue instance containing CLTypeTuple2 and a CLValueTuple2. - */ - public static newCLTuple2(val1: CLValue, val2: CLValue): CLValue { - const tupleType = new CLTypeTuple2(val1.type, val2.type); - const clValue = new CLValue(tupleType); - clValue.tuple2 = new CLValueTuple2(tupleType, val1, val2); - return clValue; - } - /** * Creates a CLValueTuple2 instance from a Uint8Array. * Parses the byte array to retrieve the two values of the tuple. diff --git a/src/types/clvalue/Tuple3.ts b/src/types/clvalue/Tuple3.ts index c9ba6db8c..df5448ad8 100644 --- a/src/types/clvalue/Tuple3.ts +++ b/src/types/clvalue/Tuple3.ts @@ -68,24 +68,6 @@ export class CLValueTuple3 { return [this.inner1, this.inner2, this.inner3]; } - /** - * Creates a new CLValue instance with a Tuple3 value. - * @param val1 - The first CLValue in the tuple. - * @param val2 - The second CLValue in the tuple. - * @param val3 - The third CLValue in the tuple. - * @returns A new CLValue instance containing CLTypeTuple3 and a CLValueTuple3. - */ - public static newCLTuple3( - val1: CLValue, - val2: CLValue, - val3: CLValue - ): CLValue { - const tupleType = new CLTypeTuple3(val1.type, val2.type, val3.type); - const clValue = new CLValue(tupleType); - clValue.tuple3 = new CLValueTuple3(tupleType, val1, val2, val3); - return clValue; - } - /** * Creates a CLValueTuple3 instance from a Uint8Array. * Parses the byte array to retrieve the three values of the tuple. diff --git a/src/types/clvalue/Unit.test.ts b/src/types/clvalue/Unit.test.ts index 8c9f09a2e..b23fbfa31 100644 --- a/src/types/clvalue/Unit.test.ts +++ b/src/types/clvalue/Unit.test.ts @@ -1,6 +1,7 @@ import { expect } from 'chai'; import { CLValueUnit } from './Unit'; import { CLValueParser } from './Parser'; +import { CLValue } from './CLValue'; describe('Unit implementation tests', () => { it('Unit value() should return proper value', () => { @@ -9,7 +10,7 @@ describe('Unit implementation tests', () => { }); it('fromJSON() / toJSON()', () => { - const unit = CLValueUnit.newCLUnit(); + const unit = CLValue.newCLUnit(); const json = CLValueParser.toJSON(unit); const expectedJson = JSON.parse('{"bytes":"","cl_type":"Unit"}'); diff --git a/src/types/clvalue/Unit.ts b/src/types/clvalue/Unit.ts index f6596e172..b407fa5b6 100644 --- a/src/types/clvalue/Unit.ts +++ b/src/types/clvalue/Unit.ts @@ -1,5 +1,4 @@ -import { CLTypeUnit } from './cltype'; -import { CLValue, IResultWithBytes } from './CLValue'; +import { IResultWithBytes } from './CLValue'; /** * Represents a Unit value in the Casper type system. @@ -43,17 +42,6 @@ export class CLValueUnit { return this.obj; } - /** - * Creates a new CLValue instance with a Unit value. - * - * @returns A new CLValue instance with CLTypeUnit and a CLValueUnit. - */ - public static newCLUnit(): CLValue { - const res = new CLValue(CLTypeUnit); - res.unit = new CLValueUnit(); - return res; - } - /** * Converts the instance to a JSON-compatible null value. * diff --git a/src/utils/auction-manager.ts b/src/utils/auction-manager.ts index 2fca29a69..a58c1d5f4 100644 --- a/src/utils/auction-manager.ts +++ b/src/utils/auction-manager.ts @@ -1,7 +1,6 @@ import { Args, CLValue, - CLValueUInt512, ContractHash, DEFAULT_DEPLOY_TTL, Deploy, @@ -27,6 +26,7 @@ export interface IMakeAuctionManagerDeployParams { paymentAmount?: string; chainName?: CasperNetworkName; ttl?: number; + contractHash?: string; timestamp?: string; } @@ -55,6 +55,7 @@ export interface IMakeAuctionManagerDeployParams { * Specifies how long the `Deploy` is valid before it expires. * Defaults 1800000 (30 minutes) * @param params.timestamp - (Optional) The timestamp in ISO 8601 format + * @param params.contractHash - (Optional) The custom contract hash * * @returns A deploy object that can be signed and sent to the network. * @@ -81,6 +82,7 @@ export const makeAuctionManagerDeploy = ({ chainName = CasperNetworkName.Mainnet, newValidatorPublicKeyHex, ttl = DEFAULT_DEPLOY_TTL, + contractHash, timestamp }: IMakeAuctionManagerDeployParams) => { const delegatorPublicKey = PublicKey.newPublicKey(delegatorPublicKeyHex); @@ -88,15 +90,23 @@ export const makeAuctionManagerDeploy = ({ const newValidatorValidatorPublicKey = newValidatorPublicKeyHex ? PublicKey.newPublicKey(newValidatorPublicKeyHex) : null; + const auctionContractHash = + contractHash ?? AuctionManagerContractHashMap[chainName]; + + if (!auctionContractHash) { + throw new Error( + `Auction contract hash is undefined for chain: ${chainName}` + ); + } const session = new ExecutableDeployItem(); session.storedContractByHash = new StoredContractByHash( - ContractHash.newContract(AuctionManagerContractHashMap[chainName]), + ContractHash.newContract(auctionContractHash), contractEntryPoint, Args.fromMap({ validator: CLValue.newCLPublicKey(validatorPublicKey), delegator: CLValue.newCLPublicKey(delegatorPublicKey), - amount: CLValueUInt512.newCLUInt512(amount), + amount: CLValue.newCLUInt512(amount), ...(newValidatorValidatorPublicKey ? { new_validator: CLValue.newCLPublicKey( diff --git a/src/utils/casper-network.ts b/src/utils/casper-network.ts index ec95944b7..1290b7f25 100644 --- a/src/utils/casper-network.ts +++ b/src/utils/casper-network.ts @@ -1,3 +1,5 @@ +import { BigNumber } from '@ethersproject/bignumber'; + import { InfoGetTransactionResult, PutDeployResult, @@ -7,7 +9,6 @@ import { import { Args, CLValue, - CLValueUInt512, ContractCallBuilder, NativeDelegateBuilder, NativeRedelegateBuilder, @@ -18,7 +19,6 @@ import { Transaction, TransactionHash } from '../types'; -import { BigNumber } from '@ethersproject/bignumber'; export class CasperNetwork { private rpcClient: RpcClient; @@ -72,7 +72,7 @@ export class CasperNetwork { Args.fromMap({ validator: CLValue.newCLPublicKey(validatorPublicKey), delegator: CLValue.newCLPublicKey(delegatorPublicKey), - amount: CLValueUInt512.newCLUInt512(amountMotes) + amount: CLValue.newCLUInt512(amountMotes) }) ) .ttl(ttl) @@ -115,7 +115,7 @@ export class CasperNetwork { Args.fromMap({ validator: CLValue.newCLPublicKey(validatorPublicKey), delegator: CLValue.newCLPublicKey(delegatorPublicKey), - amount: CLValueUInt512.newCLUInt512(amountMotes) + amount: CLValue.newCLUInt512(amountMotes) }) ) .buildFor1_5(); @@ -158,7 +158,7 @@ export class CasperNetwork { Args.fromMap({ validator: CLValue.newCLPublicKey(validatorPublicKey), delegator: CLValue.newCLPublicKey(delegatorPublicKey), - amount: CLValueUInt512.newCLUInt512(amountMotes), + amount: CLValue.newCLUInt512(amountMotes), ...(newValidatorPublicKey ? { new_validator: CLValue.newCLPublicKey(newValidatorPublicKey) diff --git a/src/utils/cep-18-transfer.ts b/src/utils/cep-18-transfer.ts index d464c8f50..5d349a8f4 100644 --- a/src/utils/cep-18-transfer.ts +++ b/src/utils/cep-18-transfer.ts @@ -1,7 +1,6 @@ import { Args, CLValue, - CLValueUInt256, ContractHash, DEFAULT_DEPLOY_TTL, Deploy, @@ -90,7 +89,7 @@ export const makeCep18TransferDeploy = ({ KeyTypeID.Account ) ), - amount: CLValueUInt256.newCLUInt256(transferAmount) + amount: CLValue.newCLUInt256(transferAmount) }) ); diff --git a/src/utils/cep-nft-transfer.ts b/src/utils/cep-nft-transfer.ts index 79463ed20..244a0b0c7 100644 --- a/src/utils/cep-nft-transfer.ts +++ b/src/utils/cep-nft-transfer.ts @@ -2,10 +2,6 @@ import { Args, CLTypeUInt256, CLValue, - CLValueBool, - CLValueList, - CLValueUInt256, - CLValueUInt64, ContractHash, DEFAULT_DEPLOY_TTL, Deploy, @@ -161,17 +157,14 @@ export const getRuntimeArgsForCep78Transfer = ({ if (tokenId) { runtimeArgs.insert( 'is_hash_identifier_mode', - CLValueBool.newCLValueBool(false) + CLValue.newCLValueBool(false) ); - runtimeArgs.insert('token_id', CLValueUInt64.newCLUint64(tokenId)); + runtimeArgs.insert('token_id', CLValue.newCLUint64(tokenId)); } if (tokenHash) { - runtimeArgs.insert( - 'is_hash_identifier_mode', - CLValueBool.newCLValueBool(true) - ); - runtimeArgs.insert('token_id', CLValueUInt64.newCLUint64(tokenHash)); + runtimeArgs.insert('is_hash_identifier_mode', CLValue.newCLValueBool(true)); + runtimeArgs.insert('token_id', CLValue.newCLUint64(tokenHash)); } return runtimeArgs; @@ -192,8 +185,6 @@ export function getRuntimeArgsForCep47Transfer({ KeyTypeID.Account ) ), - token_ids: CLValueList.newCLList(CLTypeUInt256, [ - CLValueUInt256.newCLUInt256(tokenId) - ]) + token_ids: CLValue.newCLList(CLTypeUInt256, [CLValue.newCLUInt256(tokenId)]) }); } diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 1cbc7c0cd..b7de58fd8 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -9,5 +9,7 @@ export const AuctionManagerContractHashMap: Record< [CasperNetworkName.Testnet]: '93d923e336b20a4c4ca14d592b60e5bd3fe330775618290104f9beb326db7ae2', [CasperNetworkName.Integration]: - 'e22d38bcf3454a93face78a353feaccbf1d637d1ef9ef2e061a655728ff59bbe' + 'e22d38bcf3454a93face78a353feaccbf1d637d1ef9ef2e061a655728ff59bbe', + [CasperNetworkName.DevNet]: + '93d923e336b20a4c4ca14d592b60e5bd3fe330775618290104f9beb326db7ae2' };