/
use-masa-sdk.ts
122 lines (112 loc) · 3.07 KB
/
use-masa-sdk.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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,
address,
}: UseMasaSdkArgs & { address: `0x${string}` | undefined },
deps: Array<unknown>
): Masa | undefined => {
const masa = useMemo(() => {
if (!address) return undefined;
if (!signer) {
if (verbose)
console.log('DEBUG: no signer, returning undefined for masa object');
return undefined;
}
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) {
if (verbose) console.log('DEBUG:', { 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;
}
if (verbose) {
signer
.getAddress()
.then((s) => {
console.log('DEBUG: creating new masa', {
signerAddress: s,
signer,
environment,
networkName,
verbose,
apiUrl,
});
})
.catch((error: unknown) => console.error({ error }));
}
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,
});
}, [
address,
signer,
apiUrl,
environmentName,
networkName,
arweaveConfig,
verbose,
contractAddressOverrides,
...deps, // eslint-disable-line react-hooks/exhaustive-deps
]);
return masa;
};