Skip to content

Commit

Permalink
Added parseAsWriteAccount method / getter for TransformKind data, uni…
Browse files Browse the repository at this point in the history
…t tests for different scenarios of parsing TransformKind data. Changed RawWriteCLValue / WriteCLValue values from Args to CLValue. Added RawWriteAccount class for parsing WriteAccount parsing
  • Loading branch information
alexmyshchyshyn committed Dec 24, 2024
1 parent 1690bf8 commit d6812ac
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 17 deletions.
146 changes: 146 additions & 0 deletions src/types/Transform.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { assert, expect } from 'chai';

import { TransformKind } from './Transform';
import { CLValue } from './clvalue';

describe('TransformKind JSON Parsing and CLValue Transformation', () => {
const transformJson = {
WriteCLValue: {
bytes: '0467295a93',
parsed: '2472159591',
cl_type: 'U512'
}
};

it('should correctly parse a valid TransformKind JSON object', () => {
const transform = TransformKind.fromJSON(transformJson);
expect(transform).to.not.be.undefined;
});

it('should parse WriteCLValue as a valid CLValue instance', () => {
const transform = TransformKind.fromJSON(transformJson);
const clValue = transform!.parseAsWriteCLValue()!;
expect(clValue).to.be.an.instanceOf(CLValue);
});

it('should correctly parse and match the U512 value', () => {
const transform = TransformKind.fromJSON(transformJson);
const clValue = transform!.parseAsWriteCLValue()!;
assert.deepEqual(
clValue.ui512!.toNumber(),
parseInt(transformJson.WriteCLValue.parsed, 10)
);
});

it('should correctly parse Identity tranform', () => {
const transformIdentityJson = {
transform: 'Identity'
};
const transform = TransformKind.fromJSON(transformIdentityJson);
expect(transformIdentityJson.transform).to.deep.equal(
transform?.transformationData.transform
);
});

it('should correctly parse and match the WriteAccount', () => {
const transformWriteAccountJson = {
WriteAccount:
'account-hash-7f3688d1dc6adff9ce5e3ec90628f88e2fb39b763724cbdf9a7f9f68de0205ba'
};
const transform = TransformKind.fromJSON(transformWriteAccountJson);
const accountHash = transform!.parseAsWriteAccount()!;
expect(transformWriteAccountJson.WriteAccount).to.deep.equal(
accountHash.toPrefixedString()
);
});

it('should correctly parse and match the WriteDeployInfo', () => {
const transformWriteDeployInfoJson = {
WriteDeployInfo: {
gas: '981979384',
from:
'account-hash-7f3688d1dc6adff9ce5e3ec90628f88e2fb39b763724cbdf9a7f9f68de0205ba',
source:
'uref-68b56afb28a42c3787326def04177eaa64ab31afdc7316e936f9ba1810019e4c-007',
transfers: [],
deploy_hash:
'a2a4b37e33a04d5922e435e98ec8d555370a976eb7fa9913155a615fb2536649'
}
};
const transform = TransformKind.fromJSON(transformWriteDeployInfoJson);
const deployInfo = transform!.parseAsWriteDeployInfo()!;
expect(deployInfo.from.toPrefixedString()).to.deep.equal(
transformWriteDeployInfoJson.WriteDeployInfo.from
);
expect(deployInfo.gas).to.deep.equal(
transformWriteDeployInfoJson.WriteDeployInfo.gas
);
expect(deployInfo.source.toPrefixedString()).to.deep.equal(
transformWriteDeployInfoJson.WriteDeployInfo.source
);
expect(deployInfo.transfers).to.deep.equal([]);
expect(deployInfo.deployHash.toHex()).to.deep.equal(
transformWriteDeployInfoJson.WriteDeployInfo.deploy_hash
);
});

it('should correctly parse and match the AddUInt512', () => {
const transformAddUInt512Json = {
AddUInt512: '2472159591'
};
const transform = TransformKind.fromJSON(transformAddUInt512Json);
const uInt512 = transform!.parseAsUInt512()!;
assert.deepEqual(
uInt512!.toNumber(),
parseInt(transformAddUInt512Json.AddUInt512, 10)
);
});

it('should correctly parse and match the WriteTransfer', () => {
const transformWriteJson = {
WriteTransfer: {
id: 0,
to:
'account-hash-c54c0a2fd689a68dd4114d4852fefa414d24d04f531176729070ca83bd924bea',
gas: '0',
from:
'account-hash-d9c89d3ef62f1c8c5f951d1e44f136f133b728ae7291ea5d4f36530b6f02a910',
amount: '193125900000000',
source:
'uref-b27476d0aa1d55ce26512424b40b093e3057b4d79636f124a3a5e1a9f44733e8-007',
target:
'uref-654802c4a00cf5e05ecb5c57a7d7731b1f8fa50036c17b49a4b0a5e71dd35055-004',
deploy_hash:
'683cbcf69dd1a029d4291e873d600566b50b40a7a40da2ba98e169971cd92ddd'
}
};

const transform = TransformKind.fromJSON(transformWriteJson);
const write = transform!.parseAsWriteTransfer();

expect(write.from.toPrefixedString()).to.deep.equal(
transformWriteJson.WriteTransfer.from
);
expect(write.to!.toPrefixedString()).to.deep.equal(
transformWriteJson.WriteTransfer.to
);
assert.deepEqual(
write.gas,
parseInt(transformWriteJson.WriteTransfer.gas, 10)
);
expect(write.source.toPrefixedString()).to.deep.equal(
transformWriteJson.WriteTransfer.source
);
assert.deepEqual(
write.amount.toNumber(),
parseInt(transformWriteJson.WriteTransfer.amount, 10)
);
assert.deepEqual(write.id, transformWriteJson.WriteTransfer.id);
expect(write.deployHash.toHex()).to.deep.equal(
transformWriteJson.WriteTransfer.deploy_hash
);
expect(write.target.toPrefixedString()).to.deep.equal(
transformWriteJson.WriteTransfer.target
);
});
});
38 changes: 32 additions & 6 deletions src/types/Transform.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { jsonObject, jsonMember, TypedJSON } from 'typedjson';

