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

Commit

Permalink
Merge branch 'cudos-dev' into CUDOS-553
Browse files Browse the repository at this point in the history
  • Loading branch information
aemil145 committed May 13, 2022
2 parents 5ed1e40 + e196e46 commit acfa77d
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 143 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
packages/blast-core/README.md
packages/blast-core/README.md
53 changes: 45 additions & 8 deletions packages/blast-core/cmd/node/node.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
const {
executeCompose,
executeComposeAsync,
checkDockerStatus
checkDockerStatus,
executeNodeMultiCmd
} = require('../../utilities/run-docker-commands')
const {
getNodeStatus,
checkNodeOnline,
checkNodeOffline
} = require('../../utilities/get-node-status')
const { getAdditionalAccounts } = require('../../utilities/config-utils')
const { createAdditionalAccounts } = require('../../utilities/account-utils')
const { delay } = require('../../utilities/blast-helper')
const {
generateRandomAccount,
createLocalAccountsFile
} = require('../../utilities/account-utils')
const {
getAdditionalAccounts,
getAdditionalAccountsBalances,
getAddressPrefix
} = require('../../utilities/config-utils')
const {
delay,
transferTokensByNameCommand
} = require('../../utilities/blast-helper')
const BlastError = require('../../utilities/blast-error')

