algosdk 1.24.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/README.md +4 -4
  2. package/dist/browser/algosdk.min.js +1 -1
  3. package/dist/browser/algosdk.min.js.LICENSE.txt +0 -18
  4. package/dist/browser/algosdk.min.js.map +1 -1
  5. package/dist/cjs/client/v2/algod/algod.js +1 -1
  6. package/dist/cjs/client/v2/algod/algod.js.map +1 -1
  7. package/dist/cjs/client/v2/algod/models/types.js +161 -123
  8. package/dist/cjs/client/v2/algod/models/types.js.map +1 -1
  9. package/dist/cjs/client/v2/indexer/lookupAssetTransactions.js +0 -7
  10. package/dist/cjs/client/v2/indexer/lookupAssetTransactions.js.map +1 -1
  11. package/dist/cjs/dryrun.js +16 -7
  12. package/dist/cjs/dryrun.js.map +1 -1
  13. package/dist/cjs/logicsig.js +3 -14
  14. package/dist/cjs/logicsig.js.map +1 -1
  15. package/dist/cjs/main.js +3 -6
  16. package/dist/cjs/main.js.map +1 -1
  17. package/dist/cjs/makeTxn.js +1 -193
  18. package/dist/cjs/makeTxn.js.map +1 -1
  19. package/dist/esm/client/v2/algod/algod.js +1 -1
  20. package/dist/esm/client/v2/algod/algod.js.map +1 -1
  21. package/dist/esm/client/v2/algod/models/types.js +160 -120
  22. package/dist/esm/client/v2/algod/models/types.js.map +1 -1
  23. package/dist/esm/client/v2/indexer/lookupAssetTransactions.js +0 -7
  24. package/dist/esm/client/v2/indexer/lookupAssetTransactions.js.map +1 -1
  25. package/dist/esm/dryrun.js +16 -7
  26. package/dist/esm/dryrun.js.map +1 -1
  27. package/dist/esm/logicsig.js +2 -12
  28. package/dist/esm/logicsig.js.map +1 -1
  29. package/dist/esm/main.js +1 -4
  30. package/dist/esm/main.js.map +1 -1
  31. package/dist/esm/makeTxn.js +0 -185
  32. package/dist/esm/makeTxn.js.map +1 -1
  33. package/dist/types/client/v2/algod/algod.d.ts +1 -1
  34. package/dist/types/client/v2/algod/models/types.d.ts +122 -67
  35. package/dist/types/client/v2/indexer/lookupAssetTransactions.d.ts +0 -4
  36. package/dist/types/logicsig.d.ts +2 -10
  37. package/dist/types/main.d.ts +1 -3
  38. package/dist/types/makeTxn.d.ts +1 -147
  39. package/dist/types/types/transactions/encoded.d.ts +0 -15
  40. package/package.json +1 -1
  41. package/src/client/v2/algod/algod.ts +2 -2
  42. package/src/client/v2/algod/models/types.ts +318 -249
  43. package/src/client/v2/indexer/lookupAssetTransactions.ts +0 -8
  44. package/src/dryrun.ts +13 -13
  45. package/src/logicsig.ts +2 -13
  46. package/src/main.ts +1 -5
  47. package/src/makeTxn.ts +0 -421
  48. package/src/types/transactions/encoded.ts +0 -17
  49. package/dist/cjs/client/algod.js +0 -295
  50. package/dist/cjs/client/algod.js.map +0 -1
  51. package/dist/cjs/logic/langspec.json +0 -2014
  52. package/dist/cjs/logic/logic.js +0 -262
  53. package/dist/cjs/logic/logic.js.map +0 -1
  54. package/dist/cjs/logicTemplates/dynamicfee.js +0 -192
  55. package/dist/cjs/logicTemplates/dynamicfee.js.map +0 -1
  56. package/dist/cjs/logicTemplates/htlc.js +0 -145
  57. package/dist/cjs/logicTemplates/htlc.js.map +0 -1
  58. package/dist/cjs/logicTemplates/index.js +0 -19
  59. package/dist/cjs/logicTemplates/index.js.map +0 -1
  60. package/dist/cjs/logicTemplates/limitorder.js +0 -167
  61. package/dist/cjs/logicTemplates/limitorder.js.map +0 -1
  62. package/dist/cjs/logicTemplates/periodicpayment.js +0 -163
  63. package/dist/cjs/logicTemplates/periodicpayment.js.map +0 -1
  64. package/dist/cjs/logicTemplates/split.js +0 -154
  65. package/dist/cjs/logicTemplates/split.js.map +0 -1
  66. package/dist/cjs/logicTemplates/templates.js +0 -67
  67. package/dist/cjs/logicTemplates/templates.js.map +0 -1
  68. package/dist/esm/client/algod.js +0 -295
  69. package/dist/esm/client/algod.js.map +0 -1
  70. package/dist/esm/logic/langspec.json +0 -2014
  71. package/dist/esm/logic/logic.js +0 -249
  72. package/dist/esm/logic/logic.js.map +0 -1
  73. package/dist/esm/logicTemplates/dynamicfee.js +0 -192
  74. package/dist/esm/logicTemplates/dynamicfee.js.map +0 -1
  75. package/dist/esm/logicTemplates/htlc.js +0 -145
  76. package/dist/esm/logicTemplates/htlc.js.map +0 -1
  77. package/dist/esm/logicTemplates/index.js +0 -19
  78. package/dist/esm/logicTemplates/index.js.map +0 -1
  79. package/dist/esm/logicTemplates/limitorder.js +0 -167
  80. package/dist/esm/logicTemplates/limitorder.js.map +0 -1
  81. package/dist/esm/logicTemplates/periodicpayment.js +0 -163
  82. package/dist/esm/logicTemplates/periodicpayment.js.map +0 -1
  83. package/dist/esm/logicTemplates/split.js +0 -154
  84. package/dist/esm/logicTemplates/split.js.map +0 -1
  85. package/dist/esm/logicTemplates/templates.js +0 -67
  86. package/dist/esm/logicTemplates/templates.js.map +0 -1
  87. package/dist/types/client/algod.d.ts +0 -162
  88. package/dist/types/logic/logic.d.ts +0 -45
  89. package/dist/types/logicTemplates/dynamicfee.d.ts +0 -59
  90. package/dist/types/logicTemplates/htlc.d.ts +0 -51
  91. package/dist/types/logicTemplates/index.d.ts +0 -16
  92. package/dist/types/logicTemplates/limitorder.d.ts +0 -70
  93. package/dist/types/logicTemplates/periodicpayment.d.ts +0 -58
  94. package/dist/types/logicTemplates/split.d.ts +0 -57
  95. package/dist/types/logicTemplates/templates.d.ts +0 -6
  96. package/src/client/algod.js +0 -365
  97. package/src/logic/langspec.json +0 -2014
  98. package/src/logic/logic.ts +0 -316
  99. package/src/logicTemplates/dynamicfee.js +0 -219
  100. package/src/logicTemplates/htlc.js +0 -165
  101. package/src/logicTemplates/index.js +0 -20
  102. package/src/logicTemplates/limitorder.js +0 -227
  103. package/src/logicTemplates/periodicpayment.js +0 -208
  104. package/src/logicTemplates/split.js +0 -208
  105. package/src/logicTemplates/templates.js +0 -78
