Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored CLValue creation by moving newCL methods into the CLValue class, added possibility to set contract hash for transaction builder #498

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions resources/migration-guide-v2-v5.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
File renamed without changes.
5 changes: 3 additions & 2 deletions src/@types/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export enum CasperNetworkName {
Mainnet = 'casper',
Testnet = 'casper-test',
Integration = 'integration-test'
Integration = 'integration-test',
DevNet = 'dev-net'
}

export enum AuctionManagerEntryPoint {
Expand All @@ -10,7 +11,7 @@ export enum AuctionManagerEntryPoint {
redelegate = 'redelegate',
addBid = 'add_bid',
withdrawBid = 'withdraw_bid',
activateBid = 'activate_bid',
activateBid = 'activate_bid'
}

export enum NFTTokenStandard {
Expand Down
1 change: 1 addition & 0 deletions src/types/Deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ export class Deploy {
paymentLimitedMode.gasPriceTolerance = 1;
paymentLimitedMode.paymentAmount = paymentAmount;
paymentLimitedMode.standardPayment = standardPayment;
pricingMode.paymentLimited = paymentLimitedMode;

return new Transaction(
deploy.hash,
Expand Down
46 changes: 14 additions & 32 deletions src/types/ExecutableDeployItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]);

Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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));
}
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 9 additions & 9 deletions src/types/PricingMode.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
98 changes: 51 additions & 47 deletions src/types/Transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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();
Expand Down Expand Up @@ -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: ''
}
]
]
Expand All @@ -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'
);
});
});
4 changes: 2 additions & 2 deletions src/types/TransactionBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -74,7 +74,7 @@ describe('Test TransactionBuilder', () => {
KeyTypeID.Account
)
),
amount: CLValueUInt256.newCLUInt256('1000000000')
amount: CLValue.newCLUInt256('1000000000')
})
)
.payment(2_000000000)
Expand Down
Loading
Loading