/
base-core-sdk.ts
96 lines (88 loc) · 3.01 KB
/
base-core-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
import {
Web3LibAdapter,
MetadataStorage,
MetaTxConfig,
LensContracts
} from "@bosonprotocol/common";
import { TokenInfoManager } from "../utils/tokenInfoManager";
export class BaseCoreSDK {
protected _web3Lib: Web3LibAdapter;
protected _metadataStorage?: MetadataStorage;
protected _theGraphStorage?: MetadataStorage;
protected _subgraphUrl: string;
protected _protocolDiamond: string;
protected _chainId: number;
protected _tokenInfoManager: TokenInfoManager;
protected _metaTxConfig?: Partial<MetaTxConfig>;
protected _lensContracts?: LensContracts;
/**
* Creates an instance of `BaseCoreSDK`
* @param args - Constructor args
*/
constructor(opts: {
web3Lib: Web3LibAdapter;
subgraphUrl: string;
protocolDiamond: string;
metadataStorage?: MetadataStorage;
theGraphStorage?: MetadataStorage;
chainId: number;
metaTx?: Partial<MetaTxConfig>;
lensContracts?: LensContracts;
}) {
this._web3Lib = opts.web3Lib;
this._subgraphUrl = opts.subgraphUrl;
this._protocolDiamond = opts.protocolDiamond;
this._metadataStorage = opts.metadataStorage;
this._theGraphStorage = opts.theGraphStorage;
this._chainId = opts.chainId;
this._metaTxConfig = opts.metaTx;
this._lensContracts = opts.lensContracts;
}
protected assertAndGetMetaTxConfig(
overrides: Partial<{
contractAddress: string;
metaTxConfig: Partial<Omit<MetaTxConfig, "apiIds"> & { apiId: string }>;
metaTransactionMethod: string;
}> = {}
) {
const contractAddress = overrides.contractAddress || this._protocolDiamond;
const metaTransactionMethod =
overrides.metaTransactionMethod || "executeMetaTransaction";
const metaTxRelayerUrl =
overrides.metaTxConfig?.relayerUrl || this._metaTxConfig?.relayerUrl;
const metaTxApiKey =
overrides.metaTxConfig?.apiKey || this._metaTxConfig?.apiKey;
// metaTxApiId is depending on the contract/method(=executeMetaTransaction) to be called with Biconomy
const apiIds = this._metaTxConfig?.apiIds[contractAddress.toLowerCase()];
const metaTxApiId =
overrides.metaTxConfig?.apiId ||
(apiIds && apiIds[metaTransactionMethod]);
if (!(metaTxRelayerUrl && metaTxApiKey && metaTxApiId)) {
throw new Error(
"CoreSDK not configured to relay meta transactions. Either pass in 'relayerUrl', 'apiKey' and 'apiId' during initialization OR as overrides arguments."
);
}
return {
metaTxRelayerUrl,
metaTxApiId,
metaTxApiKey,
contractAddress
};
}
}
// Doc: https://www.typescriptlang.org/docs/handbook/mixins.html#alternative-pattern
export function applyMixins(
derivedCtor: typeof BaseCoreSDK,
constructors: typeof BaseCoreSDK[]
) {
constructors.forEach((baseCtor) => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
Object.defineProperty(
derivedCtor.prototype,
name,
Object.getOwnPropertyDescriptor(baseCtor.prototype, name) ||
Object.create(null)
);
});
});
}