Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PKG - [sdk] Implement GetNetworkParameters #1420

Merged
merged 75 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 74 commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
329acd3
Add getNetwork util
Aug 22, 2022
78427df
Implement GetNetworkParameters for gRPC
Sep 20, 2022
1117f29
PKG - [sdk] Implement builder for GetNetworkParameters
Sep 20, 2022
d60d44b
DOC - [sdk] update README with GetNetworkParameters
Sep 20, 2022
2892e3f
PKG - [transport-grpc] Implement sender for GetNetworkParameters
Sep 20, 2022
ec29d82
Merge branch 'huyndo/get-network-parameters' of github.com:onflow/fcl…
Sep 20, 2022
0fa2b5f
PKG - [sdk] Add changeset for GetNetworkParameters gRPC
Sep 20, 2022
93b7393
DOC -- [fcl] Add Dapper Wallet to address list (#1370)
chasefleming Aug 22, 2022
cf8629c
[fcl] Adds WalletConnect Adapter Package and support for client plugi…
Aug 23, 2022
d605ff0
Version Packages (alpha) (#1369)
github-actions[bot] Aug 23, 2022
f766649
PKG -- [fcl] update execService with missing logger pkg (#1372)
Aug 23, 2022
81b52ec
Version Packages (alpha) (#1373)
github-actions[bot] Aug 23, 2022
8f55114
PKG -- [docs] Fix broken links on new docs site (#1376)
jribbink Aug 24, 2022
cb75c23
PKG -- [fcl] Respect service method for discovery (#1375)
chasefleming Aug 25, 2022
6c371a0
DOC --[fcl] Remove hardcoded version and dates (#1382)
chasefleming Aug 26, 2022
ce8e777
Setup a docs configuration file used by developers.flow.com (#1378)
schpet Aug 29, 2022
e352f84
[FLIP-934] Interaction Template Integration (#1323)
JeffreyDoyle Aug 30, 2022
59e7ca7
Version Packages (alpha) (#1381)
github-actions[bot] Aug 30, 2022
11ec4d4
PKG -- [fcl] Error on getInteractionTemplateAudits on mainnet until s…
JeffreyDoyle Aug 30, 2022
c0968aa
Version Packages (alpha) (#1387)
github-actions[bot] Aug 30, 2022
ff74559
Delete call-refresh-doc.yml (#1377)
justinbarry Aug 31, 2022
3e0f9a1
DOC -- Adds interaction template to docs sidebar (#1391)
JeffreyDoyle Aug 31, 2022
9a32579
Add to docs on discovery.wallet.method (#1393)
chasefleming Aug 31, 2022
2962159
DOC -- Query Interaction Template by POST search request (#1392)
JeffreyDoyle Sep 1, 2022
df1ed4c
[fcl][fcl-wc] - Updates to WC adapter API and Discovery Integration (…
Sep 1, 2022
97cc714
Version Packages (alpha) (#1398)
github-actions[bot] Sep 1, 2022
3303e69
PKG -- [fcl] Adds interaction template message getter utils (#1397)
JeffreyDoyle Sep 2, 2022
d1d128e
DOC -- Updates Interaction Template doc with auditor addresses (#1400)
JeffreyDoyle Sep 6, 2022
8fad78f
PKG -- [fcl] Hotfix to FCL query mutate prep ops (#1403)
JeffreyDoyle Sep 7, 2022
5c24628
Version Packages (alpha)
github-actions[bot] Sep 7, 2022
7c4bb22
Discovery API not loading on SSR apps in Safari and Incognito (#1404)
chasefleming Sep 7, 2022
c8ac541
Version Packages (alpha) (#1405)
github-actions[bot] Sep 7, 2022
1a9262d
PKG -- [fcl] Fix circular dep in FCL prep-template-opts (#1407)
JeffreyDoyle Sep 12, 2022
178fe8c
PKG -- [fcl] Hotfix to generate template interface id util (#1409)
JeffreyDoyle Sep 13, 2022
89c8020
[fcl-wc] add pairing modal override and sessionRequestHook (#1411)
Sep 15, 2022
387f69f
Version Packages (alpha) (#1408)
github-actions[bot] Sep 15, 2022
3e1d591
PKG -- [fcl] Adds mainnet Interaction Template Audit contract to Inte…
JeffreyDoyle Sep 16, 2022
932f519
Version Packages (alpha) (#1414)
github-actions[bot] Sep 16, 2022
27fb347
DOC -- Adds Flow Team Auditor MainNet address (#1415)
JeffreyDoyle Sep 19, 2022
73bc642
PKG -- [fcl] move base config to execService and combine with authn c…
Sep 19, 2022
d4f1f53
PKG - [fcl] Add util to get network from access node
Sep 24, 2022
ed64d32
PKG - [fcl] Update getVerifySignaturesScript to use getNetwork util
Sep 24, 2022
1522dd9
PKG - [fcl-wc] Update fcl-wc to use getNetwork util
Sep 24, 2022
ce78bec
PKG - [fcl] Update interaction templates to use getNetwork util
Sep 24, 2022
8534f08
PKG - [fcl] Add network to discovery API call
Sep 24, 2022
e249eb3
PKG - [fcl] Add network to execService
Sep 24, 2022
e239a02
Merge branch 'master' into gsantos/network-default
Sep 24, 2022
6056d13
PKG - [fcl] Fix import error in discovery/services.js
Sep 24, 2022
b152f24
Merge branch 'master' into huyndo/get-network-parameters
Oct 20, 2022
dafb20f
Export GetNetworkParameters from grpc
Oct 21, 2022
5f63462
Reformat GetNetworkParameters builder grpc
Oct 24, 2022
054b32f
Add GetNetworkParameters for HTTP
Oct 24, 2022
8a51384
Add decoder for getNetworkParameters
Oct 24, 2022
85e0a6b
Expose GetNetworkParameters to fcl.js
Oct 26, 2022
bfa163d
Merge branch 'gsantos/network-default' into huyndo/get-network-parame…
Oct 26, 2022
d8b1854
Merge branch 'master' into huyndo/get-network-parameters
Oct 26, 2022
dbb242a
Add util to get chain ID from GetNetworkParameters
Oct 28, 2022
53b2cea
Update codebase to use the new getChainId util
Oct 28, 2022
9a15177
Add a new resolver to set chainId
Oct 28, 2022
2eef653
Send chainId to wallets and discovery
Oct 28, 2022
fee3e53
Merge branch 'master' into huyndo/get-network-parameters
Oct 28, 2022
58078fc
Move chainId translation to decode
Nov 1, 2022
f2e4d56
Add opts to resolve to skip exec functions during testing
Nov 1, 2022
343369b
Change getChainId test to not hit the chain
Nov 1, 2022
ca9afa5
Update fcl-wc test to work with getChainId
Nov 1, 2022
76e013e
Add changeset
Nov 2, 2022
d704256
Merge branch 'master' into huyndo/get-network-parameters
Nov 2, 2022
0727523
Update packages/sdk/src/interaction/interaction.js
Nov 29, 2022
e302c6c
Add invariant if getChainId fails
Nov 29, 2022
da037cc
Remove TODO and change it into an issue (#1500)
Nov 29, 2022
b0b926f
Remove getChainId in resolver pipeline
Nov 29, 2022
31ba24b
Merge branch 'master' into huyndo/get-network-parameters
Nov 29, 2022
9ec827a
Remove changing config in fcl.test.js
Dec 1, 2022
b721684
Change fcl-wc test to use config.overload
Dec 2, 2022
c41dc1f
Update getChainId test to use config.overload
Dec 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/gold-cougars-run.md
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
19 changes: 17 additions & 2 deletions packages/fcl-wc/src/fcl-wc.test.js
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)
})
})
4 changes: 2 additions & 2 deletions packages/fcl-wc/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {log, LEVELS} from "@onflow/util-logger"
import {config} from "@onflow/config"
import {invariant} from "@onflow/util-invariant"
import * as fcl from "@onflow/fcl"

export let CONFIGURED_NETWORK = null

export const setConfiguredNetwork = async () => {
CONFIGURED_NETWORK = await config.get("flow.network")
CONFIGURED_NETWORK = await fcl.getChainId()
invariant(
CONFIGURED_NETWORK,
"FCL Configuration value for 'flow.network' is required"
Expand Down
10 changes: 9 additions & 1 deletion packages/fcl/TRANSITIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@

When specifying which flow network for FCL to use, use the `flow.network` config key instead of `env` (e.g. `fcl.config.put("flow.network", "testnet")`). Permitted values are currently `local`, `canarynet`, `testnet`, `mainnet`.

FCL currently falls back to `env` for the `flow.network` field but this will be removed in a future update.
FCL currently falls back to `env` for the `flow.network` field but this will be removed in a future update.

## 0002 Deprecate `flow.network` config key
- **Date:** September 26th 2022
- **Type:** Deprecation of `flow.network` in config

FCL now has a built-in utility to get which flow network to use. Manual specification in config is no longer required

FCL currently falls back to `flow.network` in case of failure but this will be removed in a future update.
19 changes: 4 additions & 15 deletions packages/fcl/src/app-utils/verify-signatures.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {config} from "@onflow/config"
import {log} from "@onflow/util-logger"
import {invariant} from "@onflow/util-invariant"
import {withPrefix, sansPrefix} from "@onflow/util-address"
import {query} from "../exec/query"
import {encodeAccountProof} from "../wallet-utils"
import {isString} from "../exec/utils/is"
import {getChainId} from "../utils"

const ACCOUNT_PROOF = "ACCOUNT_PROOF"
const USER_SIGNATURE = "USER_SIGNATURE"
Expand Down Expand Up @@ -54,25 +53,15 @@ export const validateArgs = args => {
}
}

// TODO: pass in option for contract but we're connected to testnet
// log address + network -> in sync?
const getVerifySignaturesScript = async (sig, opts) => {
const verifyFunction =
sig === "ACCOUNT_PROOF"
? "verifyAccountProofSignatures"
: "verifyUserSignatures"

let network = await config.get("flow.network")
if (!network) {
network = await config.get("env")
if (network)
log.deprecate({
pkg: "FCL",
subject:
'Using the "env" configuration key for specifying the flow network',
message: 'Please use "flow.network" instead.',
transition:
"https://github.com/onflow/flow-js-sdk/blob/master/packages/fcl/TRANSITIONS.md#0001-deprecate-env-config-key",
})
}
let network = await getChainId()

let fclCryptoContract

Expand Down
4 changes: 3 additions & 1 deletion packages/fcl/src/current-user/exec-service/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {invariant} from "@onflow/util-invariant"
import {log, LEVELS} from "@onflow/util-logger"
import {serviceRegistry} from "./plugins"
import {configLens} from "../../default-config"
import {getChainId} from "../../utils"
import {VERSION} from "../../VERSION"
import {configLens} from "../../default-config"

const execStrategy = async ({service, body, config, opts}) => {
const strategy = serviceRegistry.getStrategy(service.method)
Expand All @@ -19,6 +20,7 @@ export async function execService({service, msg = {}, config = {}, opts = {}}) {
fclVersion: VERSION,
fclLibrary: "https://github.com/onflow/fcl-js",
hostname: window?.location?.hostname ?? null,
network: await getChainId(),
},
}

Expand Down
5 changes: 5 additions & 0 deletions packages/fcl/src/default-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {config} from "@onflow/config"
import {setChainIdDefault} from "./utils/getChainId"

const isServerSide = () => typeof window === "undefined"

Expand All @@ -13,6 +14,10 @@ config({
"fcl.storage.default": SESSION_STORAGE,
})

// this is an async function but we can't await bc it's run at top level.
// NOT guaranteed that flow.network.default is set after this call (or at startup)
setChainIdDefault()

export async function configLens(regex) {
return Object.fromEntries(
Object.entries(await config().where(regex)).map(([key, value]) => [
Expand Down
2 changes: 2 additions & 0 deletions packages/fcl/src/discovery/services.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {config} from "@onflow/config"
import {invariant} from "@onflow/util-invariant"
import {serviceRegistry} from "../current-user/exec-service/plugins"
import {getChainId} from "../utils"
import {VERSION} from "../VERSION"
import {makeDiscoveryServices} from "./utils"

Expand All @@ -26,6 +27,7 @@ export async function getServices({types}) {
clientServices: await makeDiscoveryServices(),
supportedStrategies: serviceRegistry.getStrategies(),
userAgent: window?.navigator?.userAgent,
network: await getChainId(),
}),
}).then(d => d.json())
}
1 change: 1 addition & 0 deletions packages/fcl/src/discovery/services.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe("getServices", () => {
"discovery.authn.endpoint",
"https://fcl-discovery.onflow.org/api/testnet/authn"
)
.put("accessNode.api", "https://rest-testnet.onflow.org")
})

afterEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/fcl/src/exec/utils/derive-dependencies.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {config} from "@onflow/config"
import {invariant} from "@onflow/util-invariant"
import {withPrefix} from "@onflow/util-address"
import {getChainId} from "../../utils"

export async function deriveDependencies({template}) {
const network = await config.get("flow.network")
const network = await getChainId()

invariant(
network,
Expand Down
4 changes: 2 additions & 2 deletions packages/fcl/src/exec/utils/prep-template-opts.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as sdk from "@onflow/sdk"
import {retrieve} from "../../document/document.js"
import {normalizeInteractionTemplate} from "../../normalizers/interaction-template/interaction-template"
import {deriveCadenceByNetwork} from "../../interaction-template-utils/derive-cadence-by-network.js"
import {deriveDependencies} from "./derive-dependencies"
import {isString} from "./is"
import {getChainId} from "../../utils"

export async function prepTemplateOpts(opts) {
if (isString(opts?.template)) {
Expand All @@ -20,7 +20,7 @@ export async function prepTemplateOpts(opts) {
opts.cadence ||
deriveCadenceByNetwork({
template: opts.template,
network: await sdk.config().get("flow.network"),
network: await getChainId(),
})

opts.cadence = cadence
Expand Down
3 changes: 3 additions & 0 deletions packages/fcl/src/fcl.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export {AppUtils}
import * as InteractionTemplateUtils from "./interaction-template-utils"
export {InteractionTemplateUtils}

export {getChainId} from "./utils"

export {TestUtils} from "@onflow/sdk"
export {config} from "@onflow/config"
export {send} from "@onflow/sdk"
Expand Down Expand Up @@ -66,6 +68,7 @@ export {getBlockHeader} from "@onflow/sdk"
export {getCollection} from "@onflow/sdk"
export {getTransactionStatus} from "@onflow/sdk"
export {getTransaction} from "@onflow/sdk"
export {getNetworkParameters} from "@onflow/sdk"
export {authorizations, authorization} from "@onflow/sdk"
export {args, arg} from "@onflow/sdk"
export {proposer} from "@onflow/sdk"
Expand Down
1 change: 1 addition & 0 deletions packages/fcl/src/fcl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
payer,
ref,
createSignableVoucher,
config,
} from "@onflow/sdk"
import {VERSION} from "@onflow/sdk/src/VERSION"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {log, LEVELS} from "@onflow/util-logger"
import {query} from "../exec/query.js"
import {generateTemplateId} from "./generate-template-id.js"
import {normalizeInteractionTemplate} from "../normalizers/interaction-template/interaction-template.js"
import {getChainId} from "../utils"

export async function getInteractionTemplateAudits(
{template, auditors},
Expand Down Expand Up @@ -52,7 +53,7 @@ export async function getInteractionTemplateAudits(

let FlowInteractionAuditContract = opts.flowInteractionAuditContract
if (!FlowInteractionAuditContract) {
const fclNetwork = await config().get("flow.network")
const fclNetwork = await getChainId()
invariant(
fclNetwork === "mainnet" || fclNetwork === "testnet",
"getInteractionTemplateAudits Error: Unable to determine address for FlowInteractionTemplateAudit contract. Set configuration for 'fcl.network' to 'mainnet' or 'testnet'"
Expand Down
58 changes: 58 additions & 0 deletions packages/fcl/src/utils/getChainId.js
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",
})
}
}
}

gregsantos marked this conversation as resolved.
Show resolved Hide resolved
invariant(
network,
"Error getting chainId from access node. Please configure flow.network instead"
)

return network
}
13 changes: 13 additions & 0 deletions packages/fcl/src/utils/getChainId.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {config} from "@onflow/config"
import assert from "assert"
import { getChainId } from "./getChainId"

describe("getChainId", () => {

it("getChainId assuming it's already in config", async () => {
config.put("flow.network.default", "testnet")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last config.put that we should change into a config.overload !


const network = await getChainId()
assert.equal("testnet", network)
})
})
2 changes: 2 additions & 0 deletions packages/fcl/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export {getChainId} from "./getChainId"

export function isAndroid() {
return (
typeof navigator !== "undefined" && /android/i.test(navigator.userAgent)
Expand Down
10 changes: 10 additions & 0 deletions packages/sdk/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,15 @@ const response = await sdk.send(await sdk.pipe(await sdk.build([
]), { node: "http://localhost:8080" })
```

### Get Network Parameters Usage

``` javascript
import * as sdk from "@onflow/sdk"
const response = await sdk.send(await sdk.build([
sdk.getNetworkParameters()
]), { node: "http://localhost:8080" })
```

## Flow JS-SDK Exposes

- [Top Level](./src)
Expand Down Expand Up @@ -504,6 +513,7 @@ const response = await sdk.send(await sdk.pipe(await sdk.build([
- [`sdk.getLatestBlock`](./src/build/build-get-latest-block.js)
- [`sdk.getTransactionStatus`](./src/build/build-get-transaction-status.js)
- [`sdk.getTransaction`](./src/build/build-get-transaction.js)
- [`sdk.getNetworkParameters`](./src/build/build-get-network-parameters.js)
- [`sdk.invariant`](./src/build/build-invariant.js)
- [`sdk.limit`](./src/build/build-limit.js)
- [`sdk.payer`](./src/build/build-payer.js)
Expand Down
10 changes: 10 additions & 0 deletions packages/sdk/src/build/build-get-network-parameters.js
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 packages/sdk/src/build/build-get-network-parameters.test.js
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)
})
})
10 changes: 10 additions & 0 deletions packages/sdk/src/decode/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ export const decodeResponse = async (response, customDecoders = {}) => {
return response.transactionId
} else if (response.collection) {
return response.collection
} else if (response.networkParameters) {
const chainIdMap = {
"flow-testnet": "testnet",
"flow-mainnet": "mainnet",
"flow-emulator": "local",
}

return {
chainId: chainIdMap[response.networkParameters.chainId],
}
}

return null
Expand Down
Loading