Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit 8126048

Browse files
authored
Merge pull request #107 from CudoVentures/cudos-dev
Release v2.1.0 - 10.08.2022
2 parents 7342ef9 + 1010843 commit 8126048

23 files changed

+257
-71
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"ecmaVersion": "latest",
1010
"sourceType": "module"
1111
},
12-
"ignorePatterns": ["*.test.js"],
12+
"ignorePatterns": ["**/template/tests", "**/template/scripts"],
1313
"rules": {
1414
"space-before-function-paren": ["error", {
1515
"anonymous": "never",

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules/
33
.vscode
44
**/.DS_Store
55
test-blast-init*/
6+
packages/*/package-lock.json

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/blast-core/README.md

Lines changed: 93 additions & 24 deletions
Large diffs are not rendered by default.

packages/blast-core/cmd/blast-cmd.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ const { hideBin } = require('yargs/helpers')
55
const commands = require('./commands')
66
const BlastError = require('../utilities/blast-error')
77

8+
const { customTasks } = require('../utilities/task')
9+
const { getConfig } = require('../utilities/config-utils')
10+
811
async function main() {
12+
if (hideBin(process.argv)[0] !== 'init') {
13+
getConfig()
14+
}
15+
916
await yargs(hideBin(process.argv))
1017
.scriptName('blast')
1118
.usage('Usage: $0 <command> [arguments] [command options]')
@@ -16,6 +23,7 @@ async function main() {
1623
.command(commands.nodeInfo)
1724
.command(commands.runInfo)
1825
.command(commands.keysInfo)
26+
.command(customTasks)
1927
.demandCommand(1, 'No command specified!') // user must specify atleast one command
2028
.recommendCommands()
2129
.strict() // checks if the command or optional parameters are specified, if not - user friendly error

packages/blast-core/cmd/commands.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { rustTestCmd } = require('./rusttest/rusttest.js')
55
const { runCmd } = require('./run/run.js')
66
const keys = require('./keys/keys.js')
77
const node = require('./node/node.js')
8+
const { DEFAULT_DENOM } = require('../config/blast-constants')
89

910
const initInfo = {
1011
command: 'init',
@@ -113,7 +114,7 @@ const keysInfo = {
113114
alias: 't',
114115
type: 'string',
115116
required: true,
116-
describe: 'The amount of tokens in acudos. Example: --tokens 100000'
117+
describe: `The amount of tokens in ${DEFAULT_DENOM}. Example: --tokens 100000`
117118
})
118119
}, keys.keysFundCmd)
119120
.demandCommand(1, 'No command specified!') // user must specify atleast one command

packages/blast-core/cmd/init/init.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require('fs')
22
const fsExtra = require('fs-extra')
33
const path = require('path')
4+
const { spawnSync } = require('child_process')
45

56
const { getPackageRootPath } = require('../../utilities/package-info')
67
const BlastError = require('../../utilities/blast-error')
@@ -15,8 +16,14 @@ async function initCmd(argv) {
1516
} catch (error) {
1617
throw new BlastError(`Error copying folder: ${error}`)
1718
}
19+
console.log(`Sample project initialized in ${argv.dir !== '.' ? argv.dir : process.cwd()}`)
1820

19-
console.log(`Success! Sample project initialized in ${argv.dir !== '.' ? argv.dir : process.cwd()}`)
21+
console.log('Installing default dependencies...')
22+
spawnSync(`npm install --prefix "${argv.dir}"`, {
23+
stdio: 'inherit',
24+
shell: true
25+
})
26+
console.log('Project is ready')
2027
}
2128

2229
function handleCustomDirCreation(argv) {

packages/blast-core/cmd/test/test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ const {
99
const { checkNodeOnline } = require('../../utilities/get-node-status')
1010

1111
const JS_TESTS_FOLDER_NAME = 'tests'
12-
const GLOBALS_PATH = path.join(getPackageRootPath(), 'lib/bre.js')
1312

1413
async function testCmd(argv) {
15-
const TEST_DIR = path.join(getProjectRootPath(), JS_TESTS_FOLDER_NAME)
16-
if (!fs.existsSync(TEST_DIR)) {
14+
const testDir = path.join(getProjectRootPath(), JS_TESTS_FOLDER_NAME)
15+
const globalsPath = path.join(getPackageRootPath(), 'lib/bre.js')
16+
17+
if (!fs.existsSync(testDir)) {
1718
throw new BlastError('No tests folder found! Make sure to place your JavaScript tests in /' +
1819
JS_TESTS_FOLDER_NAME)
1920
}
@@ -23,8 +24,8 @@ async function testCmd(argv) {
2324

2425
process.env.BLAST_NETWORK = argv.network ?? ''
2526
// here JEST uses only initial globals file setup. process.env (not globals) persist through the new spawned process
26-
spawnSync(`npx --no jest ${TEST_DIR} ` +
27-
`--setupFilesAfterEnv=${GLOBALS_PATH} --testTimeout=15000 ${silent} --detectOpenHandles`,
27+
spawnSync(`npx --no --prefix "${getPackageRootPath()}" jest "${testDir}" ` +
28+
`--setupFilesAfterEnv="${globalsPath}" --testTimeout=15000 ${silent} --detectOpenHandles`,
2829
{
2930
stdio: 'inherit',
3031
shell: true
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/* eslint-disable object-curly-newline */
22
module.exports = {
3-
localNetwork: 'http://localhost:26657'
3+
DEFAULT_DENOM: 'acudos',
4+
GAS_AUTO: 'auto',
5+
LOCAL_NETWORK: 'http://localhost:26657'
46
}

packages/blast-core/lib/cudos-contract.js

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const {
22
GasPrice,
3-
calculateFee
3+
calculateFee,
4+
parseCoins
45
} = require('cudosjs')
56
const path = require('path')
67
const fs = require('fs')
@@ -12,18 +13,27 @@ const {
1213
getContractInfo,
1314
getCodeDetails
1415
} = require('../utilities/network-utils')
16+
const {
17+
DEFAULT_DENOM,
18+
GAS_AUTO
19+
} = require('../config/blast-constants')
20+
21+
function getGasFee(gasLimit, gasMultiplier) {
22+
if (!gasLimit || gasLimit === GAS_AUTO) {
23+
if (!gasMultiplier || gasMultiplier === GAS_AUTO) {
24+
return GAS_AUTO
25+
}
26+
return gasMultiplier
27+
}
28+
return calculateFee(gasLimit, GasPrice.fromString(getGasPrice()))
29+
}
1530

1631
module.exports.CudosContract = class CudosContract {
1732
#label
1833
#wasmPath
1934
#codeId
2035
#contractAddress
2136
#creator
22-
#gasPrice
23-
24-
constructor() {
25-
this.#gasPrice = GasPrice.fromString(getGasPrice())
26-
}
2737

2838
static constructLocal(label) {
2939
const wasmPath = path.join(getProjectRootPath(), `artifacts/${label}.wasm`)
@@ -54,27 +64,33 @@ module.exports.CudosContract = class CudosContract {
5464
}
5565

5666
// Uploads the contract's code to the network
57-
async uploadCode(options = { signer: null }) {
67+
async uploadCode(options = {
68+
signer: null, gasLimit: null, gasMultiplier: null
69+
}) {
5870
if (this.#isUploaded()) {
5971
throw new BlastError('Cannot upload contract that is already uploaded')
6072
}
6173
options.signer = options.signer ?? await getDefaultSigner()
62-
const uploadTx = await this.#uploadContract(options.signer)
74+
const uploadTx = await this.#uploadContract(options.signer, options.gasLimit, options.gasMultiplier)
6375
this.#codeId = uploadTx.codeId
6476
this.#creator = options.signer.address
6577
return uploadTx
6678
}
6779

6880
// Instantiates uploaded code without assigning the new contract to current contract object instance
6981
async instantiate(msg, label, options = {
70-
signer: null, funds: null
82+
signer: null, funds: null, gasLimit: null, gasMultiplier: null
7183
}) {
7284
if (!this.#isUploaded()) {
7385
throw new BlastError('Cannot instantiate contract that is not uploaded. ' +
7486
'Contract\'s code must exist on the network before instantiating')
7587
}
88+
if (options.funds) {
89+
options.funds = parseCoins(options.funds + DEFAULT_DENOM)
90+
}
7691
options.signer = options.signer ?? await getDefaultSigner()
77-
const instantiateTx = await this.#instantiateContract(options.signer, this.#codeId, msg, label, options.funds)
92+
const instantiateTx = await this.#instantiateContract(options.signer, this.#codeId, msg, label, options.funds,
93+
options.gasLimit, options.gasMultiplier)
7894
return instantiateTx
7995
}
8096

@@ -86,11 +102,15 @@ module.exports.CudosContract = class CudosContract {
86102
throw new BlastError('Cannot deploy contract that is already uploaded. Only new contracts can be deployed. ' +
87103
'Use "instantiate" for uploaded contracts')
88104
}
105+
if (options.funds) {
106+
options.funds = parseCoins(options.funds + DEFAULT_DENOM)
107+
}
89108
options.signer = options.signer ?? await getDefaultSigner()
90-
const uploadTx = await this.#uploadContract(options.signer)
109+
const uploadTx = await this.#uploadContract(options.signer, GAS_AUTO, GAS_AUTO)
91110
this.#codeId = uploadTx.codeId
92111
this.#creator = options.signer.address
93-
const instantiateTx = await this.#instantiateContract(options.signer, uploadTx.codeId, msg, label, options.funds)
112+
const instantiateTx = await this.#instantiateContract(options.signer, uploadTx.codeId, msg, label, options.funds,
113+
GAS_AUTO, GAS_AUTO)
94114
this.#contractAddress = instantiateTx.contractAddress
95115
this.#label = label
96116
return {
@@ -99,12 +119,14 @@ module.exports.CudosContract = class CudosContract {
99119
}
100120
}
101121

102-
async execute(msg, signer = null) {
122+
async execute(msg, signer = null, options = {
123+
gasLimit: null, gasMultiplier: null
124+
}) {
103125
if (!this.#isDeployed()) {
104126
throw new BlastError('Cannot use "execute()" on non-deployed contracts')
105127
}
106128
signer = signer ?? await getDefaultSigner()
107-
const fee = calculateFee(1_500_000, this.#gasPrice)
129+
const fee = getGasFee(options.gasLimit, options.gasMultiplier)
108130
return signer.execute(signer.address, this.#contractAddress, msg, fee)
109131
}
110132

@@ -144,19 +166,18 @@ module.exports.CudosContract = class CudosContract {
144166
return this.#creator
145167
}
146168

147-
async #uploadContract(signer) {
148-
// TODO: pass gasLimit as a param or read it from config
169+
async #uploadContract(signer, gasLimit, gasMultiplier) {
149170
const wasm = fs.readFileSync(this.#wasmPath)
150-
const uploadFee = calculateFee(1_500_000, this.#gasPrice)
171+
const uploadFee = getGasFee(gasLimit, gasMultiplier)
151172
return signer.upload(
152173
signer.address,
153174
wasm,
154175
uploadFee
155176
)
156177
}
157178

158-
async #instantiateContract(signer, codeId, msg, label, funds) {
159-
const instantiateFee = calculateFee(500_000, this.#gasPrice)
179+
async #instantiateContract(signer, codeId, msg, label, funds, gasLimit, gasMultiplier) {
180+
const instantiateFee = getGasFee(gasLimit, gasMultiplier)
160181
return signer.instantiate(
161182
signer.address,
162183
codeId,

packages/blast-core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cudos-blast",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"license": "GNU GENERAL PUBLIC",
55
"repository": "github:CudoVentures/cudos-blast",
66
"description": "Cudos Blast is a Node.js CLI tool designed to work with the Cudos blockchain. It allows you to scaffold, compile and test your Rust smart contracts.",
@@ -22,7 +22,7 @@
2222
"install-global": "npm install -g"
2323
},
2424
"engines": {
25-
"node": ">=12.5.0"
25+
"node": ">=14.15.0"
2626
},
2727
"dependencies": {
2828
"axios": "^0.26.0",

packages/blast-core/template/blast.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
/* eslint-disable object-curly-newline */
22
module.exports.config = {
33
addressPrefix: 'cudos',
4-
gasPrice: '250acudos',
54
rustOptimizerVersion: '0.12.6',
5+
gasPrice: '250acudos',
66

77
// optional parameners
8+
gasLimit: 'auto',
9+
gasMultiplier: 'auto',
810
additionalAccounts: 0,
911
customAccountBalances: 1000000000000000000,
1012
networks: {

packages/blast-core/template/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"author": "",
1010
"license": "ISC",
11-
"dependencies": {
12-
"@cosmjs/stargate": "^0.28.3"
11+
"devDependencies": {
12+
"cudos-blast": "2.0.0"
1313
}
1414
}

packages/blast-core/template/tests/alpha.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const bre = require('cudos-blast')
22

33
describe('alpha contract', () => {
44
// Optional timeout. Default is 15000
5-
jest.setTimeout(30 * 1000);
5+
jest.setTimeout(30 * 1000)
66

77
const MSG_INIT = { count: 13 }
88
const MSG_INCREMENT = { increment: {} }

packages/blast-core/utilities/account-utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ const BlastError = require('./blast-error')
88

99
let localAccounts
1010

11+
// set localAccounts once and when needed
1112
function getLocalAccounts() {
12-
if (!localAccounts) {
13+
if (typeof localAccounts === 'undefined') {
1314
const configPath = path.join(getProjectRootPath(), 'local-accounts.json')
1415
localAccounts = Object.values(require(configPath))
1516
}

packages/blast-core/utilities/blast-helper.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { DEFAULT_DENOM } = require('../config/blast-constants')
12

23
async function delay(n) {
34
return new Promise(function(resolve) {
@@ -7,7 +8,7 @@ async function delay(n) {
78

89
function transferTokensByNameCommand(fromName, toName, amount) {
910
return `cudos-noded tx bank send ${fromName} $(cudos-noded keys show ${toName} --keyring-backend test -a) ` +
10-
`${amount}acudos --keyring-backend test --chain-id cudos-network --yes`
11+
`${amount}${DEFAULT_DENOM} --keyring-backend test --chain-id cudos-network --yes`
1112
}
1213

1314
module.exports = {

packages/blast-core/utilities/config-utils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const fs = require('fs')
22
const path = require('path')
33
const BlastError = require('./blast-error')
4-
const { localNetwork } = require('../config/blast-constants')
4+
const { LOCAL_NETWORK } = require('../config/blast-constants')
55
const { getProjectRootPath } = require('./package-info')
66

77
// creating global Blast runtime environment to hold exposed core fuctions and possible plugins
@@ -28,7 +28,7 @@ function getNetwork(network) {
2828
return config.networks[network]
2929
}
3030
// network is not passed - return the local network
31-
return localNetwork
31+
return LOCAL_NETWORK
3232
}
3333

3434
function getGasPrice() {
@@ -73,6 +73,7 @@ function getRustOptimizerVersion() {
7373
}
7474

7575
module.exports = {
76+
getConfig: getConfig,
7677
getGasPrice: getGasPrice,
7778
getAddressPrefix: getAddressPrefix,
7879
getAdditionalAccounts: getAdditionalAccounts,

packages/blast-core/utilities/network-utils.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
const {
22
CosmWasmClient,
33
DirectSecp256k1HdWallet,
4-
SigningCosmWasmClient
4+
SigningCosmWasmClient,
5+
GasPrice
56
} = require('cudosjs')
6-
const { localNetwork } = require('../config/blast-constants')
7+
const { LOCAL_NETWORK } = require('../config/blast-constants')
78
const {
89
getNetwork,
9-
getAddressPrefix
10+
getAddressPrefix,
11+
getGasPrice
1012
} = require('./config-utils')
1113
const {
1214
getLocalAccounts,
@@ -18,7 +20,9 @@ const nodeUrl = getNetwork(process.env.BLAST_NETWORK)
1820

1921
async function getSigner(mnemonic) {
2022
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix: getAddressPrefix() })
21-
const signer = await SigningCosmWasmClient.connectWithSigner(nodeUrl, wallet)
23+
// gasPrice in signing client is considered only when auto gas is used
24+
const signer = await SigningCosmWasmClient.connectWithSigner(
25+
nodeUrl, wallet, { gasPrice: GasPrice.fromString(getGasPrice()) })
2226
const address = (await wallet.getAccounts())[0].address
2327
signer.address = address
2428
return signer
@@ -33,7 +37,7 @@ async function getDefaultSigner() {
3337
}
3438

3539
function getAccounts() {
36-
return (nodeUrl === localNetwork ? getLocalAccounts() : getPrivateAccounts())
40+
return (nodeUrl === LOCAL_NETWORK ? getLocalAccounts() : getPrivateAccounts())
3741
}
3842

3943
async function getContractInfo(contractAddress) {

0 commit comments

Comments
 (0)