-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
162 lines (134 loc) · 4.53 KB
/
index.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import Transaction from 'arweave/node/lib/transaction';
import config from './config';
const Arweave = require('arweave');
const fs = require('fs');
export interface config {
emission_period: number;
time_interval: number;
initial_emit_amount: number;
decay_const: number;
token_contract_id: string;
token_allocations: string[] | { address: string; weight: number }[];
}
interface status {
time_init: number;
balance: number;
distributions: { run: number; time: number; expend: number; transactions: string[] }[];
}
const keyfile = JSON.parse(process.env.KEYFILE);
/**
* math Σ function
*/
const sigma = (start: number, end: number, exp: (x: number) => number) => {
let result = 0;
for (let n = start; n <= end; ++n) result += exp(n);
return result;
};
/**
* round to the nearest interval
*/
const floorTo = (num: number, int: number) => Math.floor(num / int / 1000) * int;
/**
* set of distribution curves
*/
const dist = {
linear: (x: number) => Math.floor(config.initial_emit_amount - (x * config.time_interval * config.initial_emit_amount) / config.emission_period),
exponential: (x: number) => Math.floor(config.initial_emit_amount * Math.E ** (-config.decay_const * x * config.time_interval)),
};
const dist_curve: string = isNaN(config.decay_const) ? 'linear' : 'exponential';
const dist_total: number = sigma(0, config.emission_period / config.time_interval, dist[dist_curve]);
console.log({ config: { dist_curve, dist_total, ...config } });
// save init time & balance on first run
if (!fs.existsSync('status.json')) {
const init_status: status = {
time_init: Date.now(),
balance: dist_total,
distributions: [],
};
fs.writeFileSync('status.json', JSON.stringify(init_status, null, 2));
}
let status: status = JSON.parse(fs.readFileSync('status.json').toString());
console.log('previous status:', status);
// initialise arweave
const arweave = Arweave.init({
host: 'arweave.net',
port: 443,
protocol: 'https',
});
/**
* Generate all transactions necessary to emit.
*/
async function primeCannon(amount: number, addresses: any, time: number) {
let weightTotal = 0;
if (addresses[0]?.weight) {
// There is a weight variable added, so calculate total weight
for (let i = 0; i < addresses.length; i++) {
weightTotal += addresses[i]?.weight;
}
}
let allTransactions = [];
for (let i = 0; i < addresses.length; i++) {
const tags = {
Cannon: 'PST',
Function: dist_curve,
Completion: (time / config.emission_period) * 100,
Contract: config.token_contract_id,
'App-Name': 'SmartWeaveAction',
'App-Version': '0.3.0',
Input: JSON.stringify({
function: 'transfer',
target: addresses[i].address ?? addresses[i],
qty: addresses[i].weight ? Math.floor((amount * addresses[i].weight) / weightTotal) : Math.floor(amount / addresses.length),
}),
};
const tx: Transaction = await arweave.createTransaction(
{
target: addresses[i].address ?? addresses[i],
data: Math.random().toString().slice(-4),
},
keyfile
);
for (const [key, value] of Object.entries(tags)) {
tx.addTag(key, value.toString());
}
await arweave.transactions.sign(tx, keyfile);
allTransactions.push(tx);
}
return allTransactions;
}
/**
* Send all of the transactions to the corresponding addresses.
*/
async function emit(transactions: any) {
let txIDs = [];
for (let i = 0; i < transactions.length; i++) {
await arweave.transactions.post(transactions[i]);
txIDs.push(transactions[i].id);
}
return txIDs;
}
// start distribution
(async () => {
const time = floorTo(Date.now() - status.time_init, config.time_interval);
// get the number of token to distribute
const expend = dist[dist_curve](time / config.time_interval);
// create a transaction if conditions meet
if (time <= config.emission_period && expend > 0 && status.balance > 0) {
console.log({ time, expend, balance: status.balance });
// create transactions to send
let transactions = await primeCannon(expend, config.token_allocations, time);
// send the transactions
let sentTransactions = await emit(transactions);
status.distributions.push({
run: status.distributions.length + 1,
time,
expend,
transactions: sentTransactions,
});
status.balance -= expend;
fs.writeFileSync('status.json', JSON.stringify(status, null, 2));
console.log('Current Status:', status);
} else {
console.log('Unmet Conditions', { time, expend, balance: status.balance });
}
})();