-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDistributor.ts
107 lines (94 loc) · 2.77 KB
/
Distributor.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
import {
Address,
beginCell,
Cell,
Contract,
contractAddress,
ContractProvider,
Dictionary,
Sender,
SendMode
} from '@ton/core';
import crc32 from 'crc-32';
export type DistributorConfig = {
owner: Address;
shares: Dictionary<Address, number>;
};
export function distributorConfigToCell(config: DistributorConfig): Cell {
return beginCell()
.storeAddress(config.owner)
.storeDict(config.shares)
.endCell();
}
export const Opcode = {
addUser: 0x163990a0,
shareCoins: 0x045ab564
};
export const ExitCode = {
MUST_BE_OWNER: 1201,
SHARES_SIZE_EXCEEDED_LIMIT: 1203
};
export class Distributor implements Contract {
constructor(readonly address: Address, readonly init?: { code: Cell; data: Cell }) {
}
static createFromAddress(address: Address) {
return new Distributor(address);
}
static createFromConfig(config: DistributorConfig, code: Cell, workchain = 0) {
const data = distributorConfigToCell(config);
const init = { code, data };
return new Distributor(contractAddress(workchain, init), init);
}
async sendDeploy(provider: ContractProvider, via: Sender, value: bigint) {
await provider.internal(via, {
value,
sendMode: SendMode.PAY_GAS_SEPARATELY,
body: beginCell().endCell()
});
}
async sendAddUser(
provider: ContractProvider,
via: Sender,
opts: {
value: bigint;
queryId?: number;
userAddress: Address;
}
) {
await provider.internal(via, {
value: opts.value,
sendMode: SendMode.PAY_GAS_SEPARATELY,
body: beginCell()
.storeInt(Opcode.addUser, 32)
.storeUint(opts.queryId ?? 0, 64)
.storeAddress(opts.userAddress)
.endCell()
});
}
async sendShareCoins(
provider: ContractProvider,
via: Sender,
opts: {
value: bigint;
queryId?: number;
}
) {
await provider.internal(via, {
value: opts.value,
sendMode: SendMode.PAY_GAS_SEPARATELY,
body: beginCell()
.storeInt(Opcode.shareCoins, 32)
.storeUint(opts.queryId ?? 0, 64)
.endCell()
});
}
async getOwner(provider: ContractProvider) {
const result = await provider.get('get_owner', []);
return result.stack.readAddress();
}
async getShares(provider: ContractProvider) {
const result = await provider.get('get_shares', []);
const cell = result.stack.readCellOpt();
return Dictionary.loadDirect(Dictionary.Keys.Address(), Dictionary.Values.Uint(1), cell);
}
}