transfer ether
MODIFY
.gitignore
.gitignore
@@ -3,6 +3,7 @@ lib
.DS_Store
*.log
dump.rdb
+dist
test-blockchain/geth
test-blockchain/history

ADD
examples/transfer-ether.js
+const bn = require('bn.js')
+
+const utils = require('./utils')
+
+async function execute() {
+ const { matic, network } = await utils.getMaticClient('testnet', 'v3')
+ matic.setWallet(utils.getPrivateKey())
+
+ const amount = new bn(10).pow(new bn(17)) // 0.1 token
+ return matic.transferEthers(process.env.FROM, amount, { from: process.env.FROM })
+}
+
+execute().then(console.log)

MODIFY
package-lock.json
package-lock.json
}
}
},
+ "@maticnetwork/meta": {
+ "version": "1.0.3",
+ "resolved": "https://registry.npmjs.org/@maticnetwork/meta/-/meta-1.0.3.tgz",
+ "integrity": "sha512-Fp9iybQK4q4NdR/WxN4YlkvivIwJi1gFJEEqZ25729sCkFwaF0T+Hurbu1TADvbnggAFgMXkXWsTelFw9fpx3A=="
+ },
"@samverschueren/stream-to-observable": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/@samverschueren/stream-to-observable/-/stream-to-observable-0.3.0.tgz",
"web3-providers-ws": "1.2.1"
},
"dependencies": {
- "debug": {
- "version": "2.6.9",
- "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
- "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
- "requires": {
- "ms": "2.0.0"
- }
- },
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha512-oqsQXzu+ejJACVHy864WwIyw+oB21nw/pI65/sD95Zi98+/HQzFfNcIFneF1NC4bVF3VNX4YHTNq2I2o97LAiA==",
"requires": {
"underscore": "1.9.1",
- "web3-core-helpers": "1.2.1"
+ "web3-core-helpers": "1.2.1",
+ "websocket": "github:web3-js/WebSocket-Node#905deb4812572b344f5801f8c9ce8bb02799d82e"
},
"dependencies": {
"websocket": {
"version": "github:web3-js/WebSocket-Node#905deb4812572b344f5801f8c9ce8bb02799d82e",
- "from": "github:web3-js/WebSocket-Node#905deb4812572b344f5801f8c9ce8bb02799d82e",
+ "from": "github:web3-js/WebSocket-Node#polyfill/globalThis",
"requires": {
"debug": "^2.2.0",
"es5-ext": "^0.10.50",
"nan": "^2.14.0",
"typedarray-to-buffer": "^3.1.5",
"yaeti": "^0.0.6"
+ },
+ "dependencies": {
+ "debug": {
+ "version": "2.6.9",
+ "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
+ "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
+ "requires": {
+ "ms": "2.0.0"
+ }
+ }
}
}
}
"version": "github:web3-js/WebSocket-Node#905deb4812572b344f5801f8c9ce8bb02799d82e",
"from": "github:web3-js/WebSocket-Node#polyfill/globalThis",
"requires": {
- "debug": "^2.2.0",
"es5-ext": "^0.10.50",
"nan": "^2.14.0",
"typedarray-to-buffer": "^3.1.5",

MODIFY
package.json
package.json
},
"homepage": "https://github.com/maticnetwork/matic.js#readme",
"dependencies": {
+ "@maticnetwork/meta": "^1.0.3",
"@types/bluebird": "^3.5.29",
"@types/bn.js": "4.11.5",
"@types/web3": "1.0.19",

MODIFY
src/common/ContractsBase.ts
src/common/ContractsBase.ts
@@ -2,6 +2,7 @@ import Web3Client from './Web3Client'
import BN from 'bn.js'
import ChildERC20Artifact from 'matic-protocol/contracts-core/artifacts/ChildERC20.json'
import ChildERC721Artifact from 'matic-protocol/contracts-core/artifacts/ChildERC721.json'
+import ChildMaticArtifact from 'matic-protocol/contracts-core/artifacts/MaticChildERC20.json'
import { address } from '../types/Common'
@@ -34,13 +35,15 @@ export default class ContractsBase {
return new web3.eth.Contract(ChildERC721Artifact.abi, token)
}
+ public getChildMaticContract(childMaticAddress: address) {
+ return new this.web3Client.web3.eth.Contract(ChildMaticArtifact.abi, childMaticAddress)
+ }
+
async _fillOptions(options, txObject, web3) {
delete txObject.chainId
const from = options.from
if (!from) {
- throw new Error(
- '`from` required in options or set wallet using maticObject.wallet = <private key>',
- )
+ throw new Error('`from` required in options or set wallet using maticObject.wallet = <private key>')
}
const [gasLimit, gasPrice, nonce, chainId] = await Promise.all([
@@ -48,9 +51,7 @@ export default class ContractsBase {
? txObject.estimateGas({ from, value: options.value })
: options.gasLimit || options.gas,
!options.gasPrice ? web3.eth.getGasPrice() : options.gasPrice,
- !options.nonce
- ? web3.eth.getTransactionCount(from, 'pending')
- : options.nonce,
+ !options.nonce ? web3.eth.getTransactionCount(from, 'pending') : options.nonce,
!options.chainId ? web3.eth.net.getId() : options.chainId,
])
return {

MODIFY
src/index.ts
src/index.ts
@@ -15,29 +15,23 @@ export default class Matic extends ContractsBase {
public withdrawManager: WithdrawManager
public registry: Registry
public childChainAddress: address
+ public childMaticAddress: address
constructor(options: any = {}) {
const web3Client = new Web3Client(
options.parentProvider,
options.maticProvider,
options.parentDefaultOptions || {},
- options.maticDefaultOptions || {},
+ options.maticDefaultOptions || {}
)
super(web3Client)
this.web3Client = web3Client
this.registry = new Registry(options.registry, this.web3Client)
this.rootChain = new RootChain(options.rootChain, this.web3Client)
- this.depositManager = new DepositManager(
- options.depositManager,
- this.web3Client,
- )
- this.withdrawManager = new WithdrawManager(
- options.withdrawManager,
- this.rootChain,
- this.web3Client,
- this.registry,
- )
+ this.depositManager = new DepositManager(options.depositManager, this.web3Client)
+ this.withdrawManager = new WithdrawManager(options.withdrawManager, this.rootChain, this.web3Client, this.registry)
this.childChainAddress = options.childChain
+ this.childMaticAddress = '0000000000000000000000000000000000001010'
}
initialize() {
@@ -53,10 +47,9 @@ export default class Matic extends ContractsBase {
throw new Error('token address or user address is missing')
}
- const balance = this.getERC20TokenContract(
- token,
- options.parent,
- ).methods.balanceOf(userAddress).call()
+ const balance = this.getERC20TokenContract(token, options.parent)
+ .methods.balanceOf(userAddress)
+ .call()
return balance
}
@@ -65,10 +58,9 @@ export default class Matic extends ContractsBase {
throw new Error('token address or user address is missing')
}
- const balance = this.getERC721TokenContract(
- token,
- options.parent,
- ).methods.balanceOf(userAddress).call()
+ const balance = this.getERC721TokenContract(token, options.parent)
+ .methods.balanceOf(userAddress)
+ .call()
return balance
}
@@ -77,34 +69,23 @@ export default class Matic extends ContractsBase {
throw new Error('token address or user address is missing')
}
- const tokenID = this.getERC721TokenContract(
- token,
- options.parent,
- ).methods.tokenOfOwnerByIndex(userAddress, index).call()
+ const tokenID = this.getERC721TokenContract(token, options.parent)
+ .methods.tokenOfOwnerByIndex(userAddress, index)
+ .call()
return tokenID
}
- async transferERC20Tokens(
- token: address,
- to: address,
- amount: BN | string,
- options?: SendOptions,
- ) {
+ async transferERC20Tokens(token: address, to: address, amount: BN | string, options?: SendOptions) {
if (options && (!options.from || !amount || !token || !to)) {
throw new Error('options.from, to, token or amount is missing')
}
- const txObject = this.getERC20TokenContract(
- token,
- options.parent,
- ).methods.transfer(to, this.encode(amount))
+ const txObject = this.getERC20TokenContract(token, options.parent).methods.transfer(to, this.encode(amount))
const _options = await this._fillOptions(
options,
txObject,
- options.parent
- ? this.web3Client.getParentWeb3()
- : this.web3Client.getMaticWeb3(),
+ options.parent ? this.web3Client.getParentWeb3() : this.web3Client.getMaticWeb3()
)
if (options.encodeAbi) {
@@ -116,27 +97,17 @@ export default class Matic extends ContractsBase {
return this.web3Client.send(txObject, _options)
}
- async transferERC721Tokens(
- token: address,
- to: address,
- tokenId: string,
- options?: SendOptions,
- ) {
+ async transferERC721Tokens(token: address, to: address, tokenId: string, options?: SendOptions) {
if (options && (!options.from || !tokenId || !token || !to)) {
throw new Error('options.from, to, token or tokenId is missing')
}
- const txObject = this.getERC721TokenContract(
- token,
- options.parent,
- ).methods.transferFrom(options.from, to, tokenId)
+ const txObject = this.getERC721TokenContract(token, options.parent).methods.transferFrom(options.from, to, tokenId)
const _options = await this._fillOptions(
options,
txObject,
- options.parent
- ? this.web3Client.getParentWeb3()
- : this.web3Client.getMaticWeb3(),
+ options.parent ? this.web3Client.getParentWeb3() : this.web3Client.getMaticWeb3()
)
if (options.encodeAbi) {
@@ -148,10 +119,33 @@ export default class Matic extends ContractsBase {
return this.web3Client.send(txObject, _options)
}
- depositEther(
- amount: BN | string,
- options?: SendOptions,
- ) {
+ async transferEthers(toAddress: address, amount: BN | string, options?: SendOptions) {
+ if (options && (!options.from || !amount || !toAddress)) {
+ throw new Error('Missing Parameters')
+ }
+ const from = options.from
+ const value = this.encode(amount)
+ if (!options.parent) {
+ const maticWeth = await this.registry.registry.methods.getWethTokenAddress().call()
+ return this.transferERC20Tokens(maticWeth, toAddress, value, options)
+ }
+ const web3Object = this.web3Client.getParentWeb3()
+ const gasLimit = await web3Object.eth.estimateGas({
+ from,
+ value,
+ })
+ options.gas = gasLimit
+ options.value = value
+ options.to = toAddress
+ const _options = await this._fillOptions(options, {}, web3Object)
+ if (options.encodeAbi) {
+ return _options
+ }
+
+ return this.web3Client.wrapWeb3Promise(web3Object.eth.sendTransaction(_options), _options)
+ }
+
+ depositEther(amount: BN | string, options?: SendOptions) {
if (options && (!options.from || !amount)) {
throw new Error('options.from or amount is missing')
}
@@ -164,7 +158,7 @@ export default class Matic extends ContractsBase {
return 'Transaction hash is not Found'
}
const newDepositEvent = depositReceipt.logs.find(
- l => l.topics[0].toLowerCase() === DepositManager.NEW_DEPOSIT_EVENT_SIG,
+ l => l.topics[0].toLowerCase() === DepositManager.NEW_DEPOSIT_EVENT_SIG
)
const data = newDepositEvent.data
@@ -176,11 +170,7 @@ export default class Matic extends ContractsBase {
return depositReceipt
}
- approveERC20TokensForDeposit(
- token: address,
- amount: BN | string,
- options?: SendOptions,
- ) {
+ approveERC20TokensForDeposit(token: address, amount: BN | string, options?: SendOptions) {
if (options && (!options.from || !amount || !token)) {
throw new Error('options.from, token or amount is missing')
}
@@ -188,12 +178,7 @@ export default class Matic extends ContractsBase {
return this.depositManager.approveERC20(token, amount, options)
}
- depositERC20ForUser(
- token: address,
- user: address,
- amount: BN | string,
- options?: SendOptions,
- ) {
+ depositERC20ForUser(token: address, user: address, amount: BN | string, options?: SendOptions) {
if (options && (!options.from || !amount || !token)) {
throw new Error('options.from, token or amount is missing')
}
@@ -204,17 +189,14 @@ export default class Matic extends ContractsBase {
if (options && (!options.from || !tokenId || !token)) {
throw new Error('options.from, token or tokenId is missing')
}
- const txObject = this.getERC721TokenContract(
- token,
- true,
- ).methods.safeTransferFrom(options.from, this.depositManager.getAddress(), tokenId)
-
- const _options = await this._fillOptions(
- options,
- txObject,
- this.web3Client.getParentWeb3()
+ const txObject = this.getERC721TokenContract(token, true).methods.safeTransferFrom(
+ options.from,
+ this.depositManager.getAddress(),
+ tokenId
)
+ const _options = await this._fillOptions(options, txObject, this.web3Client.getParentWeb3())
+
if (options.encodeAbi) {
_options.data = txObject.encodeABI()
_options.to = token
@@ -259,8 +241,7 @@ export default class Matic extends ContractsBase {
}
private _validateInputs(token: address, amountOrTokenId: BN | string, options?: SendOptions) {
- if (!this.web3Client.web3.utils.isAddress(
- this.web3Client.web3.utils.toChecksumAddress(token))) {
+ if (!this.web3Client.web3.utils.isAddress(this.web3Client.web3.utils.toChecksumAddress(token))) {
throw new Error(`${token} is not a valid token address`)
}
if (!amountOrTokenId) {

MODIFY
src/types/Common.ts
src/types/Common.ts
@@ -7,4 +7,5 @@ export declare interface SendOptions {
gasPrice?: string | number
encodeAbi?: boolean
parent?: boolean
+ to?: string
}

transfer ether