Skip to content

Commit

Permalink
add method CRC20 to Conflux object to easy communicate with token (#117)
Browse files Browse the repository at this point in the history
* add crc20 method

* optimize format.address method

* abi encode method auto completetion for fixed-size bytes

* sync with fullnode trace method update

Co-authored-by: PanaW <wangpan@conflux-chain.org>
  • Loading branch information
Pana and PanaW authored Apr 26, 2021
1 parent e8022e4 commit faec50e
Show file tree
Hide file tree
Showing 14 changed files with 455 additions and 26 deletions.
12 changes: 12 additions & 0 deletions CHANGE_LOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# change log

## v1.16.0
This version is corresponding to conflux-rust v1.1.3, check it's [changelog](https://github.com/Conflux-Chain/conflux-rust/blob/master/changelogs/CHANGELOG-1.1.x.md#113) for detail info.

* `format.address` will respect `networkId`, `verbose` flag even if the first parameter is an CIP37 address.
* Add support for standard token contract through `Conflux.CRC20`
* `cfx_getLogs` filter option add one more field `offset`
* Add one RPC method `cfx_getAccountPendingInfo` to get account's transaction pending info
* `epochs` pubsub now accept one parameter `subscription_epoch` the supported values are `latest_mined` (default) and `latest_state`
* Include `blockHash`, `epochHash`, `epochNumber`, `transactionHash`, and `transactionPosition` for trace RPCs
* When abi encoding `bytes-N` type, if the data's length is not enough, will auto pad (right) to `N`


## v1.5.13

* `getStatus` method rethurn three new fields `latestState`, `latestConfirmed`, `latestCheckpoint`
Expand Down
2 changes: 1 addition & 1 deletion example/2_send_transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const conflux = new Conflux({
// logger: console, // use console to print log
});

const accountAlice = conflux.wallet.addPrivateKey('0xa816a06117e572ca7ae2f786a046d2bc478051d0717bf5cc4f5397923258d393');
const accountAlice = conflux.wallet.addPrivateKey('0xa816a06117e572ca7ae2f786a046d2bc478051d0717bf5cc4f5397923258d393', 1);
const addressBob = 'cfxtest:aatm5bvugvjwdyp86ruecmhf5vmng5ysy2pehzpz9h';

/*
Expand Down
58 changes: 58 additions & 0 deletions example/wrapProvider/work-with-portal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<html>
<head>
<title>Test sign</title>
<script src='/dist/js-conflux-sdk.umd.min.js'></script>
<!-- <script src='https://cdn.jsdelivr.net/npm/js-conflux-sdk/dist/js-conflux-sdk.umd.min.js'></script> -->
</head>
<body>
<button class="enableEthereumButton">Enable Portal</button>
<h2>Account: <span class="showAccount"></span></h2>
<script>
const ethereumButton = document.querySelector('.enableEthereumButton');
const showAccount = document.querySelector('.showAccount');

ethereumButton.addEventListener('click', () => {
getAccount();
});

let account;

async function getAccount() {
const accounts = await conflux.enable();
account = accounts[0];
showAccount.innerHTML = account;
}
</script>

<br/><br/>
<button class="sendTxBtn2">SendTx with js-conflux-sdk</button>
<script>

// let url = 'https://testnet-rpc.conflux-chain.org.cn/v2';
let cfx = new Conflux.Conflux({
url,
networkId: 1,
logger: console,
});
cfx.provider = conflux;
const sendTxButton2 = document.querySelector('.sendTxBtn2');
sendTxButton2.addEventListener('click', async () => {
let nonce = await cfx.getStatus();
console.log("status", nonce);
return;
try {
let b = await cfx.getBalance('cfxtest:aak2rra2njvd77ezwjvx04kkds9fzagfe6d5r8e957');
console.log("balance", b.toString());
let hash = await cfx.sendTransaction({
from: account,
to: '0x0000000000000000000000000000000000000000',
value: '0x01',
});
console.log(hash);
} catch(e) {
console.error(e) ;
}
});
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "js-conflux-sdk",
"description": "JavaScript Conflux Software Development Kit",
"version": "1.5.15",
"version": "1.6.0",
"license": "LGPL-3.0",
"author": "Haizhou@conflux-chain.org",
"repository": "https://github.com/Conflux-Chain/js-conflux-sdk.git",
Expand Down
35 changes: 33 additions & 2 deletions src/Conflux.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const providerFactory = require('./provider');
const Wallet = require('./wallet');
const Contract = require('./contract');
const internalContract = require('./contract/internal');
const { CRC20_ABI } = require('./contract/standard');
const PendingTransaction = require('./subscribe/PendingTransaction');
const Subscription = require('./subscribe/Subscription');
const pkg = require('../package.json');
Expand Down Expand Up @@ -180,6 +181,16 @@ class Conflux {
return this.Contract(options);
}

/**
* Create an token CRC20 contract with standard CRC20 abi
*
* @param address {string}
* @returns {Contract} - A token contract instance
*/
CRC20(address) {
return this.Contract({ address, abi: CRC20_ABI });
}

/**
* close connection.
*
Expand Down Expand Up @@ -1047,6 +1058,24 @@ class Conflux {
return format.sponsorInfo(result);
}

/**
* Return pending info of an account
*
* @param address {string} - Address to account
* @returns {Promise<object>} An account pending info object.
* - localNonce `BigInt`: then next nonce can use in the transaction pool
* - nextPendingTx `string`: the hash of next pending transaction
* - pendingCount `BigInt`: the count of pending transactions
* - pendingNonce `BigInt`: the nonce of pending transaction
*
*/
async getAccountPendingInfo(address) {
const result = await this.provider.call('cfx_getAccountPendingInfo',
this._formatAddress(address),
);
return format.accountPendingInfo(result);
}

/**
* Returns the size of the collateral storage of given address, in Byte.
*
Expand Down Expand Up @@ -1292,6 +1321,8 @@ class Conflux {
* If you see the same epoch twice, this suggests a pivot chain reorg has happened (this might happen for recent epochs).
* For each epoch, the last hash in epochHashesOrdered is the hash of the pivot block.
*
* @param [sub_epoch] {string} Available values are latest_mined(default value) and latest_state
*
* @return {Promise<Subscription>} EventEmitter instance with the follow events:
* - 'data':
* - epochNumber `number`: epoch number
Expand All @@ -1316,8 +1347,8 @@ class Conflux {
]
}
*/
async subscribeEpochs() {
const id = await this.subscribe('epochs');
async subscribeEpochs(sub_epoch = CONST.EPOCH_NUMBER.LATEST_MINED) {
const id = await this.subscribe('epochs', sub_epoch);
const subscription = new Subscription(id);

this.provider.on(id, data => {
Expand Down
4 changes: 2 additions & 2 deletions src/Drip.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class Drip extends String {
* @return {Drip}
*
* @example
* > Drip(1.00)
* > new Drip(1.00)
[String (Drip): '1']
* > Drip('0xab')
* > new Drip('0xab')
[String (Drip): '171']
*/
constructor(value) {
Expand Down
19 changes: 12 additions & 7 deletions src/contract/abi/BytesCoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,18 @@ class BytesCoder extends BaseCoder {
encode(value) {
value = format.bytes(value);

if (this.size !== undefined) {
assert(value.length === this.size, {
message: 'length not match',
expect: this.size,
got: value.length,
coder: this,
});
if (this.size !== undefined && this.size !== value.length) {
if (value.length < this.size) {
// if short than the expect size, auto complete it
value = Buffer.concat([value, Buffer.alloc(this.size - value.length)]);
} else {
assert(false, {
message: 'length not match',
expect: this.size,
got: value.length,
coder: this,
});
}
}

let buffer = alignBuffer(value, true);
Expand Down
Loading

0 comments on commit faec50e

Please sign in to comment.