forked from loomnetwork/transfer-gateway-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2619c0
commit 175ebcd
Showing
27 changed files
with
675 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0xf1a50e805416d2568d6011c42a9129be0c9a4c5a | ||
0x1aa76056924bf4768d63357eca6d6a56ec929131 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0x85fb9206da279c05d007d5885dd0dfdd04257778 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0x56b969603232cdb811b3a6e0396e31b978a3ca6ab5dca6fb1eb1538fbdf32a5d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const { readFileSync } = require('fs') | ||
const Web3 = require('web3') | ||
|
||
const { | ||
NonceTxMiddleware, | ||
SignedTxMiddleware, | ||
Client, | ||
Address, | ||
Contracts, | ||
LocalAddress, | ||
CryptoUtils, | ||
createJSONRPCClient, | ||
Web3Signer, | ||
soliditySha3 | ||
} = require('loom-js') | ||
|
||
const coinAddress = readFileSync('../game_token_address', 'utf-8') | ||
const coinDAppChainAddress = readFileSync('../game_token_dappchain_address', 'utf-8') | ||
const coinTx = readFileSync('../game_token_tx_hash', 'utf-8') | ||
;(async () => { | ||
const privateKey = CryptoUtils.B64ToUint8Array(readFileSync('../dappchain/private_key', 'utf-8')) | ||
const publicKey = CryptoUtils.publicKeyFromPrivateKey(privateKey) | ||
|
||
const chainId = 'default' | ||
const writeUrl = 'http://127.0.0.1:46658/rpc' | ||
const readUrl = 'http://127.0.0.1:46658/query' | ||
|
||
const writer = createJSONRPCClient({ protocols: [{ url: writeUrl }] }) | ||
const reader = createJSONRPCClient({ protocols: [{ url: readUrl }] }) | ||
const client = new Client(chainId, writer, reader) | ||
|
||
console.log('Client created') | ||
|
||
// required middleware | ||
client.txMiddleware = [new NonceTxMiddleware(publicKey, client), new SignedTxMiddleware(privateKey)] | ||
|
||
const transferGateway = await Contracts.TransferGateway.createAsync( | ||
client, | ||
new Address(client.chainId, LocalAddress.fromPublicKey(publicKey)) | ||
) | ||
|
||
console.log('Transfer Gateway client created') | ||
|
||
const foreignContract = new Address('eth', LocalAddress.fromHexString(coinAddress)) | ||
const localContract = new Address(client.chainId, LocalAddress.fromHexString(coinDAppChainAddress)) | ||
|
||
const web3 = new Web3('http://localhost:8545') | ||
const accounts = await web3.eth.getAccounts() | ||
const owner = accounts[0] | ||
|
||
console.log('Account used for sign', owner) | ||
|
||
const web3Signer = new Web3Signer(web3, owner) | ||
const hash = soliditySha3( | ||
{ type: 'address', value: foreignContract.local.toString().slice(2) }, | ||
{ type: 'address', value: localContract.local.toString().slice(2) } | ||
) | ||
|
||
const foreignContractCreatorSig = await web3Signer.signAsync(hash) | ||
|
||
console.log(`Sign foreign contract and local contracts ${coinAddress} ${coinDAppChainAddress}`) | ||
|
||
const foreignContractCreatorTxHash = Buffer.from(coinTx.slice(2), 'hex') | ||
|
||
await transferGateway.addContractMappingAsync({ | ||
foreignContract, | ||
localContract, | ||
foreignContractCreatorSig, | ||
foreignContractCreatorTxHash | ||
}) | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"dependencies": { | ||
"web3": "^1.0.0-beta.35", | ||
"loom-js": "^1.17.0" | ||
"loom-js": "^1.18.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
pragma solidity ^0.4.24; | ||
|
||
import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol"; | ||
|
||
/** | ||
* @title ERC20 interface for token contracts deployed to Loom DAppChains. | ||
*/ | ||
contract ERC20DAppToken is ERC20 { | ||
// Called by the DAppChain Gateway contract to mint tokens that have been deposited to the | ||
// Mainnet Gateway. | ||
// | ||
// NOTE: This function will only be called by the DAppChain Gateway contract if it doesn't have | ||
// enough tokens to complete a transfer, so it's possible to omit this function if you wish to | ||
// manually allocate tokens for the Gateway. | ||
function mintToGateway(uint256 amount) public; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
pragma solidity ^0.4.24; | ||
|
||
/** | ||
* @title ERC20 token receiver interface | ||
* @dev Interface for any contract that wants to support safeTransfers | ||
* from ERC20 asset contracts. | ||
*/ | ||
contract ERC20Receiver { | ||
/** | ||
* @dev Magic value to be returned upon successful reception of an NFT | ||
* Equals to `bytes4(keccak256("onERC20Received(address,uint256,bytes)"))`, | ||
* which can be also obtained as `ERC20Receiver(0).onERC20Received.selector` | ||
*/ | ||
bytes4 constant ERC20_RECEIVED = 0xbc04f0af; | ||
|
||
function onERC20Received(address _from, uint256 amount) public returns(bytes4); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
pragma solidity ^0.4.24; | ||
|
||
import "openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol"; | ||
import "./ERC20DAppToken.sol"; | ||
import "./ERC20Receiver.sol"; | ||
|
||
/** | ||
* @title Full ERC20 Token for Loom DAppChains | ||
*/ | ||
contract GameTokenDappChain is ERC20DAppToken, StandardToken, ERC20Receiver { | ||
// Transfer Gateway contract address | ||
address public gateway; | ||
|
||
string public name = "GameTokenDappChain"; | ||
string public symbol = "GTKDC"; | ||
uint8 public decimals = 18; | ||
|
||
uint256 public constant INITIAL_SUPPLY = 1000000000; | ||
|
||
/** | ||
* @dev Constructor function | ||
*/ | ||
constructor (address _gateway) public { | ||
gateway = _gateway; | ||
totalSupply_ = INITIAL_SUPPLY * (10 ** uint256(decimals)); | ||
balances[_gateway] = totalSupply_; | ||
} | ||
|
||
// Called by the gateway contract to mint tokens that have been deposited to the Mainnet gateway. | ||
function mintToGateway(uint256 _amount) public { | ||
require(msg.sender == gateway); | ||
totalSupply_ = totalSupply_.add(_amount); | ||
balances[gateway] = balances[gateway].add(_amount); | ||
} | ||
|
||
function onERC20Received( | ||
address _from, | ||
uint256 amount | ||
) public returns(bytes4) { | ||
return ERC20_RECEIVED; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.