This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
lambda-prevout-provider
fallback-prevout-provider
#128
Draft
monstrobishi
wants to merge
4
commits into
main
Choose a base branch
from
monstrobishi/lambda-prevout-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
89 changes: 89 additions & 0 deletions
89
packages/salmon-lambda-functions/src/generic/lambdaPrevoutProvider.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,89 @@ | ||
import { Prevout, PrevoutProvider } from '@defichain/jellyfish-transaction-builder/dist' | ||
import AWS from 'aws-sdk' | ||
import BigNumber from 'bignumber.js' | ||
|
||
const SEEK_MINUTES = 10 | ||
const VOUT_LOG_START = '\tINFO\t' | ||
const isLambda = process.env.LAMBDA_TASK_ROOT !== undefined | ||
const functionName = process.env.AWS_LAMBDA_FUNCTION_NAME ?? '' | ||
const logGroupPrefix = '/aws/lambda/' | ||
|
||
function mapEventsToPrevouts (events: AWS.CloudWatchLogs.OutputLogEvents): Prevout[] { | ||
return events.flatMap(event => { | ||
try { | ||
const message = event.message | ||
if (message === undefined) { | ||
return [] | ||
} | ||
|
||
const startPos: number = message.indexOf(VOUT_LOG_START) | ||
if (startPos === -1) { | ||
return [] | ||
} | ||
|
||
const trimmedMessage = message.substring(startPos + VOUT_LOG_START.length) | ||
const prevout = JSON.parse(trimmedMessage) | ||
if (prevout.vout !== undefined && | ||
prevout.txid !== undefined && | ||
prevout.value !== undefined && | ||
prevout.script !== undefined && | ||
prevout.tokenId !== undefined) { | ||
return [prevout] | ||
} | ||
} catch {} | ||
|
||
return [] | ||
}) | ||
} | ||
|
||
async function resolvePrevoutsFromCloudWatchLogs (cloudwatchlogs: AWS.CloudWatchLogs, | ||
groupParams: AWS.CloudWatchLogs.DescribeLogStreamsRequest): Promise<Prevout[]> { | ||
return await new Promise<Prevout[]>((resolve, reject) => { | ||
cloudwatchlogs.describeLogStreams(groupParams, (_, groupData) => { | ||
const logStreams = groupData.logStreams | ||
if (logStreams === undefined || logStreams.length === 0) { | ||
reject(new Error('Log streams empty or undefined')) | ||
return | ||
} | ||
|
||
const params: AWS.CloudWatchLogs.GetLogEventsRequest = { | ||
logGroupName: groupParams.logGroupName, | ||
logStreamName: logStreams[0].logStreamName ?? '', | ||
startTime: Date.now() - (1000 * 60 * SEEK_MINUTES) | ||
} | ||
|
||
cloudwatchlogs.getLogEvents(params, (_, data) => { | ||
const mapped = mapEventsToPrevouts(data.events ?? []) | ||
if (mapped.length === 0) { | ||
reject(new Error('No prevouts available')) | ||
return | ||
} | ||
|
||
// Return strictly the latest prevout as upstream may | ||
// choose in any order | ||
resolve(mapped.slice(-1)) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
export class LambdaPrevoutProvider implements PrevoutProvider { | ||
async all (): Promise<Prevout[]> { | ||
if (!isLambda) { | ||
return [] | ||
} | ||
|
||
const cloudwatchlogs = new AWS.CloudWatchLogs({ apiVersion: '2014-03-28' }) | ||
const groupParams: AWS.CloudWatchLogs.DescribeLogStreamsRequest = { | ||
logGroupName: `${logGroupPrefix}${functionName}`, | ||
descending: true, | ||
orderBy: 'LastEventTime' | ||
} | ||
return await resolvePrevoutsFromCloudWatchLogs(cloudwatchlogs, groupParams) | ||
} | ||
|
||
async collect (_: BigNumber): Promise<Prevout[]> { | ||
// TODO(fuxingloh): min balance filtering | ||
return await this.all() | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './oraclesManager' | ||
export * from './salmonWalletAccount' |
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
58 changes: 58 additions & 0 deletions
58
packages/salmon-oracles-functions/src/salmonWalletAccount.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,58 @@ | ||
import { WalletEllipticPair } from '@defichain/jellyfish-wallet' | ||
import { WhaleApiClient } from '@defichain/whale-api-client' | ||
import { WhaleWalletAccount } from '@defichain/whale-api-wallet' | ||
import { Network } from '@defichain/jellyfish-network' | ||
import { P2WPKHTransactionBuilder, Prevout, PrevoutProvider } from '@defichain/jellyfish-transaction-builder/dist' | ||
import BigNumber from 'bignumber.js' | ||
|
||
/** | ||
* FallbackPrevoutProvider provides an abstraction that takes | ||
* a fallback prevout provider, and a preferred prevout provider. | ||
* If the preferred provider is empty or throws an error, the fallback | ||
* provider is used. | ||
*/ | ||
export class FallbackPrevoutProvider implements PrevoutProvider { | ||
constructor ( | ||
protected readonly fallbackPrevoutProvider: PrevoutProvider, | ||
protected readonly preferredPrevoutProvider?: PrevoutProvider | ||
) { | ||
} | ||
|
||
async all (): Promise<Prevout[]> { | ||
if (this.preferredPrevoutProvider !== undefined) { | ||
try { | ||
const prevouts: Prevout[] = await this.preferredPrevoutProvider.all() | ||
if (prevouts.length > 0) { | ||
return prevouts | ||
} | ||
} catch {} | ||
} | ||
|
||
return await this.fallbackPrevoutProvider.all() | ||
} | ||
|
||
async collect (_: BigNumber): Promise<Prevout[]> { | ||
// TODO(fuxingloh): min balance filtering | ||
return await this.all() | ||
} | ||
} | ||
|
||
export class SalmonWalletAccount extends WhaleWalletAccount { | ||
protected readonly salmonPrevoutProvider: PrevoutProvider | ||
|
||
constructor ( | ||
public readonly client: WhaleApiClient, | ||
walletEllipticPair: WalletEllipticPair, | ||
network: Network, | ||
preferredPrevoutProvider?: PrevoutProvider | ||
) { | ||
super(client, walletEllipticPair, network, 10) | ||
this.salmonPrevoutProvider = new FallbackPrevoutProvider(this.prevoutProvider, preferredPrevoutProvider) | ||
} | ||
|
||
withTransactionBuilder (): P2WPKHTransactionBuilder { | ||
return new P2WPKHTransactionBuilder(this.feeRateProvider, this.salmonPrevoutProvider, { | ||
get: (_) => this | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With specific log group, literally we have partitioned, filterable time series DB :D