Skip to content

Commit 75539f8

Browse files
authored
Merge pull request #449 from bandada-infra/ref/blockchain-validators
Make `network` part of blockchain validator criteria
2 parents 72cca86 + ab73138 commit 75539f8

File tree

15 files changed

+149
-70
lines changed

15 files changed

+149
-70
lines changed

apps/api/.env.example

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ TWITTER_REDIRECT_URI=
2323
TWITTER_CLIENT_ID=
2424
TWITTER_CLIENT_SECRET=
2525

26-
BLOCKCHAIN_SEPOLIA_RPC_URL=
26+
# The network name must be the same as the id that appears in the `blockchainCredentialSupportedNetworks` list
27+
# exported from the `@bandada/utils` package but with capital letters. E.g. polygon_mumbai would be POLYGON_MUMBAI.
28+
29+
SEPOLIA_RPC_URL=
30+
POLYGON_MUMBAI_RPC_URL=
31+
OPTIMISM_SEPOLIA_RPC_URL=
32+
ARBITRUM_SEPOLIA_RPC_URL=
33+
AVALANCHE_C_CHAIN_FUJI_RPC_URL=

apps/api/src/app/credentials/credentials.controller.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ export class CredentialsController {
2121
return this.credentialsService.addMember(
2222
dto.oAuthState,
2323
dto.oAuthCode,
24-
dto.address,
25-
dto.network
24+
dto.address
2625
)
2726
}
2827
}

apps/api/src/app/credentials/credentials.service.test.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ jest.mock("@bandada/utils", () => ({
1818
logs: ["1"]
1919
}),
2020
getGroups: () => []
21-
})
21+
}),
22+
blockchainCredentialSupportedNetworks: [
23+
{
24+
id: "sepolia",
25+
name: "Sepolia"
26+
}
27+
]
2228
}))
2329

