-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontract.service.ts
59 lines (46 loc) · 1.53 KB
/
contract.service.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
import { Injectable, Logger } from '@nestjs/common';
import { createContract, Contract } from 'crossbell';
import { setJsonRpcAddress } from 'crossbell/network';
import assert from 'assert';
export type ContractType = 'readonly' | 'newbie-villa';
@Injectable()
export class CrossbellContractService {
protected readonly logger = new Logger('CrossbellContractService');
private contracts = new Map<ContractType, Contract>();
createContractV1(type: ContractType) {
setJsonRpcAddress(process.env.RPC_ENDPOINT_HTTP!);
const cached = this.contracts.get(type);
if (cached) {
return cached;
} else {
const contract = createContract(this.getPrivateKey(type), {
contractAddresses: {
cbt: '0x3D1b588a6Bcd728Bb61570ced6656eA4C05e404f',
},
});
this.logger.debug(`Create contract for ${type}`);
this.contracts.set(type, contract);
return contract;
}
}
private getPrivateKey(type: ContractType) {
switch (type) {
case 'readonly': {
return undefined;
}
case 'newbie-villa': {
const privateKey = process.env
.NEWBIE_VILLA_WALLET_PRIVATE_KEY as `0x${string}`;
assert(
privateKey,
'NEWBIE_VILLA_WALLET_PRIVATE_KEY is missing from environment variables',
);
assert(
privateKey.startsWith('0x'),
`Invalid NEWBIE_VILLA_WALLET_PRIVATE_KEY format. Please ensure you prepend '0x' to the beginning of your key`,
);
return privateKey;
}
}
}
}