const startNodeCmd = async function(argv) {
Expand All @@ -25,11 +36,17 @@ const startNodeCmd = async function(argv) {
await waitForRunningNode()
console.log('Cudos Blast local node is ready')

const additionalAccounts = getAdditionalAccounts()
if (additionalAccounts > 0) {
console.log('Creating additional accounts ...')
await createAdditionalAccounts(additionalAccounts)
const additionalAccounts = await addAdditionalAccountsToNode()

// get all local accounts by merging default with additional ones
let localAccounts = require('../../blast-config/default-accounts.json')
localAccounts = {
...localAccounts,
...additionalAccounts
}

createLocalAccountsFile(localAccounts)
console.log('Local accounts information file created')
}

const stopNodeCmd = async function() {
Expand Down Expand Up @@ -59,6 +76,26 @@ async function waitForRunningNode() {
await delay(4)
}

async function addAdditionalAccountsToNode() {
const numberOfAdditionalAccounts = getAdditionalAccounts()
const additionalAccounts = {}
if (numberOfAdditionalAccounts > 0) {
const customBalance = getAdditionalAccountsBalances()
const addressPrefix = getAddressPrefix()
for (let i = 1; i <= numberOfAdditionalAccounts; i++) {
additionalAccounts[`account${10 + i}`] = await generateRandomAccount(addressPrefix)

// add new account from mnemonic to the local node and fund it
executeNodeMultiCmd(
`echo ${additionalAccounts[`account${10 + i}`].mnemonic} | ` +
`cudos-noded keys add account${10 + i} --recover --keyring-backend test && ` +
transferTokensByNameCommand('faucet', `account${10 + i}`, `${customBalance}`)
)
}
}
return additionalAccounts
}

module.exports = {
startNodeCmd: startNodeCmd,
stopNodeCmd: stopNodeCmd,
Expand Down
6 changes: 4 additions & 2 deletions packages/blast-core/lib/bre.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ const {
} = require('cudosjs')
const {
getNetwork,
getAddressPrefix,
getAddressPrefix
} = require('../utilities/config-utils')
const {
getAccounts,
getPrivateAccounts
} = require('../utilities/config-utils')
} = require('../utilities/account-utils')

const nodeUrl = getNetwork(process.env.BLAST_NETWORK)
const accounts = getAccounts()
Expand Down
7 changes: 2 additions & 5 deletions packages/blast-core/lib/cudos-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports.CudosContract = class CudosContract {
}
}

async deploy(initMsg, funds, label = this.#contractLabel) {
async deploy(initMsg, signer = this.#signer, label = this.#contractLabel, funds) {
this.#signer = signer
const uploadTx = await this.#uploadContract()
const initTx = await this.#initContract(uploadTx.codeId, initMsg, label, funds)
this.#contractAddress = initTx.contractAddress
Expand All @@ -46,10 +47,6 @@ module.exports.CudosContract = class CudosContract {
return await signer.queryContractSmart(this.#contractAddress, queryMsg)
}

connectSigner(signer) {
this.#signer = signer
}

getAddress() {
return this.#contractAddress
}
Expand Down
52 changes: 0 additions & 52 deletions packages/blast-core/template/accounts.json

This file was deleted.

4 changes: 2 additions & 2 deletions packages/blast-core/template/scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
async function main () {
const [alice, bob] = await bre.getSigners()
const contract = await bre.getContractFactory('alpha', bob)
const contract = await bre.getContractFactory('alpha')

const MSG_INIT = { count: 13 }
const deploy = await contract.deploy(MSG_INIT)
const deploy = await contract.deploy(MSG_INIT, bob)
const contractAddress = deploy.initTx.contractAddress
console.log(`Contract deployed at: ${contractAddress}`)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/blast-core/template/scripts/interact.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
async function main() {
const [alice, bob] = await bre.getSigners()
const contract = await bre.getContractFromAddress('cudos1uul3yzm2lgskp3dxpj0zg558hppxk6pt8t00qe')
const contract = await bre.getContractFromAddress('cudos1uul3yzm2lgskp3dxpj0zg558hppxk6pt8t00qe', bob)

const QUERY_GET_COUNT = { get_count: {} }
let count = await contract.query(QUERY_GET_COUNT)
console.log('Initial count: ' + count.count)

const MSG_INCREMENT = { increment: {} }
const result = await contract.execute(MSG_INCREMENT, bob)
const result = await contract.execute(MSG_INCREMENT)
console.log(result)

count = await contract.query(QUERY_GET_COUNT, alice)
Expand Down
7 changes: 5 additions & 2 deletions packages/blast-core/template/tests/alpha.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ describe('alpha contract', () => {

let alice, bob, contract

// Optional timeout. Default is 15000
jest.setTimeout(30 * 1000);

beforeAll(async () => {
[alice, bob] = await bre.getSigners()
contract = await bre.getContractFactory('alpha', bob)
await contract.deploy(MSG_INIT)
contract = await bre.getContractFactory('alpha')
await contract.deploy(MSG_INIT, bob)
})

test('increment count', async () => {
Expand Down
69 changes: 32 additions & 37 deletions packages/blast-core/utilities/account-utils.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
const bip39 = require('bip39')
const fs = require('fs')
const path = require('path')
const bip39 = require('bip39')
const { DirectSecp256k1HdWallet } = require('cudosjs')

const { executeNodeMultiCmd } = require('./run-docker-commands')
const defaultAccounts = require('../config/default-accounts.json')
const { transferTokensByNameCommand } = require('./blast-helper')
const { getProjectRootPath } = require('./package-info')
const BlastError = require('./blast-error')
const {
getAdditionalAccountsBalances,
getAddressPrefix
} = require('./config-utils')

async function createAdditionalAccounts(numberOfAdditionalAccounts) {
const accounts = {}
const customBalance = getAdditionalAccountsBalances()
const addressPrefix = getAddressPrefix()
for (let i = 1; i <= numberOfAdditionalAccounts; i++) {
const mnemonic = bip39.generateMnemonic(256)
const address = await getAddressFromMnemonic(mnemonic, addressPrefix)
accounts[`account${10 + i}`] = {
mnemonic: mnemonic,
address: address
}
function getAccounts() {
const configPath = path.join(getProjectRootPath(), 'accounts.json')
return Object.values(require(configPath))
}

function getPrivateAccounts() {
const configPath = path.join(getProjectRootPath(), 'private-accounts.json')
const privateAccounts = require(configPath)
delete privateAccounts.comment
return privateAccounts
}

executeNodeMultiCmd(
`echo ${mnemonic} | cudos-noded keys add account${10 + i} --recover --keyring-backend test && ` +
transferTokensByNameCommand('faucet', `account${10 + i}`, `${customBalance}`)
)
async function generateRandomAccount(addressPrefix) {
const mnemonic = bip39.generateMnemonic(256)
const address = await getAddressFromMnemonic(mnemonic, addressPrefix)
return {
mnemonic: mnemonic,
address: address
}
const accountsToSave = combineAccountObjects(defaultAccounts, accounts)
saveAccounts(accountsToSave)
}

async function getAddressFromMnemonic(mnemonic, addressPrefix) {
Expand All @@ -39,20 +33,21 @@ async function getAddressFromMnemonic(mnemonic, addressPrefix) {
return acc.address
}

function combineAccountObjects(defaultAccounts, newAccounts) {
const prepareDefaultAccounts = JSON.stringify(defaultAccounts).slice(0, -1) + ','
const prepareNewAccounts = JSON.stringify(newAccounts).substring(1)
return prepareDefaultAccounts.concat(prepareNewAccounts)
}

function saveAccounts(accounts) {
const projectRoot = getProjectRootPath()
const parsed = JSON.parse(accounts)
function createLocalAccountsFile(accounts) {
const accountFilePath = path.join(getProjectRootPath(), 'accounts.json')
// delete accounts file if exists
fs.rmSync(accountFilePath, { force: true })
try {
fs.writeFileSync(`${projectRoot}/accounts.json`, JSON.stringify(parsed, 0, 4))
// create accounts file as read-only
fs.writeFileSync(accountFilePath, JSON.stringify(accounts, 0, 4), { mode: 0o444 })
} catch (error) {
throw new BlastError(`Failed to create file at ${projectRoot}/additional-accounts.json with error: ${error}`)
throw new BlastError(`Failed to create file at ${accountFilePath} with error: ${error}`)
}
}

module.exports = { createAdditionalAccounts: createAdditionalAccounts }
module.exports = {
getAccounts: getAccounts,
getPrivateAccounts: getPrivateAccounts,
generateRandomAccount: generateRandomAccount,
createLocalAccountsFile: createLocalAccountsFile
}
21 changes: 1 addition & 20 deletions packages/blast-core/utilities/config-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,11 @@ function getRustOptimizerVersion() {
return config.rustOptimizerVersion
}

// Accounts config utils

function getAccounts() {
const configPath = path.join(getProjectRootPath(), 'accounts.json')
return Object.values(require(configPath))
}

// Private accounts config utils

function getPrivateAccounts() {
const configPath = path.join(getProjectRootPath(), 'private-accounts.json')
const privateAccounts = require(configPath)
delete privateAccounts.comment
return privateAccounts
}

module.exports = {
getGasPrice: getGasPrice,
getAddressPrefix: getAddressPrefix,
getAdditionalAccounts: getAdditionalAccounts,
getAdditionalAccountsBalances: getAdditionalAccountsBalances,
getRustOptimizerVersion: getRustOptimizerVersion,
getNetwork: getNetwork,
getAccounts: getAccounts,
getPrivateAccounts: getPrivateAccounts,
getConfig: getConfig
getNetwork: getNetwork
}
8 changes: 5 additions & 3 deletions packages/e2e-tests/tests/compile-run.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ source ./vars.sh

init_folder="$INIT_FOLDER-compile"
cp -R $PATH_TO_TEMPLATE $init_folder &> /dev/null
#manually supply the testing folder with accounts.json
cp -f $DEFAULT_ACCOUNTS_FILE_PATH "$init_folder/accounts.json"
cd $init_folder

echo -n 'blast compile...'
Expand Down Expand Up @@ -45,10 +47,10 @@ fi

echo -n 'deploy and fund contract...'

perl -pi -e $'s|defaultNetwork: \'https://an-inhospitable-node.cudos.org:26657\'|defaultNetwork: \'\'|' blast.config.js
# tweak the deploy script to get cudos and pass it to the deploy function
perl -i -pe $'if($. == 1) {s||const { coin } = require(\'\@cosmjs/stargate\');\n\n|}' ./scripts/deploy.js
perl -i -pe $'if($. == 4) {s|| const fund = [coin(321, \'acudos\')];\n|}' ./scripts/deploy.js
perl -pi -e 's|\(MSG_INIT\)|(MSG_INIT, fund)|' ./scripts/deploy.js
perl -pi -e 's|deploy\(MSG_INIT|deploy(MSG_INIT, undefined, undefined, fund|' ./scripts/deploy.js

deployed_contract=`blast run ./scripts/deploy.js`
if [[ $deployed_contract =~ 'cudos' ]]; then
Expand All @@ -69,5 +71,5 @@ else
exit_status=1
fi

rm -r ./$init_folder &> /dev/null || true
rm -r -f ./$init_folder &> /dev/null || true
exit $exit_status
2 changes: 1 addition & 1 deletion packages/e2e-tests/tests/init-d.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ else
exit_status=1
fi

rm -r ../$INIT_FOLDER &> /dev/null
rm -r -f ../$INIT_FOLDER &> /dev/null
exit $exit_status
2 changes: 1 addition & 1 deletion packages/e2e-tests/tests/init.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ else
exit_status=1
fi

rm -r ../$INIT_FOLDER &> /dev/null
rm -r -f ../$INIT_FOLDER &> /dev/null
exit $exit_status
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ else
echo -e $PASSED
fi

rm -r $init_folder &> /dev/null || true
rm -r -f $init_folder &> /dev/null || true
exit $exit_status
2 changes: 1 addition & 1 deletion packages/e2e-tests/tests/node-start-status.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ if [[ $exit_status != 1 ]]; then
fi
fi

rm -r ../$init_folder &> /dev/null || true
rm -r -f ../$init_folder &> /dev/null || true
exit $exit_status
Loading

0 comments on commit acfa77d

Please sign in to comment.