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

@@ -233,7 +233,7 @@ export class HandlerBasedContract<State> implements Contract<State> {
}
const interactionTx = await this.createInteraction(input, tags, emptyTransfer, strict);
const response = await fetch(`${this._evaluationOptions.bundlerAddress}gateway/sequencer/register`, {
const response = await fetch(`${this._evaluationOptions.bundlerUrl}gateway/sequencer/register`, {
method: 'POST',
body: JSON.stringify(interactionTx),
headers: {

View File

@@ -104,7 +104,7 @@ export class DefaultEvaluationOptions implements EvaluationOptions {
saveState: false
};
bundlerAddress = 'https://gateway.redstone.finance/';
bundlerUrl = 'https://gateway.redstone.finance/';
gasLimit = Number.MAX_SAFE_INTEGER;
@@ -115,6 +115,8 @@ export class DefaultEvaluationOptions implements EvaluationOptions {
useVM2 = false;
allowUnsafeClient = false;
walletBalanceUrl = 'http://nyc-1.dev.arweave.net:1984/';
}
// an interface for the contract EvaluationOptions - can be used to change the behaviour of some of the features.
@@ -159,7 +161,7 @@ export interface EvaluationOptions {
saveState: boolean;
};
bundlerAddress: string;
bundlerUrl: string;
gasLimit: number;
@@ -181,4 +183,7 @@ export interface EvaluationOptions {
// if set to false - calling unsafe clinet in contract code will
// result in throwing an exception
allowUnsafeClient: boolean;
// an endpoint for retrieving wallet balance info
walletBalanceUrl: string;
}

View File

@@ -46,10 +46,14 @@ export class HandlerExecutorFactory implements ExecutorFactory<HandlerApi<unknow
contractDefinition: ContractDefinition<State>,
evaluationOptions: EvaluationOptions
): Promise<HandlerApi<State>> {
const swGlobal = new SmartWeaveGlobal(this.arweave, {
id: contractDefinition.txId,
owner: contractDefinition.owner
});
const swGlobal = new SmartWeaveGlobal(
this.arweave,
{
id: contractDefinition.txId,
owner: contractDefinition.owner
},
evaluationOptions
);
if (contractDefinition.contractType == 'wasm') {
this.logger.info('Creating handler for wasm contract', contractDefinition.txId);

View File

@@ -1,6 +1,7 @@
/* eslint-disable */
import Arweave from 'arweave';
import { GQLNodeInterface, GQLTagInterface } from './gqlResult';
import {EvaluationOptions} from "@smartweave/core";
/**
*
@@ -51,9 +52,9 @@ export class SmartWeaveGlobal {
caller?: string;
constructor(arweave: Arweave, contract: { id: string; owner: string }, gasLimit = Number.MAX_SAFE_INTEGER) {
constructor(arweave: Arweave, contract: { id: string; owner: string }, evaluationOptions: EvaluationOptions) {
this.gasUsed = 0;
this.gasLimit = gasLimit;
this.gasLimit = Number.MAX_SAFE_INTEGER;
this.unsafeClient = arweave;
this.arweave = {
ar: arweave.ar,
@@ -61,6 +62,24 @@ export class SmartWeaveGlobal {
wallets: arweave.wallets,
crypto: arweave.crypto
};
this.arweave.wallets.getBalance = async (address: string): Promise<string> => {
if (!this._activeTx) {
throw new Error("Cannot read balance - active tx is not set.");
}
if (!this.block.height) {
throw new Error("Cannot read balance - block height not set.")
}
console.log(`${evaluationOptions.walletBalanceUrl}block/height/${this.block.height}/wallet/${address}/balance`);
// http://nyc-1.dev.arweave.net:1984/block/height/914387/wallet/M-mpNeJbg9h7mZ-uHaNsa5jwFFRAq0PsTkNWXJ-ojwI/balance
return await fetch(`${evaluationOptions.walletBalanceUrl}block/height/${this.block.height}/wallet/${address}/balance`)
.then((res) => {
return res.ok ? res.text() : Promise.reject(res);
})
.catch((error) => {
throw new Error(`Unable to read wallet balance. ${error.status}. ${error.body?.message}`);
});
}
this.contract = contract;
this.transaction = new Transaction(this);
this.block = new Block(this);

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": {}
}

View File

@@ -28,11 +28,11 @@ async function main() {
const initialState = fs.readFileSync(path.join(__dirname, 'data/js/token-pst.json'), 'utf8');
// case 1 - full deploy, js contract
const contractTxId = await smartweave.createContract.deploy({
/* const contractTxId = await smartweave.createContract.deploy({
wallet,
initState: initialState,
src: jsContractSrc,
}, true);
}, true);*/
// case 2 - deploy from source, js contract
/*const contractTxId = await smartweave.createContract.deployFromSourceTx({
@@ -57,7 +57,14 @@ async function main() {
srcTxId: "5wXT-A0iugP9pWEyw-iTbB0plZ_AbmvlNKyBfGS3AUY",
}, true);*/
const {state, validity} = await smartweave.contract(contractTxId).readState();
const contract = smartweave.contract("CRMRwP-poSTYI7f9fI-vF0CKaTTJTIl74KQYbnJ4hnE").connect(wallet);
const result = await contract.writeInteraction<any>({
function: "storeBalance",
target: "M-mpNeJbg9h7mZ-uHaNsa5jwFFRAq0PsTkNWXJ-ojwI",
});
//const {state, validity} = await contract.readState();
//logger.info("Result", state);