Skip to content

Commit 97fbbd7

Browse files
authored
chore: upgrade to stacks.js v4.x (#1295)
* chore: upgrade to stacks.js v4.x * chore: hardcode tx fees in rosetta tests * chore: hardcode tx fees in token tests * chore: hardcode tx fees in rosetta-cli-data tests * chore: remove bugged fees in token tests * chore: remove bugged fees in rosetta-cli-data tests * chore: hardcode tx fees in rosetta-cli-data tests * chore: fixes for bns tests
1 parent c7cc900 commit 97fbbd7

File tree

18 files changed

+565
-376
lines changed

18 files changed

+565
-376
lines changed

package-lock.json

Lines changed: 366 additions & 203 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,24 @@
100100
"@promster/express": "6.0.0",
101101
"@promster/server": "6.0.6",
102102
"@promster/types": "3.2.3",
103-
"@stacks/network": "1.2.2",
104-
"@stacks/stacking": "v2.0.1",
105-
"@stacks/transactions": "2.0.1",
103+
"@stacks/network": "4.4.0-stacks2.1-alpha.0",
104+
"@stacks/stacking": "4.4.0-stacks2.1-alpha.0",
105+
"@stacks/transactions": "4.4.0-stacks2.1-alpha.0",
106106
"@types/express-list-endpoints": "4.0.1",
107107
"@types/lru-cache": "5.1.1",
108108
"@types/ws": "7.4.7",
109109
"ajv": "6.12.6",
110110
"bignumber.js": "9.0.2",
111111
"bitcoinjs-lib": "5.2.0",
112112
"bluebird": "3.7.2",
113-
"bn.js": "4.12.0",
114113
"c32check": "1.1.3",
115114
"chokidar": "3.5.3",
116115
"coinselect": "3.1.12",
117116
"cors": "2.8.5",
118117
"cross-env": "7.0.3",
119118
"dotenv": "8.6.0",
120119
"dotenv-flow": "3.2.0",
120+
"elliptic": "6.5.4",
121121
"escape-goat": "3.0.0",
122122
"evt": "1.10.1",
123123
"express": "4.17.3",
@@ -162,9 +162,9 @@
162162
"@stacks/prettier-config": "0.0.10",
163163
"@types/ajv": "1.0.0",
164164
"@types/bluebird": "3.5.36",
165-
"@types/bn.js": "4.11.6",
166165
"@types/cors": "2.8.12",
167166
"@types/dotenv-flow": "3.2.0",
167+
"@types/elliptic": "6.4.14",
168168
"@types/express": "4.17.13",
169169
"@types/is-ci": "3.0.0",
170170
"@types/jest": "27.4.1",

src/api/routes/debug.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as express from 'express';
2-
import * as BN from 'bn.js';
32
import * as btc from 'bitcoinjs-lib';
43
import { stacksToBitcoinAddress } from 'stacks-encoding-native-js';
54
import * as bodyParser from 'body-parser';
@@ -80,13 +79,19 @@ const testnetKeyMap: Record<
8079
);
8180

8281
export function getStacksTestnetNetwork() {
83-
const stacksNetwork = new StacksTestnet();
84-
stacksNetwork.coreApiUrl = `http://${getCoreNodeEndpoint()}`;
85-
return stacksNetwork;
82+
return new StacksTestnet({
83+
url: `http://${getCoreNodeEndpoint()}`,
84+
});
85+
}
86+
87+
function getRandomInt(min: number, max: number) {
88+
min = Math.ceil(min);
89+
max = Math.floor(max);
90+
return Math.floor(Math.random() * (max - min) + min);
8691
}
8792

