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

Commit

Permalink
Change return type of balance queries to BigNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
zie1ony committed Feb 4, 2021
1 parent 1bfd901 commit 66b9a44
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 32 deletions.
14 changes: 10 additions & 4 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

All notable changes to casper-client-sdk.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.17]

### Added

- Added `DeployUtils.addArgToDeploy(deploy: Deploy, name: string, value: CLValue)` to be able to modify Deploy's session arguments. It creates a new deploy instance. Can not be used on signed deploys.

### Changed
- Default `gasPrice` changed from `10` to `1`.

- Default `gasPrice` changed from `10` to `1`.
- Casper balances checks return `BigNumber` now.
## [1.0.15]

### Added

- Started using CHANGELOG.md.

### Changed
- Changed CLValue's `value` to `value()` and `remainder` to `remainder()`.

- Changed CLValue's `value` to `value()` and `remainder` to `remainder()`.
13 changes: 8 additions & 5 deletions packages/sdk/src/lib/CasperClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { encodeBase16 } from './Conversions';
import { Deploy, DeployParams, ExecutableDeployItem } from './DeployUtil';
import { AsymmetricKey, SignatureAlgorithm } from './Keys';
import { CasperHDKey } from './CasperHDKey';
import { BigNumber } from '@ethersproject/bignumber';

