Skip to content
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

Feature/test chain util fix #176

Merged
merged 4 commits into from
Jul 5, 2024
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
1 change: 0 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
}
]
],
"plugins": ["rewire"],
"ignore": ["dist/*.js"],
"env": {
"test": {
Expand Down
23 changes: 18 additions & 5 deletions build/webpack.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

/* eslint-env node */
const { merge } = require('webpack-merge');
const webpack = require('webpack');
const baseConfig = require('./webpack.common');
const {OUTPUT_PATH} = require('./utils');
const { OUTPUT_PATH } = require('./utils');

const browserConfig = {
mode: 'production',
Expand All @@ -16,12 +17,13 @@ const browserConfig = {
library: 'AElf',
libraryTarget: 'umd',
libraryExport: 'default',
globalObject: "globalThis",
globalObject: 'globalThis',
umdNamedDefine: true
},
resolve: {
alias: {},
fallback: {
process: false,
assert: require.resolve('assert'),
buffer: require.resolve('buffer'),
crypto: require.resolve('crypto-browserify'),
Expand All @@ -47,14 +49,25 @@ const browserConfig = {
}
},
target: 'web',
node: {
global: true
},
optimization: {
removeEmptyChunks: true,
chunkIds: 'total-size',
moduleIds: 'size',
sideEffects: true,
minimize: true
}
minimize: false
},
plugins: [
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer']
}),
// fix "process is not defined" error:
new webpack.ProvidePlugin({
process: 'process/browser'
})
]
};


module.exports = merge(baseConfig, browserConfig);
9 changes: 3 additions & 6 deletions build/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@
/* eslint-env node */
const path = require('path');
const webpack = require('webpack');
const {ROOT} = require('./utils');
const {version, name} = require(path.resolve(ROOT, './package.json'));
const { ROOT } = require('./utils');
const { version, name } = require(path.resolve(ROOT, './package.json'));

const banner = `${name}.js v${version} \n(c) 2019-${new Date().getFullYear()} AElf \nReleased under MIT License`;

