-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathbond-token-flow.js
338 lines (291 loc) · 9.97 KB
/
bond-token-flow.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
const { convert } = require("@algo-builder/algob");
const { Runtime, AccountStore } = require("@algo-builder/runtime");
const { types } = require("@algo-builder/web");
const { assert } = require("chai");
const {
optInLsigToBond,
createDex,
minBalance,
initialBalance,
issue,
redeem,
approvalProgram,
clearProgram,
placeholderParam,
} = require("./common/common");
const { buyTxRuntime, issueTx } = require("../scripts/run/common/common");
/**
* Test for the scenario described in Readme.md
*/
describe("Bond token Tests", function () {
let issuerAddress = new AccountStore(minBalance);
const master = new AccountStore(1000e6);
let appManager;
let bondTokenCreator;
let elon;
let bob;
let dex1;
let dex2;
const runtime = new Runtime([master, issuerAddress]);
[appManager, bondTokenCreator, elon, bob, dex1, dex2] = runtime.defaultAccounts();
let appStorageConfig;
let applicationId;
let issuerLsigAddress;
let lsig;
this.beforeAll(function () {
appStorageConfig = {
localInts: 1,
localBytes: 1,
globalInts: 8,
globalBytes: 15,
};
});
const getGlobal = (key) => runtime.getGlobalState(applicationId, key);
// fetch latest account state
function syncAccounts() {
issuerAddress = runtime.getAccount(issuerAddress.address);
[appManager, bondTokenCreator, elon, bob, dex1, dex2] = runtime.defaultAccounts();
}
// Bond-Dapp initialization parameters
const appManagerPk = convert.addressToPk(appManager.address);
const issuePrice = "int:1000";
const couponValue = "int:20";
const maxIssuance = "int:1000000";
const bondCreator = convert.addressToPk(bondTokenCreator.address);
it("Bond token application", function () {
/**
* Issue initial bond tokens to the issuer
* In epoch_0 elon buys 10 bonds
* In epoch 0 elon sells 2 bonds to bob for 2020 ALGO (in a group transaction)
* Manager creates dex 1
* Elon redeems his bonds (8), Elon buys 4 more bonds (so he will have 12 bonds in total)
* Manager creates dex 2
* Elon redeems all his bonds.
* Bob redeems his bonds from epoch 0 and 1
* Maturity period is set to 240 seconds(4 min) after the contract deployment.
* At maturity, manager creates and funds buyback and both elon and
* bob can exit all their tokens (12 and 2 respectively).
*/
const currentBondIndex = runtime.deployASA("bond-token-0", {
creator: { ...bondTokenCreator.account, name: "bond-token-creator" },
}).assetIndex;
const creationFlags = Object.assign({}, appStorageConfig);
const creationArgs = [
appManagerPk,
bondCreator,
issuePrice,
couponValue,
`int:${currentBondIndex}`,
maxIssuance,
];
// create application
applicationId = runtime.deployApp(
appManager.account,
{
...creationFlags,
appName: "bond",
metaType: types.MetaType.SOURCE_CODE,
approvalProgramCode: approvalProgram,
clearProgramCode: clearProgram,
appArgs: creationArgs,
},
{},
placeholderParam
).appID;
// setup lsig account
// Initialize issuer lsig with bond-app ID
const scInitParam = {
TMPL_APPLICATION_ID: applicationId,
TMPL_OWNER: bondTokenCreator.address,
TMPL_APP_MANAGER: appManager.address,
};
lsig = runtime.loadLogic("issuer-lsig.py", scInitParam);
issuerLsigAddress = lsig.address();
// sync escrow account
issuerAddress = runtime.getAccount(issuerLsigAddress);
console.log("Issuer Address: ", issuerLsigAddress);
// fund escrow with some minimum balance first
runtime.fundLsig(master.account, issuerLsigAddress, minBalance + 10000);
// verify global state
assert.isDefined(applicationId);
assert.deepEqual(getGlobal("app_manager"), convert.addressToPk(appManager.address));
assert.deepEqual(getGlobal("issue_price"), 1000n);
assert.deepEqual(getGlobal("coupon_value"), 20n);
assert.deepEqual(getGlobal("epoch"), 0n);
assert.deepEqual(getGlobal("current_bond"), BigInt(currentBondIndex));
// update application with correct issuer account address
const appArgs = ["str:update_issuer_address", convert.addressToPk(issuerLsigAddress)]; // converts algorand address to Uint8Array
const appCallParams = {
type: types.TransactionType.CallApp,
sign: types.SignType.SecretKey,
fromAccount: appManager.account,
appID: applicationId,
payFlags: {},
appArgs: appArgs,
};
runtime.executeTx([appCallParams]);
// verify issuer address
assert.isDefined(applicationId);
assert.deepEqual(getGlobal("issuer_address"), convert.addressToPk(issuerLsigAddress));
// opt-in to app
runtime.optInToApp(appManager.address, applicationId, {}, {});
runtime.optInToApp(issuerAddress.address, applicationId, {}, {});
syncAccounts();
assert.isDefined(appManager.appsLocalState.get(applicationId));
assert.isDefined(issuerAddress.appsLocalState.get(applicationId));
optInLsigToBond(runtime, lsig, currentBondIndex, appManager);
// Issue tokens to issuer from bond token creator
let groupTx = issueTx(bondTokenCreator.account, lsig, applicationId, currentBondIndex);
runtime.executeTx(groupTx);
syncAccounts();
assert.equal(issuerAddress.getAssetHolding(currentBondIndex)?.amount, 1000000n);
// at epoch_0 elon buys 10 bonds
runtime.optInToASA(currentBondIndex, elon.address, {});
runtime.optInToApp(elon.address, applicationId, {}, {});
let amount = 10;
let algoAmount = amount * issue;
groupTx = buyTxRuntime(
runtime,
elon.account,
lsig,
algoAmount,
applicationId,
currentBondIndex
);
runtime.executeTx(groupTx);
syncAccounts();
assert.equal(elon.getAssetHolding(currentBondIndex)?.amount, 10n);
runtime.optInToASA(currentBondIndex, bob.address, {});
// elon sells 2 bonds to bob for 2020 Algo
const sellTx = [
{
type: types.TransactionType.TransferAlgo,
sign: types.SignType.SecretKey,
fromAccount: bob.account,
toAccountAddr: bob.address,
amountMicroAlgos: 2020,
payFlags: { totalFee: 1000 },
},
{
type: types.TransactionType.TransferAsset,
sign: types.SignType.SecretKey,
fromAccount: elon.account,
toAccountAddr: bob.address,
amount: 2,
assetID: currentBondIndex,
payFlags: { totalFee: 1000 },
},
];
runtime.executeTx(sellTx);
syncAccounts();
assert.equal(elon.getAssetHolding(currentBondIndex)?.amount, 8n);
assert.equal(bob.getAssetHolding(currentBondIndex)?.amount, 2n);
// manager starts epoch 1 (create dex)
const dexLsig1 = createDex(runtime, bondTokenCreator, appManager, 1, master, lsig);
syncAccounts();
// sync dex account
dex1 = runtime.getAccount(dexLsig1.address());
console.log("Dex 1 Address: ", dexLsig1.address());
// elon redeems his 8 bonds
redeem(runtime, elon, 1, 8, dexLsig1);
amount = 4;
algoAmount = amount * issue;
const bond1 = runtime.getAssetInfoFromName("bond-token-1").assetIndex;
// elon buys 4 more bonds
groupTx = buyTxRuntime(runtime, elon.account, lsig, algoAmount, applicationId, bond1);
runtime.executeTx(groupTx);
syncAccounts();
assert.equal(elon.getAssetHolding(bond1)?.amount, 12n);
// manager starts epoch 2 (create dex)
const dexLsig2 = createDex(runtime, bondTokenCreator, appManager, 2, master, lsig);
syncAccounts();
// sync dex account
dex2 = runtime.getAccount(dexLsig2.address());
console.log("Dex 2 Address: ", dexLsig2.address());
// elon redeems his 12 bonds
redeem(runtime, elon, 2, 12, dexLsig2);
// bob redeems bond_1
redeem(runtime, bob, 1, 2, dexLsig1);
syncAccounts();
// bob redeems bond_2
redeem(runtime, bob, 2, 2, dexLsig2);
const bond2 = runtime.getAssetInfoFromName("bond-token-2").assetIndex;
// create buyback
const scParam = {
TMPL_APPLICATION_ID: applicationId,
TMPL_APP_MANAGER: appManager.address,
TMPL_BOND: bond2,
};
const buybackLsig = runtime.loadLogic("buyback-lsig.py", scParam);
// fund dex with some minimum balance first
runtime.fundLsig(master.account, buybackLsig.address(), minBalance + 10000);
const buybackTx = [
{
type: types.TransactionType.CallApp,
sign: types.SignType.SecretKey,
fromAccount: appManager.account,
appID: applicationId,
payFlags: {},
appArgs: ["str:set_buyback", convert.addressToPk(buybackLsig.address())],
},
];
optInLsigToBond(runtime, buybackLsig, bond2, appManager);
runtime.executeTx(buybackTx);
runtime.setRoundAndTimestamp(3, Math.round(new Date().getTime() / 1000) + 250);
syncAccounts();
// Algo balance before exit
const beforeExitElon = elon.balance();
const beforeExitBob = bob.balance();
const exitBond = 12;
const nominalPrice = 1000;
const exitAmount = Number(exitBond) * Number(nominalPrice);
// Exit tokens from elon
const exitTx = [
// Bond token transfer to buyback address
{
type: types.TransactionType.TransferAsset,
sign: types.SignType.SecretKey,
fromAccount: elon.account,
toAccountAddr: buybackLsig.address(),
amount: exitBond,
assetID: bond2,
payFlags: { totalFee: 2000 },
},
// Nominal price * amount paid to buyer
{
type: types.TransactionType.TransferAlgo,
sign: types.SignType.LogicSignature,
fromAccountAddr: buybackLsig.address(),
lsig: buybackLsig,
toAccountAddr: elon.address,
amountMicroAlgos: exitAmount,
payFlags: { totalFee: 0 },
},
// call to bond-dapp
{
type: types.TransactionType.CallApp,
sign: types.SignType.SecretKey,
fromAccount: elon.account,
appID: applicationId,
payFlags: { totalFee: 1000 },
appArgs: ["str:exit"],
},
];
runtime.executeTx(exitTx);
// Exit tokens from bob
exitTx[0].fromAccount = bob.account;
exitTx[0].amount = 2;
exitTx[1].toAccountAddr = bob.address;
exitTx[1].amountMicroAlgos = 2000;
exitTx[2].fromAccount = bob.account;
runtime.executeTx(exitTx);
syncAccounts();
const feePaid = 3000n;
assert.equal(bob.getAssetHolding(bond1)?.amount, 0n);
assert.equal(bob.getAssetHolding(bond2)?.amount, 0n);
assert.equal(elon.getAssetHolding(bond1)?.amount, 0n);
assert.equal(elon.getAssetHolding(bond2)?.amount, 0n);
assert.equal(beforeExitElon + 12000n - feePaid, elon.balance());
assert.equal(beforeExitBob + 2000n - feePaid, bob.balance());
});
});