feat!: Replace old fluence-js with JS Client (#425)

This commit is contained in:
Pavel
2023-02-21 12:20:51 +03:00
committed by GitHub
parent e8fc89f28a
commit e9e57b09f4
67 changed files with 166356 additions and 32066 deletions

View File

@ -0,0 +1,16 @@
import { justStop, runServer } from '../main.js';
import { demoCalculation } from '../_aqua/demo-calculation.js';
describe('smoke test', () => {
it('should work', async () => {
try {
await runServer();
const res = await demoCalculation();
expect(res).toBe(7);
} finally {
await justStop();
}
}, 15000);
});

View File

@ -0,0 +1,5 @@
import { runServer, waitForKeypressAndStop } from './main.js';
runServer()
.then(waitForKeypressAndStop)
.catch((e) => console.error('error: ', e));

View File

@ -0,0 +1,70 @@
import '@fluencelabs/js-client.node';
import { Fluence } from '@fluencelabs/js-client.api';
import { kras } from '@fluencelabs/fluence-network-environment';
import { registerCalc, CalcDef } from './_aqua/calc.js';
class Calc implements CalcDef {
private _state: number = 0;
add(n: number) {
this._state += n;
}
subtract(n: number) {
this._state -= n;
}
multiply(n: number) {
this._state *= n;
}
divide(n: number) {
this._state /= n;
}
reset() {
this._state = 0;
}
getResult() {
return this._state;
}
}
const relay = kras[0];
// generated with `npx aqua create_keypair`
const skBase64 = 'tOpsT08fvYMnRypj3VtSoqWMN5W/AptKsP39yanlkg4=';
export async function runServer() {
const skBytes = Buffer.from(skBase64, 'base64');
await Fluence.connect(relay, {
keyPair: {
type: 'Ed25519',
source: Uint8Array.from(skBytes),
},
});
registerCalc(new Calc());
const client = await Fluence.getClient();
console.log('application started');
console.log('peer id is: ', client.getPeerId());
console.log('relay is: ', client.getRelayPeerId());
console.log('press any key to quit...');
}
export async function justStop() {
await Fluence.disconnect();
}
export async function waitForKeypressAndStop() {
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', async () => {
await Fluence.disconnect();
process.exit(0);
});
}