test: viewState testcased added

This commit is contained in:
ppedziwiatr
2021-09-06 19:34:52 +02:00
committed by Piotr Pędziwiatr
parent b857f88595
commit 88520ef194
5 changed files with 22 additions and 30 deletions

View File

@@ -5,8 +5,6 @@ export function handle (state, action) {
const input = action.input const input = action.input
const caller = action.caller const caller = action.caller
console.log(action.input);
if (input.function === 'transfer') { if (input.function === 'transfer') {
const target = input.target const target = input.target
const qty = input.qty const qty = input.qty
@@ -45,11 +43,11 @@ export function handle (state, action) {
const ticker = state.ticker const ticker = state.ticker
if (typeof target !== 'string') { if (typeof target !== 'string') {
throw new ContractError('Must specificy target to get balance for') throw new ContractError('Must specify target to get balance for')
} }
if (typeof balances[target] !== 'number') { if (typeof balances[target] !== 'number') {
throw new ContractError('Cannnot get balance, target does not exist') throw new ContractError('Cannot get balance, target does not exist')
} }
return { result: { target, ticker, balance: balances[target] } } return { result: { target, ticker, balance: balances[target] } }

View File

@@ -42,8 +42,6 @@ describe('Testing the SmartWeave client', () => {
}); });
LoggerFactory.INST.logLevel('error'); LoggerFactory.INST.logLevel('error');
// LoggerFactory.INST.logLevel('debug', 'CacheableContractInteractionsLoader');
// LoggerFactory.INST.logLevel('debug', 'DefaultStateEvaluator');
smartweave = SmartWeaveNodeFactory.memCached(arweave); smartweave = SmartWeaveNodeFactory.memCached(arweave);
@@ -93,14 +91,10 @@ describe('Testing the SmartWeave client', () => {
expect((await contract.readState()).state.counter).toEqual(559); expect((await contract.readState()).state.counter).toEqual(559);
}); });
/*
note: ArLocal currently doest not support the "block" endpoint, which
is required by the interactRead/viewState methods
it('should properly view contract state', async () => { it('should properly view contract state', async () => {
const result = await contract.viewState<any, number>({ function: 'value' }); const interactionResult = await contract.viewState<any, number>({ function: 'value' });
expect(result).toEqual(559); expect(interactionResult.result).toEqual(559);
});*/ });
}); });
async function mine() { async function mine() {

View File

@@ -71,12 +71,10 @@ describe('Testing the Profit Sharing Token', () => {
it('should read pst state and balance data', async () => { it('should read pst state and balance data', async () => {
expect(await pst.currentState()).toEqual(initialState); expect(await pst.currentState()).toEqual(initialState);
/**
* ArLocal currently does not support 'block' endpoint, which is required by interactRead / viewState expect((await pst.currentBalance('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M')).balance).toEqual(10000000);
*/ expect((await pst.currentBalance('33F0QHcb22W7LwWR1iRC8Az1ntZG09XQ03YWuw2ABqA')).balance).toEqual(23111222);
// expect(await pst.currentBalance('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M')).toEqual(10000000); expect((await pst.currentBalance(walletAddress)).balance).toEqual(555669);
// expect(await pst.currentBalance('33F0QHcb22W7LwWR1iRC8Az1ntZG09XQ03YWuw2ABqA')).toEqual(23111222);
// expect(await pst.currentBalance(walletAddress)).toEqual(555669);
}); });
it('should properly transfer tokens', async () => { it('should properly transfer tokens', async () => {
@@ -91,14 +89,12 @@ describe('Testing the Profit Sharing Token', () => {
expect((await pst.currentState()).balances['uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M']).toEqual(10000000 + 555); expect((await pst.currentState()).balances['uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M']).toEqual(10000000 + 555);
}); });
/*
note: ArLocal currently doest not support the "block" endpoint, which
is required by the interactRead/viewState methods
it('should properly view contract state', async () => { it('should properly view contract state', async () => {
const result = await contract.viewState<any, number>({ function: 'value' }); const result = await pst.currentBalance('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M');
expect(result).toEqual(559); expect(result.balance).toEqual(10000000 + 555);
});*/ expect(result.ticker).toEqual('EXAMPLE_PST_TOKEN');
expect(result.target).toEqual('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M');
});
}); });
async function mine() { async function mine() {

View File

@@ -25,7 +25,7 @@ export interface TransferInput {
* Profit Sharing Tokens. * Profit Sharing Tokens.
*/ */
export interface PstContract extends Contract { export interface PstContract extends Contract {
currentBalance(target: string): Promise<InteractionResult<PstState, BalanceResult>>; currentBalance(target: string): Promise<BalanceResult>;
currentState(): Promise<PstState>; currentState(): Promise<PstState>;

View File

@@ -1,13 +1,17 @@
import { BalanceResult, HandlerBasedContract, PstContract, PstState, TransferInput } from '@smartweave/contract'; import { BalanceResult, HandlerBasedContract, PstContract, PstState, TransferInput } from '@smartweave/contract';
import { InteractionResult } from '@smartweave/core';
interface BalanceInput { interface BalanceInput {
function: string;
target: string; target: string;
} }
export class PstContractImpl extends HandlerBasedContract<PstState> implements PstContract { export class PstContractImpl extends HandlerBasedContract<PstState> implements PstContract {
async currentBalance(target: string): Promise<InteractionResult<PstState, BalanceResult>> { async currentBalance(target: string): Promise<BalanceResult> {
return await super.viewState<BalanceInput, BalanceResult>({ target }); const interactionResult = await super.viewState<BalanceInput, BalanceResult>({ function: 'balance', target });
if (interactionResult.type !== 'ok') {
throw Error(interactionResult.errorMessage);
}
return interactionResult.result;
} }
async currentState(): Promise<PstState> { async currentState(): Promise<PstState> {