Skip to content

Commit

Permalink
Fix race condition when initting sdk in ddex (#7456)
Browse files Browse the repository at this point in the history
  • Loading branch information
theoilie authored Feb 5, 2024
1 parent 9f169e7 commit 9d75ad2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 65 deletions.
2 changes: 1 addition & 1 deletion packages/ddex/webapp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Notes:
### Creating a bucket in S3
1. Create a new bucket in the S3 console with the name `ddex-[dev|staging]-<label/distributor>-raw`. Use all the defaults, including "ACLs disabled"
2. Do the same for a bucket named `ddex-[dev|staging]-<label/distributor>-indexed`. Use all the defaults, including "ACLs disabled"
3. Create an IAM Policy (here](https://us-east-1.console.aws.amazon.com/iamv2/home?region=us-west-2#/policies/create) (or search IAM and click Policies > Create Policy).
3. Create an IAM Policy (here](https://us-east-1.console.aws.amazon.com/iamv2/home?region=us-west-2#/policies/create) (or search IAM and click Policies > Create Policy). Select S3.
* Under `Read` choose `GetObject` and `GetObjectAttributes`.
* Under `Write` choose `DeleteObject` and `PutObject`.
* Under `List` choose `ListBucket`.
Expand Down
123 changes: 59 additions & 64 deletions packages/ddex/webapp/client/src/providers/AudiusSdkProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ export const AudiusSdkProvider = ({ children }: { children: ReactNode }) => {
const envVars = useEnvVars()
const { didInit, getFeatureEnabled } = useRemoteConfig()

// @ts-expect-error ts(2741). This is only here for debugging and should eventually be removed
window.audiusSdk = audiusSdk

/**
* Decodes a string id into an int. Returns null if an invalid ID. */
const decodeHashId = (id: string): number | null => {
Expand Down Expand Up @@ -99,83 +96,81 @@ export const AudiusSdkProvider = ({ children }: { children: ReactNode }) => {
}

const initSdk = () => {
if (!window.Web3 || !didInit) {
if (audiusSdk || !window.Web3 || !didInit) {
return
}

if (!audiusSdk) {
const logger = new Logger({ logLevel: 'info' })

// Determine config to use
let config = developmentConfig as ServicesConfig
let initialSelectedNode = 'http://audius-protocol-discovery-provider-1'
if (envVars.env === 'prod') {
config = productionConfig as ServicesConfig
initialSelectedNode = 'https://discoveryprovider.audius.co'
} else if (envVars.env === 'stage') {
config = stagingConfig as ServicesConfig
initialSelectedNode = 'https://discoveryprovider.staging.audius.co'
}
const logger = new Logger({ logLevel: 'info' })

// Determine config to use
let config = developmentConfig as ServicesConfig
let initialSelectedNode = 'http://audius-protocol-discovery-provider-1'
if (envVars.env === 'prod') {
config = productionConfig as ServicesConfig
initialSelectedNode = 'https://discoveryprovider.audius.co'
} else if (envVars.env === 'stage') {
config = stagingConfig as ServicesConfig
initialSelectedNode = 'https://discoveryprovider.staging.audius.co'
}

// Get keys
if (!envVars.ddexKey) {
setIsLoading(false)
console.error(`Skipping sdk initialization: ddexKey missing from env`)
return
}
// Get keys
if (!envVars.ddexKey) {
setIsLoading(false)
console.error(`Skipping sdk initialization: ddexKey missing from env`)
return
}

// Init SDK
const discoveryNodeSelector = new DiscoveryNodeSelector({
initialSelectedNode
})
const storageNodeSelector = new StorageNodeSelector({
auth: new AppAuth(envVars.ddexKey),
// Init SDK
const discoveryNodeSelector = new DiscoveryNodeSelector({
initialSelectedNode
})
const storageNodeSelector = new StorageNodeSelector({
auth: new AppAuth(envVars.ddexKey),
discoveryNodeSelector,
bootstrapNodes: config.storageNodes,
logger
})
const sdkInst = sdk({
services: {
discoveryNodeSelector,
bootstrapNodes: config.storageNodes,
logger
})
const sdkInst = sdk({
services: {
entityManager: new EntityManager({
discoveryNodeSelector,
entityManager: new EntityManager({
discoveryNodeSelector,
web3ProviderUrl: config.web3ProviderUrl,
contractAddress: config.entityManagerContractAddress,
identityServiceUrl: config.identityServiceUrl,
useDiscoveryRelay: true,
logger
}),
storageNodeSelector,
web3ProviderUrl: config.web3ProviderUrl,
contractAddress: config.entityManagerContractAddress,
identityServiceUrl: config.identityServiceUrl,
useDiscoveryRelay: true,
logger
},
apiKey: envVars.ddexKey,
appName: 'DDEX Demo'
})

let env: OAuthEnv = 'production'
if (envVars.env === 'stage') {
env = 'staging'
}
sdkInst.oauth!.init({
env,
successCallback: (user: DecodedUserToken) => {
setOauthError('')
checkUserAllowlisted(user)
},
errorCallback: (error) => {
setOauthError(error)
}
})
setAudiusSdk(sdkInst as AudiusSdkType)
}),
storageNodeSelector,
logger
},
apiKey: envVars.ddexKey,
appName: 'DDEX Demo'
})

let env: OAuthEnv = 'production'
if (envVars.env === 'stage') {
env = 'staging'
}
sdkInst.oauth!.init({
env,
successCallback: (user: DecodedUserToken) => {
setOauthError('')
checkUserAllowlisted(user)
},
errorCallback: (error) => {
setOauthError(error)
}
})
setAudiusSdk(sdkInst as AudiusSdkType)

setIsLoading(false)
}

useEffect(() => {
initSdk()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [didInit])
}, [didInit, envVars])

const contextValue = {
audiusSdk,
Expand Down

0 comments on commit 9d75ad2

Please sign in to comment.