Skip to content

Commit

Permalink
Merge cc0b742 into dad3492
Browse files Browse the repository at this point in the history
  • Loading branch information
GinMu authored Jun 29, 2019
2 parents dad3492 + cc0b742 commit c84b3c1
Show file tree
Hide file tree
Showing 13 changed files with 310 additions and 212 deletions.
36 changes: 25 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ npm i jcc-moac-utils

## Usage

Breaking change since 0.1.8, if you used 0.1.7 see [this demo](https://github.com/JCCDex/jcc-moac-utils/blob/master/docs/demo_below_0.1.7.md).

```javascript
// demo
import { Fingate } from "jcc-moac-utils";
import { Fingate, Moac, ERC20 } from "jcc-moac-utils";

// Moac node
const node = 'https://moac1ma17f1.jccdex.cn';
Expand All @@ -67,18 +69,21 @@ const scAddress = '0x66c9b619215db959ec137ede6b96f3fa6fd35a8a';

try {
// deposit 1 MOAC
const inst = new Fingate(node, production);
inst.initMoacContract(scAddress);
const moac = new Moac(node, production);
moac.initChain3();

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

// 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(moacAddress);

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

// start to transfer 1 MOAC to fingate address
const hash = await inst.deposit(swtcAddress, amount, moacSecret);
const hash = await fingateInstance.deposit(swtcAddress, amount, moacSecret);
console.log(hash);
} catch (error) {
console.log(error);
Expand All @@ -92,22 +97,27 @@ try {
// CKM contract address
const ckmContractAddress = "0x4d206d18fd036423aa74815511904a2a40e25fb1";

const inst = new Fingate(node, production);
const moac = new Moac(node, production);
moac.initChain3();

inst.initMoacContract(scAddress);
const fingateInstance = new Fingate();
fingateInstance.init(scAddress, moac);

const erc20Instance = new ERC20();
erc20Instance.init(ckmContractAddress, moac);

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

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

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

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

// Warning:
Expand All @@ -119,3 +129,7 @@ try {
}

```

## API

see [API.md](https://github.com/JCCDex/jcc-moac-utils/blob/master/docs/API.md)
8 changes: 4 additions & 4 deletions API.md → docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
*/
```

### clearChain3
### destroyChain3

```javascript
/**
Expand Down Expand Up @@ -252,7 +252,7 @@
*/
```

### initMoacContract
### init

```javascript
/**
Expand All @@ -263,11 +263,11 @@
*/
```

### close
### destroy

```javascript
/**
* close chain3 & destroy instance of contract
* destroy chain3 & destroy instance of contract
*
* @memberof Fingate
*/
Expand Down
84 changes: 84 additions & 0 deletions docs/demo_below_0.1.7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Usage

```javascript

npm i jcc-moac-utils@0.1.7

// demo
import { MoacFingate, ERC20Fingate } from "jcc-moac-utils";

// Moac node
const node = 'https://moac1ma17f1.jccdex.cn';

// Production network or not
const production = true;

// Your moac secret
const moacSecret = '';

// Your moac address
const moacAddress = '';

// Your swtc address
const swtcAddress = '';

// Deposit amount
const amount = 1;

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

try {
// deposit 1 MOAC
const inst = new MoacFingate(node, production);
inst.initMoacContract(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 MOAC to fingate address
const hash = await inst.deposit(swtcAddress, amount, moacSecret);
console.log(hash);
} catch (error) {
console.log(error);
}

// deposit erc20 token

try {
// deposit 1 CKM

// CKM contract address
const ckmContractAddress = "0x4d206d18fd036423aa74815511904a2a40e25fb1";

const inst = new ERC20Fingate(node, production);

inst.initErc20Contract(scAddress, ckmContractAddress);

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

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

// The first step to transfer 1 CKM to fingate address.
const transferHash = await inst.transfer(amount, moacSecret);

// The next step to submit previous transfer hash.
const depositHash = await inst.depositToken(swtcAddress, amount, transferHash, moacSecret);
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-moac-utils",
"version": "0.1.7",
"version": "0.1.8",
"description": "Toolkit of crossing chain from MOAC chain to SWTC chain",
"main": "lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class ERC20 {
*
* @memberof ERC20
*/
public close() {
public destroy() {
this._instance = null;
}

Expand Down Expand Up @@ -120,7 +120,7 @@ class ERC20 {
try {
const bnBalance = await this._instance.balanceOf(address);
const decimals = this._instance.decimals();
balance = bnBalance.dividedBy(10 ** decimals).toFormat(decimals);
balance = bnBalance.dividedBy(10 ** decimals).toString(10);
} catch (error) {
balance = "0";
}
Expand Down
2 changes: 1 addition & 1 deletion src/erc721.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ERC721 {
*
* @memberof ERC721
*/
public close() {
public destroy() {
this._instance = null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/fingate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Fingate {
* @memberof Fingate
*/
@validate
public initMoacContract(@isValidMoacAddress fingateAddress: string, moac: Moac) {
public init(@isValidMoacAddress fingateAddress: string, moac: Moac) {
try {
if (!moac.contractInitialied(this._instance, fingateAddress)) {
this._address = fingateAddress;
Expand All @@ -85,7 +85,7 @@ class Fingate {
*
* @memberof Fingate
*/
public close() {
public destroy() {
this._instance = null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/moac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export default class Moac {
*
* @memberof Moac
*/
public clearChain3() {
public destroyChain3() {
try {
this._chain3.setProvider(null);
} catch (error) {
Expand Down
Loading

0 comments on commit c84b3c1

Please sign in to comment.