-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: unnecessary call to arweave.networkInfo when block height is re…
…quest from client #70
- Loading branch information
1 parent
027019f
commit a49d27a
Showing
13 changed files
with
136 additions
and
52 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
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
47 changes: 47 additions & 0 deletions
47
src/core/modules/impl/RedstoneGatewayContractDefinitionLoader.ts
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,47 @@ | ||
import { ContractDefinition, ContractDefinitionLoader, LoggerFactory, stripTrailingSlash, SwCache } from '@smartweave'; | ||
import Arweave from 'arweave'; | ||
import 'isomorphic-fetch'; | ||
|
||
/** | ||
* An extension to {@link ContractDefinitionLoader} that makes use of | ||
* Redstone Gateway ({@link https://github.com/redstone-finance/redstone-sw-gateway}) | ||
* to load Contract Data. | ||
* | ||
* If the contract data is not available on RedStone Gateway - it fallbacks to default implementation | ||
* in {@link ContractDefinitionLoader} - i.e. loads the definition from Arweave gateway. | ||
*/ | ||
export class RedstoneGatewayContractDefinitionLoader extends ContractDefinitionLoader { | ||
private readonly rLogger = LoggerFactory.INST.create('ContractDefinitionLoader'); | ||
|
||
constructor( | ||
private readonly baseUrl: string, | ||
arweave: Arweave, | ||
cache?: SwCache<string, ContractDefinition<unknown>> | ||
) { | ||
super(arweave, cache); | ||
this.baseUrl = stripTrailingSlash(baseUrl); | ||
} | ||
|
||
async doLoad<State>(contractTxId: string, forcedSrcTxId?: string): Promise<ContractDefinition<State>> { | ||
if (forcedSrcTxId) { | ||
// no support for the evolve... | ||
return await super.doLoad(contractTxId, forcedSrcTxId); | ||
} | ||
|
||
try { | ||
return await fetch(`${this.baseUrl}/gateway/contracts/${contractTxId}`) | ||
.then((res) => { | ||
return res.ok ? res.json() : Promise.reject(res); | ||
}) | ||
.catch((error) => { | ||
if (error.body?.message) { | ||
this.rLogger.error(error.body.message); | ||
} | ||
throw new Error(`Unable to retrieve contract data. Redstone gateway responded with status ${error.status}.`); | ||
}); | ||
} catch (e) { | ||
this.rLogger.debug('Falling back to default contracts loader'); | ||
return await super.doLoad(contractTxId, forcedSrcTxId); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.