test: viewState testcased added
This commit is contained in:
committed by
Piotr Pędziwiatr
parent
b857f88595
commit
88520ef194
@@ -5,8 +5,6 @@ export function handle (state, action) {
|
||||
const input = action.input
|
||||
const caller = action.caller
|
||||
|
||||
console.log(action.input);
|
||||
|
||||
if (input.function === 'transfer') {
|
||||
const target = input.target
|
||||
const qty = input.qty
|
||||
@@ -45,11 +43,11 @@ export function handle (state, action) {
|
||||
const ticker = state.ticker
|
||||
|
||||
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') {
|
||||
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] } }
|
||||
|
||||
@@ -42,8 +42,6 @@ describe('Testing the SmartWeave client', () => {
|
||||
});
|
||||
|
||||
LoggerFactory.INST.logLevel('error');
|
||||
// LoggerFactory.INST.logLevel('debug', 'CacheableContractInteractionsLoader');
|
||||
// LoggerFactory.INST.logLevel('debug', 'DefaultStateEvaluator');
|
||||
|
||||
smartweave = SmartWeaveNodeFactory.memCached(arweave);
|
||||
|
||||
@@ -93,14 +91,10 @@ describe('Testing the SmartWeave client', () => {
|
||||
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 () => {
|
||||
const result = await contract.viewState<any, number>({ function: 'value' });
|
||||
expect(result).toEqual(559);
|
||||
});*/
|
||||
const interactionResult = await contract.viewState<any, number>({ function: 'value' });
|
||||
expect(interactionResult.result).toEqual(559);
|
||||
});
|
||||
});
|
||||
|
||||
async function mine() {
|
||||
|
||||
@@ -71,12 +71,10 @@ describe('Testing the Profit Sharing Token', () => {
|
||||
|
||||
it('should read pst state and balance data', async () => {
|
||||
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')).toEqual(10000000);
|
||||
// expect(await pst.currentBalance('33F0QHcb22W7LwWR1iRC8Az1ntZG09XQ03YWuw2ABqA')).toEqual(23111222);
|
||||
// expect(await pst.currentBalance(walletAddress)).toEqual(555669);
|
||||
|
||||
expect((await pst.currentBalance('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M')).balance).toEqual(10000000);
|
||||
expect((await pst.currentBalance('33F0QHcb22W7LwWR1iRC8Az1ntZG09XQ03YWuw2ABqA')).balance).toEqual(23111222);
|
||||
expect((await pst.currentBalance(walletAddress)).balance).toEqual(555669);
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
/*
|
||||
note: ArLocal currently doest not support the "block" endpoint, which
|
||||
is required by the interactRead/viewState methods
|
||||
|
||||
it('should properly view contract state', async () => {
|
||||
const result = await contract.viewState<any, number>({ function: 'value' });
|
||||
expect(result).toEqual(559);
|
||||
});*/
|
||||
const result = await pst.currentBalance('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M');
|
||||
expect(result.balance).toEqual(10000000 + 555);
|
||||
expect(result.ticker).toEqual('EXAMPLE_PST_TOKEN');
|
||||
expect(result.target).toEqual('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M');
|
||||
});
|
||||
});
|
||||
|
||||
async function mine() {
|
||||
|
||||
@@ -25,7 +25,7 @@ export interface TransferInput {
|
||||
* Profit Sharing Tokens.
|
||||
*/
|
||||
export interface PstContract extends Contract {
|
||||
currentBalance(target: string): Promise<InteractionResult<PstState, BalanceResult>>;
|
||||
currentBalance(target: string): Promise<BalanceResult>;
|
||||
|
||||
currentState(): Promise<PstState>;
|
||||
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
import { BalanceResult, HandlerBasedContract, PstContract, PstState, TransferInput } from '@smartweave/contract';
|
||||
import { InteractionResult } from '@smartweave/core';
|
||||
|
||||
interface BalanceInput {
|
||||
function: string;
|
||||
target: string;
|
||||
}
|
||||
|
||||
export class PstContractImpl extends HandlerBasedContract<PstState> implements PstContract {
|
||||
async currentBalance(target: string): Promise<InteractionResult<PstState, BalanceResult>> {
|
||||
return await super.viewState<BalanceInput, BalanceResult>({ target });
|
||||
async currentBalance(target: string): Promise<BalanceResult> {
|
||||
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> {
|
||||
|
||||
Reference in New Issue
Block a user