Skip to content

Commit

Permalink
Merge game tokens with crypto cards
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardonunesp committed Aug 28, 2018
1 parent a2619c0 commit 175ebcd
Show file tree
Hide file tree
Showing 27 changed files with 675 additions and 225 deletions.
2 changes: 1 addition & 1 deletion dappchain/loom.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ChainID: 'default'
RegistryVersion: 2
LogDestination: 'file://loom2.log'
#LogDestination: 'file://loom2.log'
LoomLogLevel: 'debug'
ContractLogLevel: 'debug'
BlockchainLogLevel: 'error'
Expand Down
2 changes: 1 addition & 1 deletion game_token_address
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0xf1a50e805416d2568d6011c42a9129be0c9a4c5a
0x1aa76056924bf4768d63357eca6d6a56ec929131
1 change: 1 addition & 0 deletions game_token_dappchain_address
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x85fb9206da279c05d007d5885dd0dfdd04257778
1 change: 1 addition & 0 deletions game_token_tx_hash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x56b969603232cdb811b3a6e0396e31b978a3ca6ab5dca6fb1eb1538fbdf32a5d
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { readFileSync } = require('fs')
const Web3 = require('web3')

const {
NonceTxMiddleware, SignedTxMiddleware, Client, Address, TransferGateway,
NonceTxMiddleware, SignedTxMiddleware, Client, Address, Contracts,
LocalAddress, CryptoUtils, createJSONRPCClient, Web3Signer, soliditySha3
} = require('loom-js')

Expand Down Expand Up @@ -30,7 +30,7 @@ const cryptoCardsTx = readFileSync('../crypto_cards_tx_hash', 'utf-8')
new SignedTxMiddleware(privateKey)
]

const transferGateway = await TransferGateway.createAsync(
const transferGateway = await Contracts.TransferGateway.createAsync(
client,
new Address(client.chainId, LocalAddress.fromPublicKey(publicKey))
)
Expand Down
71 changes: 71 additions & 0 deletions transfer-gateway-scripts/mapping_game_token.js
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
})
})()
2 changes: 1 addition & 1 deletion transfer-gateway-scripts/package.json
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"
}
}
6 changes: 3 additions & 3 deletions transfer-gateway-scripts/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1101,9 +1101,9 @@ keypather@^1.10.2:
dependencies:
"101" "^1.0.0"

loom-js@^1.17.0:
version "1.17.0"
resolved "https://registry.yarnpkg.com/loom-js/-/loom-js-1.17.0.tgz#a7b96a109ae1342d06c3ea4806fbb8cf8cc99ca9"
loom-js@^1.18.0:
version "1.18.0"
resolved "https://registry.yarnpkg.com/loom-js/-/loom-js-1.18.0.tgz#1552460aac74c2917e5b16bf1b0698a6228be3dd"
dependencies:
axios "^0.18.0"
bn.js "^4.11.8"
Expand Down
3 changes: 2 additions & 1 deletion transfer_gateway
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ function deploy_truffle_dappchain {
function run_mapping {
echo "Running mapping"
cd transfer-gateway-scripts
node index.js > /dev/null 2>&1 &
node mapping_crypto_cards.js > /dev/null 2>&1
node mapping_game_token.js > /dev/null 2>&1
cd ..
}

Expand Down
16 changes: 16 additions & 0 deletions truffle-dappchain/contracts/ERC20DAppToken.sol
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;
}
17 changes: 17 additions & 0 deletions truffle-dappchain/contracts/ERC20Receiver.sol
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);
}
42 changes: 42 additions & 0 deletions truffle-dappchain/contracts/GameTokenDappChain.sol
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;
}
}
7 changes: 7 additions & 0 deletions truffle-dappchain/migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { writeFileSync, readFileSync } = require('fs')

const CryptoCardsDappChain = artifacts.require('CryptoCardsDappChain')
const GameTokenDappChain = artifacts.require('GameTokenDappChain')

module.exports = (deployer, network, accounts) => {
const gatewayAddress = readFileSync('../gateway_dappchain_address', 'utf-8')
Expand All @@ -10,4 +11,10 @@ module.exports = (deployer, network, accounts) => {
console.log(`CryptoCardsDAppChain deployed at address: ${cryptoCardsDAppChainInstance.address}`)
writeFileSync('../crypto_cards_dappchain_address', cryptoCardsDAppChainInstance.address)
})

deployer.deploy(GameTokenDappChain, gatewayAddress).then(async () => {
const GameTokenDappChainInstance = await GameTokenDappChain.deployed()
console.log(`GameTokenDappChain deployed at address: ${GameTokenDappChainInstance.address}`)
writeFileSync('../game_token_dappchain_address', GameTokenDappChainInstance.address)
})
}
14 changes: 13 additions & 1 deletion truffle-ethereum/migrations/2_deploy_mainnet_contracts.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { writeFileSync } = require('fs')

const CryptoCards = artifacts.require('CryptoCards')
const GameToken = artifacts.require('GameToken')
const Gateway = artifacts.require('Gateway')

module.exports = (deployer, network, accounts) => {
module.exports = (deployer, _network, accounts) => {
const [_, user] = accounts
const validator = accounts[9]
deployer.deploy(Gateway, [validator], 3, 4).then(async () => {
Expand All @@ -14,14 +15,25 @@ module.exports = (deployer, network, accounts) => {
const cryptoCardsContract = await deployer.deploy(CryptoCards, gatewayInstance.address)
const cryptoCardsInstance = await CryptoCards.deployed()

const gameTokenContract = await deployer.deploy(GameToken, gatewayInstance.address)
const gameTokenInstance = await GameToken.deployed()

console.log(`CryptoCards deployed at address: ${cryptoCardsInstance.address}`)
console.log(`CryptoCards transaction at hash: ${cryptoCardsContract.transactionHash}`)

console.log(`GameToken deployed at address: ${gameTokenInstance.address}`)
console.log(`GameToken transaction at hash: ${gameTokenContract.transactionHash}`)

await gatewayInstance.toggleToken(cryptoCardsInstance.address, { from: validator })
await cryptoCardsInstance.register(user)

await gatewayInstance.toggleToken(gameTokenInstance.address, { from: validator })
await gameTokenInstance.transfer(user, 100)

writeFileSync('../gateway_address', gatewayInstance.address)
writeFileSync('../crypto_cards_address', cryptoCardsInstance.address)
writeFileSync('../crypto_cards_tx_hash', cryptoCardsContract.transactionHash)
writeFileSync('../game_token_address', gameTokenInstance.address)
writeFileSync('../game_token_tx_hash', gameTokenContract.transactionHash)
})
}
96 changes: 0 additions & 96 deletions webclient/src/components/dappchain_cards.js

This file was deleted.

Loading

0 comments on commit 175ebcd

Please sign in to comment.