import { Key } from './key';
import { AccountHash, Key } from './key';
import { UnbondingPurse } from './UnbondingPurse';
import { AddressableEntity } from './AddressableEntity';
import { Package } from './Package';
import { BidKind } from './BidKind';
import { MessageChecksum, MessageTopicSummary } from './MessageTopic';
import { DeployInfo } from './DeployInfo';
import { CLValueUInt512 } from './clvalue';
import { CLValue, CLValueUInt512 } from './clvalue';
import { Args } from './Args';
import { deserializeArgs, serializeArgs } from './SerializationUtils';
import {
Expand All @@ -17,6 +17,7 @@ import {
RawDataMessageTopic,
RawDataNamedKey,
RawUInt512,
RawWriteAccount,
RawWriteCLValue,
RawWriteCLValueV2,
RawWriteDeployInfo,
Expand All @@ -40,14 +41,23 @@ export class TransformKind {
*
* @param data The transformation data as a string.
*/
constructor(data = '') {
constructor(data: any) {
this.data = data;
}

/**
* Getter for transformation data.
*
* @returns The transformation data.
*/
public get transformationData(): any {
return this.data;
}

/**
* Creates a `TransformKind` instance from a JSON string.
*
* @param data The transformation data as a string.
* @param json The transformation data as a string.
* @returns The `TransformKind` instance.
*/
static fromJSON(json: any): TransformKind | undefined {
Expand All @@ -62,7 +72,7 @@ export class TransformKind {
*
* @returns The transformation data as a string.
*/
public toJSON(): string {
public toJSON(): any {
return this.data;
}

Expand Down Expand Up @@ -369,6 +379,22 @@ export class TransformKind {
return jsonRes.UInt512;
}

/**
* Attempts to parse the transformation as a WriteDeployInfo.
*
* @returns A `DeployInfo` object if the data matches, otherwise `throw an error`.
*/
public parseAsWriteAccount(): AccountHash {
const serializer = new TypedJSON(RawWriteAccount);
const jsonRes = serializer.parse(this.data);

if (!jsonRes || !jsonRes.WriteAccount) {
throw new Error(`Error parsing as DeployInfo`);
}

return jsonRes.WriteAccount;
}

/**
* Attempts to parse the transformation as a WriteDeployInfo.
*
Expand All @@ -390,7 +416,7 @@ export class TransformKind {
*
* @returns The `Args` object if the data matches, otherwise `throw an error`.
*/
public parseAsWriteCLValue(): Args {
public parseAsWriteCLValue(): CLValue {
const serializer = new TypedJSON(RawWriteCLValue);
const jsonRes = serializer.parse(this.data);

Expand Down
52 changes: 41 additions & 11 deletions src/types/TransformRaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import { AddressableEntity } from './AddressableEntity';
import { Package } from './Package';
import { BidKind } from './BidKind';
import { MessageTopicSummary } from './MessageTopic';
import { CLValueUInt512 } from './clvalue';
import { CLValue, CLValueParser, CLValueUInt512 } from './clvalue';
import { DeployInfo } from './DeployInfo';
import { Args } from './Args';
import { deserializeArgs, serializeArgs } from './SerializationUtils';
import { BigNumber } from '@ethersproject/bignumber';
import { AccountHash, Hash, URef } from './key';

Expand Down Expand Up @@ -330,6 +328,26 @@ export class RawWriteDeployInfo {
WriteDeployInfo?: DeployInfo;
}

/**
* Represents raw write data for account hash.
*/
@jsonObject
export class RawWriteAccount {
/**
* The account information in the write operation.
*/
@jsonMember({
constructor: AccountHash,
name: 'WriteAccount',
deserializer: json => {
if (!json) return;
return AccountHash.fromJSON(json);
},
serializer: (value: AccountHash) => value.toJSON()
})
WriteAccount?: AccountHash;
}

/**
* Represents raw write data for a CLValue.
* Used for serializing and deserializing the arguments of a CLValue write operation.
Expand All @@ -339,12 +357,18 @@ export class RawWriteCLValue {
/**
* The write operation on a CLValue represented as `Args`.
*/
@jsonMember(() => Args, {
deserializer: deserializeArgs,
serializer: (args: Args) => serializeArgs(args, false),
@jsonMember(() => CLValue, {
deserializer: json => {
if (!json) return;
return CLValueParser.fromJSON(json);
},
serializer: (value: CLValue) => {
if (!value) return;
return CLValueParser.toJSON(value);
},
name: 'WriteCLValue'
})
WriteCLValue?: Args;
WriteCLValue?: CLValue;
}

/**
Expand All @@ -355,12 +379,18 @@ export class WriteCLValue {
/**
* The CLValue write operation represented as `Args`.
*/
@jsonMember(() => Args, {
deserializer: deserializeArgs,
serializer: (args: Args) => serializeArgs(args, false),
@jsonMember(() => CLValue, {
deserializer: json => {
if (!json) return;
return CLValueParser.fromJSON(json);
},
serializer: (value: CLValue) => {
if (!value) return;
return CLValueParser.toJSON(value);
},
name: 'CLValue'
})
CLValue?: Args;
CLValue?: CLValue;
}

/**
Expand Down

0 comments on commit d6812ac

Please sign in to comment.