Skip to content

Commit

Permalink
feat: removed Jakarta conditions in on chain view related files (#2138)
Browse files Browse the repository at this point in the history
  • Loading branch information
hui-an-yang authored Nov 28, 2022
1 parent fd18fcb commit c93f36f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 573 deletions.
9 changes: 2 additions & 7 deletions integration-tests/contract-execute-on-chain-view.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { codeViewsTopLevel } from "./data/contract_views_top_level";
import { CONFIGS } from "./config";
import BigNumber from 'bignumber.js';
import { Protocols, ViewSimulationError } from "@taquito/taquito";
import { HttpResponseError } from "@taquito/http-utils";
import { ViewSimulationError } from "@taquito/taquito";

CONFIGS().forEach(({ lib, rpc, setup }) => {
const Tezos = lib;
Expand Down Expand Up @@ -75,11 +74,7 @@ CONFIGS().forEach(({ lib, rpc, setup }) => {
await contract.contractViews.test_failwith(3).executeView({ viewCaller: contract.address });
} catch (error: any) {
const protocol = (await Tezos.rpc.getProtocols()).protocol
if(protocol === Protocols.PtJakart2) {
expect(error).toBeInstanceOf(HttpResponseError)
} else {
expect(error).toBeInstanceOf(ViewSimulationError)
}
expect(error).toBeInstanceOf(ViewSimulationError)
}

const viewSuccResult = await contract.contractViews.succ({ 0: 16, 1: contract.address }).executeView({ source, viewCaller: contract.address });
Expand Down
6 changes: 3 additions & 3 deletions integration-tests/rpc-nodes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CONFIGS } from './config';
import { Protocols, ChainIds } from "@taquito/taquito";
import { Protocols } from "@taquito/taquito";
import { RpcClientCache, RpcClient, RPCRunViewParam, RPCRunScriptViewParam } from '@taquito/rpc';
import { encodeExpr } from '@taquito/utils';
import { Schema } from '@taquito/michelson-encoder';
Expand Down Expand Up @@ -126,7 +126,7 @@ CONFIGS().forEach(
done();
});

kathmandunetAndAlpha(`Fetches voting information about a delegate from RPC`, async (done) => {
it(`Fetches voting information about a delegate from RPC`, async (done) => {
const votinInfo = await rpcClient.getVotingInfo(knownBaker);
expect(votinInfo).toBeDefined();
done();
Expand Down Expand Up @@ -365,7 +365,7 @@ CONFIGS().forEach(
done();
});

kathmandunetAndAlpha('Verify that rpcClient.runScriptView executes michelson view', async (done) => {
it('Verify that rpcClient.runScriptView executes michelson view', async (done) => {
const chainId = await Tezos.rpc.getChainId();
const params: RPCRunScriptViewParam = {
contract: knownViewContract!,
Expand Down
161 changes: 31 additions & 130 deletions packages/taquito/src/contract/contract-methods/contract-on-chain-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,6 @@ import {
ViewSimulationError,
validateAndExtractFailwith,
} from '../errors';
import { Protocols } from '../../constants';

const runCodeHelper = (
viewArgsType: MichelsonV1ExpressionExtended,
viewReturnType: MichelsonV1ExpressionExtended,
contractStorageType: MichelsonV1Expression,
viewInstructions: MichelsonV1ExpressionExtended[],
viewArgs: MichelsonV1Expression,
contractStorageValue: MichelsonV1Expression,
balance: string,
chain_id: string,
source?: string,
amount = '0'
): RPCRunCodeParam => {
return {
script: [
{ prim: 'parameter', args: [{ prim: 'pair', args: [viewArgsType, contractStorageType] }] },
{ prim: 'storage', args: [{ prim: 'option', args: [viewReturnType] }] },
{
prim: 'code',
args: [
[
{ prim: 'CAR' },
viewInstructions,
{ prim: 'SOME' },
{ prim: 'NIL', args: [{ prim: 'operation' }] },
{ prim: 'PAIR' },
],
],
},
],
storage: { prim: 'None' },
input: { prim: 'Pair', args: [viewArgs, contractStorageValue] },
amount,
balance,
chain_id,
source,
};
};

export interface ExecutionContextParams {
source?: string;
Expand Down Expand Up @@ -85,49 +46,20 @@ export class OnChainView {
* @param executionContext.viewCaller the contract address which is the caller of view.
*/
async executeView(executionContext: ExecutionContextParams) {
const protocol = (await this._rpc.getProtocols()).protocol;
// TODO: remove if/else when support for Jakartanet is removed
if (protocol === Protocols.PtJakart2) {
this.verifyContextExecution(executionContext);
const balance = (
await this._readProvider.getBalance(this._contractAddress, 'head')
).toString();
const chainId = await this._readProvider.getChainId();
const storage = await this._readProvider.getStorage(this._contractAddress, 'head');
return this.executeViewAndDecodeResult(
runCodeHelper(
this._smartContractViewSchema.viewArgsType,
this._smartContractViewSchema.viewReturnType,
this._contractStorageType,
this.adaptViewCodeToContext(
this._smartContractViewSchema.instructions,
executionContext.viewCaller,
balance
),
this.transformArgsToMichelson(),
storage,
balance,
chainId,
executionContext.source
),
protocol
);
} else {
this.verifyContextExecution(executionContext);
const chainId = await this._readProvider.getChainId();
const viewArgs = this.transformArgsToMichelson();
const scriptView: RPCRunScriptViewParam = {
contract: this._contractAddress,
view: this._smartContractViewSchema.viewName,
input: viewArgs,
chain_id: chainId,
source: executionContext.viewCaller,
};
if (executionContext.source) {
scriptView.payer = executionContext.source;
}
return this.executeViewAndDecodeResult(scriptView, protocol);
this.verifyContextExecution(executionContext);
const chainId = await this._readProvider.getChainId();
const viewArgs = this.transformArgsToMichelson();
const scriptView: RPCRunScriptViewParam = {
contract: this._contractAddress,
view: this._smartContractViewSchema.viewName,
input: viewArgs,
chain_id: chainId,
source: executionContext.viewCaller,
};
if (executionContext.source) {
scriptView.payer = executionContext.source;
}
return this.executeViewAndDecodeResult(scriptView);
}

private verifyContextExecution(executionContext: ExecutionContextParams) {
Expand Down Expand Up @@ -198,55 +130,24 @@ export class OnChainView {
return instructions;
}

private async executeViewAndDecodeResult(
viewScript: RPCRunScriptViewParam | RPCRunCodeParam,
protocol: string
) {
// TODO: remove if/else when support for Jakartanet is removed
if (protocol === Protocols.PtJakart2) {
let storage: MichelsonV1ExpressionExtended;
try {
storage = (await this._rpc.runCode(viewScript as RPCRunCodeParam))
.storage as MichelsonV1ExpressionExtended;
} catch (error: any) {
const failWith = validateAndExtractFailwith(error);
throw failWith
? new ViewSimulationError(
`The simulation of the on-chain view named ${
this._smartContractViewSchema.viewName
} failed with: ${JSON.stringify(failWith)}`,
this._smartContractViewSchema.viewName,
failWith,
error
)
: error;
}
if (!storage.args) {
throw new ViewSimulationError(
`View simulation failed with an invalid result: ${storage}`,
this._smartContractViewSchema.viewName
);
}
return this._smartContractViewSchema.decodeViewResult(storage.args[0]);
} else {
let storage: MichelsonV1ExpressionExtended;
try {
storage = (await this._rpc.runScriptView(viewScript as RPCRunScriptViewParam))
.data as MichelsonV1ExpressionExtended;
} catch (error: any) {
const failWith = validateAndExtractFailwith(error);
throw failWith
? new ViewSimulationError(
`The simulation of the on-chain view named ${
this._smartContractViewSchema.viewName
} failed with: ${JSON.stringify(failWith)}`,
this._smartContractViewSchema.viewName,
failWith,
error
)
: error;
}
return this._smartContractViewSchema.decodeViewResult(storage);
private async executeViewAndDecodeResult(viewScript: RPCRunScriptViewParam | RPCRunCodeParam) {
let storage: MichelsonV1ExpressionExtended;
try {
storage = (await this._rpc.runScriptView(viewScript as RPCRunScriptViewParam))
.data as MichelsonV1ExpressionExtended;
} catch (error: any) {
const failWith = validateAndExtractFailwith(error);
throw failWith
? new ViewSimulationError(
`The simulation of the on-chain view named ${
this._smartContractViewSchema.viewName
} failed with: ${JSON.stringify(failWith)}`,
this._smartContractViewSchema.viewName,
failWith,
error
)
: error;
}
return this._smartContractViewSchema.decodeViewResult(storage);
}
}
Loading

0 comments on commit c93f36f

Please sign in to comment.