@@ -1,20 +0,0 @@
1
- const dynamicFeeTemplate = require('./dynamicfee');
2
- const htlcTemplate = require('./htlc');
3
- const limitOrderTemplate = require('./limitorder');
4
- const splitTemplate = require('./split');
5
- const periodicPayTemplate = require('./periodicpayment');
6
-
7
- module.exports = {
8
- DynamicFee: dynamicFeeTemplate.DynamicFee,
9
- getDynamicFeeTransactions: dynamicFeeTemplate.getDynamicFeeTransactions,
10
- signDynamicFee: dynamicFeeTemplate.signDynamicFee,
11
- HTLC: htlcTemplate.HTLC,
12
- signTransactionWithHTLCUnlock: htlcTemplate.signTransactionWithHTLCUnlock,
13
- LimitOrder: limitOrderTemplate.LimitOrder,
14
- getSwapAssetsTransaction: limitOrderTemplate.getSwapAssetsTransaction,
15
- Split: splitTemplate.Split,
16
- getSplitFundsTransaction: splitTemplate.getSplitFundsTransaction,
17
- PeriodicPayment: periodicPayTemplate.PeriodicPayment,
18
- getPeriodicPaymentWithdrawalTransaction:
19
- periodicPayTemplate.getPeriodicPaymentWithdrawalTransaction,
20
- };
@@ -1,227 +0,0 @@
1
- const address = require('../encoding/address');
2
- const makeTxn = require('../makeTxn');
3
- const group = require('../group');
4
- const logic = require('../logic/logic');
5
- const logicSig = require('../logicsig');
6
- const nacl = require('../nacl/naclWrappers');
7
- const templates = require('./templates');
8
- const utils = require('../utils/utils');
9
-
10
- class LimitOrder {
11
- /**
12
- * MakeLimitOrder allows a user to exchange some number of assets for some number of algos.
13
- * Fund the contract with some number of Algos to limit the maximum number of
14
- * Algos you're willing to trade for some other asset.
15
- *
16
- * Works on two cases:
17
- * * trading Algos for some other asset
18
- * * closing out Algos back to the originator after a timeout
19
- *
20
- * trade case, a 2 transaction group:
21
- * gtxn[0] (this txn) Algos from Me to Other
22
- * gtxn[1] asset from Other to Me
23
- *
24
- * We want to get _at least_ some amount of the other asset per our Algos
25
- * gtxn[1].AssetAmount / gtxn[0].Amount >= N / D
26
- * ===
27
- * gtxn[1].AssetAmount * D >= gtxn[0].Amount * N
28
- *
29
- * close-out case:
30
- * txn alone, close out value after timeout
31
- *
32
- * @deprecated This class will be removed in v2.
33
- *
34
- * Constructor Parameters:
35
- * @param {string} owner: the address to refund funds to on timeout
36
- * @param {int} assetid: the ID of the transferred asset
37
- * @param {int} ratn: exchange rate (N asset per D Algos, or better)
38
- * @param {int} ratd: exchange rate (N asset per D Algos, or better)
39
- * @param {int} expiryRound: the round at which the account expires
40
- * @param {int} minTrade: the minimum amount (of Algos) to be traded away
41
- * @param {int} maxFee: maximum fee used by the limit order transaction
42
- * @returns {LimitOrder}
43
- */
44
- constructor(owner, assetid, ratn, ratd, expiryRound, minTrade, maxFee) {
45
- // don't need to validate owner - it will be validated by template.insert
46
- if (!Number.isSafeInteger(assetid) || assetid < 0)
47
- throw Error('assetid must be a positive number and smaller than 2^53-1');
48
- if (!Number.isSafeInteger(ratn) || ratn < 0)
49
- throw Error('ratn must be a positive number and smaller than 2^53-1');
50
- if (!Number.isSafeInteger(ratd) || ratd < 0)
51
- throw Error('ratd must be a positive number and smaller than 2^53-1');
52
- if (!Number.isSafeInteger(expiryRound) || expiryRound < 0)
53
- throw Error(
54
- 'expiryRound must be a positive number and smaller than 2^53-1'
55
- );
56
- if (!Number.isSafeInteger(minTrade) || minTrade < 0)
57
- throw Error('minTrade must be a positive number and smaller than 2^53-1');
58
- if (!Number.isSafeInteger(maxFee) || maxFee < 0)
59
- throw Error('maxFee must be a positive number and smaller than 2^53-1');
60
-
61
- const referenceProgramB64 =
62
- 'ASAKAAEFAgYEBwgJCiYBIP68oLsUSlpOp7Q4pGgayA5soQW8tgf8VlMlyVaV9qITMRYiEjEQIxIQMQEkDhAyBCMSQABVMgQlEjEIIQQNEDEJMgMSEDMBECEFEhAzAREhBhIQMwEUKBIQMwETMgMSEDMBEiEHHTUCNQExCCEIHTUENQM0ATQDDUAAJDQBNAMSNAI0BA8QQAAWADEJKBIxAiEJDRAxBzIDEhAxCCISEBA=';
63
- const referenceProgramBytes = Buffer.from(referenceProgramB64, 'base64');
64
- const referenceOffsets = [
65
- 5 /* minTrade */,
66
- 7 /* assetID */,
67
- 9 /* ratd */,
68
- 10 /* ratn */,
69
- 11 /* expiryRound */,
70
- 12 /* owner */,
71
- 16,
72
- ];
73
- const injectionVector = [
74
- maxFee,
75
- minTrade,
76
- assetid,
77
- ratd,
78
- ratn,
79
- expiryRound,
80
- owner,
81
- ];
82
- const injectionTypes = [
83
- templates.valTypes.INT,
84
- templates.valTypes.INT,
85
- templates.valTypes.INT,
86
- templates.valTypes.INT,
87
- templates.valTypes.INT,
88
- templates.valTypes.INT,
89
- templates.valTypes.ADDRESS,
90
- ];
91
- const injectedBytes = templates.inject(
92
- referenceProgramBytes,
93
- referenceOffsets,
94
- injectionVector,
95
- injectionTypes
96
- );
97
- this.programBytes = injectedBytes;
98
- const lsig = new logicSig.LogicSig(injectedBytes, undefined);
99
- this.address = lsig.address();
100
- this.owner = owner;
101
- this.assetid = assetid;
102
- }
103
-
104
- /**
105
- * returns the program bytes
106
- * @returns {Uint8Array}
107
- */
108
- getProgram() {
109
- return this.programBytes;
110
- }
111
-
112
- /**
113
- * returns the string address of the contract
114
- * @returns {string}
115
- */
116
- getAddress() {
117
- return this.address;
118
- }
119
- }
120
-
121
- /**
122
- * returns a group transactions array which transfer funds according to the contract's ratio
123
- *
124
- * @deprecated This feature will be removed in v2.
125
- *
126
- * @param {Uint8Array} contract: byteform of the contract from the payer
127
- * @param {int} assetAmount: the amount of assets to be sent
128
- * @param {int} microAlgoAmount: number of microAlgos to transfer
129
- * @param {Uint8Array} secretKey: secret key for signing transaction
130
- * @param {int} fee: the fee per byte to pay in microAlgos
131
- * @param {int} firstRound: the first round on which these txns will be valid
132
- * @param {int} lastRound: the last round on which these txns will be valid
133
- * @param {string} genesisHash: the b64-encoded genesis hash indicating the network for this transaction
134
- * @returns {Uint8Array}
135
- * the first payment sends money (Algos) from contract to the recipient (we'll call him Buyer), closing the rest of the account to Owner
136
- * the second payment sends money (the asset) from Buyer to the Owner
137
- * these transactions will be rejected if they do not meet the restrictions set by the contract
138
- * @throws error if arguments fail contract validation
139
- */
140
- function getSwapAssetsTransaction(
141
- contract,
142
- assetAmount,
143
- microAlgoAmount,
144
- secretKey,
145
- fee,
146
- firstRound,
147
- lastRound,
148
- genesisHash
149
- ) {
150
- const buyerKeyPair = nacl.keyPairFromSecretKey(secretKey);
151
- const buyerAddr = address.encodeAddress(buyerKeyPair.publicKey);
152
- const programOutputs = logic.readProgram(contract, undefined);
153
- const ints = programOutputs[0];
154
- const byteArrays = programOutputs[1];
155
-
156
- let noCloseRemainder;
157
- let noAssetRevocationTarget;
158
- const contractAssetID = ints[6];
159
- const contractOwner = address.encodeAddress(byteArrays[0]);
160
- const lsig = logicSig.makeLogicSig(contract, undefined);
161
- const contractAddress = lsig.address();
162
- const algosForAssets = makeTxn.makePaymentTxn(
163
- contractAddress,
164
- buyerAddr,
165
- fee,
166
- microAlgoAmount,
167
- noCloseRemainder,
168
- firstRound,
169
- lastRound,
170
- undefined,
171
- genesisHash,
172
- undefined
173
- );
174
- const assetsForAlgos = makeTxn.makeAssetTransferTxn(
175
- buyerAddr,
176
- contractOwner,
177
- noCloseRemainder,
178
- noAssetRevocationTarget,
179
- fee,
180
- assetAmount,
181
- firstRound,
182
- lastRound,
183
- undefined,
184
- genesisHash,
185
- undefined,
186
- contractAssetID
187
- );
188
- const txns = [algosForAssets, assetsForAlgos];
189
- const txGroup = group.assignGroupID(txns);
190
-
191
- const ratd = ints[7];
192
- const ratn = ints[8];
193
- if (assetAmount * ratd < microAlgoAmount * ratn) {
194
- throw new Error(
195
- `bad payment ratio, ${assetAmount.toString()}*${ratd.toString()} !>= ${microAlgoAmount.toString()}*${ratn.toString()}`
196
- );
197
- }
198
- const minTrade = ints[4];
199
- if (microAlgoAmount < minTrade) {
200
- throw new Error(
201
- `payment amount ${microAlgoAmount.toString()} less than minimum trade ${minTrade.toString()}`
202
- );
203
- }
204
- const maxFee = ints[2];
205
- if (txGroup[0].fee > maxFee) {
206
- throw new Error(
207
- `final fee of payment transaction ${txGroup[0].fee.toString()} greater than transaction max fee ${maxFee.toString()}`
208
- );
209
- }
210
- if (txGroup[1].fee > maxFee) {
211
- throw new Error(
212
- `final fee of asset transaction ${txGroup[1].fee.toString()} greater than transaction max fee ${maxFee.toString()}`
213
- );
214
- }
215
-
216
- const algosForAssetsSigned = logicSig.signLogicSigTransactionObject(
217
- txGroup[0],
218
- lsig
219
- );
220
- const assetsForAlgosSigned = txGroup[1].signTxn(secretKey);
221
- return utils.concatArrays(algosForAssetsSigned.blob, assetsForAlgosSigned);
222
- }
223
-
224
- module.exports = {
225
- LimitOrder,
226
- getSwapAssetsTransaction,
227
- };
@@ -1,208 +0,0 @@
1
- const address = require('../encoding/address');
2
- const makeTxn = require('../makeTxn');
3
- const logic = require('../logic/logic');
4
- const logicSig = require('../logicsig');
5
- const nacl = require('../nacl/naclWrappers');
6
- const templates = require('./templates');
7
-
8
- class PeriodicPayment {
9
- /**
10
- * MakePeriodicPayment allows some account to execute periodic withdrawal of funds.
11
- * This is a contract account.
12
- *
13
- * This allows receiver to withdraw amount every
14
- * period rounds for withdrawWindow after every multiple
15
- * of period.
16
- *
17
- * After expiryRound, all remaining funds in the escrow
18
- * are available to receiver.
19
- *
20
- * @deprecated This class will be removed in v2.
21
- *
22
- * Constructor Parameters:
23
- * @param {string} receiver: address which is authorized to receive withdrawals
24
- * @param {int} amount: the amount to send each period
25
- * @param {int} withdrawalWindow: the duration of a withdrawal period
26
- * @param {int} period: the time between a pair of withdrawal periods
27
- * @param {int} expiryRound: the round at which the account expires
28
- * @param {int} maxFee: maximum fee used by the withdrawal transaction
29
- * @param {string} lease: b64 representation of lease to use, or leave undefined to generate one
30
- * @returns {PeriodicPayment}
31
- */
32
- constructor(
33
- receiver,
34
- amount,
35
- withdrawalWindow,
36
- period,
37
- expiryRound,
38
- maxFee,
39
- lease
40
- ) {
41
- // don't need to validate receiver or lease, it's validated by template insert
42
- this.receiver = receiver;
43
- if (!Number.isSafeInteger(amount) || amount < 0)
44
- throw Error('amount must be a positive number and smaller than 2^53-1');
45
- this.amount = amount;
46
- if (!Number.isSafeInteger(withdrawalWindow) || withdrawalWindow < 0)
47
- throw Error(
48
- 'withdrawalWindow must be a positive number and smaller than 2^53-1'
49
- );
50
- this.withdrawalWindow = withdrawalWindow;
51
- if (!Number.isSafeInteger(period) || period < 0)
52
- throw Error('period must be a positive number and smaller than 2^53-1');
53
- this.period = period;
54
- if (!Number.isSafeInteger(expiryRound) || expiryRound < 0)
55
- throw Error(
56
- 'expiryRound must be a positive number and smaller than 2^53-1'
57
- );
58
- this.expiryRound = expiryRound;
59
- if (!Number.isSafeInteger(maxFee) || maxFee < 0)
60
- throw Error('maxFee must be a positive number and smaller than 2^53-1');
61
- this.maxFee = maxFee;
62
-
63
- if (lease === undefined) {
64
- const leaseBytes = nacl.randomBytes(32);
65
- this.lease = Buffer.from(leaseBytes).toString('base64');
66
- } else {
67
- this.lease = lease;
68
- }
69
-
70
- this.programBytes = this.getProgram();
71
- const lsig = new logicSig.LogicSig(this.programBytes, undefined);
72
- this.address = lsig.address();
73
- }
74
-
75
- /**
76
- * returns the program bytes
77
- * @returns {Uint8Array}
78
- */
79
- getProgram() {
80
- const referenceProgramB64 =
81
- 'ASAHAQYFAAQDByYCIAECAwQFBgcIAQIDBAUGBwgBAgMEBQYHCAECAwQFBgcIIJKvkYTkEzwJf2arzJOxERsSogG9nQzKPkpIoc4TzPTFMRAiEjEBIw4QMQIkGCUSEDEEIQQxAggSEDEGKBIQMQkyAxIxBykSEDEIIQUSEDEJKRIxBzIDEhAxAiEGDRAxCCUSEBEQ';
82
- const referenceProgramBytes = Buffer.from(referenceProgramB64, 'base64');
83
- const referenceOffsets = [
84
- 4 /* period */,
85
- 5 /* withdrawWindow */,
86
- 7 /* amount */,
87
- 8 /* expiryRound */,
88
- 9 /* lease */,
89
- 12 /* receiver */,
90
- 46,
91
- ];
92
- const injectionVector = [
93
- this.maxFee,
94
- this.period,
95
- this.withdrawalWindow,
96
- this.amount,
97
- this.expiryRound,
98
- this.lease,
99
- this.receiver,
100
- ];
101
- const injectionTypes = [
102
- templates.valTypes.INT,
103
- templates.valTypes.INT,
104
- templates.valTypes.INT,
105
- templates.valTypes.INT,
106
- templates.valTypes.INT,
107
- templates.valTypes.BASE64,
108
- templates.valTypes.ADDRESS,
109
- ];
110
- return templates.inject(
111
- referenceProgramBytes,
112
- referenceOffsets,
113
- injectionVector,
114
- injectionTypes
115
- );
116
- }
117
-
118
- /**
119
- * returns the string address of the contract
120
- * @returns {string}
121
- */
122
- getAddress() {
123
- return this.address;
124
- }
125
- }
126
-
127
- /**
128
- * getPeriodicPaymentWithdrawalTransaction returns a signed transaction extracting funds form the contract
129
- *
130
- * @deprecated This feature will be removed in v2.
131
- *
132
- * @param {Uint8Array} contract: the bytearray defining the contract, received from the payer
133
- * @param {int} fee: the fee per byte for the transaction
134
- * @param {int} firstValid: the first round on which the txn will be valid
135
- * @param {string} genesisHash: the hash representing the network for the txn
136
- * @returns {Object} Object containing txID and blob representing signed transaction
137
- * @throws error on failure
138
- */
139
- function getPeriodicPaymentWithdrawalTransaction(
140
- contract,
141
- fee,
142
- firstValid,
143
- genesisHash
144
- ) {
145
- const readResult = logic.readProgram(contract, undefined);
146
- const ints = readResult[0];
147
- const byteArrays = readResult[1];
148
- const period = ints[2];
149
- const duration = ints[4];
150
- const amount = ints[5];
151
- if (firstValid % period !== 0) {
152
- throw new Error(
153
- `firstValid round ${firstValid.toString()} was not a multiple of contract period ${period.toString()}`
154
- );
155
- }
156
-
157
- // extract receiver and convert as needed
158
- const receiverBytes = byteArrays[1];
159
- const receiver = address.encodeAddress(receiverBytes);
160
- // extract lease and convert
161
- const leaseBuffer = byteArrays[0];
162
- const lease = new Uint8Array(leaseBuffer);
163
- const lastValid = firstValid + duration;
164
- const to = receiver;
165
- let noCloseRemainder;
166
- let noNote;
167
- const lsig = logicSig.makeLogicSig(contract, undefined);
168
- const from = lsig.address();
169
- const txn = {
170
- from,
171
- to,
172
- fee,
173
- amount,
174
- closeRemainderTo: noCloseRemainder,
175
- firstRound: firstValid,
176
- lastRound: lastValid,
177
- note: noNote,
178
- genesisHash,
179
- genesisID: '',
180
- type: 'pay',
181
- lease,
182
- };
183
-
184
- // check fee
185
- const tempTxn = makeTxn.makePaymentTxn(
186
- from,
187
- to,
188
- fee,
189
- amount,
190
- noCloseRemainder,
191
- firstValid,
192
- lastValid,
193
- noNote,
194
- genesisHash,
195
- ''
196
- );
197
- if (tempTxn.fee > ints[1]) {
198
- throw new Error(
199
- `final fee of payment transaction${tempTxn.fee.toString()}greater than transaction max fee${ints[1].toString()}`
200
- );
201
- }
202
-
203
- return logicSig.signLogicSigTransaction(txn, lsig);
204
- }
205
- module.exports = {
206
- PeriodicPayment,
207
- getPeriodicPaymentWithdrawalTransaction,
208
- };
@@ -1,208 +0,0 @@
1
- const address = require('../encoding/address');
2
- const makeTxn = require('../makeTxn');
3
- const group = require('../group');
4
- const logicsig = require('../logicsig');
5
- const logic = require('../logic/logic');
6
- const templates = require('./templates');
7
- const utils = require('../utils/utils');
8
-
9
- class Split {
10
- /**
11
- * Split splits money sent to some account to two recipients at some ratio.
12
- * This is a contract account.
13
- *
14
- * This allows either a two-transaction group, for executing a
15
- * split, or single transaction, for closing the account.
16
- *
17
- * Withdrawals from this account are allowed as a group transaction which
18
- * sends receiverOne and receiverTwo amounts with exactly the specified ratio:
19
- * (rat1*amountForReceiverOne) = (rat2*amountForReceiverTwo)
20
- * At least minPay must be sent to receiverOne.
21
- * (CloseRemainderTo must be zero.)
22
- *
23
- * After expiryRound passes, all funds can be refunded to owner.
24
- *
25
- * @deprecated This feature will be removed in v2.
26
- *
27
- * Constructor Parameters:
28
- * @param {string} owner: the address to refund funds to on timeout
29
- * @param {string} receiverOne: the first recipient in the split account
30
- * @param {string} receiverTwo: the second recipient in the split account
31
- * @param {int} rat1: fraction of money to be paid to the 1st recipient
32
- * @param {int} rat2: fraction of money to be paid to the 2nd recipient
33
- * @param {int} expiryRound: the round at which the account expires
34
- * @param {int} minPay: minimum amount to be paid out of the account
35
- * @param {int} maxFee: half of the maximum fee used by each split forwarding group transaction
36
- * @returns {Split}
37
- */
38
- constructor(
39
- owner,
40
- receiverOne,
41
- receiverTwo,
42
- rat1,
43
- rat2,
44
- expiryRound,
45
- minPay,
46
- maxFee
47
- ) {
48
- // don't need to validate owner, receiverone, receivertwo - they'll be validated by template.insert
49
- if (!Number.isSafeInteger(rat2) || rat2 < 0)
50
- throw Error('rat2 must be a positive number and smaller than 2^53-1');
51
- if (!Number.isSafeInteger(rat1) || rat1 < 0)
52
- throw Error('rat1 must be a positive number and smaller than 2^53-1');
53
- if (!Number.isSafeInteger(expiryRound) || expiryRound < 0)
54
- throw Error(
55
- 'expiryRound must be a positive number and smaller than 2^53-1'
56
- );
57
- if (!Number.isSafeInteger(minPay) || minPay < 0)
58
- throw Error('minPay must be a positive number and smaller than 2^53-1');
59
- if (!Number.isSafeInteger(maxFee) || maxFee < 0)
60
- throw Error('maxFee must be a positive number and smaller than 2^53-1');
61
-
62
- const referenceProgramB64 =
63
- 'ASAIAQUCAAYHCAkmAyCztwQn0+DycN+vsk+vJWcsoz/b7NDS6i33HOkvTpf+YiC3qUpIgHGWE8/1LPh9SGCalSN7IaITeeWSXbfsS5wsXyC4kBQ38Z8zcwWVAym4S8vpFB/c0XC6R4mnPi9EBADsPDEQIhIxASMMEDIEJBJAABkxCSgSMQcyAxIQMQglEhAxAiEEDRAiQAAuMwAAMwEAEjEJMgMSEDMABykSEDMBByoSEDMACCEFCzMBCCEGCxIQMwAIIQcPEBA=';
64
- const referenceProgramBytes = Buffer.from(referenceProgramB64, 'base64');
65
- const referenceOffsets = [
66
- 4 /* timeout */,
67
- 7 /* rat2 */,
68
- 8 /* rat1 */,
69
- 9 /* minPay */,
70
- 10 /* owner */,
71
- 14 /* receiver1 */,
72
- 47 /* receiver2 */,
73
- 80,
74
- ];
75
- const injectionVector = [
76
- maxFee,
77
- expiryRound,
78
- rat2,
79
- rat1,
80
- minPay,
81
- owner,
82
- receiverOne,
83
- receiverTwo,
84
- ];
85
- const injectionTypes = [
86
- templates.valTypes.INT,
87
- templates.valTypes.INT,
88
- templates.valTypes.INT,
89
- templates.valTypes.INT,
90
- templates.valTypes.INT,
91
- templates.valTypes.ADDRESS,
92
- templates.valTypes.ADDRESS,
93
- templates.valTypes.ADDRESS,
94
- ];
95
- const injectedBytes = templates.inject(
96
- referenceProgramBytes,
97
- referenceOffsets,
98
- injectionVector,
99
- injectionTypes
100
- );
101
- this.programBytes = injectedBytes;
102
- const lsig = logicsig.makeLogicSig(injectedBytes, undefined);
103
- this.address = lsig.address();
104
- }
105
-
106
- /**
107
- * returns the program bytes
108
- * @returns {Uint8Array}
109
- */
110
- getProgram() {
111
- return this.programBytes;
112
- }
113
-
114
- /**
115
- * returns the string address of the contract
116
- * @returns {string}
117
- */
118
- getAddress() {
119
- return this.address;
120
- }
121
- }
122
- /**
123
- * returns a group transactions array which transfer funds according to the contract's ratio
124
- *
125
- * @deprecated This feature will be removed in v2.
126
- *
127
- * @param {Uint8Array} contract: bytes representing the contract in question
128
- * @param {int} amount: the amount to be transferred
129
- * @param {int} firstRound: the first round on which the transaction group will be valid
130
- * @param {int} lastRound: the last round on which the transaction group will be valid
131
- * @param {int} fee: the fee to pay in microAlgos
132
- * @param {string} genesisHash: the b64-encoded genesis hash indicating the network for this transaction
133
- * @returns {Uint8Array}
134
- */
135
- function getSplitFundsTransaction(
136
- contract,
137
- amount,
138
- firstRound,
139
- lastRound,
140
- fee,
141
- genesisHash
142
- ) {
143
- const programOutputs = logic.readProgram(contract, undefined);
144
- const ints = programOutputs[0];
145
- const byteArrays = programOutputs[1];
146
- let rat2 = ints[6];
147
- let rat1 = ints[5];
148
- let amountForReceiverOne = 0;
149
- // reduce fractions
150
- const gcdFn = (a, b) => {
151
- if (typeof a !== 'number' || typeof b !== 'number')
152
- throw new Error('gcd operates only on positive integers');
153
- if (!b) {
154
- return a;
155
- }
156
- return gcdFn(b, a % b);
157
- };
158
- const gcd = gcdFn(rat2, rat1);
159
- rat2 = Math.floor(rat2 / gcd);
160
- rat1 = Math.floor(rat1 / gcd);
161
- const ratio = rat1 / rat2;
162
- amountForReceiverOne = Math.round(amount / (1 + ratio));
163
- const amountForReceiverTwo = amount - amountForReceiverOne;
164
- if (rat1 * amountForReceiverOne !== rat2 * amountForReceiverTwo) {
165
- throw Error(
166
- 'could not split funds in a way that satisfied the contract ratio'
167
- );
168
- }
169
-
170
- const logicSig = logicsig.makeLogicSig(contract, undefined); // no args
171
- const from = logicSig.address();
172
- const receiverOne = address.encodeAddress(byteArrays[1]);
173
- const receiverTwo = address.encodeAddress(byteArrays[2]);
174
- const tx1 = makeTxn.makePaymentTxn(
175
- from,
176
- receiverOne,
177
- fee,
178
- amountForReceiverOne,
179
- undefined,
180
- firstRound,
181
- lastRound,
182
- undefined,
183
- genesisHash
184
- );
185
- const tx2 = makeTxn.makePaymentTxn(
186
- from,
187
- receiverTwo,
188
- fee,
189
- amountForReceiverTwo,
190
- undefined,
191
- firstRound,
192
- lastRound,
193
- undefined,
194
- genesisHash
195
- );
196
- const txns = [tx1, tx2];
197
- const txGroup = group.assignGroupID(txns);
198
-
199
- const signedTxns = txGroup.map(
200
- (txn) => logicsig.signLogicSigTransactionObject(txn, logicsig).blob
201
- );
202
- return utils.concatArrays(signedTxns[0], signedTxns[1]);
203
- }
204
-
205
- module.exports = {
206
- Split,
207
- getSplitFundsTransaction,
208
- };