-
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.
PKG - [sdk] Implement GetNetworkParameters (#1420)
* Add getNetwork util * Implement GetNetworkParameters interaction
- Loading branch information
Huy Nguyen
authored
Dec 6, 2022
1 parent
00a5cb0
commit c20bc34
Showing
35 changed files
with
361 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"@onflow/fcl": minor | ||
"@onflow/sdk": minor | ||
"@onflow/transport-grpc": minor | ||
"@onflow/transport-http": minor | ||
"@onflow/fcl-wc": patch | ||
--- | ||
|
||
Add GetNetworkParameters interaction and a util to get chain ID |
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,8 +1,23 @@ | ||
import * as fclWC from "./fcl-wc" | ||
import {config} from "@onflow/config" | ||
|
||
describe("Init Client", () => { | ||
it("should throw without projectId", async () => { | ||
async function testFn() { | ||
|
||
// Mock transport then import fcl-wc because startup of fcl will call getChainId util which hits the chain | ||
await config.overload( | ||
{ | ||
"flow.network.default": "testnet", | ||
"sdk.transport": async ix => ix | ||
}, | ||
async () => { | ||
const fclWC = require("./fcl-wc") | ||
await fclWC.init() | ||
} | ||
) | ||
} | ||
|
||
expect.assertions(1) | ||
await expect(fclWC.init()).rejects.toThrow(Error) | ||
await expect(testFn).rejects.toThrow(Error) | ||
}) | ||
}) |
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
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
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 * as sdk from "@onflow/sdk" | ||
import {config} from "@onflow/config" | ||
import {log} from "@onflow/util-logger" | ||
import { invariant } from "@onflow/util-invariant" | ||
|
||
async function getChainIdFromAccessNode() { | ||
const response = await sdk.send([sdk.getNetworkParameters()]).then(sdk.decode) | ||
return response.chainId | ||
} | ||
|
||
export async function setChainIdDefault() { | ||
const network = await getChainIdFromAccessNode() | ||
config.put("flow.network.default", network) | ||
return network | ||
} | ||
|
||
export async function getChainId() { | ||
let network = await config.get("flow.network.default") | ||
|
||
if (!network) { | ||
network = await setChainIdDefault() | ||
|
||
if (!network) { | ||
network = await config.get("flow.network") | ||
|
||
if (network) { | ||
log.deprecate({ | ||
pkg: "FCL", | ||
subject: | ||
'Using the "flow.network" configuration key for specifying the flow network', | ||
message: "Configuring flow.network is no longer required", | ||
transition: | ||
"https://github.com/onflow/flow-js-sdk/blob/master/packages/fcl/TRANSITIONS.md#0002-deprecate-flow.network-config-key", | ||
}) | ||
} else { | ||
network = await config.get("env") | ||
|
||
if (network) | ||
log.deprecate({ | ||
pkg: "FCL", | ||
subject: | ||
'Using the "env" configuration key for specifying the flow network', | ||
message: | ||
"Configuring to specify flow network is no longer required", | ||
transition: | ||
"https://github.com/onflow/flow-js-sdk/blob/master/packages/fcl/TRANSITIONS.md#0001-deprecate-env-config-key", | ||
}) | ||
} | ||
} | ||
} | ||
|
||
invariant( | ||
network, | ||
"Error getting chainId from access node. Please configure flow.network instead" | ||
) | ||
|
||
return network | ||
} |
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,15 @@ | ||
import {config} from "@onflow/config" | ||
import assert from "assert" | ||
import {getChainId} from "./getChainId" | ||
|
||
describe("getChainId", () => { | ||
it("getChainId assuming it's already in config", async () => { | ||
let network | ||
|
||
await config.overload({"flow.network.default": "testnet"}, async () => { | ||
network = await getChainId() | ||
}) | ||
|
||
assert.equal(network, "testnet") | ||
}) | ||
}) |
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,10 @@ | ||
import {pipe, Ok, makeGetNetworkParameters} from "../interaction/interaction.js" | ||
|
||
export function getNetworkParameters() { | ||
return pipe([ | ||
makeGetNetworkParameters, | ||
ix => { | ||
return Ok(ix) | ||
}, | ||
]) | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/sdk/src/build/build-get-network-parameters.test.js
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,13 @@ | ||
import { | ||
interaction, | ||
isGetNetworkParameters, | ||
} from "../interaction/interaction.js" | ||
import {getNetworkParameters} from "./build-get-network-parameters.js" | ||
|
||
describe("Build Get Network Parameters", () => { | ||
test("Get Network Parameters", async () => { | ||
let ix = await getNetworkParameters()(interaction()) | ||
|
||
expect(isGetNetworkParameters(ix)).toBe(true) | ||
}) | ||
}) |
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.