-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVanity.ts
More file actions
85 lines (73 loc) · 2.71 KB
/
Vanity.ts
File metadata and controls
85 lines (73 loc) · 2.71 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
import { Address, beginCell, Cell, Contract, ContractProvider, Sender, SendMode, StateInit } from '@ton/core';
export type VanityConfig = {
owner: Address;
salt: Buffer;
fixedPrefixLength: number | undefined;
special: number | undefined;
};
type VanityExtra = {
sendDeployVanity: (provider: ContractProvider, via: Sender, value: bigint) => Promise<void>;
};
export type ContractWithVanity<T extends Contract = Contract> = T & VanityExtra;
export class Vanity implements Contract {
constructor(
readonly address: Address,
readonly init?: StateInit,
) {}
static createFromLine(line: string) {
type VanityInitJson = {
code: string;
data?: string;
fixedPrefixLength?: number;
special?: { tick: boolean; tock: boolean } | null;
};
type VanityLineJson = { address: string; init: VanityInitJson };
const obj = JSON.parse(line) as VanityLineJson;
const address = Address.parse(obj.address);
const init: StateInit = {
code: Cell.fromBase64(obj.init.code),
splitDepth: obj.init.fixedPrefixLength ?? undefined,
};
if (obj.init.data) {
init.data = Cell.fromBase64(obj.init.data);
}
if (obj.init.special) {
init.special = {
tick: !!obj.init.special.tick,
tock: !!obj.init.special.tock,
};
}
return new Vanity(address, init);
}
installContract<T extends Contract>(contract: T): ContractWithVanity<T> {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
const extra: VanityExtra = {
async sendDeployVanity(provider: ContractProvider, via: Sender, value: bigint) {
await provider.internal(via, {
value,
sendMode: SendMode.PAY_GAS_SEPARATELY,
body: beginCell()
.storeRef(contract.init?.code ?? Cell.EMPTY)
.storeRef(contract.init?.data ?? Cell.EMPTY)
.endCell(),
});
},
};
const proxy = new Proxy(contract as T & VanityExtra, {
get(target, prop, receiver) {
if (prop === 'init') {
return self.init;
}
if (prop === 'address') {
return self.address;
}
if (prop === 'sendDeployVanity') {
return extra.sendDeployVanity;
}
return Reflect.get(target, prop, receiver);
},
});
return proxy as ContractWithVanity<T>;
}
}