transfer ether and add support for parent
MODIFY
README.md
README.md
@@ -34,6 +34,9 @@ const matic = new Matic({
// Set rootchain contract. See below for more information
rootChainAddress: <root-contract-address>,
+ // Set matic network's WETH address. See below for more information
+ maticWethAddress: <matic-weth-address>,
+
// Syncer API URL
// Fetches tx/receipt proof data instead of fetching whole block on client side
syncerUrl: 'https://eth-syncer.api.matic.network/api/v1', // (optional)
@@ -98,13 +101,13 @@ The flow for asset transfers on the Matic Network is as follows:
**Matic Testnet**
-* RPC endpoint host: https://testnet.matic.network
-* TEST childchain ERC20 token: 0x343461c74133E3fA476Dbbc614a87473270a226c
+- RPC endpoint host: https://testnet.matic.network
+- TEST childchain ERC20 token: 0x343461c74133E3fA476Dbbc614a87473270a226c
**Kovan testnet addresses**
-* TEST mainchain ERC20 token: 0x670568761764f53E6C10cd63b71024c31551c9EC
-* Root Contract: 0x24e01716a6ac34D5f2C4C082F553D86a557543a7
+- TEST mainchain ERC20 token: 0x670568761764f53E6C10cd63b71024c31551c9EC
+- Root Contract: 0x24e01716a6ac34D5f2C4C082F553D86a557543a7
### Faucet
@@ -117,6 +120,7 @@ Please write to info@matic.network to request TEST tokens for development purpos
- <a href="#depositTokens"><code>matic.<b>depositTokens()</b></code></a>
- <a href="#depositEthers"><code>matic.<b>depositEthers()</b></code></a>
- <a href="#transferTokens"><code>matic.<b>transferTokens()</b></code></a>
+- <a href="#transferEthers"><code>matic.<b>transferEthers()</b></code></a>
- <a href="#startWithdraw"><code>matic.<b>startWithdraw()</b></code></a>
- <a href="#getHeaderObject"><code>matic.<b>getHeaderObject()</b></code></a>
- <a href="#withdraw"><code>matic.<b>withdraw()</b></code></a>
@@ -132,20 +136,20 @@ Please write to info@matic.network to request TEST tokens for development purpos
Creates Matic SDK instance with give options. It returns a MaticSDK object.
```js
-import Matic from 'maticjs'
+import Matic from "maticjs"
const matic = new Matic(options)
```
- `options` is simple Javascript `object` which can have following fields:
- `maticProvider` can be `string` or `Web3.providers` instance. This provider must connect to Matic chain. Value can be anyone of following:
- * `'https://testnet.matic.network'`
- * `new Web3.providers.HttpProvider('http://localhost:8545')`
- * [WalletConnect Provider instance](https://github.com/WalletConnect/walletconnect-monorepo#for-web3-provider-web3js)
+ - `'https://testnet.matic.network'`
+ - `new Web3.providers.HttpProvider('http://localhost:8545')`
+ - [WalletConnect Provider instance](https://github.com/WalletConnect/walletconnect-monorepo#for-web3-provider-web3js)
- `parentProvider` can be `string` or `Web3.providers` instance. This provider must connect to Ethereum chain (testnet or mainchain). Value can be anyone of following:
- * `'https://kovan.infura.io'`
- * `new Web3.providers.HttpProvider('http://localhost:8545')`
- * [WalletConnect Provider instance](https://github.com/WalletConnect/walletconnect-monorepo#for-web3-provider-web3js)
+ - `'https://kovan.infura.io'`
+ - `new Web3.providers.HttpProvider('http://localhost:8545')`
+ - [WalletConnect Provider instance](https://github.com/WalletConnect/walletconnect-monorepo#for-web3-provider-web3js)
- `rootChainAddress` must be valid Ethereum contract address.
- `syncerUrl` must be valid API host. MaticSDK uses this value to fetch receipt/tx proofs instead of getting whole block to client side.
- `watcherUrl` must be valid API host. MaticSDK uses this value to fetch headerBlock info from mainchain and to find appropriate headerBlock for given blockNumber.
@@ -161,25 +165,27 @@ Approves given `amount` of `token` to `rootChainContract`.
- `token` must be valid ERC20 token address
- `amount` must be token amount in wei (string, not in Number)
- `options` (optional) must be valid javascript object containing `from`, `gasPrice`, `gasLimit`, `nonce`, `value`, `onTransactionHash`, `onReceipt` or `onError`
- * `from` must be valid account address
- * `gasPrice` same as Ethereum `sendTransaction`
- * `gasLimit` same as Ethereum `sendTransaction`
- * `nonce` same as Ethereum `sendTransaction`
- * `nonce` same as Ethereum `sendTransaction`
- * `value` contains ETH value. Same as Ethereum `sendTransaction`.
- * `onTransactionHash` must be `function`. This function will be called when transaction will be broadcasted.
- * `onReceipt` must be `function`. This function will be called when transaction will be included in block (when transaction gets confirmed)
- * `onError` must be `function`. This function will be called when sending transaction fails.
+ - `from` must be valid account address
+ - `gasPrice` same as Ethereum `sendTransaction`
+ - `gasLimit` same as Ethereum `sendTransaction`
+ - `nonce` same as Ethereum `sendTransaction`
+ - `nonce` same as Ethereum `sendTransaction`
+ - `value` contains ETH value. Same as Ethereum `sendTransaction`.
+ - `onTransactionHash` must be `function`. This function will be called when transaction will be broadcasted.
+ - `onReceipt` must be `function`. This function will be called when transaction will be included in block (when transaction gets confirmed)
+ - `onError` must be `function`. This function will be called when sending transaction fails.
This returns `Promise` object, which will be fulfilled when transaction gets confirmed (when receipt is generated).
Example:
```js
-matic.approveTokensForDeposit('0x718Ca123...', '1000000000000000000', {
- from: '0xABc578455...'
-}).on('onTransactionHash', (txHash) => {
- console.log('New transaction', txHash)
+matic
+ .approveTokensForDeposit("0x718Ca123...", "1000000000000000000", {
+ from: "0xABc578455..."
+ })
+ .on("onTransactionHash", txHash => {
+ console.log("New transaction", txHash)
})
```
@@ -244,6 +250,7 @@ Transfer given `amount` of `token` to `user`.
- `user` must be value account address
- `amount` must be token amount in wei (string, not in Number)
- `options` see [more infomation here](#approveTokensForDeposit)
+ - `parent` must be boolean value. For token transfer on Main chain, use `parent: true`
This returns `Promise` object, which will be fulfilled when transaction gets confirmed (when receipt is generated).
@@ -253,7 +260,38 @@ Example:
const user = <your-address> or <any-account-address>
matic.transferTokens('0x718Ca123...', user, '1000000000000000000', {
- from: '0xABc578455...'
+ from: '0xABc578455...',
+
+ // For token transfer on Main network
+ // parent: true
+})
+```
+
+---
+
+<a name="transferEthers"></a>
+
+#### matic.transferEthers(user, amount, options)
+
+Transfer given `amount` of ethers to `user`.
+
+- `user` must be value account address
+- `amount` must be ethers amount in wei (string, not in Number)
+- `options` see [more infomation here](#approveTokensForDeposit)
+ - `parent` must be boolean value. For ether transfer on Main chain, use `parent: true`
+
+This returns `Promise` object, which will be fulfilled when transaction gets confirmed (when receipt is generated).
+
+Example:
+
+```js
+const user = <your-address> or <any-account-address>
+
+matic.transferEthers(user, '1000000000000000000', {
+ from: '0xABc578455...',
+
+ // For token transfer on Main network
+ // parent: true
})
```
@@ -274,9 +312,11 @@ This returns `Promise` object, which will be fulfilled when transaction gets con
Example:
```js
-matic.startWithdraw('0x718Ca123...', '1000000000000000000', {
- from: '0xABc578455...'
-}).on('onTransactionHash', txHash => {
+matic
+ .startWithdraw("0x718Ca123...", "1000000000000000000", {
+ from: "0xABc578455..."
+ })
+ .on("onTransactionHash", txHash => {
console.log("Started withdraw process with txId", txHash)
})
```
@@ -320,8 +360,8 @@ This returns `Promise` object, which will be fulfilled when transaction gets con
Example:
```js
-matic.withdraw('0xabcd...789', {
- from: '0xABc578455...'
+matic.withdraw("0xabcd...789", {
+ from: "0xABc578455..."
})
```
@@ -340,7 +380,9 @@ This returns `Promise` object.
Example:
```js
-matic.getTx("0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b").then((txObject) => {
+matic
+ .getTx("0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b")
+ .then(txObject => {
console.log(txObject)
})
```
@@ -360,7 +402,11 @@ This returns `Promise` object.
Example:
```js
-matic.getReceipt("0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b").then((obj) => {
+matic
+ .getReceipt(
+ "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b"
+ )
+ .then(obj => {
console.log(obj)
})
```
@@ -372,4 +418,3 @@ Please write to info@matic.network for integration support. If you have any quer
### License
MIT
-

MODIFY
package-lock.json
package-lock.json
},
"chalk": {
"version": "1.1.3",
- "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz",
+ "resolved": "http://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz",
"integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=",
"dev": true,
"requires": {
},
"webpack": {
"version": "4.1.0",
- "resolved": "https://registry.npmjs.org/webpack/-/webpack-4.1.0.tgz",
+ "resolved": "http://registry.npmjs.org/webpack/-/webpack-4.1.0.tgz",
"integrity": "sha512-ZFYcAZ44kOT+xsS5MS2H1fQr0PJkwQdYem/d17wacDkkupzsAkBJ3hDShWHdPVvWluFs6pfhHWw/dVso1m0rsA==",
"dev": true,
"requires": {

MODIFY
src/index.js
src/index.js
@@ -33,7 +33,7 @@ export default class Matic {
this._syncerUrl = options.syncerUrl
this._watcherUrl = options.watcherUrl
this._rootChainAddress = options.rootChainAddress
-
+ this._maticWethAddress = options.maticWethAddress
// create rootchain contract
this._rootChainContract = new this._parentWeb3.eth.Contract(
RootChainArtifacts.abi,
@@ -128,15 +128,41 @@ export default class Matic {
}
async transferTokens(token, user, amount, options = {}) {
- const _tokenContract = this._getChildTokenContract(token)
- const transferTx = _tokenContract.methods.transfer(user, amount)
- const _options = await this._fillOptions(options, transferTx, this._web3)
+ let web3Object = this._web3
+ if (options.parent) {
+ web3Object = this._parentWeb3
+ }
+ const _tokenContract = this._getERC20TokenContract(token, web3Object)
+ const transferTx = _tokenContract.methods.transfer(user, amount)
+ const _options = await this._fillOptions(options, transferTx, web3Object)
return this._wrapWeb3Promise(transferTx.send(_options), options)
}
+ async transferEthers(to, amount, options = {}) {
+ const from = options.from
+
+ // if matic chain, transfer normal WETH tokens
+ if (!options.parent) {
+ return this.transferTokens(this._maticWethAddress, to, amount, options)
+ }
+
+ const gasLimit = await this._parentWeb3.eth.estimateGas({
+ from,
+ value: amount,
+ })
+ options.gasLimit = gasLimit
+ options.value = amount
+ const _options = await this._fillOptions(options, {}, this._parentWeb3)
+
+ return this._wrapWeb3Promise(
+ this._parentWeb3.eth.sendTransaction(_options),
+ options
+ )
+ }
+
async startWithdraw(token, amount, options = {}) {
- const _tokenContract = this._getChildTokenContract(token)
+ const _tokenContract = this._getERC20TokenContract(token, this._web3)
const withdrawTx = _tokenContract.methods.withdraw(amount)
const _options = await this._fillOptions(options, withdrawTx, this._web3)
return this._wrapWeb3Promise(withdrawTx.send(_options), options)
@@ -344,15 +370,12 @@ export default class Matic {
}
}
- _getChildTokenContract(token) {
+ _getERC20TokenContract(token, web3) {
const _token = token.toLowerCase()
let _tokenContract = this._tokenCache[_token]
if (!_tokenContract) {
- _tokenContract = new this._web3.eth.Contract(
- ChildERC20Artifacts.abi,
- _token
- )
+ _tokenContract = new web3.eth.Contract(ChildERC20Artifacts.abi, _token)
// update token cache
this._tokenCache[_token] = _tokenContract
}

transfer ether and add support for parent