export class CasperClient {
private nodeClient: CasperServiceByJsonRPC;
Expand Down Expand Up @@ -190,37 +191,39 @@ export class CasperClient {
/**
* Get the balance of public key
*/
public async balanceOfByPublicKey(publicKey: PublicKey): Promise<number> {
public async balanceOfByPublicKey(publicKey: PublicKey): Promise<BigNumber> {
return this.balanceOfByAccountHash(encodeBase16(publicKey.toAccountHash()));
}

/**
* Get the balance by account hash
*/
public async balanceOfByAccountHash(accountHashStr: string): Promise<number> {
public async balanceOfByAccountHash(
accountHashStr: string
): Promise<BigNumber> {
try {
const stateRootHash = await this.nodeClient
.getLatestBlockInfo()
.then(it => it.block?.header.state_root_hash);
// Find the balance Uref and cache it if we don't have it.
if (!stateRootHash) {
return 0;
return BigNumber.from(0);
}
const balanceUref = await this.nodeClient.getAccountBalanceUrefByPublicKeyHash(
stateRootHash,
accountHashStr
);

if (!balanceUref) {
return 0;
return BigNumber.from(0);
}

return await this.nodeClient.getAccountBalance(
stateRootHash,
balanceUref
);
} catch (e) {
return 0;
return BigNumber.from(0);
}
}

Expand Down
21 changes: 12 additions & 9 deletions packages/sdk/src/lib/DeployUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,6 @@ export class ExecutableDeployItem implements ToBytes {
throw new Error('failed to serialize ExecutableDeployItemJsonWrapper');
}


public setArg(name: string, value: CLValue) {
if (this.isModuleBytes()) {
return this.moduleBytes!.setArg(name, value);
Expand Down Expand Up @@ -954,22 +953,26 @@ export const deployFromJson = (json: any) => {
return serializer.parse(json.deploy);
};

export const addArgToDeploy = (deploy: Deploy, name: string, value: CLValue): Deploy => {
if(deploy.approvals.length != 0) {
throw Error("Can not add argument to already signed deploy.");
export const addArgToDeploy = (
deploy: Deploy,
name: string,
value: CLValue
): Deploy => {
if (deploy.approvals.length !== 0) {
throw Error('Can not add argument to already signed deploy.');
}
let deployParams = new DeployUtil.DeployParams(

const deployParams = new DeployUtil.DeployParams(
deploy.header.account,
deploy.header.chainName,
deploy.header.gasPrice,
deploy.header.ttl,
deploy.header.dependencies,
deploy.header.timestamp
);
let session = deploy.session;

const session = deploy.session;
session.setArg(name, value);

return makeDeploy(deployParams, session, deploy.payment);
}
};
3 changes: 2 additions & 1 deletion packages/sdk/src/services/BalanceServiceByJsonRPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { CasperServiceByJsonRPC } from './CasperServiceByJsonRPC';
import { PublicKey } from '../lib';
import { BigNumber } from '@ethersproject/bignumber';

export class BalanceServiceByJsonRPC {
private balanceUrefs = new Map<string, string>();
Expand All @@ -20,7 +21,7 @@ export class BalanceServiceByJsonRPC {
public async getAccountBalance(
blockHashBase16: string,
publicKey: PublicKey
): Promise<number | undefined> {
): Promise<BigNumber | undefined> {
try {
const stateRootHash = await this.casperService.getStateRootHash(
blockHashBase16
Expand Down
5 changes: 3 additions & 2 deletions packages/sdk/src/services/CasperServiceByJsonRPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DeployUtil, encodeBase16, PublicKey } from '..';
import { deployToJson } from '../lib/DeployUtil';
import { TypedJSON } from 'typedjson';
import { StoredValue } from '../lib/StoredValue';
import { BigNumber } from '@ethersproject/bignumber';

interface RpcResult {
api_version: string;
Expand Down Expand Up @@ -236,7 +237,7 @@ export class CasperServiceByJsonRPC {
public async getAccountBalance(
stateRootHash: string,
balanceUref: string
): Promise<number> {
): Promise<BigNumber> {
return await this.client
.request({
method: 'state_get_balance',
Expand All @@ -245,7 +246,7 @@ export class CasperServiceByJsonRPC {
purse_uref: balanceUref
}
})
.then(res => parseInt(res.balance_value, 10));
.then(res => BigNumber.from(res.balance_value));
}

public async getStateRootHash(
Expand Down
18 changes: 9 additions & 9 deletions packages/sdk/test/lib/DeployUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ describe('DeployUtil', () => {

// Serialize deploy to JSON.
let json = DeployUtil.deployToJson(deploy);

// Deserialize deploy from JSON.
deploy = DeployUtil.deployFromJson(json)!;


assert.isTrue(deploy.isTransfer());
assert.isTrue(deploy.isStandardPayment());
Expand Down Expand Up @@ -101,7 +100,11 @@ describe('DeployUtil', () => {
let oldDeploy = DeployUtil.makeDeploy(deployParams, session, payment);

// Add new argument.
let deploy = DeployUtil.addArgToDeploy(oldDeploy, "custom_id", CLValue.u32(customId));
let deploy = DeployUtil.addArgToDeploy(
oldDeploy,
'custom_id',
CLValue.u32(customId)
);

// Serialize and deserialize deploy.
let json = DeployUtil.deployToJson(deploy);
Expand Down Expand Up @@ -162,13 +165,10 @@ describe('DeployUtil', () => {
let payment = DeployUtil.standardPayment(paymentAmount);
let deploy = DeployUtil.makeDeploy(deployParams, session, payment);
deploy = DeployUtil.signDeploy(deploy, senderKey);

expect(() => {
// Add new argument.
DeployUtil.addArgToDeploy(deploy, "custom_id", CLValue.u32(customId));
}).to.throw("Can not add argument to already signed deploy.");

DeployUtil.addArgToDeploy(deploy, 'custom_id', CLValue.u32(customId));
}).to.throw('Can not add argument to already signed deploy.');
});


});
2 changes: 1 addition & 1 deletion packages/ui/src/containers/AuthContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class AuthContainer {
this.balances.set(accountHash, {
checkedAt: now,
blockHashBase16: latestBlockHash,
balance: latestAccountBalance
balance: latestAccountBalance?.toNumber()
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/containers/BlockContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class BlockContainer {
PublicKey.fromEd25519(decodeBase16(accountKey.slice(2)))
);
if (balance !== undefined) {
this.balances.set(accountKey, balance);
this.balances.set(accountKey, balance.toNumber());
}
}
}
Expand Down

0 comments on commit 66b9a44

Please sign in to comment.