-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
161 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { LoggerFactory } from '../logging/LoggerFactory'; | ||
import { Warp } from './Warp'; | ||
|
||
export interface FetchRequest { | ||
input: RequestInfo | URL; | ||
init: Partial<RequestInit>; | ||
} | ||
|
||
export class WarpFetchWrapper { | ||
private readonly name = 'WarpFetchWrapper'; | ||
private readonly logger = LoggerFactory.INST.create(this.name); | ||
|
||
constructor(private warp: Warp) { | ||
this.warp = warp; | ||
} | ||
|
||
fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> { | ||
let fetchOptions: RequestInit; | ||
|
||
if (this.warp.hasPlugin('fetch-options')) { | ||
const fetchOptionsPlugin = this.warp.loadPlugin<FetchRequest, Partial<RequestInit>>('fetch-options'); | ||
try { | ||
const updatedFetchOptions = fetchOptionsPlugin.process({ input, init: init || {} }); | ||
fetchOptions = { ...init, ...updatedFetchOptions }; | ||
} catch (e) { | ||
if (e.message) { | ||
this.logger.error(e.message); | ||
} | ||
throw new Error(`Unable to process fetch options: ${e.message}`); | ||
} | ||
} else { | ||
fetchOptions = init; | ||
} | ||
|
||
return fetch(input, fetchOptions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { WarpPlugin, WarpPluginType } from '../src/core/WarpPlugin'; | ||
import { FetchRequest } from '../src/core/WarpFetchWrapper'; | ||
import { JWKInterface } from 'arweave/node/lib/wallet'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { LoggerFactory } from '../src/logging/LoggerFactory'; | ||
import { defaultCacheOptions, WarpFactory } from '../src/core/WarpFactory'; | ||
|
||
class FetchOptionsPlugin implements WarpPlugin<FetchRequest, RequestInit> { | ||
process(request: FetchRequest): Partial<RequestInit> { | ||
const url = request.input; | ||
|
||
let fetchOptions: Partial<RequestInit> = {}; | ||
|
||
if (url == `https://d1o5nlqr4okus2.cloudfront.net/gateway/sequencer/register`) { | ||
fetchOptions = { | ||
keepalive: true | ||
}; | ||
} | ||
|
||
return fetchOptions; | ||
} | ||
|
||
type(): WarpPluginType { | ||
return 'fetch-options'; | ||
} | ||
} | ||
|
||
async function main() { | ||
const wallet: JWKInterface = readJSON('./.secrets/jwk.json'); | ||
LoggerFactory.INST.logLevel('debug'); | ||
const logger = LoggerFactory.INST.create('FetchOptionsPlugin'); | ||
|
||
try { | ||
const warp = WarpFactory.forMainnet({ ...defaultCacheOptions, inMemory: true }).use(new FetchOptionsPlugin()); | ||
|
||
const jsContractSrc = fs.readFileSync(path.join(__dirname, 'data/js/token-pst.js'), 'utf8'); | ||
const initialState = fs.readFileSync(path.join(__dirname, 'data/js/token-pst.json'), 'utf8'); | ||
|
||
const { contractTxId } = await warp.createContract.deploy({ | ||
wallet, | ||
initState: initialState, | ||
src: jsContractSrc | ||
}); | ||
|
||
const contract = warp.contract(contractTxId).connect(wallet); | ||
|
||
await contract.writeInteraction({ | ||
function: 'transfer', | ||
target: 'uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M', | ||
qty: 55555 | ||
}); | ||
|
||
const { cachedValue } = await contract.readState(); | ||
logger.info(`Cached value: ${cachedValue}`); | ||
} catch (e) { | ||
logger.error(e); | ||
} | ||
} | ||
|
||
export function readJSON(path: string): JWKInterface { | ||
const content = fs.readFileSync(path, 'utf-8'); | ||
try { | ||
return JSON.parse(content); | ||
} catch (e) { | ||
throw new Error(`File "${path}" does not contain a valid JSON`); | ||
} | ||
} | ||
|
||
main().catch((e) => console.error(e)); |