fix: viewState interaction transaction does not take 'evolve' into account #14

This commit is contained in:
ppedziwiatr
2021-09-08 18:03:47 +02:00
committed by Piotr Pędziwiatr
parent 717c325a36
commit ce5a589b10
13 changed files with 206 additions and 54 deletions

View File

@@ -1,67 +1,66 @@
export function handle (state, action) {
const balances = state.balances
const canEvolve = state.canEvolve
const input = action.input
const caller = action.caller
export function handle(state, action) {
const balances = state.balances;
const canEvolve = state.canEvolve;
const input = action.input;
const caller = action.caller;
if (input.function === 'transfer') {
const target = input.target
const qty = input.qty
const target = input.target;
const qty = input.qty;
if (!Number.isInteger(qty)) {
throw new ContractError('Invalid value for "qty". Must be an integer')
throw new ContractError('Invalid value for "qty". Must be an integer');
}
if (!target) {
throw new ContractError('No target specified')
throw new ContractError('No target specified');
}
if (qty <= 0 || caller === target) {
throw new ContractError('Invalid token transfer')
throw new ContractError('Invalid token transfer');
}
if (balances[caller] < qty) {
throw new ContractError(`Caller balance not high enough to send ${qty} token(s)!`)
throw new ContractError(`Caller balance not high enough to send ${qty} token(s)!`);
}
// Lower the token balance of the caller
balances[caller] -= qty
balances[caller] -= qty;
if (target in balances) {
// Wallet already exists in state, add new tokens
balances[target] += qty
balances[target] += qty;
} else {
// Wallet is new, set starting balance
balances[target] = qty
balances[target] = qty;
}
return { state }
return { state };
}
if (input.function === 'balance') {
const target = input.target
const ticker = state.ticker
const target = input.target;
const ticker = state.ticker;
if (typeof target !== 'string') {
throw new ContractError('Must specify target to get balance for')
throw new ContractError('Must specify target to get balance for');
}
if (typeof balances[target] !== 'number') {
throw new ContractError('Cannot 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] } };
}
if(input.function === 'evolve' && canEvolve) {
if(state.owner !== caller) {
if (input.function === 'evolve' && canEvolve) {
if (state.owner !== caller) {
throw new ContractError('Only the owner can evolve a contract.');
}
state.evolve = input.value
state.evolve = input.value;
return { state }
return { state };
}
throw new ContractError(`No function supplied or function not recognised: "${input.function}"`)
throw new ContractError(`No function supplied or function not recognised: "${input.function}"`);
}