Skip to content

Commit

Permalink
[v0.1.4] Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
GinMu committed Jul 5, 2019
1 parent 612d6c0 commit 7bda843
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 28 deletions.
44 changes: 26 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,19 @@ npm i jcc-ethereum-utils

## CDN

`jcc_ethereum_utils` as a global variable
`jcc_ethereum_utils` as a global variable.

```javascript
<script src="https://unpkg.com/jcc-ethereum-utils/dist/jcc-ethereum-utils.min.js"></script>
```

## Usage

```javascript
Breaking changes since 0.1.4, if you used 0.1.3 see [this demo](https://github.com/JCCDex/jcc-ethereum-utils/blob/master/docs/demo_below_0.1.4.md).

```javascript
// demo
import { EtherFingate, Erc20Fingate } from "jcc-ethereum-utils";
import { Fingate, Ethereum, ERC20 } from "jcc-ethereum-utils";

// Ethereum node
const node = "https://eth626892d.jccdex.cn";
Expand All @@ -72,18 +73,21 @@ const scAddress = '0x3907acb4c1818adf72d965c08e0a79af16e7ffb8';

try {
// deposit 1 ETH
const inst = new EtherFingate(node, production);
inst.initEtherContract(scAddress);
const ethereumInstance = new Ethereum(node, true);
ethereumInstance.initWeb3();

const fingateInstance = new Fingate();
fingateInstance.init(scAddress, ethereumInstance);

// Check if has pending order, if has don't call the next deposit api
const state = await inst.depositState(moacAddress);
const state = await fingateInstance.depositState(ethereumAddress);

if (inst.isPending(state)) {
if (fingateInstance.isPending(state)) {
return;
}

// start to transfer 1 ETH to fingate address
const hash = await inst.deposit(ethereumSecret, swtcAddress, amount);
const hash = await fingateInstance.deposit(ethereumSecret, swtcAddress, amount);
console.log(hash);
} catch (error) {
console.log(error);
Expand All @@ -97,24 +101,29 @@ try {
// JCC contract address
const jccContractAddress = "0x9BD4810a407812042F938d2f69f673843301cfa6";

const inst = new Erc20Fingate(node, production);
const ethereumInstance = new Ethereum(node, true);
ethereumInstance.initWeb3();

inst.initErc20Contract(scAddress, jccContractAddress);
const erc20Instance = new ERC20();
erc20Instance.init(jcContract, ethereumInstance);

const fingateInstance = new Fingate();
fingateInstance.init(scAddress, ethereumInstance);

// Check if has pending order, if has don't call transfer api
const state = await inst.depositState(ethereumAddress, jccContractAddress);
const state = await fingateInstance.depositState(ethereumAddress, jccContractAddress);

if (inst.isPending(state)) {
if (fingateInstance.isPending(state)) {
return;
}
// Please set decimals of erc20 token, if not will throw an error.
inst.decimals = 18;

// The first step to transfer 1 JCC to jcc contract address.
const transferHash = await inst.transfer(amount, secret);
const decimals = await erc20Instance.decimals();

// The first step to transfer 1 JCC to fingate address.
const transferHash = await erc20Instance.transfer(ethereumAddress, scAddress, amount);

// The next step to submit previous transfer hash.
const depositHash = await inst.depositToken(swtcAddress, amount, transferHash, ethereumSecret);
const depositHash = await fingateInstance.depositToken(swtcAddress, jccContractAddress, decimals, amount, transferHash, ethereumSecret);
console.log(depositHash);

// Warning:
Expand All @@ -124,5 +133,4 @@ try {
} catch (error) {
console.log(error);
}

```
88 changes: 88 additions & 0 deletions docs/demo_below_0.1.4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Usage

```shell
npm i jcc-ethereum-utils@0.1.3
```

```javascript

// demo
import { EtherFingate, Erc20Fingate } from "jcc-ethereum-utils";

// Ethereum node
const node = "https://eth626892d.jccdex.cn";

// Production network or not
const production = true;

// Your ethereum secret
const ethereumSecret = '';

// Your ethereum address
const ethereumAddress = '';

// Your swtc address
const swtcAddress = '';

// Deposit amount
const amount = "1";

// Ethereum fingate contract address, don't change it.
const scAddress = '0x3907acb4c1818adf72d965c08e0a79af16e7ffb8';

try {
// deposit 1 ETH
const inst = new EtherFingate(node, production);
inst.initEtherContract(scAddress);

// Check if has pending order, if has don't call the next deposit api
const state = await inst.depositState(moacAddress);

if (inst.isPending(state)) {
return;
}

// start to transfer 1 ETH to fingate address
const hash = await inst.deposit(ethereumSecret, swtcAddress, amount);
console.log(hash);
} catch (error) {
console.log(error);
}

// deposit erc20 token

try {
// deposit 1 JCC

// JCC contract address
const jccContractAddress = "0x9BD4810a407812042F938d2f69f673843301cfa6";

const inst = new Erc20Fingate(node, production);

inst.initErc20Contract(scAddress, jccContractAddress);

// Check if has pending order, if has don't call transfer api
const state = await inst.depositState(ethereumAddress, jccContractAddress);

if (inst.isPending(state)) {
return;
}
// Please set decimals of erc20 token, if not will throw an error.
inst.decimals = 18;

// The first step to transfer 1 JCC to jcc contract address.
const transferHash = await inst.transfer(amount, secret);

// The next step to submit previous transfer hash.
const depositHash = await inst.depositToken(swtcAddress, amount, transferHash, ethereumSecret);
console.log(depositHash);

// Warning:
// This is not an atomic operating to deposit erc20 tokens for now,
// If the first step is successful but next step is failed, please contact us.
// The next version will make it an atomic operating after the next version of solidity contract upgrade.
} catch (error) {
console.log(error);
}

```
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jcc-ethereum-utils",
"version": "0.1.3",
"version": "0.1.4",
"description": "Toolkit of crossing chain from Ethereum to SWTC chain",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
1 change: 0 additions & 1 deletion src/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export default class ERC20 {
*/
public destroy() {
this._contract = null;
this._ethereum.destroyWeb3();
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/fingate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ export default class Fingate {
*/
public destroy() {
this._contract = null;
this._ethereum.destroyWeb3();
}

/**
Expand Down
3 changes: 0 additions & 3 deletions test/erc20.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ describe('test EtherFingate', function() {
const ethereum = new Ethereum(config.MOCK_NODE, true);
ethereum.initWeb3();
inst.init(config.ETHEREUM_ADDRESS, ethereum);
const spy = sandbox.spy(inst._ethereum, "destroyWeb3");
inst.destroy();
expect(spy.calledOnce).to.true;
expect(inst._contract).to.equal(null);
sandbox.restore();
})

describe("test balanceOf", function() {
Expand Down
3 changes: 0 additions & 3 deletions test/fingate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ describe('test Fingate', function() {
const ethereum = new Ethereum(config.MOCK_NODE, true);
ethereum.initWeb3();
inst.init(config.ETHEREUM_ADDRESS, ethereum);
const spy = sandbox.spy(inst._ethereum, "destroyWeb3");
inst.destroy();
expect(spy.calledOnce).to.true;
expect(inst._contract).to.equal(null);
sandbox.restore();
})
})

Expand Down

0 comments on commit 7bda843

Please sign in to comment.