2430
jest.mock("@bandada/credentials", () => ({
@@ -293,7 +299,8 @@ describe("CredentialsService", () => {
293299
credentials: JSON.stringify({
294300
id: "BLOCKCHAIN_TRANSACTIONS",
295301
criteria: {
296-
minTransactions: 12
302+
minTransactions: 12,
303+
network: "sepolia"
297304
}
298305
})
299306
},
@@ -311,8 +318,7 @@ describe("CredentialsService", () => {
311318
const clientRedirectUri = await credentialsService.addMember(
312319
_stateId,
313320
undefined,
314-
"0x",
315-
"sepolia"
321+
"0x"
316322
)
317323

318324
expect(clientRedirectUri).toBeUndefined()
@@ -328,8 +334,7 @@ describe("CredentialsService", () => {
328334
const clientRedirectUri = await credentialsService.addMember(
329335
_stateId,
330336
undefined,
331-
"0x1",
332-
"sepolia"
337+
"0x1"
333338
)
334339

335340
expect(clientRedirectUri).toBeUndefined()

apps/api/src/app/credentials/credentials.service.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
Web2Context,
88
BlockchainContext
99
} from "@bandada/credentials"
10+
import { blockchainCredentialSupportedNetworks } from "@bandada/utils"
1011
import { id } from "@ethersproject/hash"
1112
import {
1213
BadRequestException,
@@ -78,15 +79,12 @@ export class CredentialsService {
7879
* @param OAuthState OAuth state to prevent forgery attacks.
7980
* @param oAuthCode OAuth code to exchange for an access token.
8081
* @param address Account address.
81-
* @param network Network name.
82-
* @param blockNumber Block number.
8382
* @returns Redirect URI
8483
*/
8584
async addMember(
8685
oAuthState: string,
8786
oAuthCode?: string,
88-
address?: string,
89-
network?: string
87+
address?: string
9088
): Promise<string> {
9189
if (!this.oAuthState.has(oAuthState)) {
9290
throw new BadRequestException(`OAuth state does not exist`)
@@ -108,10 +106,19 @@ export class CredentialsService {
108106
let context: Web2Context | BlockchainContext
109107

110108
if (address) {
109+
const { network } = JSON.parse(group.credentials).criteria
110+
111+
const supportedNetwork = blockchainCredentialSupportedNetworks.find(
112+
(n) => n.name.toLowerCase() === network.toLowerCase()
113+
)
114+
115+
if (supportedNetwork === undefined)
116+
throw new BadRequestException(`The network is not supported`)
117+
118+
const networkEnvVariableName = supportedNetwork.id.toUpperCase()
119+
111120
const web3providerRpcURL =
112-
process.env[
113-
`${providerName.toUpperCase()}_${network.toUpperCase()}_RPC_URL`
114-
]
121+
process.env[`${networkEnvVariableName}_RPC_URL`]
115122

116123
const jsonRpcProvider = await (
117124
provider as BlockchainProvider

apps/api/src/app/credentials/dto/add-member.dto.ts

-8
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,4 @@ export class AddMemberDto {
1111
@IsOptional()
1212
@IsString()
1313
readonly address: string
14-
15-
@IsOptional()
16-
@IsString()
17-
readonly network: string
18-
19-
@IsOptional()
20-
@IsString()
21-
readonly blockNumber: string
2214
}

apps/dashboard/src/api/bandadaAPI.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,15 @@ export async function setOAuthState(
202202
export async function addMemberByCredentials(
203203
oAuthState: string,
204204
oAuthCode?: string,
205-
address?: string,
206-
network?: string,
207-
blockNumber?: string
205+
address?: string
208206
): Promise<string | null> {
209207
try {
210208
return await request(`${API_URL}/credentials`, {
211209
method: "POST",
212210
data: {
213211
oAuthState,
214212
oAuthCode,
215-
address,
216-
network,
217-
blockNumber
213+
address
218214
}
219215
})
220216
} catch (error: any) {

apps/dashboard/src/components/new-group-stepper/access-mode-step.tsx

+57-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { validators } from "@bandada/credentials"
2+
import { blockchainCredentialSupportedNetworks } from "@bandada/utils"
23
import {
34
Box,
45
Button,
@@ -198,31 +199,62 @@ export default function AccessModeStep({
198199
</NumberInputStepper>
199200
</NumberInput>
200201
)}
201-
{parameter[1].type === "string" && (
202-
<Input
203-
size="lg"
204-
value={
205-
_credentials.criteria[
206-
parameter[0]
207-
]
208-
}
209-
onChange={(event) =>
210-
setCredentials({
211-
..._credentials,
212-
criteria: {
213-
..._credentials.criteria,
214-
[parameter[0]]:
215-
event.target.value
216-
}
217-
})
218-
}
219-
placeholder={
220-
parameter[0] === "repository"
221-
? "<repo-owner>/<repo-name>"
222-
: undefined
223-
}
224-
/>
225-
)}
202+
{parameter[0] !== "network" &&
203+
parameter[1].type === "string" && (
204+
<Input
205+
size="lg"
206+
value={
207+
_credentials.criteria[
208+
parameter[0]
209+
]
210+
}
211+
onChange={(event) =>
212+
setCredentials({
213+
..._credentials,
214+
criteria: {
215+
..._credentials.criteria,
216+
[parameter[0]]:
217+
event.target
218+
.value
219+
}
220+
})
221+
}
222+
placeholder={
223+
parameter[0] ===
224+
"repository"
225+
? "<repo-owner>/<repo-name>"
226+
: undefined
227+
}
228+
/>
229+
)}
230+
{parameter[0] === "network" &&
231+
parameter[1].type === "string" && (
232+
<Select
233+
size="lg"
234+
placeholder="Select network"
235+
onChange={(event) =>
236+
setCredentials({
237+
..._credentials,
238+
criteria: {
239+
..._credentials.criteria,
240+
[parameter[0]]:
241+
event.target
242+
.value
243+
}
244+
})
245+
}
246+
>
247+
{blockchainCredentialSupportedNetworks.map(
248+
(network: any) => (
249+
<option
250+
value={network.name}
251+
>
252+
{network.name}
253+
</option>
254+
)
255+
)}
256+
</Select>
257+
)}
226258
{parameter[1].type === "boolean" && (
227259
<Checkbox
228260
isChecked={

apps/dashboard/src/pages/credentials.tsx

+2-10
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ export default function CredentialsPage() {
4040
const providerName = _searchParams.get("provider")
4141
const clientRedirectUri =
4242
_searchParams.get("redirect_uri") || undefined
43-
const network = _searchParams.get("network")
44-
const blockNumber = _searchParams.get("block")
4543

4644
const state = await setOAuthState(
4745
groupId as string,
@@ -57,17 +55,11 @@ export default function CredentialsPage() {
5755
isLoggedInAdmin() &&
5856
state
5957
) {
60-
let blockNumberVal
61-
if (blockNumber) {
62-
blockNumberVal = blockNumber
63-
}
64-
if (state && admin && network) {
58+
if (state && admin) {
6559
const redirectUrl = await addMemberByCredentials(
6660
state,
6761
undefined,
68-
admin.address,
69-
network,
70-
blockNumberVal
62+
admin.address
7163
)
7264

7365
if (redirectUrl) {

libs/credentials/src/validators/blockchainBalance/index.test.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ describe("BlockchainBalance", () => {
1414
{
1515
id: blockchainBalance.id,
1616
criteria: {
17-
minBalance: "10"
17+
minBalance: "10",
18+
network: "sepolia"
1819
}
1920
},
2021
{
@@ -34,6 +35,7 @@ describe("BlockchainBalance", () => {
3435
id: blockchainBalance.id,
3536
criteria: {
3637
minBalance: "10",
38+
network: "sepolia",
3739
blockNumber: 4749638
3840
}
3941
},
@@ -71,6 +73,7 @@ describe("BlockchainBalance", () => {
7173
id: blockchainBalance.id,
7274
criteria: {
7375
minBalance: "100",
76+
network: "sepolia",
7477
minStars: 200
7578
}
7679
},
@@ -91,7 +94,8 @@ describe("BlockchainBalance", () => {
9194
{
9295
id: blockchainBalance.id,
9396
criteria: {
94-
minBalance: 100
97+
minBalance: 100,
98+
network: "sepolia"
9599
}
96100
},
97101
{

libs/credentials/src/validators/blockchainBalance/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { BlockchainContext, Validator } from "../.."
33

44
export type Criteria = {
55
minBalance: string
6+
network: string
67
blockNumber?: number
78
}
89

@@ -14,6 +15,10 @@ const validator: Validator = {
1415
type: "string",
1516
optional: false
1617
},
18+
network: {
19+
type: "string",
20+
optional: false
21+
},
1722
blockNumber: {
1823
type: "number",
1924
optional: true

libs/credentials/src/validators/blockchainTransactions/index.test.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ describe("BlockchainTransactions", () => {
1313
{
1414
id: blockchainTransactions.id,
1515
criteria: {
16-
minTransactions: 10
16+
minTransactions: 10,
17+
network: "sepolia"
1718
}
1819
},
1920
{
@@ -33,6 +34,7 @@ describe("BlockchainTransactions", () => {
3334
id: blockchainTransactions.id,
3435
criteria: {
3536
minTransactions: 10,
37+
network: "sepolia",
3638
blockNumber: 4749638
3739
}
3840
},
@@ -70,6 +72,7 @@ describe("BlockchainTransactions", () => {
7072
id: blockchainTransactions.id,
7173
criteria: {
7274
minTransactions: 100,
75+
network: "sepolia",
7376
minStars: 200
7477
}
7578
},
@@ -90,7 +93,8 @@ describe("BlockchainTransactions", () => {
9093
{
9194
id: blockchainTransactions.id,
9295
criteria: {
93-
minTransactions: "100"
96+
minTransactions: "100",
97+
network: "sepolia"
9498
}
9599
},
96100
{

libs/credentials/src/validators/blockchainTransactions/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { BlockchainContext, Validator } from "../.."
22

33
export type Criteria = {
44
minTransactions: number
5+
network: string
56
blockNumber?: number
67
}
78

@@ -13,6 +14,10 @@ const validator: Validator = {
1314
type: "number",
1415
optional: false
1516
},
17+
network: {
18+
type: "string",
19+
optional: false
20+
},
1621
blockNumber: {
1722
type: "number",
1823
optional: true

0 commit comments

Comments
 (0)