feat: simplified configuration for redstone gateway

This commit is contained in:
ppedziwiatr
2022-03-09 20:49:33 +01:00
committed by Piotr Pędziwiatr
parent 7fe41f6516
commit bb62c91e16
3 changed files with 95 additions and 112 deletions

View File

@@ -92,7 +92,7 @@ In order to use the [Redstone Gateway](https://github.com/redstone-finance/redst
configure the smartweave instance in the following way: configure the smartweave instance in the following way:
```ts ```ts
const smartweave = SmartWeaveNodeFactory.memCachedBased(arweave) const smartweave = SmartWeaveNodeFactory.memCachedBased(arweave)
.setInteractionsLoader(new RedstoneGatewayInteractionsLoader(gatewayUrl)) .useRedStoneGateway()
.build(); .build();
``` ```
The gateway is currently available under [https://gateway.redstone.finance](https://gateway.redstone.finance) url. The gateway is currently available under [https://gateway.redstone.finance](https://gateway.redstone.finance) url.
@@ -104,7 +104,7 @@ all the interactions. There is a risk of returning [corrupted transactions](http
2. `{confirmed: true}` - returns only confirmed transactions - the most safe mode, eg: 2. `{confirmed: true}` - returns only confirmed transactions - the most safe mode, eg:
```ts ```ts
const smartweave = SmartWeaveNodeFactory.memCachedBased(arweave) const smartweave = SmartWeaveNodeFactory.memCachedBased(arweave)
.setInteractionsLoader(new RedstoneGatewayInteractionsLoader(gatewayUrl, {confirmed: true})) .useRedStoneGateway( {confirmed: true} )
.build(); .build();
``` ```
@@ -112,7 +112,7 @@ const smartweave = SmartWeaveNodeFactory.memCachedBased(arweave)
Not as safe as previous mode, but good if you want combine high level of safety with the most recent data. Not as safe as previous mode, but good if you want combine high level of safety with the most recent data.
```ts ```ts
const smartweave = SmartWeaveNodeFactory.memCachedBased(arweave) const smartweave = SmartWeaveNodeFactory.memCachedBased(arweave)
.setInteractionsLoader(new RedstoneGatewayInteractionsLoader(gatewayUrl, {notCorrupted: true})) .useRedStoneGateway( {notCorrupted: true} )
.build(); .build();
``` ```

View File

@@ -1,33 +1,33 @@
/* eslint-disable */ /* eslint-disable */
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import { interactRead, readContract } from 'smartweave'; import {interactRead, readContract} from 'smartweave';
import Arweave from 'arweave'; import Arweave from 'arweave';
import { import {
LoggerFactory, LoggerFactory,
MemCache, MemCache,
RedstoneGatewayContractDefinitionLoader, RedstoneGatewayContractDefinitionLoader,
RedstoneGatewayInteractionsLoader, RedstoneGatewayInteractionsLoader,
SmartWeaveNodeFactory, SmartWeaveNodeFactory,
SmartWeaveWebFactory, SmartWeaveWebFactory,
SourceType SourceType
} from '@smartweave'; } from '@smartweave';
const stringify = require('safe-stable-stringify'); const stringify = require('safe-stable-stringify');
function* chunks(arr, n) { function* chunks(arr, n) {
for (let i = 0; i < arr.length; i += n) { for (let i = 0; i < arr.length; i += n) {
// note: wrapping with an array to make it compatible with describe.each // note: wrapping with an array to make it compatible with describe.each
yield [arr.slice(i, i + n)]; yield [arr.slice(i, i + n)];
} }
} }
const arweave = Arweave.init({ const arweave = Arweave.init({
host: 'arweave.net', host: 'arweave.net',
port: 443, port: 443,
protocol: 'https', protocol: 'https',
timeout: 30000, timeout: 30000,
logging: false logging: false
}); });
LoggerFactory.INST.logLevel('fatal'); LoggerFactory.INST.logLevel('fatal');
@@ -39,107 +39,90 @@ const chunked: string[][][] = [...chunks(testCases, 10)];
const chunkedGw: string[][][] = [...chunks(testCasesGw, 10)]; const chunkedGw: string[][][] = [...chunks(testCasesGw, 10)];
describe.each(chunked)('v1 compare.suite %#', (contracts: string[]) => { describe.each(chunked)('v1 compare.suite %#', (contracts: string[]) => {
// note: concurrent doesn't seem to be working here, duh... // note: concurrent doesn't seem to be working here, duh...
// will probably need to manually split all the test cases to separate test files // will probably need to manually split all the test cases to separate test files
it.concurrent.each(contracts)( it.concurrent.each(contracts)(
'.test %# %o', '.test %# %o',
async (contractTxId: string) => { async (contractTxId: string) => {
const blockHeight = 850127; const blockHeight = 850127;
console.log('readContract', contractTxId); console.log('readContract', contractTxId);
const resultString = fs const resultString = fs
.readFileSync(path.join(__dirname, 'test-cases', 'contracts', `${contractTxId}.json`), 'utf-8') .readFileSync(path.join(__dirname, 'test-cases', 'contracts', `${contractTxId}.json`), 'utf-8')
.trim(); .trim();
console.log('readState', contractTxId); console.log('readState', contractTxId);
const result2 = await SmartWeaveNodeFactory.memCachedBased(arweave, 1) const result2 = await SmartWeaveNodeFactory.memCachedBased(arweave, 1)
.setInteractionsLoader( .useRedStoneGateway(null, SourceType.ARWEAVE)
new RedstoneGatewayInteractionsLoader('https://gateway.redstone.finance', null, SourceType.ARWEAVE) .build()
) .contract(contractTxId)
.build() .setEvaluationOptions({
.contract(contractTxId) useFastCopy: true
.setEvaluationOptions({ })
useFastCopy: true .readState(blockHeight);
}) const result2String = stringify(result2.state).trim();
.readState(blockHeight); expect(result2String).toEqual(resultString);
const result2String = stringify(result2.state).trim(); },
expect(result2String).toEqual(resultString); 800000
}, );
800000
);
}); });
fdescribe.each(chunkedGw)('gateways compare.suite %#', (contracts: string[]) => { fdescribe.each(chunkedGw)('gateways compare.suite %#', (contracts: string[]) => {
// note: concurrent doesn't seem to be working here, duh... // note: concurrent doesn't seem to be working here, duh...
// will probably need to manually split all the test cases to separate test files // will probably need to manually split all the test cases to separate test files
it.concurrent.each(contracts)( it.concurrent.each(contracts)(
'.test %# %o', '.test %# %o',
async (contractTxId: string) => { async (contractTxId: string) => {
const blockHeight = 855134; const blockHeight = 855134;
console.log('readState Redstone Gateway', contractTxId); console.log('readState Redstone Gateway', contractTxId);
const smartweaveR = SmartWeaveWebFactory.memCachedBased(arweave, 1) const smartweaveR = SmartWeaveWebFactory.memCachedBased(arweave, 1)
.setInteractionsLoader( .useRedStoneGateway(null, SourceType.ARWEAVE)
new RedstoneGatewayInteractionsLoader('https://gateway.redstone.finance', null, SourceType.ARWEAVE) .build();
) const result = await smartweaveR.contract(contractTxId).readState(blockHeight);
.setDefinitionLoader( const resultString = stringify(result.state).trim();
new RedstoneGatewayContractDefinitionLoader('https://gateway.redstone.finance', arweave, new MemCache())
)
.build();
const result = await smartweaveR.contract(contractTxId).readState(blockHeight);
const resultString = stringify(result.state).trim();
console.log('readState Arweave Gateway', contractTxId); console.log('readState Arweave Gateway', contractTxId);
const result2 = await SmartWeaveNodeFactory.memCached(arweave, 1).contract(contractTxId).readState(blockHeight); const result2 = await SmartWeaveNodeFactory.memCached(arweave, 1).contract(contractTxId).readState(blockHeight);
const result2String = stringify(result2.state).trim(); const result2String = stringify(result2.state).trim();
expect(result2String).toEqual(resultString); expect(result2String).toEqual(resultString);
}, },
800000 800000
); );
}); });
describe('readState', () => { describe('readState', () => {
it('should properly read state at requested block height', async () => { it('should properly read state at requested block height', async () => {
const contractTxId = 'CbGCxBJn6jLeezqDl1w3o8oCSeRCb-MmtZNKPodla-0'; const contractTxId = 'CbGCxBJn6jLeezqDl1w3o8oCSeRCb-MmtZNKPodla-0';
const blockHeight = 707892; const blockHeight = 707892;
const result = await readContract(arweave, contractTxId, blockHeight); const result = await readContract(arweave, contractTxId, blockHeight);
const resultString = stringify(result).trim(); const resultString = stringify(result).trim();
const result2 = await SmartWeaveNodeFactory.memCachedBased(arweave, 1) const result2 = await SmartWeaveNodeFactory.memCachedBased(arweave, 1)
.setInteractionsLoader( .useRedStoneGateway(null, SourceType.ARWEAVE)
new RedstoneGatewayInteractionsLoader('https://gateway.redstone.finance', null, SourceType.ARWEAVE) .build()
) .contract(contractTxId)
.setDefinitionLoader( .readState(blockHeight);
new RedstoneGatewayContractDefinitionLoader('https://gateway.redstone.finance', arweave, new MemCache()) const result2String = stringify(result2.state).trim();
)
.build()
.contract(contractTxId)
.readState(blockHeight);
const result2String = stringify(result2.state).trim();
expect(result2String).toEqual(resultString); expect(result2String).toEqual(resultString);
}, 800000); }, 800000);
it('should properly check balance of a PST contract', async () => { it('should properly check balance of a PST contract', async () => {
const jwk = await arweave.wallets.generate(); const jwk = await arweave.wallets.generate();
const contractTxId = '-8A6RexFkpfWwuyVO98wzSFZh0d6VJuI-buTJvlwOJQ'; const contractTxId = '-8A6RexFkpfWwuyVO98wzSFZh0d6VJuI-buTJvlwOJQ';
const v1Result = await interactRead(arweave, jwk, contractTxId, { const v1Result = await interactRead(arweave, jwk, contractTxId, {
function: 'balance', function: 'balance',
target: '6Z-ifqgVi1jOwMvSNwKWs6ewUEQ0gU9eo4aHYC3rN1M' target: '6Z-ifqgVi1jOwMvSNwKWs6ewUEQ0gU9eo4aHYC3rN1M'
}); });
const v2Result = await SmartWeaveNodeFactory.memCachedBased(arweave, 1) const v2Result = await SmartWeaveNodeFactory.memCachedBased(arweave, 1)
.setInteractionsLoader( .useRedStoneGateway(null, SourceType.ARWEAVE)
new RedstoneGatewayInteractionsLoader('https://gateway.redstone.finance', null, SourceType.ARWEAVE) .build()
) .contract(contractTxId)
.setDefinitionLoader( .connect(jwk)
new RedstoneGatewayContractDefinitionLoader('https://gateway.redstone.finance', arweave, new MemCache()) .viewState({
) function: 'balance',
.build() target: '6Z-ifqgVi1jOwMvSNwKWs6ewUEQ0gU9eo4aHYC3rN1M'
.contract(contractTxId) });
.connect(jwk)
.viewState({
function: 'balance',
target: '6Z-ifqgVi1jOwMvSNwKWs6ewUEQ0gU9eo4aHYC3rN1M'
});
expect(v1Result).toEqual(v2Result.result); expect(v1Result).toEqual(v2Result.result);
}, 800000); }, 800000);
}); });

View File

@@ -28,7 +28,7 @@ export interface RedstoneGatewayInteractions {
message?: string; message?: string;
} }
type ConfirmationStatus = export type ConfirmationStatus =
| { | {
notCorrupted?: boolean; notCorrupted?: boolean;
confirmed?: null; confirmed?: null;