This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.js
149 lines (136 loc) · 4.14 KB
/
agent.js
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
139
140
141
142
143
144
145
146
147
148
149
/* eslint-disable import/extensions */
import { Account } from '@eversdk/appkit';
import {
TonClient,
signerNone,
MessageBodyType,
} from '@eversdk/core';
import { libNode } from '@eversdk/lib-node';
import BigNumber from 'bignumber.js';
import { Address } from 'everscale-inpage-provider';
import { ResponseType } from '@eversdk/core/dist/bin.js';
import * as IPFS from 'ipfs-core';
import { AssetFactoryContract } from './artifacts/AssetFactory.js';
import { EverwalletContract } from './artifacts/EverwalletContract.js';
import { config } from './config/config.js';
const ipfs = await IPFS.create({ repo: 'ipfs' });
const {
ipfsTopic, auditorAddressConfig, auditorSeed, endpoint
} = config;
let { assetFactoryAddress } = config;
TonClient.useBinaryLibrary(libNode);
const client = new TonClient({
network: {
endpoints: [endpoint],
},
});
// eslint-disable-next-line no-unused-vars
const auditorKeyPair = await client.crypto.mnemonic_derive_sign_keys({
phrase: auditorSeed,
});
assetFactoryAddress = new Address(assetFactoryAddress);
const assetFactoryContract = new Account(AssetFactoryContract, {
signer: signerNone(),
client,
initData: {},
});
function assetsCalculation(log) {
/**
* Check log and calculate assets to mint
* TO DO: calculation
*/
const totalAssets = 5;
return totalAssets;
}
async function mintAsset(amount, auditor, tokenName, tokenSymbol, user) {
console.log(`Minting ${amount} tokens for ${user}`);
const auditorAddress = new Address(auditor)
const userAddress = new Address(user)
const inputDeployRoot = {
answerId: 0,
name: tokenName,
symbol: tokenSymbol,
decimals: 9,
owner: auditorAddress,
initialSupplyTo: userAddress,
initialSupply: amount * 10 ** 9,
deployWalletValue: new BigNumber(0.2).shiftedBy(9).toFixed(0),
mintDisabled: false,
burnByRootDisabled: false,
burnPaused: false,
remainingGasTo: auditorAddress,
upgradeable: false,
};
const deployRootMessage = (await client.abi.encode_message_body({
abi: assetFactoryContract.abi,
call_set: {
function_name: 'deployRoot',
input: inputDeployRoot,
},
is_internal: true,
signer: signerNone(),
})).body;
const messageParams = {
send_events: false,
message_encode_params: {
address: auditorAddress,
abi: {
type: 'Contract',
value: EverwalletContract.abi,
},
call_set: {
function_name: 'sendTransaction',
input: {
dest: assetFactoryAddress,
value: amount * 10 ** 9,
bounce: true, // Send funds back on any error in desctination account
flags: 1, // Pay delivery fee from the multisig balance, not from the value.
payload: deployRootMessage,
},
},
signer: {
type: 'Keys',
keys: auditorKeyPair,
},
},
};
const messageSubscription = await client.net.subscribe_collection({
collection: 'messages',
filter: {
src: { eq: assetFactoryAddress },
OR: {
dst: { eq: assetFactoryAddress },
},
},
result: 'boc msg_type id src dst',
}, async (params, responseType) => {
try {
if (responseType === ResponseType.Custom) {
const { boc } = params.result;
const decoded = (await client.abi.decode_message({
abi: assetFactoryContract.abi,
message: boc,
}));
if (decoded.body_type === MessageBodyType.Event) {
console.log(`${tokenName} Asset deployed at ${decoded.value.root}`);
}
}
} catch (err) {}
});
const result = await client.processing.process_message(messageParams);
await client.net.unsubscribe(messageSubscription);
}
async function receiveMsg(msg) {
try {
// eslint-disable-next-line no-unused-vars
const { log, tokenName, tokenSymbol, auditor, user } = JSON.parse(new TextDecoder().decode(msg.data));
if (auditor === auditorAddressConfig) {
const assetsToMint = assetsCalculation(log);
mintAsset(assetsToMint, auditor, tokenName, tokenSymbol, user);
}
} catch (error) {
console.error(error);
return;
}
}
await ipfs.pubsub.subscribe(ipfsTopic, receiveMsg);