const baseConfig = {
entry: path.resolve(ROOT, 'src/index.js'),
devtool: 'source-map',
resolve: {
modules: [
path.resolve(ROOT, 'src'),
'node_modules'
],
modules: [path.resolve(ROOT, 'src'), 'node_modules'],
extensions: ['.ts', '.js']
},
module: {
Expand Down
10 changes: 5 additions & 5 deletions build/webpack.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
/* eslint-env node */
const { merge } = require('webpack-merge');
const baseConfig = require('./webpack.common');
const {OUTPUT_PATH} = require('./utils');
const { OUTPUT_PATH } = require('./utils');

const nodeConfig = {
const nodeConfig = {
mode: 'production',
output: {
path: OUTPUT_PATH,
filename: 'aelf.cjs.js',
library: 'AElf',
libraryTarget: 'commonjs2',
library: {
type: 'commonjs2'
},
libraryExport: 'default'
},
resolve: {
Expand All @@ -32,5 +33,4 @@ const nodeConfig = {
}
};


module.exports = merge(baseConfig, nodeConfig);
119 changes: 54 additions & 65 deletions examples/browser/index.html
Original file line number Diff line number Diff line change
@@ -1,71 +1,60 @@
<!DOCTYPE html>
<html>
<head>
<title>Hello AElf</title>
</head>
<body>
<p>Hello AElf.</p>

<script type="text/javascript" src="../../dist/aelf.umd.js"></script>
<script type="text/javascript">
const AElf = window.AElf;
const Wallet = AElf.wallet;
const sha256 = AElf.utils.sha256;
<head>
<title>Hello AElf</title>
</head>

const wallet = AElf.wallet.createNewWallet();
const walletGetByKey = AElf.wallet.getWalletByPrivateKey(wallet.privateKey);
const walletGetByMn = AElf.wallet.getWalletByMnemonic(wallet.mnemonic);
console.log(wallet.address);
console.log(walletGetByKey);
console.log(walletGetByMn);
// link to Blockchain
const aelf = new AElf(new AElf.providers.HttpProvider('http://18.162.41.20:8000'));
if (!aelf.isConnected()) {
alert('Blockchain Node is not running.');
}
<body>
<p>Hello AElf.</p>

// the contract you want to query
const tokenContractName = 'AElf.ContractNames.Token';
const {
// directly accessible information
GenesisContractAddress
} = aelf.chain.getChainStatus({ sync: true });
aelf.chain
.contractAt(GenesisContractAddress, wallet)
.then(zeroC => {
// return contract's address which you query by contract's name
return zeroC.GetContractAddressByName.call(sha256(tokenContractName));
})
.then(tokenContractAddress => {
// return contract's instance and you can call the methods on this instance
return aelf.chain.contractAt(tokenContractAddress, wallet, {
refBlockNumberStrategy: -8
});
})
.then(async tokenContract => {
// return token's info
const result = await tokenContract.GetTokenInfo.call({
symbol: 'ELF'
});
console.log(result, 'view token(ELF) info');
return tokenContract;
})
.then(async tokenContract => {
const sendResult = tokenContract.Transfer(
{
symbol: 'ELF',
// memo: '',
to: 'ELF_s3tE7DA1TNndCKsXM4frMAsYfiaU3kXc1obVxxSJ2bhQEC7HA_tDVW',
amount: 100
},
{
refBlockNumberStrategy: -16,
sync: true
}
);
console.log(sendResult, 'sendResult==');
// alert(JSON.stringify(res));
<script type="text/javascript" src="../../dist/aelf.umd.js"></script>
<script type="text/javascript">
const AElf = window.AElf;
const Wallet = AElf.wallet;
const sha256 = AElf.utils.sha256;

const wallet = AElf.wallet.createNewWallet();
const walletGotByKey = AElf.wallet.getWalletByPrivateKey(wallet.privateKey);
const walletGotByMn = AElf.wallet.getWalletByMnemonic(wallet.mnemonic);
console.log(wallet.address);
console.log(walletGotByKey);
console.log(walletGotByMn);
// link to Blockchain
const aelf = new AElf(new AElf.providers.HttpProvider(
'https://tdvw-test-node.aelf.io/'
));

if (!aelf.isConnected()) {
alert('Blockchain Node is not running.');
}

// the contract you want to query
const tokenContractName = 'AElf.ContractNames.Token';
const {
// directly accessible information
GenesisContractAddress
} = aelf.chain.getChainStatus({ sync: true });
aelf.chain.contractAt(GenesisContractAddress,wallet)
.then(zeroC => {
// return contract's address which you query by contract's name
return zeroC.GetContractAddressByName.call(sha256(tokenContractName));
})
.then(tokenContractAddress => {
// return contract's instance and you can call the methods on this instance
return aelf.chain.contractAt(tokenContractAddress,wallet);
})
.then(tokenContract => {
// return token's info
return tokenContract.GetTokenInfo.call({
symbol: 'ELF'
});
</script>
</body>
</html>
})
.then(res => {
console.log(res);
alert(JSON.stringify(res));
});
</script>
</body>

</html>
8 changes: 4 additions & 4 deletions examples/node/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-env node */
const AElf = require('../../dist/aelf.cjs');

const Wallet = AElf.wallet;
const { sha256 } = AElf.utils;

Expand All @@ -10,7 +9,7 @@ const defaultPrivateKey = 'bdb3b39ef4cd18c2697a920eb6d9e8c3cf1a930570beb37d04fb5
// const walletCreatedByMethod = Wallet.createNewWallet();
const wallet = Wallet.getWalletByPrivateKey(defaultPrivateKey);
// link to Blockchain
const aelf = new AElf(new AElf.providers.HttpProvider('http://18.162.41.20:8000'));
const aelf = new AElf(new AElf.providers.HttpProvider('https://tdvw-test-node.aelf.io/'));

if (!aelf.isConnected()) {
console.log('Blockchain Node is not running.');
Expand All @@ -21,8 +20,9 @@ const tokenContractName = 'AElf.ContractNames.Token';
const {
// directly accessible information
GenesisContractAddress
} = aelf.chain.getChainStatus({sync: true});
aelf.chain.contractAt(GenesisContractAddress, wallet)
} = aelf.chain.getChainStatus({ sync: true });
aelf.chain
.contractAt(GenesisContractAddress, wallet)
.then(zeroC => {
// return contract's address which you query by contract's name
return zeroC.GetContractAddressByName.call(sha256(tokenContractName));
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@
"assert": "^2.0.0",
"babel-plugin-rewire": "^1.2.0",
"bignumber.js": "^9.0.0",
"crypto-browserify": "^3.12.0",
"gbk-string": "^0.1.0",
"isomorphic-fetch": "^3.0.0",
"js-sha256": "^0.9.0",
"keccak": "^3.0.3",
"bip39": "^3.0.2",
"bn.js": "^5.2.1",
"browserify-cipher": "^1.0.1",
"bs58": "^4.0.1",
"buffer": "^5.2.1",
"crypto-browserify": "^3.12.0",
"crypto-js": "^4.2.0",
"elliptic": "^6.4.1",
"gbk-string": "^0.1.0",
"hdkey": "^1.1.1",
"isomorphic-fetch": "^3.0.0",
"js-sha256": "^0.9.0",
"keccak": "^3.0.3",
"node-fetch": "2",
"query-string": "5.1.1",
"randombytes": "^2.1.0",
Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class AElf {
static version = process.env.SDK_VERSION;

static providers = {
HttpProvider,
HttpProvider
};

/**
Expand All @@ -41,11 +41,11 @@ export default class AElf {
...utils,
...bloom,
sha256,
transform,
transform
};

providers = {
HttpProvider,
HttpProvider
};

settings = new Settings();
Expand All @@ -55,7 +55,7 @@ export default class AElf {
* @type {{api: string}}
*/
version = {
api: process.env.SDK_VERSION,
api: process.env.SDK_VERSION
};

/**
Expand Down
Loading
Loading