Skip to content

chore: upgrade to stacks.js v4.x #1295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
569 changes: 366 additions & 203 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,24 @@
"@promster/express": "6.0.0",
"@promster/server": "6.0.6",
"@promster/types": "3.2.3",
"@stacks/network": "1.2.2",
"@stacks/stacking": "v2.0.1",
"@stacks/transactions": "2.0.1",
"@stacks/network": "4.4.0-stacks2.1-alpha.0",
"@stacks/stacking": "4.4.0-stacks2.1-alpha.0",
"@stacks/transactions": "4.4.0-stacks2.1-alpha.0",
"@types/express-list-endpoints": "4.0.1",
"@types/lru-cache": "5.1.1",
"@types/ws": "7.4.7",
"ajv": "6.12.6",
"bignumber.js": "9.0.2",
"bitcoinjs-lib": "5.2.0",
"bluebird": "3.7.2",
"bn.js": "4.12.0",
"c32check": "1.1.3",
"chokidar": "3.5.3",
"coinselect": "3.1.12",
"cors": "2.8.5",
"cross-env": "7.0.3",
"dotenv": "8.6.0",
"dotenv-flow": "3.2.0",
"elliptic": "6.5.4",
"escape-goat": "3.0.0",
"evt": "1.10.1",
"express": "4.17.3",
Expand Down Expand Up @@ -162,9 +162,9 @@
"@stacks/prettier-config": "0.0.10",
"@types/ajv": "1.0.0",
"@types/bluebird": "3.5.36",
"@types/bn.js": "4.11.6",
"@types/cors": "2.8.12",
"@types/dotenv-flow": "3.2.0",
"@types/elliptic": "6.4.14",
"@types/express": "4.17.13",
"@types/is-ci": "3.0.0",
"@types/jest": "27.4.1",
Expand Down
43 changes: 28 additions & 15 deletions src/api/routes/debug.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as express from 'express';
import * as BN from 'bn.js';
import * as btc from 'bitcoinjs-lib';
import { stacksToBitcoinAddress } from 'stacks-encoding-native-js';
import * as bodyParser from 'body-parser';
Expand Down Expand Up @@ -80,13 +79,19 @@ const testnetKeyMap: Record<
);

export function getStacksTestnetNetwork() {
const stacksNetwork = new StacksTestnet();
stacksNetwork.coreApiUrl = `http://${getCoreNodeEndpoint()}`;
return stacksNetwork;
return new StacksTestnet({
url: `http://${getCoreNodeEndpoint()}`,
});
}

function getRandomInt(min: number, max: number) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}

