Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving contract bindings and tests in general #308

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis_e2e_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -euxo pipefail

eval "$(GIMME_GO_VERSION=1.10.2 gimme)"

export BUILD_ID=build-1213
export BUILD_ID=build-1268

bash e2e_tests.sh

Expand Down
26 changes: 18 additions & 8 deletions e2e_support/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@
]
}
},
{
"vm": "plugin",
"format": "plugin",
"name": "ethcoin",
"location": "ethcoin:1.0.0",
"init": {
"accounts": [
{
"owner": {
"chain_id": "default",
"local": "dHVZ95Bqnn3dsget6tyq72cJS3E="
},
"balance": 100
}
]
}
},
{
"vm": "plugin",
"format": "plugin",
Expand Down Expand Up @@ -66,13 +83,6 @@
]
}
},
{
"vm": "plugin",
"format": "plugin",
"name": "ethcoin",
"location": "ethcoin:1.0.0",
"init": null
},
{
"vm": "plugin",
"format": "plugin",
Expand All @@ -88,7 +98,7 @@
"init": {
"owner": {
"chain_id": "default",
"local": "c/IFoEFkm4+D3wdqLmFU9F3t3Sk="
"local": "dHVZ95Bqnn3dsget6tyq72cJS3E="
},
"oracles": [
{
Expand Down
20 changes: 20 additions & 0 deletions e2e_support/solidity-contracts/SimpleStore.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pragma solidity ^0.4.25;

contract SimpleStore {
uint256 value;

constructor() public {
value = 10;
}

event NewValueSet(uint indexed _value, address sender);

function set(uint _value) public {
value = _value;
emit NewValueSet(value, msg.sender);
}

function get() public view returns (uint) {
return value;
}
}
26 changes: 26 additions & 0 deletions e2e_support/solidity-contracts/SimpleStore2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pragma solidity ^0.4.25;

contract SimpleStore {
uint value;

constructor() public {
value = 10;
}

event NewValueSet(uint indexed _value);
event NewValueSetAgain(uint indexed _value);

function set(uint _value) public {
value = _value;
emit NewValueSet(value);
}

function setAgain(uint _value) public {
value = _value;
emit NewValueSetAgain(value);
}

function get() public view returns (uint) {
return value;
}
}
27 changes: 27 additions & 0 deletions e2e_support/solidity-contracts/SimpleStore3.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pragma solidity ^0.4.25;

contract SimpleStore {
uint value;

constructor() public {
value = 10;
}

event NewValueSet(uint indexed _value);
event NewValueSetAgain(uint indexed _value);

function set(uint _value) public {
value = _value;
require(_value != 100, "Magic value");
emit NewValueSet(value);
}

function setAgain(uint _value) public {
value = _value;
emit NewValueSetAgain(value);
}

function get() public view returns (uint) {
return value;
}
}
30 changes: 15 additions & 15 deletions scripts/abi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

const shell = require('shelljs')
const typechain = require('typechain')
const ts_generator = require("ts-generator");

const ts_generator = require('ts-generator')

const cwd = process.cwd()
const options = {
files: "src/mainnet-contracts/*.json",
target: "ethers",
outDir: "src/mainnet-contracts"
files: 'src/mainnet-contracts/*.json',
target: 'ethers',
outDir: 'src/mainnet-contracts'
}

ts_generator.tsGenerator(
{ cwd, loggingLvl: "info" },
new typechain.Typechain({ cwd, rawConfig: options })
).then(() => {
ts_generator
.tsGenerator({ cwd, loggingLvl: 'info' }, new typechain.Typechain({ cwd, rawConfig: options }))
.then(() => {
shell.mkdir('-p', './dist/mainnet-contracts')
shell.cp(
'./src/mainnet-contracts/*', // copy everything regarding types
'./dist/mainnet-contracts/'
)
})

shell.mkdir('-p', './dist/mainnet-contracts')
shell.cp(
'./src/mainnet-contracts/*', // copy everything regarding types
'./dist/mainnet-contracts/'
)
})
shell.mkdir('-p', './dist/tests/e2e/artifacts')
shell.cp('./src/tests/e2e/artifacts/*', './dist/tests/e2e/artifacts')
67 changes: 47 additions & 20 deletions src/tests/e2e/address-mapper-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ import {
} from '../../index'
import { createTestHttpClient } from '../helpers'
import { EthersSigner, getJsonRPCSignerAsync } from '../../solidity-helpers'
import { ethers, Signer } from 'ethers'

async function getClientAndContract(
createClient: () => Client
createClient: () => Client,
privateKey: string
): Promise<{
client: Client
addressMapper: Contracts.AddressMapper
pubKey: Uint8Array
}> {
const privKey = CryptoUtils.B64ToUint8Array(
'RkNvOsko0nQFrJnXXVbmjGyaVmjQyr+ecJG8qGiF1LisazmV44qDcpsVsYvQZ9jxx7mIWJuZumIzYyLL6FOb4A=='
)
const privKey = CryptoUtils.B64ToUint8Array(privateKey)
const pubKey = CryptoUtils.publicKeyFromPrivateKey(privKey)
const client = createClient()
client.txMiddleware = createDefaultTxMiddleware(client, privKey)
Expand All @@ -34,31 +32,60 @@ async function getClientAndContract(
return { client, addressMapper, pubKey }
}

async function testAddIdentity(t: test.Test, createClient: () => Client) {
const { client, addressMapper, pubKey } = await getClientAndContract(createClient)
test('Should add identity to mapping', async t => {
const { client, addressMapper, pubKey } = await getClientAndContract(
createTestHttpClient,
'RkNvOsko0nQFrJnXXVbmjGyaVmjQyr+ecJG8qGiF1LisazmV44qDcpsVsYvQZ9jxx7mIWJuZumIzYyLL6FOb4A=='
)

const ethAddress = '0xffcf8fdee72ac11b5c542428b35eef5769c409f0'
const from = new Address('eth', LocalAddress.fromHexString(ethAddress))
const to = new Address(client.chainId, LocalAddress.fromPublicKey(pubKey))
try {
const ethAddress = '0xffcf8fdee72ac11b5c542428b35eef5769c409f0'
const from = new Address('eth', LocalAddress.fromHexString(ethAddress))
const to = new Address(client.chainId, LocalAddress.fromPublicKey(pubKey))

const ethers = await getJsonRPCSignerAsync('http://localhost:8545', 1)
const ethersSigner = new EthersSigner(ethers)
const ethers = await getJsonRPCSignerAsync('http://localhost:8545', 1)
const ethersSigner = new EthersSigner(ethers)

await addressMapper.addIdentityMappingAsync(from, to, ethersSigner)
await addressMapper.addIdentityMappingAsync(from, to, ethersSigner)

const result = await addressMapper.getMappingAsync(from)
const result = await addressMapper.getMappingAsync(from)

t.assert(from.equals(result.from), 'Identity "from" correctly returned')
t.assert(to.equals(result.to), 'Identity "to" correctly returned')
t.assert(from.equals(result.from), 'Identity "from" correctly returned')
t.assert(to.equals(result.to), 'Identity "to" correctly returned')
} catch (err) {
t.error(err)
}

client.disconnect()
}
t.end()
})

test('Should has mapping identity added correctly', async t => {
const { client, addressMapper, pubKey } = await getClientAndContract(
createTestHttpClient,
'1UgjYgUMZUhGzCfoRxEYFIOJPZQ3y4JjiJ0LdWdvrQO3RySLGno9bhlSWdtqaImycU2wcslcF/7K6GAHGMxe+A=='
)

test('Address Mapper', async t => {
try {
await testAddIdentity(t, createTestHttpClient)
const ethAddress = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'
const from = new Address('eth', LocalAddress.fromHexString(ethAddress))
const to = new Address(client.chainId, LocalAddress.fromPublicKey(pubKey))

const ethers = await getJsonRPCSignerAsync('http://localhost:8545', 2)
const ethersSigner = new EthersSigner(ethers)

await addressMapper.addIdentityMappingAsync(from, to, ethersSigner)

const result = await addressMapper.hasMappingAsync(from)

t.assert(result, 'Mapping has added with success')
} catch (err) {
t.fail(err)
t.error(err)
}

client.disconnect()
t.end()
})

// TODO: Add Binance test
test.skip('Should add Binance identity to mapping', async t => {})
Loading