-
Notifications
You must be signed in to change notification settings - Fork 116
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
14 changed files
with
450 additions
and
166 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,23 @@ | ||
import {pipe, Ok, makeSubscribeEvents} from "../interaction/interaction.js" | ||
|
||
export function subscribeEvents({ | ||
startBlockId, | ||
startHeight, | ||
eventTypes, | ||
addresses, | ||
contracts, | ||
heartbeatInterval, | ||
}) { | ||
return pipe([ | ||
makeSubscribeEvents, | ||
ix => { | ||
ix.subscribeEvents.startBlockId = startBlockId | ||
ix.subscribeEvents.startHeight = startHeight | ||
ix.subscribeEvents.eventTypes = eventTypes | ||
ix.subscribeEvents.addresses = addresses | ||
ix.subscribeEvents.contracts = contracts | ||
ix.subscribeEvents.heartbeatInterval = heartbeatInterval | ||
return Ok(ix) | ||
}, | ||
]) | ||
} |
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,26 @@ | ||
import {interaction} from "../interaction/interaction.js" | ||
import {subscribeEvents} from "./build-subscribe-events.js" | ||
|
||
describe("Subscribe Events", () => { | ||
test("Subscribe Events", async () => { | ||
let ix = await subscribeEvents({ | ||
startBlockId: "abc123", | ||
startHeight: 1, | ||
eventTypes: ["A.7e60df042a9c0868.FlowToken.TokensWithdrawn"], | ||
addresses: ["0x1", "0x2"], | ||
contracts: ["A.7e60df042a9c0868.FlowToken"], | ||
heartbeatInterval: 1000, | ||
})(interaction()) | ||
|
||
expect(ix.subscribeEvents.startBlockId).toBe("abc123") | ||
expect(ix.subscribeEvents.startHeight).toBe(1) | ||
expect(ix.subscribeEvents.eventTypes).toEqual([ | ||
"A.7e60df042a9c0868.FlowToken.TokensWithdrawn", | ||
]) | ||
expect(ix.subscribeEvents.addresses).toEqual(["0x1", "0x2"]) | ||
expect(ix.subscribeEvents.contracts).toEqual([ | ||
"A.7e60df042a9c0868.FlowToken", | ||
]) | ||
expect(ix.subscribeEvents.heartbeatInterval).toBe(1000) | ||
}) | ||
}) |
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,8 @@ | ||
export function subscription({onData, onError, onComplete}) { | ||
return ix => { | ||
ix.subscription.onData = onData | ||
ix.subscription.onError = onError | ||
ix.subscription.onComplete = onComplete | ||
return ix | ||
} | ||
} |
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,20 @@ | ||
import {interaction} from "../interaction/interaction.js" | ||
import {subscription} from "./build-subscription.js" | ||
|
||
describe("Build Subscription", () => { | ||
test("build subscription", async () => { | ||
const onData = () => {} | ||
const onError = () => {} | ||
const onComplete = () => {} | ||
|
||
let ix = await subscription({ | ||
onData, | ||
onError, | ||
onComplete, | ||
})(interaction()) | ||
|
||
expect(ix.subscription.onData).toBe(onData) | ||
expect(ix.subscription.onError).toBe(onError) | ||
expect(ix.subscription.onComplete).toBe(onComplete) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import {invariant} from "@onflow/util-invariant" | ||
import {subscribeWs as defaultSubscribeWs} from "./subscribe-ws" | ||
|
||
function constructData(ix, context, data) { | ||
let ret = context.response() | ||
ret.tag = ix.tag | ||
|
||
ret.events = data.events | ||
? data.events.map(event => ({ | ||
blockId: data.BlockId, | ||
blockHeight: Number(data.Height), | ||
blockTimestamp: data.Timestamp, | ||
type: event.Type, | ||
transactionId: event.TransactionId, | ||
transactionIndex: Number(event.TransactionIndex), | ||
eventIndex: Number(event.EventIndex), | ||
payload: JSON.parse( | ||
context.Buffer.from(event.Payload, "base64").toString() | ||
), | ||
})) | ||
: [] | ||
|
||
return ret | ||
} | ||
|
||
function constructResponse(ix, context, callback) { | ||
let ret = context.response() | ||
ret.tag = ix.tag | ||
|
||
ret.unsubscribeCallback = callback | ||
|
||
return ret | ||
} | ||
|
||
export async function sendSubscribeEvents(ix, context = {}, opts = {}) { | ||
invariant(opts.node, `SDK Send Get Events Error: opts.node must be defined.`) | ||
invariant( | ||
context.response, | ||
`SDK Send Get Events Error: context.response must be defined.` | ||
) | ||
invariant( | ||
context.Buffer, | ||
`SDK Send Get Events Error: context.Buffer must be defined.` | ||
) | ||
|
||
ix = await ix | ||
|
||
const subscribeWs = opts.subscribeWs || defaultSubscribeWs | ||
const {onData, onError, onComplete} = ix.subscription | ||
|
||
const unsubscribe = subscribeWs({ | ||
hostname: opts.node, | ||
path: `/v1/subscribe_events`, | ||
params: { | ||
start_block_id: ix.subscribeEvents.startBlockId, | ||
start_height: ix.subscribeEvents.startHeight, | ||
event_types: ix.subscribeEvents.eventTypes, | ||
addresses: ix.subscribeEvents.addresses, | ||
contracts: ix.subscribeEvents.contracts, | ||
heartbeat_interval: ix.subscribeEvents.heartbeatInterval, | ||
}, | ||
onData: handleData, | ||
onError, | ||
onComplete, | ||
}) | ||
|
||
function handleData(data) { | ||
const resp = constructData(ix, context, data) | ||
onData(resp) | ||
} | ||
|
||
return constructResponse(ix, context, unsubscribe) | ||
} |
Oops, something went wrong.