Skip to content

Commit

Permalink
info_get_deploy working
Browse files Browse the repository at this point in the history
  • Loading branch information
ryo-casper committed Apr 18, 2024
1 parent 44d29f7 commit 9f2cdc6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 116 deletions.
34 changes: 10 additions & 24 deletions e2e/services/CasperServiceByJsonRPC.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,17 @@ describe('CasperServiceByJsonRPC', () => {
expect(balance.eq(faucetBalance)).to.be;
});

it.only('query_balance', async () => {
const faucetBalance = '1000000000000000000000000000000000';

it('query_balance', async () => {
const balanceByPublicKey = await client.queryBalance(
PurseIdentifier.MainPurseUnderPublicKey,
faucetKey.publicKey.toHex(false)
);
expect(balanceByPublicKey.eq(faucetBalance)).to.be;

const balanceByAccountHash = await client.queryBalance(
PurseIdentifier.MainPurseUnderAccountHash,
faucetKey.publicKey.toAccountHashStr()
);
expect(balanceByAccountHash.eq(faucetBalance)).to.be;
expect(balanceByAccountHash.eq(balanceByPublicKey)).to.be;

const entity = await client.getEntity({
PublicKey: faucetKey.publicKey.toHex(false)
Expand All @@ -248,15 +245,15 @@ describe('CasperServiceByJsonRPC', () => {
PurseIdentifier.PurseUref,
entity.AddressableEntity.main_purse
);
expect(balanceByUref.eq(faucetBalance)).to.be;
expect(balanceByUref.eq(balanceByPublicKey)).to.be;
});

it('should transfer native token by session', async () => {
// for native-transfers payment price is fixed
const paymentAmount = 10000000000;
const id = Date.now();

const amount = '25000000000';
const amount = '5000000000';

const deployParams = new DeployUtil.DeployParams(
faucetKey.publicKey,
Expand Down Expand Up @@ -284,25 +281,14 @@ describe('CasperServiceByJsonRPC', () => {

expect(deploy_hash).to.be.equal(result.deploy.hash);
expect(result.deploy.session).to.have.property('Transfer');
expect(result.execution_results[0].result).to.have.property('Success');

transferBlockHash = result.execution_results[0].block_hash;
transferBlockHash = result.block_hash;

let balance = BigNumber.from(0);
const balance = await client.queryBalance(
PurseIdentifier.MainPurseUnderPublicKey,
toPublicKey.toHex(false)
);

if (isAfterDot5) {
balance = await client.queryBalance(
PurseIdentifier.MainPurseUnderPublicKey,
toPublicKey.toHex(false)
);
} else {
const stateRootHash = await client.getStateRootHash();
const uref = await client.getAccountBalanceUrefByPublicKey(
stateRootHash,
toPublicKey
);
balance = await client.getAccountBalance(stateRootHash, uref);
}
expect(amount).to.be.equal(balance.toString());
});

Expand Down Expand Up @@ -403,7 +389,7 @@ describe('CasperServiceByJsonRPC', () => {

assert.equal(result.deploy.hash, deploy_hash);
expect(result.deploy.session).to.have.property('StoredContractByHash');
expect(result.execution_results[0].result).to.have.property('Success');
// expect(result.execution_results[0].result).to.have.property('Success');

const balanceOfRecipient = await balanceOf(erc20, recipient);
assert.equal(balanceOfRecipient.toNumber(), transferAmount);
Expand Down
58 changes: 0 additions & 58 deletions src/services/BalanceServiceByJsonRPC.ts

This file was deleted.

46 changes: 20 additions & 26 deletions src/services/CasperServiceByJsonRPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ import { BigNumber } from '@ethersproject/bignumber';
import { RequestManager, HTTPTransport, Client } from '@open-rpc/client-js';
import { TypedJSON, jsonMember, jsonObject } from 'typedjson';

import {
DeployUtil,
encodeBase16,
CLPublicKey,
StoredValue,
Transfers
} from '..';
import { DeployUtil, encodeBase16, StoredValue, Transfers } from '..';

import ProviderTransport, {
SafeEventEmitterProvider
Expand Down Expand Up @@ -395,24 +389,6 @@ export class CasperServiceByJsonRPC {
return entity;
}

/**
* Get the reference to an account balance uref by an account's public key, so it may be cached
* @param _stateRootHash The state root hash at which the main purse URef will be queried
* @param _publicKey The public key of the account
* @param _props optional request props
* @returns The account's main purse URef
* @see [getAccountBalanceUrefByPublicKeyHash](#L380)
*/
public async getAccountBalanceUrefByPublicKey(
_stateRootHash: string,
_publicKey: CLPublicKey,
_props?: RpcRequestProps
): Promise<string> {
throw new Error(
'This method has been removed, please use getAccount or getEntity instead.'
);
}

/**
* Get the balance of an account using its main purse URef
* @param stateRootHash The state root hash at which the account balance will be queried
Expand Down Expand Up @@ -586,6 +562,9 @@ export class CasperServiceByJsonRPC {
checkApproval?: boolean;
}
): Promise<DeployResult> {
console.warn(
'This method has been deprecated, please use sendTransaction instead'
);
this.checkDeploySize(signedDeploy);

const { checkApproval = false } = props ?? {};
Expand Down Expand Up @@ -622,7 +601,22 @@ export class CasperServiceByJsonRPC {
const deployHash =
typeof deploy === 'string' ? deploy : encodeBase16(deploy.hash);
const deployInfo = await this.getDeployInfo(deployHash);
if (deployInfo.execution_results.length > 0) {

let successful = false;

if (!deployInfo.execution_result) {
successful = false;
} else {
if ('Version1' in deployInfo.execution_result) {
successful = !!deployInfo.execution_result.Version1.Success;
}
if ('Version2' in deployInfo.execution_result) {
successful =
deployInfo.execution_result.Version2.error_message === null;
}
}

if (successful) {
clearTimeout(timer);
return deployInfo;
} else {
Expand Down
1 change: 0 additions & 1 deletion src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './CasperServiceByJsonRPC';
export * from './BalanceServiceByJsonRPC';
export * from './EventStream';
export * from './types';
31 changes: 24 additions & 7 deletions src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,22 +180,39 @@ interface ExecutionResultBody {
effect: Effect;
}

/** Result interface for an execution result */
export interface ExecutionResult {
Success?: ExecutionResultBody;
Failure?: ExecutionResultBody;
}

/** Result interface for a JSON execution result */
export interface JsonExecutionResult {
block_hash: JsonBlockHash;
result: ExecutionResult;
}

export interface ExecutionResultV1 {
Success?: ExecutionResultBody;
Failure?: ExecutionResultBody;
}

export interface ExecutionResultV2 {
initiator: any;
/** If error_message is null, the execution was successful */
error_message: string | null;
limit: string;
consumed: string;
cost: string;
payment: { source: string }[];
transfers: any[];
effect: Effect;
}

export type ExecutionResult =
| { Version1: ExecutionResultV1 }
| { Version2: ExecutionResultV2 };

/** Result interface for a get-deploy call */
export interface GetDeployResult extends RpcResult {
deploy: JsonDeploy;
execution_results: JsonExecutionResult[];
block_hash: string;
block_height: number;
execution_result: ExecutionResult | undefined;
}

export interface BlockIdentifier {
Expand Down

0 comments on commit 9f2cdc6

Please sign in to comment.