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
simodrws
committed
Jun 9, 2023
1 parent
5a3cd10
commit 54fe92d
Showing
9 changed files
with
205 additions
and
27 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
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,29 @@ | ||
import React, { ReactNode, createContext, useContext, useMemo } from 'react'; | ||
import type { Masa } from '@masa-finance/masa-sdk'; | ||
import { useMasaClient } from './masa-client'; | ||
|
||
export interface MasaClientProviderValue { | ||
masa?: Masa; | ||
} | ||
|
||
export const MasaClientContext = createContext({} as MasaClientProviderValue); | ||
|
||
export const MasaClientProvider = ({ children }: { children: ReactNode }) => { | ||
const masa = useMasaClient(); | ||
|
||
const masaClientProviderValue = useMemo( | ||
() => ({ | ||
masa, | ||
}), | ||
[masa] | ||
); | ||
return ( | ||
<MasaClientContext.Provider value={masaClientProviderValue}> | ||
{children} | ||
</MasaClientContext.Provider> | ||
); | ||
}; | ||
|
||
export const useMasaClientProvider = (): MasaClientProviderValue => | ||
useContext(MasaClientContext); | ||
export default MasaClientProvider; |
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,3 +1,20 @@ | ||
export const useMasaAccount = () => { | ||
return {}; | ||
} | ||
import type { Signer } from 'ethers'; | ||
import { useConfig } from '../base-provider'; | ||
import { useWallet } from '../wallet-client/wallet/use-wallet'; | ||
import { useMasaSDK } from './use-masa-sdk'; | ||
|
||
export const useMasaClient = () => { | ||
const { masaConfig } = useConfig(); | ||
const { signer } = useWallet(); | ||
|
||
const masa = useMasaSDK( | ||
{ | ||
signer: signer as Signer | undefined, | ||
...masaConfig, | ||
environmentName: masaConfig.environment, | ||
}, | ||
[signer, masaConfig] | ||
); | ||
|
||
return masa; | ||
}; |
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,15 +1,101 @@ | ||
import { Masa } from '@masa-finance/masa-sdk'; | ||
import { Signer } from 'ethers'; | ||
import { | ||
Environment, | ||
EnvironmentName, | ||
environments, | ||
IIdentityContracts, | ||
Masa, | ||
NetworkName, | ||
} from '@masa-finance/masa-sdk'; | ||
import { | ||
SoulName__factory, | ||
SoulStore__factory, | ||
} from '@masa-finance/masa-contracts-identity'; | ||
|
||
import type { Signer } from 'ethers'; | ||
import { useMemo } from 'react'; | ||
import { ArweaveConfig } from '../config'; | ||
|
||
export interface UseMasaSdkArgs { | ||
signer?: Signer; | ||
environmentName?: EnvironmentName; | ||
networkName?: NetworkName; | ||
arweaveConfig?: ArweaveConfig; | ||
verbose?: boolean; | ||
apiUrl?: string; | ||
contractAddressOverrides?: { | ||
SoulNameAddress: string; | ||
SoulStoreAddress: string; | ||
}; | ||
} | ||
export const useMasaSDK = ( | ||
{ | ||
signer, | ||
environmentName, | ||
networkName = 'unknown', | ||
arweaveConfig, | ||
verbose, | ||
apiUrl, | ||
contractAddressOverrides, | ||
}: UseMasaSdkArgs, | ||
deps: Array<unknown> | ||
): Masa | undefined => { | ||
const masa = useMemo(() => { | ||
if (!signer) return undefined; | ||
|
||
console.log({ environmentName }); | ||
const environment: Environment | undefined = environments.find( | ||
(singleEnvironment: Environment) => | ||
singleEnvironment.name === environmentName | ||
); | ||
|
||
if (!environment) { | ||
console.error(`Unable to find environment ${environmentName as string}`); | ||
return undefined; | ||
} | ||
|
||
let contractOverrides: Partial<IIdentityContracts> | undefined; | ||
|
||
if (contractAddressOverrides) { | ||
console.log({ contractAddressOverrides }); | ||
|
||
contractOverrides = {} as Partial<IIdentityContracts>; | ||
contractOverrides.SoulStoreContract = SoulStore__factory.connect( | ||
contractAddressOverrides.SoulStoreAddress, | ||
signer | ||
); | ||
contractOverrides.SoulStoreContract.hasAddress = true; | ||
|
||
contractOverrides.SoulNameContract = SoulName__factory.connect( | ||
contractAddressOverrides.SoulNameAddress, | ||
signer | ||
); | ||
contractOverrides.SoulNameContract.hasAddress = true; | ||
} | ||
|
||
export const useMasaSDK = ({ signer }: { signer: Signer }) => { | ||
const masa = useMemo( | ||
() => | ||
new Masa({ | ||
signer, | ||
}), | ||
[signer] | ||
); | ||
return new Masa({ | ||
signer, | ||
apiUrl: apiUrl ?? environment.apiUrl, | ||
networkName, | ||
environment: environment.environment, | ||
arweave: { | ||
host: arweaveConfig?.host || 'arweave.net', | ||
port: Number.parseInt(arweaveConfig?.port || '443', 10), | ||
protocol: arweaveConfig?.protocol || 'https', | ||
logging: !!arweaveConfig?.logging || false, | ||
}, | ||
contractOverrides, | ||
verbose, | ||
}); | ||
}, [ | ||
signer, | ||
apiUrl, | ||
environmentName, | ||
networkName, | ||
arweaveConfig, | ||
verbose, | ||
contractAddressOverrides, | ||
...deps, | ||
]); | ||
|
||
return masa; | ||
}; |
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