-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathTreasury.ts
138 lines (124 loc) · 3.96 KB
/
Treasury.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import {
Address,
beginCell,
Cell,
Contract,
contractAddress,
ContractProvider,
Dictionary,
DictionaryValue,
internal,
loadMessageRelaxed,
MessageRelaxed,
Sender,
SenderArguments,
SendMode,
StateInit,
storeMessageRelaxed
} from "@ton/core";
const DictionaryMessageValue: DictionaryValue<{ sendMode: SendMode, message: MessageRelaxed }> = {
serialize(src, builder) {
builder.storeUint(src.sendMode, 8);
builder.storeRef(beginCell().store(storeMessageRelaxed(src.message)));
},
parse(src) {
let sendMode = src.loadUint(8);
let message = loadMessageRelaxed(src.loadRef().beginParse());
return { sendMode, message };
},
}
export type Treasury = Sender & {
address: Address
}
function senderArgsToMessageRelaxed(args: SenderArguments): MessageRelaxed {
return internal({
to: args.to,
value: args.value,
init: args.init,
body: args.body,
bounce: args.bounce
})
}
/**
* @class TreasuryContract is Wallet v4 alternative. For additional information see {@link Blockchain.treasury}
*/
export class TreasuryContract implements Contract {
static readonly code = Cell.fromBase64('te6cckEBBAEARQABFP8A9KQT9LzyyAsBAgEgAwIAWvLT/+1E0NP/0RK68qL0BNH4AH+OFiGAEPR4b6UgmALTB9QwAfsAkTLiAbPmWwAE0jD+omUe')
static create(workchain: number, subwalletId: bigint) {
return new TreasuryContract(workchain, subwalletId);
}
readonly address: Address;
readonly init: StateInit;
readonly subwalletId: bigint;
constructor(workchain: number, subwalletId: bigint) {
const data = beginCell()
.storeUint(subwalletId, 256)
.endCell();
this.init = { code: TreasuryContract.code, data };
this.address = contractAddress(workchain, this.init);
this.subwalletId = subwalletId;
}
/**
* Send bulk messages using one external message.
* @param messages Messages to send
* @param sendMode Send mode of every message
*/
async sendMessages(provider: ContractProvider, messages: MessageRelaxed[], sendMode?: SendMode) {
let transfer = this.createTransfer({
sendMode: sendMode,
messages: messages
})
await provider.external(transfer)
}
/**
* Sends message by arguments specified.
*/
async send(provider: ContractProvider, args: SenderArguments) {
await this.sendMessages(provider, [senderArgsToMessageRelaxed(args)], args.sendMode ?? undefined)
}
/**
* @returns Sender
*/
getSender(provider: ContractProvider): Treasury {
return {
address: this.address,
send: async (args) => {
let transfer = this.createTransfer({
sendMode: args.sendMode ?? undefined,
messages: [senderArgsToMessageRelaxed(args)]
});
await provider.external(transfer);
}
};
}
/**
* @returns wallet balance in nanoTONs
*/
async getBalance(provider: ContractProvider): Promise<bigint> {
return (await provider.getState()).balance
}
/**
* Creates transfer cell for {@link sendMessages}.
*/
createTransfer(args: {
messages: MessageRelaxed[]
sendMode?: SendMode,
}) {
let sendMode = SendMode.PAY_GAS_SEPARATELY;
if (args.sendMode !== null && args.sendMode !== undefined) {
sendMode = args.sendMode;
}
if (args.messages.length > 255) {
throw new Error('Maximum number of messages is 255');
}
let messages = Dictionary.empty(Dictionary.Keys.Int(16), DictionaryMessageValue);
let index = 0;
for (let m of args.messages) {
messages.set(index++, { sendMode, message: m });
}
return beginCell()
.storeUint(this.subwalletId, 256)
.storeDict(messages)
.endCell();
}
}