export function createDebugRouter(db: PgStore): express.Router {
const defaultTxFee = 12345;
const defaultTxFee = 123450;
const stacksNetwork = getStacksTestnetNetwork();

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

const transferTx = await makeUnsignedSTXTokenTransfer({
recipient: recipient_address,
amount: new BN(stx_amount),
amount: BigInt(stx_amount),
memo: memo,
network: stacksNetwork,
numSignatures: sigsRequired,
publicKeys: signerPubKeys,
sponsored: sponsored,
fee: new BN(500),
fee: defaultTxFee,
anchorMode: AnchorMode.Any,
});

Expand All @@ -228,6 +233,7 @@ export function createDebugRouter(db: PgStore): express.Router {
network: stacksNetwork,
transaction: transferTx,
sponsorPrivateKey: sponsorKey,
fee: defaultTxFee,
});
serialized = sponsoredTx.serialize();
expectedTxId = sponsoredTx.txid();
Expand Down Expand Up @@ -332,12 +338,13 @@ export function createDebugRouter(db: PgStore): express.Router {

const transferTx = await makeSTXTokenTransfer({
recipient: recipientAddress,
amount: new BN(stx_amount),
amount: BigInt(stx_amount),
memo: memo,
network: stacksNetwork,
senderKey: origin_key,
sponsored: sponsored,
anchorMode: AnchorMode.Any,
fee: defaultTxFee,
});

let serialized: Buffer;
Expand All @@ -348,6 +355,7 @@ export function createDebugRouter(db: PgStore): express.Router {
network: stacksNetwork,
transaction: transferTx,
sponsorPrivateKey: sponsorKey,
fee: defaultTxFee,
});
serialized = sponsoredTx.serialize();
expectedTxId = sponsoredTx.txid();
Expand Down Expand Up @@ -444,13 +452,14 @@ export function createDebugRouter(db: PgStore): express.Router {
const anchorMode: AnchorMode = Number(anchor_mode);
const transferTx = await makeSTXTokenTransfer({
recipient: recipient_address,
amount: new BN(stx_amount),
amount: BigInt(stx_amount),
senderKey: origin_key,
network: stacksNetwork,
memo: memo,
sponsored: sponsored,
nonce: new BN(txNonce),
nonce: txNonce,
anchorMode: anchorMode,
fee: defaultTxFee,
});

let serialized: Buffer;
Expand All @@ -461,6 +470,7 @@ export function createDebugRouter(db: PgStore): express.Router {
network: stacksNetwork,
transaction: transferTx,
sponsorPrivateKey: sponsorKey,
fee: defaultTxFee,
});
serialized = sponsoredTx.serialize();
expectedTxId = sponsoredTx.txid();
Expand Down Expand Up @@ -593,7 +603,7 @@ export function createDebugRouter(db: PgStore): express.Router {
uintCV(minStxAmount.toString()),
tupleCV({
hashbytes: bufferCV(data),
version: bufferCV(new BN(hashMode).toBuffer()),
version: bufferCV(Buffer.from([hashMode])),
}),
uintCV(coreInfo.burn_block_height),
uintCV(cycles),
Expand Down Expand Up @@ -636,7 +646,7 @@ export function createDebugRouter(db: PgStore): express.Router {

<label for="contract_name">Contract name</label>
<input type="text" id="contract_name" name="contract_name" value="${htmlEscape(
SampleContracts[0].contractName
`${SampleContracts[0].contractName}-${getRandomInt(1000, 9999)}`
)}" pattern="^[a-zA-Z]([a-zA-Z0-9]|[-_!?+&lt;&gt;=/*])*$|^[-+=/*]$|^[&lt;&gt;]=?$" maxlength="128">

<label for="source_code">Contract Clarity source code</label>
Expand Down Expand Up @@ -668,10 +678,11 @@ export function createDebugRouter(db: PgStore): express.Router {
.replace(/\t/g, ' ');
const contractDeployTx = await makeContractDeploy({
contractName: contract_name,
clarityVersion: 2,
codeBody: normalized_contract_source,
senderKey: origin_key,
network: stacksNetwork,
fee: new BN(defaultTxFee),
network: getStacksTestnetNetwork(),
fee: defaultTxFee,
postConditionMode: PostConditionMode.Allow,
sponsored: sponsored,
anchorMode: AnchorMode.Any,
Expand All @@ -685,6 +696,7 @@ export function createDebugRouter(db: PgStore): express.Router {
network: stacksNetwork,
transaction: contractDeployTx,
sponsorPrivateKey: sponsorKey,
fee: defaultTxFee,
});
serializedTx = sponsoredTx.serialize();
expectedTxId = sponsoredTx.txid();
Expand Down Expand Up @@ -841,7 +853,7 @@ export function createDebugRouter(db: PgStore): express.Router {
functionArgs: clarityValueArgs,
senderKey: originKey,
network: stacksNetwork,
fee: new BN(defaultTxFee),
fee: defaultTxFee,
postConditionMode: PostConditionMode.Allow,
sponsored: sponsored,
anchorMode: AnchorMode.Any,
Expand All @@ -855,6 +867,7 @@ export function createDebugRouter(db: PgStore): express.Router {
network: stacksNetwork,
transaction: contractCallTx,
sponsorPrivateKey: sponsorKey,
fee: defaultTxFee,
});
serialized = sponsoredTx.serialize();
expectedTxId = sponsoredTx.txid();
Expand Down
12 changes: 6 additions & 6 deletions src/api/routes/faucets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import * as express from 'express';
import { asyncHandler } from '../async-handler';
import * as btc from 'bitcoinjs-lib';
import PQueue from 'p-queue';
import * as BN from 'bn.js';
import { BigNumber } from 'bignumber.js';
import { AnchorMode, makeSTXTokenTransfer, SignedTokenTransferOptions } from '@stacks/transactions';
import { StacksNetwork } from '@stacks/network';
import { StacksNetwork, StacksTestnet } from '@stacks/network';
import { makeBtcFaucetPayment, getBtcBalance } from '../../btc-faucet';
import { DbFaucetRequestCurrency } from '../../datastore/common';
import { intMax, logger, stxToMicroStx } from '../../helpers';
Expand All @@ -25,8 +24,9 @@ export function getStxFaucetNetworks(): StacksNetwork[] {
logger.error(error);
throw new Error(error);
}
const network = getStacksTestnetNetwork();
network.coreApiUrl = `http://${faucetNodeHostOverride}:${faucetNodePortOverride}`;
const network = new StacksTestnet({
url: `http://${faucetNodeHostOverride}:${faucetNodePortOverride}`,
});
networks.push(network);
}
return networks;
Expand Down Expand Up @@ -186,7 +186,7 @@ export function createFaucetRouter(db: PgWriteStore): express.Router {
const generateTx = async (network: StacksNetwork, nonce?: bigint, fee?: bigint) => {
const txOpts: SignedTokenTransferOptions = {
recipient: address,
amount: new BN(stxAmount.toString()),
amount: stxAmount,
senderKey: privateKey,
network: network,
memo: 'Faucet',
Expand All @@ -208,7 +208,7 @@ export function createFaucetRouter(db: PgWriteStore): express.Router {
try {
const tx = await generateTx(network);
nonces.push(tx.auth.spendingCondition?.nonce ?? BigInt(0));
fees.push(tx.auth.getFee());
fees.push(tx.auth.spendingCondition.fee);
} catch (error: any) {
txGenFetchError = error;
}
Expand Down
35 changes: 17 additions & 18 deletions src/api/routes/rosetta/construction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { asyncHandler } from '../../async-handler';
import * as BN from 'bn.js';
import {
NetworkIdentifier,
RosettaAccountIdentifier,
Expand Down Expand Up @@ -190,14 +189,14 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
// dummy transaction to calculate size
const dummyTokenTransferTx: UnsignedTokenTransferOptions = {
recipient: options.token_transfer_recipient_address as string,
amount: new BN(options.amount as string),
amount: BigInt(options.amount as string),
// We don't know the fee yet but need a placeholder
fee: new BN(0),
fee: 0,
// placeholder public key
publicKey: '000000000000000000000000000000000000000000000000000000000000000000',
network: getStacksNetwork(),
// We don't know the non yet but need a placeholder
nonce: new BN(0),
nonce: 0,
memo: req.body.metadata?.memo,
anchorMode: AnchorMode.Any,
};
Expand All @@ -217,7 +216,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
// dummy transaction to calculate size
const poxAddress = options.pox_addr;
const { hashMode, data } = decodeBtcAddress(poxAddress);
const hashModeBuffer = bufferCV(new BN(hashMode, 10).toArrayLike(Buffer));
const hashModeBuffer = bufferCV(Buffer.from([hashMode]));
const hashbytes = bufferCV(data);
const poxAddressCV = tupleCV({
hashbytes,
Expand All @@ -240,8 +239,8 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
],
validateWithAbi: false,
network: getStacksNetwork(),
fee: new BN(0),
nonce: new BN(0),
fee: 0,
nonce: 0,
anchorMode: AnchorMode.Any,
};
transaction = await makeUnsignedContractCall(dummyStackingTx);
Expand All @@ -262,7 +261,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
if (options.pox_addr) {
const dummyPoxAddress = options.pox_addr;
const { hashMode, data } = decodeBtcAddress(dummyPoxAddress);
const hashModeBuffer = bufferCV(new BN(hashMode, 10).toArrayLike(Buffer));
const hashModeBuffer = bufferCV(Buffer.from([hashMode]));
const hashbytes = bufferCV(data);
optionalPoxAddressCV = someCV(
tupleCV({
Expand All @@ -285,8 +284,8 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
],
validateWithAbi: false,
network: getStacksNetwork(),
fee: new BN(0),
nonce: new BN(0),
fee: 0,
nonce: 0,
anchorMode: AnchorMode.Any,
};
transaction = await makeUnsignedContractCall(dummyStackingTx);
Expand Down Expand Up @@ -624,7 +623,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
return;
}

const nonce = new BN(req.body.metadata.account_sequence);
const nonce = BigInt(req.body.metadata.account_sequence);

if (publicKeys.length !== 1) {
//TODO support multi-sig in the future.
Expand Down Expand Up @@ -652,8 +651,8 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
// signel signature
const tokenTransferOptions: UnsignedTokenTransferOptions = {
recipient: recipientAddress,
amount: new BN(amount),
fee: new BN(fee),
amount: BigInt(amount),
fee: BigInt(fee),
publicKey: publicKeys[0].hex_bytes,
network: getStacksNetwork(),
nonce: nonce,
Expand All @@ -672,7 +671,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
}
const poxBTCAddress = options.pox_addr;
const { hashMode, data } = decodeBtcAddress(poxBTCAddress);
const hashModeBuffer = bufferCV(new BN(hashMode, 10).toArrayLike(Buffer));
const hashModeBuffer = bufferCV(Buffer.from([hashMode]));
const hashbytes = bufferCV(data);
const poxAddressCV = tupleCV({
hashbytes,
Expand Down Expand Up @@ -709,7 +708,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
uintCV(req.body.metadata.burn_block_height),
uintCV(options.number_of_cycles),
],
fee: new BN(options.fee),
fee: BigInt(options.fee),
nonce: nonce,
validateWithAbi: false,
network: getStacksNetwork(),
Expand All @@ -724,7 +723,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
if (options.pox_addr) {
const poxBTCAddress = options.pox_addr;
const { hashMode, data } = decodeBtcAddress(poxBTCAddress);
const hashModeBuffer = bufferCV(new BN(hashMode, 10).toArrayLike(Buffer));
const hashModeBuffer = bufferCV(Buffer.from([hashMode]));
const hashbytes = bufferCV(data);
poxAddressCV = someCV(
tupleCV({
Expand Down Expand Up @@ -767,7 +766,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
expire_burn_block_heightCV,
poxAddressCV,
],
fee: new BN(options.fee),
fee: BigInt(options.fee),
nonce: nonce,
validateWithAbi: false,
network: getStacksNetwork(),
Expand All @@ -785,7 +784,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):

const signer = new TransactionSigner(transaction);

const prehash = makeSigHashPreSign(signer.sigHash, AuthType.Standard, new BN(fee), nonce);
const prehash = makeSigHashPreSign(signer.sigHash, AuthType.Standard, BigInt(fee), nonce);
const accountIdentifier: RosettaAccountIdentifier = {
address: senderAddress,
};
Expand Down
5 changes: 3 additions & 2 deletions src/bns-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ export function getNewOwner(
}

export function GetStacksNetwork(chainId: ChainID) {
const network = chainId === ChainID.Mainnet ? new StacksMainnet() : new StacksTestnet();
network.coreApiUrl = `http://${getCoreNodeEndpoint()}`;
const url = `http://${getCoreNodeEndpoint()}`;
const network =
chainId === ChainID.Mainnet ? new StacksMainnet({ url }) : new StacksTestnet({ url });
return network;
}

Expand Down
Loading