This repository was archived by the owner on Oct 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathhelpers.ts
More file actions
103 lines (95 loc) · 3.43 KB
/
helpers.ts
File metadata and controls
103 lines (95 loc) · 3.43 KB
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
import {
TitleEscrow,
TitleEscrow__factory,
TradeTrustToken,
TradeTrustToken__factory,
} from "@govtechsg/token-registry/contracts";
import { Wallet, constants } from "ethers";
import signale from "signale";
import { ConnectedSigner } from "../utils/wallet";
interface ConnectToTitleEscrowArgs {
tokenId: string;
address: string;
wallet: UserWallet;
}
interface ConnectToTokenRegistryArgs {
address: string;
wallet: UserWallet;
}
type UserWallet = Wallet | ConnectedSigner;
export const assertAddressIsSmartContract = async (
address: string,
account: Wallet | ConnectedSigner
): Promise<void> => {
const code = await account.provider.getCode(address);
const isContract = code !== "0x" && code !== "0x0"; // Ganache uses 0x0 instead
if (!isContract) throw new Error(`Address ${address} is not a valid Contract`);
};
export const connectToTitleEscrow = async ({
tokenId,
address,
wallet,
}: ConnectToTitleEscrowArgs): Promise<TitleEscrow> => {
const tokenRegistry: TradeTrustToken = await TradeTrustToken__factory.connect(address, wallet);
const titleEscrowAddress = await tokenRegistry.ownerOf(tokenId);
return await TitleEscrow__factory.connect(titleEscrowAddress, wallet);
};
export const connectToTokenRegistry = async ({
address,
wallet,
}: ConnectToTokenRegistryArgs): Promise<TradeTrustToken> => {
await assertAddressIsSmartContract(address, wallet);
const tokenRegistryInstance: TradeTrustToken = await TradeTrustToken__factory.connect(address, wallet);
// const isTokenRegistry = await supportsInterface(tokenRegistryInstance, "0x8a198f04")
// if(!isTokenRegistry) throw new Error(`Address ${address} is not a supported token registry contract`)
await tokenRegistryInstance.callStatic.genesis();
return tokenRegistryInstance;
};
interface validateEndorseChangeOwnerArgs {
newHolder: string;
newOwner: string;
titleEscrow: TitleEscrow;
}
export const validateEndorseChangeOwner = async ({
newHolder,
newOwner,
titleEscrow,
}: validateEndorseChangeOwnerArgs): Promise<void> => {
const beneficiary = await titleEscrow.beneficiary();
const holder = await titleEscrow.holder();
if (newOwner === beneficiary && newHolder === holder) {
const error = "new owner and new holder addresses are the same as the current owner and holder addresses";
signale.error(error);
throw new Error(error);
}
};
interface validateNominateBeneficiaryArgs {
beneficiaryNominee: string;
titleEscrow: TitleEscrow;
}
export const validateNominateBeneficiary = async ({
beneficiaryNominee,
titleEscrow,
}: validateNominateBeneficiaryArgs): Promise<void> => {
const beneficiary = await titleEscrow.beneficiary();
if (beneficiaryNominee === beneficiary) {
const error = "new beneficiary address is the same as the current beneficiary address";
signale.error(error);
throw new Error(error);
}
};
interface validateEndorseTransferOwnerArgs {
approvedOwner: string | undefined;
approvedHolder: string | undefined;
}
const GENESIS_ADDRESS = constants.AddressZero;
export const validateEndorseTransferOwner = ({
approvedOwner,
approvedHolder,
}: validateEndorseTransferOwnerArgs): void => {
if (!approvedOwner || !approvedHolder || approvedOwner === GENESIS_ADDRESS || approvedHolder === GENESIS_ADDRESS) {
const error = `there is no approved owner or holder or the approved owner or holder is equal to the genesis address: ${GENESIS_ADDRESS}`;
signale.error(error);
throw new Error(error);
}
};