feat: extensions for SmartWeave global
This commit is contained in:
@@ -92,11 +92,11 @@
|
||||
"eslint-plugin-prettier": "^3.4.1",
|
||||
"gen-esm-wrapper": "^1.1.3",
|
||||
"jest": "^28.1.3",
|
||||
"node-nlp": "^4.24.0",
|
||||
"prettier": "^2.3.2",
|
||||
"rimraf": "^3.0.2",
|
||||
"simple-statistics": "^7.7.0",
|
||||
"smartweave": "0.4.48",
|
||||
"sqlite3": "^5.0.2",
|
||||
"ts-jest": "^28.0.7",
|
||||
"ts-node": "^10.2.1",
|
||||
"typescript": "^4.7.4",
|
||||
@@ -110,7 +110,6 @@
|
||||
"archiver": false,
|
||||
"stream-buffers": false,
|
||||
"constants": false,
|
||||
"knex": false,
|
||||
"os": false,
|
||||
"process": false
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export const knownWarpPlugins = ['evm-signature-verification'] as const;
|
||||
export const knownWarpPlugins = ['evm-signature-verification', 'smartweave-extension'] as const;
|
||||
export type WarpPluginType = typeof knownWarpPlugins[number];
|
||||
|
||||
export interface WarpPlugin<T, R> {
|
||||
|
||||
@@ -36,6 +36,12 @@ export class JsHandlerApi<State> extends AbstractContractHandler<State> {
|
||||
this.assignWrite(executionContext, currentTx);
|
||||
this.assignRefreshState(executionContext);
|
||||
|
||||
const { warp } = executionContext;
|
||||
if (warp.hasPlugin('smartweave-extension')) {
|
||||
const extension = warp.loadPlugin<any, void>('smartweave-extension');
|
||||
extension.process(this.swGlobal.extensions);
|
||||
}
|
||||
|
||||
const handlerResult = await Promise.race([timeoutPromise, this.contractFunction(stateCopy, interaction)]);
|
||||
|
||||
if (handlerResult && (handlerResult.state !== undefined || handlerResult.result !== undefined)) {
|
||||
|
||||
@@ -50,6 +50,8 @@ export class SmartWeaveGlobal {
|
||||
refreshState: () => Promise<any>;
|
||||
};
|
||||
|
||||
extensions: any;
|
||||
|
||||
_activeTx?: GQLNodeInterface;
|
||||
|
||||
caller?: string;
|
||||
@@ -91,6 +93,8 @@ export class SmartWeaveGlobal {
|
||||
|
||||
this.useGas = this.useGas.bind(this);
|
||||
this.getBalance = this.getBalance.bind(this);
|
||||
|
||||
this.extensions = {};
|
||||
}
|
||||
|
||||
useGas(gas: number) {
|
||||
|
||||
@@ -4,6 +4,36 @@ export async function handle(state, action) {
|
||||
const input = action.input;
|
||||
const caller = action.caller;
|
||||
|
||||
if (input.function === 'train') {
|
||||
const manager = new SmartWeave.extensions.NlpManager({languages: ['en'], forceNER: true});
|
||||
manager.addDocument('en', 'goodbye for now', 'greetings.bye');
|
||||
manager.addDocument('en', 'bye bye take care', 'greetings.bye');
|
||||
manager.addDocument('en', 'okay see you later', 'greetings.bye');
|
||||
manager.addDocument('en', 'bye for now', 'greetings.bye');
|
||||
manager.addDocument('en', 'i must go', 'greetings.bye');
|
||||
manager.addDocument('en', 'hello', 'greetings.hello');
|
||||
manager.addDocument('en', 'hi', 'greetings.hello');
|
||||
manager.addDocument('en', 'howdy', 'greetings.hello');
|
||||
|
||||
manager.addAnswer('en', 'greetings.bye', 'Till next time');
|
||||
manager.addAnswer('en', 'greetings.bye', 'see you soon!');
|
||||
manager.addAnswer('en', 'greetings.hello', 'Hey there!');
|
||||
manager.addAnswer('en', 'greetings.hello', 'Greetings!');
|
||||
|
||||
await manager.train();
|
||||
manager.save();
|
||||
const response = await manager.process('en', 'I should go now');
|
||||
state.nlp = response;
|
||||
return {
|
||||
state
|
||||
};
|
||||
}
|
||||
|
||||
if (input.function === 'require') {
|
||||
const fs = require('fs');
|
||||
console.log(fs);
|
||||
}
|
||||
|
||||
if (input.function === 'storeBalance') {
|
||||
const target = input.target;
|
||||
const height = SmartWeave.block.height;
|
||||
|
||||
@@ -4,7 +4,9 @@ import {defaultCacheOptions, defaultWarpGwOptions, LoggerFactory, WarpFactory} f
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import {JWKInterface} from 'arweave/node/lib/wallet';
|
||||
import {LmdbCache} from "warp-contracts-lmdb";
|
||||
import {WarpPlugin, WarpPluginType} from "../src/core/WarpPlugin";
|
||||
|
||||
const { NlpManager } = require('node-nlp');
|
||||
|
||||
async function main() {
|
||||
let wallet: JWKInterface = readJSON('./.secrets/33F0QHcb22W7LwWR1iRC8Az1ntZG09XQ03YWuw2ABqA.json');;
|
||||
@@ -17,18 +19,21 @@ async function main() {
|
||||
protocol: 'https'
|
||||
});
|
||||
|
||||
class NlpExtension implements WarpPlugin<any, void> {
|
||||
process(input: any): void {
|
||||
input.NlpManager = NlpManager;
|
||||
}
|
||||
|
||||
type(): WarpPluginType {
|
||||
return 'smartweave-extension';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
try {
|
||||
const warp = WarpFactory
|
||||
.forMainnet({...defaultCacheOptions, inMemory: false})
|
||||
.useStateCache(new LmdbCache({
|
||||
...defaultCacheOptions,
|
||||
dbLocation: `./cache/warp/state`
|
||||
}
|
||||
))
|
||||
.useContractCache(new LmdbCache({
|
||||
...defaultCacheOptions,
|
||||
dbLocation: `./cache/warp/contracts`
|
||||
}));
|
||||
.forMainnet({...defaultCacheOptions, inMemory: true})
|
||||
.use(new NlpExtension());
|
||||
/*const warp = WarpFactory
|
||||
.custom(arweave, {
|
||||
...defaultCacheOptions,
|
||||
@@ -47,11 +52,12 @@ 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 warp.createContract.deploy({
|
||||
/*const {contractTxId} = await warp.createContract.deploy({
|
||||
wallet,
|
||||
initState: initialState,
|
||||
src: jsContractSrc,
|
||||
});
|
||||
});*/
|
||||
|
||||
// case 2 - deploy from source, js contract
|
||||
/*const {contractTxId} = await warp.createContract.deployFromSourceTx({
|
||||
wallet,
|
||||
@@ -75,34 +81,31 @@ async function main() {
|
||||
srcTxId: "5wXT-A0iugP9pWEyw-iTbB0plZ_AbmvlNKyBfGS3AUY",
|
||||
});*/
|
||||
|
||||
const contract = warp.contract('hYZBzN5FsC7P90cmfNZ0eokOUyiIvPve6JUd-9EdPAQ')
|
||||
const contract = warp.contract<any>('QZfrcazIy1xhWhdztArMDSivrM23B0F4tAEFf6XJzt4')
|
||||
.setEvaluationOptions({
|
||||
})
|
||||
.connect(wallet);
|
||||
|
||||
/*await contract.writeInteraction({
|
||||
function: 'transfer',
|
||||
target: 'fffAfVHv6-qJmB9EISNLno6Tqcjp2-MS3R-m7d3hpOc',
|
||||
qty: 55555
|
||||
await contract.writeInteraction<any>({
|
||||
function: "train",
|
||||
});
|
||||
await contract.writeInteraction({
|
||||
function: 'transfer',
|
||||
target: 'fffAfVHv6-qJmB9EISNLno6Tqcjp2-MS3R-m7d3hpOc',
|
||||
qty: 55555
|
||||
|
||||
/*await contract.writeInteraction<any>({
|
||||
function: "storeBalance",
|
||||
target: "M-mpNeJbg9h7mZ-uHaNsa5jwFFRAq0PsTkNWXJ-ojwI",
|
||||
});
|
||||
await contract.writeInteraction({
|
||||
function: 'transfer',
|
||||
target: 'fffAfVHv6-qJmB9EISNLno6Tqcjp2-MS3R-m7d3hpOc',
|
||||
qty: 55555
|
||||
});
|
||||
await contract.writeInteraction({
|
||||
function: 'transfer',
|
||||
target: 'fffAfVHv6-qJmB9EISNLno6Tqcjp2-MS3R-m7d3hpOc',
|
||||
qty: 55555
|
||||
|
||||
await contract.writeInteraction<any>({
|
||||
function: "storeBalance",
|
||||
target: "M-mpNeJbg9h7mZ-uHaNsa5jwFFRAq0PsTkNWXJ-ojwI",
|
||||
});*/
|
||||
|
||||
const {cachedValue} = await contract.readState();
|
||||
|
||||
logger.info("Result", cachedValue.state);
|
||||
logger.info("errors", cachedValue.errorMessages);
|
||||
logger.info("Result");
|
||||
console.dir(cachedValue.state);
|
||||
|
||||
|
||||
} catch (e) {
|
||||
logger.error(e)
|
||||
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
/* eslint-disable */
|
||||
import Arweave from 'arweave';
|
||||
import {defaultCacheOptions, defaultWarpGwOptions, LoggerFactory, WarpFactory} from '../src';
|
||||
|
||||
LoggerFactory.INST.logLevel('debug');
|
||||
|
||||
async function main() {
|
||||
const arweave = Arweave.init({
|
||||
host: 'arweave.net', // Hostname or IP address for a Arweave host
|
||||
port: 443, // Port
|
||||
protocol: 'https', // Network protocol http or https
|
||||
timeout: 60000, // Network request timeouts in milliseconds
|
||||
logging: false // Enable network request logging
|
||||
});
|
||||
|
||||
const warp = WarpFactory.forMainnet({
|
||||
...defaultCacheOptions,
|
||||
dbLocation: './tools/.leveldb'
|
||||
});
|
||||
|
||||
const result = await warp.migrationTool.migrateSqlite("./tools/sqlite/contracts-3008.sqlite");
|
||||
|
||||
console.log(result);
|
||||
|
||||
const dump = await warp.stateEvaluator.dumpCache();
|
||||
|
||||
console.log(dump);
|
||||
}
|
||||
|
||||
|
||||
main().catch((e) => console.error(e));
|
||||
Reference in New Issue
Block a user