8893
export function createDebugRouter(db: PgStore): express.Router {
89-
const defaultTxFee = 12345;
94+
const defaultTxFee = 123450;
9095
const stacksNetwork = getStacksTestnetNetwork();
9196

9297
const router = express.Router();
@@ -201,13 +206,13 @@ export function createDebugRouter(db: PgStore): express.Router {
201206

202207
const transferTx = await makeUnsignedSTXTokenTransfer({
203208
recipient: recipient_address,
204-
amount: new BN(stx_amount),
209+
amount: BigInt(stx_amount),
205210
memo: memo,
206211
network: stacksNetwork,
207212
numSignatures: sigsRequired,
208213
publicKeys: signerPubKeys,
209214
sponsored: sponsored,
210-
fee: new BN(500),
215+
fee: defaultTxFee,
211216
anchorMode: AnchorMode.Any,
212217
});
213218

@@ -228,6 +233,7 @@ export function createDebugRouter(db: PgStore): express.Router {
228233
network: stacksNetwork,
229234
transaction: transferTx,
230235
sponsorPrivateKey: sponsorKey,
236+
fee: defaultTxFee,
231237
});
232238
serialized = sponsoredTx.serialize();
233239
expectedTxId = sponsoredTx.txid();
@@ -332,12 +338,13 @@ export function createDebugRouter(db: PgStore): express.Router {
332338

333339
const transferTx = await makeSTXTokenTransfer({
334340
recipient: recipientAddress,
335-
amount: new BN(stx_amount),
341+
amount: BigInt(stx_amount),
336342
memo: memo,
337343
network: stacksNetwork,
338344
senderKey: origin_key,
339345
sponsored: sponsored,
340346
anchorMode: AnchorMode.Any,
347+
fee: defaultTxFee,
341348
});
342349

343350
let serialized: Buffer;
@@ -348,6 +355,7 @@ export function createDebugRouter(db: PgStore): express.Router {
348355
network: stacksNetwork,
349356
transaction: transferTx,
350357
sponsorPrivateKey: sponsorKey,
358+
fee: defaultTxFee,
351359
});
352360
serialized = sponsoredTx.serialize();
353361
expectedTxId = sponsoredTx.txid();
@@ -444,13 +452,14 @@ export function createDebugRouter(db: PgStore): express.Router {
444452
const anchorMode: AnchorMode = Number(anchor_mode);
445453
const transferTx = await makeSTXTokenTransfer({
446454
recipient: recipient_address,
447-
amount: new BN(stx_amount),
455+
amount: BigInt(stx_amount),
448456
senderKey: origin_key,
449457
network: stacksNetwork,
450458
memo: memo,
451459
sponsored: sponsored,
452-
nonce: new BN(txNonce),
460+
nonce: txNonce,
453461
anchorMode: anchorMode,
462+
fee: defaultTxFee,
454463
});
455464

456465
let serialized: Buffer;
@@ -461,6 +470,7 @@ export function createDebugRouter(db: PgStore): express.Router {
461470
network: stacksNetwork,
462471
transaction: transferTx,
463472
sponsorPrivateKey: sponsorKey,
473+
fee: defaultTxFee,
464474
});
465475
serialized = sponsoredTx.serialize();
466476
expectedTxId = sponsoredTx.txid();
@@ -593,7 +603,7 @@ export function createDebugRouter(db: PgStore): express.Router {
593603
uintCV(minStxAmount.toString()),
594604
tupleCV({
595605
hashbytes: bufferCV(data),
596-
version: bufferCV(new BN(hashMode).toBuffer()),
606+
version: bufferCV(Buffer.from([hashMode])),
597607
}),
598608
uintCV(coreInfo.burn_block_height),
599609
uintCV(cycles),
@@ -636,7 +646,7 @@ export function createDebugRouter(db: PgStore): express.Router {
636646
637647
<label for="contract_name">Contract name</label>
638648
<input type="text" id="contract_name" name="contract_name" value="${htmlEscape(
639-
SampleContracts[0].contractName
649+
`${SampleContracts[0].contractName}-${getRandomInt(1000, 9999)}`
640650
)}" pattern="^[a-zA-Z]([a-zA-Z0-9]|[-_!?+&lt;&gt;=/*])*$|^[-+=/*]$|^[&lt;&gt;]=?$" maxlength="128">
641651
642652
<label for="source_code">Contract Clarity source code</label>
@@ -668,10 +678,11 @@ export function createDebugRouter(db: PgStore): express.Router {
668678
.replace(/\t/g, ' ');
669679
const contractDeployTx = await makeContractDeploy({
670680
contractName: contract_name,
681+
clarityVersion: 2,
671682
codeBody: normalized_contract_source,
672683
senderKey: origin_key,
673-
network: stacksNetwork,
674-
fee: new BN(defaultTxFee),
684+
network: getStacksTestnetNetwork(),
685+
fee: defaultTxFee,
675686
postConditionMode: PostConditionMode.Allow,
676687
sponsored: sponsored,
677688
anchorMode: AnchorMode.Any,
@@ -685,6 +696,7 @@ export function createDebugRouter(db: PgStore): express.Router {
685696
network: stacksNetwork,
686697
transaction: contractDeployTx,
687698
sponsorPrivateKey: sponsorKey,
699+
fee: defaultTxFee,
688700
});
689701
serializedTx = sponsoredTx.serialize();
690702
expectedTxId = sponsoredTx.txid();
@@ -841,7 +853,7 @@ export function createDebugRouter(db: PgStore): express.Router {
841853
functionArgs: clarityValueArgs,
842854
senderKey: originKey,
843855
network: stacksNetwork,
844-
fee: new BN(defaultTxFee),
856+
fee: defaultTxFee,
845857
postConditionMode: PostConditionMode.Allow,
846858
sponsored: sponsored,
847859
anchorMode: AnchorMode.Any,
@@ -855,6 +867,7 @@ export function createDebugRouter(db: PgStore): express.Router {
855867
network: stacksNetwork,
856868
transaction: contractCallTx,
857869
sponsorPrivateKey: sponsorKey,
870+
fee: defaultTxFee,
858871
});
859872
serialized = sponsoredTx.serialize();
860873
expectedTxId = sponsoredTx.txid();

src/api/routes/faucets.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import * as express from 'express';
33
import { asyncHandler } from '../async-handler';
44
import * as btc from 'bitcoinjs-lib';
55
import PQueue from 'p-queue';
6-
import * as BN from 'bn.js';
76
import { BigNumber } from 'bignumber.js';
87
import { AnchorMode, makeSTXTokenTransfer, SignedTokenTransferOptions } from '@stacks/transactions';
9-
import { StacksNetwork } from '@stacks/network';
8+
import { StacksNetwork, StacksTestnet } from '@stacks/network';
109
import { makeBtcFaucetPayment, getBtcBalance } from '../../btc-faucet';
1110
import { DbFaucetRequestCurrency } from '../../datastore/common';
1211
import { intMax, logger, stxToMicroStx } from '../../helpers';
@@ -25,8 +24,9 @@ export function getStxFaucetNetworks(): StacksNetwork[] {
2524
logger.error(error);
2625
throw new Error(error);
2726
}
28-
const network = getStacksTestnetNetwork();
29-
network.coreApiUrl = `http://${faucetNodeHostOverride}:${faucetNodePortOverride}`;
27+
const network = new StacksTestnet({
28+
url: `http://${faucetNodeHostOverride}:${faucetNodePortOverride}`,
29+
});
3030
networks.push(network);
3131
}
3232
return networks;
@@ -186,7 +186,7 @@ export function createFaucetRouter(db: PgWriteStore): express.Router {
186186
const generateTx = async (network: StacksNetwork, nonce?: bigint, fee?: bigint) => {
187187
const txOpts: SignedTokenTransferOptions = {
188188
recipient: address,
189-
amount: new BN(stxAmount.toString()),
189+
amount: stxAmount,
190190
senderKey: privateKey,
191191
network: network,
192192
memo: 'Faucet',
@@ -208,7 +208,7 @@ export function createFaucetRouter(db: PgWriteStore): express.Router {
208208
try {
209209
const tx = await generateTx(network);
210210
nonces.push(tx.auth.spendingCondition?.nonce ?? BigInt(0));
211-
fees.push(tx.auth.getFee());
211+
fees.push(tx.auth.spendingCondition.fee);
212212
} catch (error: any) {
213213
txGenFetchError = error;
214214
}

src/api/routes/rosetta/construction.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { asyncHandler } from '../../async-handler';
2-
import * as BN from 'bn.js';
32
import {
43
NetworkIdentifier,
54
RosettaAccountIdentifier,
@@ -190,14 +189,14 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
190189
// dummy transaction to calculate size
191190
const dummyTokenTransferTx: UnsignedTokenTransferOptions = {
192191
recipient: options.token_transfer_recipient_address as string,
193-
amount: new BN(options.amount as string),
192+
amount: BigInt(options.amount as string),
194193
// We don't know the fee yet but need a placeholder
195-
fee: new BN(0),
194+
fee: 0,
196195
// placeholder public key
197196
publicKey: '000000000000000000000000000000000000000000000000000000000000000000',
198197
network: getStacksNetwork(),
199198
// We don't know the non yet but need a placeholder
200-
nonce: new BN(0),
199+
nonce: 0,
201200
memo: req.body.metadata?.memo,
202201
anchorMode: AnchorMode.Any,
203202
};
@@ -217,7 +216,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
217216
// dummy transaction to calculate size
218217
const poxAddress = options.pox_addr;
219218
const { hashMode, data } = decodeBtcAddress(poxAddress);
220-
const hashModeBuffer = bufferCV(new BN(hashMode, 10).toArrayLike(Buffer));
219+
const hashModeBuffer = bufferCV(Buffer.from([hashMode]));
221220
const hashbytes = bufferCV(data);
222221
const poxAddressCV = tupleCV({
223222
hashbytes,
@@ -240,8 +239,8 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
240239
],
241240
validateWithAbi: false,
242241
network: getStacksNetwork(),
243-
fee: new BN(0),
244-
nonce: new BN(0),
242+
fee: 0,
243+
nonce: 0,
245244
anchorMode: AnchorMode.Any,
246245
};
247246
transaction = await makeUnsignedContractCall(dummyStackingTx);
@@ -262,7 +261,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
262261
if (options.pox_addr) {
263262
const dummyPoxAddress = options.pox_addr;
264263
const { hashMode, data } = decodeBtcAddress(dummyPoxAddress);
265-
const hashModeBuffer = bufferCV(new BN(hashMode, 10).toArrayLike(Buffer));
264+
const hashModeBuffer = bufferCV(Buffer.from([hashMode]));
266265
const hashbytes = bufferCV(data);
267266
optionalPoxAddressCV = someCV(
268267
tupleCV({
@@ -285,8 +284,8 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
285284
],
286285
validateWithAbi: false,
287286
network: getStacksNetwork(),
288-
fee: new BN(0),
289-
nonce: new BN(0),
287+
fee: 0,
288+
nonce: 0,
290289
anchorMode: AnchorMode.Any,
291290
};
292291
transaction = await makeUnsignedContractCall(dummyStackingTx);
@@ -624,7 +623,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
624623
return;
625624
}
626625

627-
const nonce = new BN(req.body.metadata.account_sequence);
626+
const nonce = BigInt(req.body.metadata.account_sequence);
628627

629628
if (publicKeys.length !== 1) {
630629
//TODO support multi-sig in the future.
@@ -652,8 +651,8 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
652651
// signel signature
653652
const tokenTransferOptions: UnsignedTokenTransferOptions = {
654653
recipient: recipientAddress,
655-
amount: new BN(amount),
656-
fee: new BN(fee),
654+
amount: BigInt(amount),
655+
fee: BigInt(fee),
657656
publicKey: publicKeys[0].hex_bytes,
658657
network: getStacksNetwork(),
659658
nonce: nonce,
@@ -672,7 +671,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
672671
}
673672
const poxBTCAddress = options.pox_addr;
674673
const { hashMode, data } = decodeBtcAddress(poxBTCAddress);
675-
const hashModeBuffer = bufferCV(new BN(hashMode, 10).toArrayLike(Buffer));
674+
const hashModeBuffer = bufferCV(Buffer.from([hashMode]));
676675
const hashbytes = bufferCV(data);
677676
const poxAddressCV = tupleCV({
678677
hashbytes,
@@ -709,7 +708,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
709708
uintCV(req.body.metadata.burn_block_height),
710709
uintCV(options.number_of_cycles),
711710
],
712-
fee: new BN(options.fee),
711+
fee: BigInt(options.fee),
713712
nonce: nonce,
714713
validateWithAbi: false,
715714
network: getStacksNetwork(),
@@ -724,7 +723,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
724723
if (options.pox_addr) {
725724
const poxBTCAddress = options.pox_addr;
726725
const { hashMode, data } = decodeBtcAddress(poxBTCAddress);
727-
const hashModeBuffer = bufferCV(new BN(hashMode, 10).toArrayLike(Buffer));
726+
const hashModeBuffer = bufferCV(Buffer.from([hashMode]));
728727
const hashbytes = bufferCV(data);
729728
poxAddressCV = someCV(
730729
tupleCV({
@@ -767,7 +766,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
767766
expire_burn_block_heightCV,
768767
poxAddressCV,
769768
],
770-
fee: new BN(options.fee),
769+
fee: BigInt(options.fee),
771770
nonce: nonce,
772771
validateWithAbi: false,
773772
network: getStacksNetwork(),
@@ -785,7 +784,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
785784

786785
const signer = new TransactionSigner(transaction);
787786

788-
const prehash = makeSigHashPreSign(signer.sigHash, AuthType.Standard, new BN(fee), nonce);
787+
const prehash = makeSigHashPreSign(signer.sigHash, AuthType.Standard, BigInt(fee), nonce);
789788
const accountIdentifier: RosettaAccountIdentifier = {
790789
address: senderAddress,
791790
};

src/bns-helpers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,9 @@ export function getNewOwner(
211211
}
212212

213213
export function GetStacksNetwork(chainId: ChainID) {
214-
const network = chainId === ChainID.Mainnet ? new StacksMainnet() : new StacksTestnet();
215-
network.coreApiUrl = `http://${getCoreNodeEndpoint()}`;
214+
const url = `http://${getCoreNodeEndpoint()}`;
215+
const network =
216+
chainId === ChainID.Mainnet ? new StacksMainnet({ url }) : new StacksTestnet({ url });
216217
return network;
217218
}
218219

0 commit comments

Comments
 (0)