feat: add an option to read wallet balance at a given height

This commit is contained in:
ppe
2022-05-09 22:48:43 +02:00
committed by just_ppe
parent 53d644ba09
commit ecf780de25
7 changed files with 65 additions and 17 deletions

View File

@@ -1,9 +1,20 @@
export function handle(state, action) {
export async function handle(state, action) {
const balances = state.balances;
const canEvolve = state.canEvolve;
const input = action.input;
const caller = action.caller;
if (input.function === 'storeBalance') {
const target = input.target;
const height = SmartWeave.block.height;
if (state.wallets.height === undefined) {
state.wallets[height] = {};
}
state.wallets[height][target] = await SmartWeave.arweave.wallets.getBalance(target);
return { state };
}
if (input.function === 'transfer') {
const target = input.target;
const qty = input.qty;
@@ -34,7 +45,7 @@ export function handle(state, action) {
balances[target] = qty;
}
return { state };
return {state};
}
if (input.function === 'balance') {
@@ -49,7 +60,7 @@ export function handle(state, action) {
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) {
@@ -59,8 +70,9 @@ export function handle(state, action) {
state.evolve = input.value;
return { state };
return {state};
}
throw new ContractError(`No function supplied or function not recognised: "${input.function}"`);
}

View File

@@ -5,5 +5,6 @@
"balances": {
"uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M": 10000000,
"33F0QHcb22W7LwWR1iRC8Az1ntZG09XQ03YWuw2ABqA": 23111222
}
},
"wallets": {}
}