Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

Commit

Permalink
Deploy serialization tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
zie1ony committed Feb 9, 2021
1 parent 059879d commit 37ff810
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/sdk/src/lib/CLValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1744,8 +1744,8 @@ export class CLValue implements ToBytes {
if (!this.isList()) {
throw new Error('The CLValue is not an instance of List');
}
let innerType = this.clType as ListType;
let list = List.fromBytes(innerType, this.clValueBytes());
const innerType = this.clType as ListType;
const list = List.fromBytes(innerType, this.clValueBytes());
if (list.hasError()) {
throw new Error('The CLValue can not be parsed to list.');
}
Expand Down
75 changes: 66 additions & 9 deletions packages/sdk/test/lib/DeployUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Keys, DeployUtil, CLValue } from '../../src/lib';
import { TypedJSON } from 'typedjson';

describe('DeployUtil', () => {
it('should stringify/parse DeployHeader correctly', function () {
it('should stringify/parse DeployHeader correctly', function() {
const ed25519Key = Keys.Ed25519.new();
const deployHeader = new DeployUtil.DeployHeader(
ed25519Key.publicKey,
Expand All @@ -20,7 +20,7 @@ describe('DeployUtil', () => {
expect(deployHeader1).to.deep.equal(deployHeader);
});

it('should allow to extract data from Transfer', function () {
it('should allow to extract data from Transfer', function() {
const senderKey = Keys.Ed25519.new();
const recipientKey = Keys.Ed25519.new();
const networkName = 'test-network';
Expand Down Expand Up @@ -53,11 +53,17 @@ describe('DeployUtil', () => {
assert.isTrue(deploy.isStandardPayment());
assert.deepEqual(deploy.header.account, senderKey.publicKey);
assert.deepEqual(
deploy.payment.getArgByName('amount')!.asBigNumber().toNumber(),
deploy.payment
.getArgByName('amount')!
.asBigNumber()
.toNumber(),
paymentAmount
);
assert.deepEqual(
deploy.session.getArgByName('amount')!.asBigNumber().toNumber(),
deploy.session
.getArgByName('amount')!
.asBigNumber()
.toNumber(),
transferAmount
);
assert.deepEqual(
Expand All @@ -77,7 +83,7 @@ describe('DeployUtil', () => {
assert.deepEqual(deploy.approvals[1].signer, recipientKey.accountHex());
});

it('should allow to add arg to Deploy', function () {
it('should allow to add arg to Deploy', function() {
const senderKey = Keys.Ed25519.new();
const recipientKey = Keys.Ed25519.new();
const networkName = 'test-network';
Expand Down Expand Up @@ -111,18 +117,27 @@ describe('DeployUtil', () => {
deploy = DeployUtil.deployFromJson(json)!;

assert.deepEqual(
deploy.session.getArgByName('custom_id')!.asBigNumber().toNumber(),
deploy.session
.getArgByName('custom_id')!
.asBigNumber()
.toNumber(),
customId
);
assert.isTrue(deploy.isTransfer());
assert.isTrue(deploy.isStandardPayment());
assert.deepEqual(deploy.header.account, senderKey.publicKey);
assert.deepEqual(
deploy.payment.getArgByName('amount')!.asBigNumber().toNumber(),
deploy.payment
.getArgByName('amount')!
.asBigNumber()
.toNumber(),
paymentAmount
);
assert.deepEqual(
deploy.session.getArgByName('amount')!.asBigNumber().toNumber(),
deploy.session
.getArgByName('amount')!
.asBigNumber()
.toNumber(),
transferAmount
);
assert.deepEqual(
Expand All @@ -143,7 +158,7 @@ describe('DeployUtil', () => {
assert.notEqual(oldDeploy.header.bodyHash, deploy.header.bodyHash);
});

it('should not allow to add arg to a signed Deploy', function () {
it('should not allow to add arg to a signed Deploy', function() {
const senderKey = Keys.Ed25519.new();
const recipientKey = Keys.Ed25519.new();
const networkName = 'test-network';
Expand Down Expand Up @@ -171,4 +186,46 @@ describe('DeployUtil', () => {
DeployUtil.addArgToDeploy(deploy, 'custom_id', CLValue.u32(customId));
}).to.throw('Can not add argument to already signed deploy.');
});

it('should allow to extract additional args from Transfer.', function() {
const from = Keys.Ed25519.new();
const to = Keys.Ed25519.new();
const networkName = 'test-network';
const paymentAmount = 10000000000000;
const transferAmount = 10;
const id = 34;

let deployParams = new DeployUtil.DeployParams(from.publicKey, networkName);
let session = DeployUtil.ExecutableDeployItem.newTransfer(
transferAmount,
to.publicKey,
undefined,
id
);
let payment = DeployUtil.standardPayment(paymentAmount);
let deploy = DeployUtil.makeDeploy(deployParams, session, payment);
let fromRawPK = from.publicKey.rawPublicKey;

let transferDeploy = DeployUtil.addArgToDeploy(
deploy,
'fromPublicKey',
CLValue.publicKey(fromRawPK)
);

assert.deepEqual(
transferDeploy.session.getArgByName('fromPublicKey')?.asPublicKey()
.rawPublicKey,
fromRawPK
);

let newTransferDeploy = DeployUtil.deployFromJson(
DeployUtil.deployToJson(transferDeploy)
);

assert.deepEqual(
newTransferDeploy?.session.getArgByName('fromPublicKey')?.asPublicKey()
.rawPublicKey,
fromRawPK
);
});
});
3 changes: 0 additions & 3 deletions packages/sdk/test/lib/RuntimeArgs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ describe(`RuntimeArgs`, () => {
CLTypedAndToBytesHelper.bytes(account1)
])
});
let serializer = new TypedJSON(RuntimeArgs);
let json = serializer.stringify(runtimeArgs);

let accounts = runtimeArgs.args.get('accounts')!.asList();
assert.deepEqual(accounts[0].asBytesArray(), account0);
assert.deepEqual(accounts[1].asBytesArray(), account1);
Expand Down

0 comments on commit 37ff810

Please sign in to comment.