-
Notifications
You must be signed in to change notification settings - Fork 47
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
cd082a9
commit 49cfe56
Showing
2 changed files
with
126 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import test from 'tape' | ||
|
||
import { LocalAddress, CryptoUtils } from '../../index' | ||
import { createTestClient, waitForMillisecondsAsync } from '../helpers' | ||
|
||
import { LoomProvider } from '../../loom-provider' | ||
import { deployContract } from '../evm-helpers' | ||
|
||
// import Web3 from 'web3' | ||
const Web3 = require('web3') | ||
|
||
/** | ||
* Requires the SimpleStore solidity contract deployed on a loomchain. | ||
* go-loom/examples/plugins/evmexample/contract/SimpleStore.sol | ||
* | ||
* pragma solidity ^0.4.22; | ||
* | ||
* contract SimpleStore { | ||
* uint value; | ||
* constructor() public { | ||
* value = 10; | ||
* } | ||
* | ||
* event NewValueSet(uint indexed _value); | ||
* | ||
* function set(uint _value) public { | ||
* value = _value; | ||
* emit NewValueSet(value); | ||
* } | ||
* | ||
* function get() public view returns (uint) { | ||
* return value; | ||
* } | ||
* } | ||
* | ||
* | ||
*/ | ||
|
||
const newContractAndClient = async () => { | ||
const privKey = CryptoUtils.generatePrivateKey() | ||
const client = createTestClient() | ||
const from = LocalAddress.fromPublicKey(CryptoUtils.publicKeyFromPrivateKey(privKey)).toString() | ||
const loomProvider = new LoomProvider(client, privKey) | ||
const web3 = new Web3(loomProvider) | ||
|
||
const contractData = | ||
'0x608060405234801561001057600080fd5b50600a60008190555061010e806100286000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146078575b600080fd5b348015605957600080fd5b5060766004803603810190808035906020019092919050505060a0565b005b348015608357600080fd5b50608a60d9565b6040518082815260200191505060405180910390f35b806000819055506000547fb922f092a64f1a076de6f21e4d7c6400b6e55791cc935e7bb8e7e90f7652f15b60405160405180910390a250565b600080549050905600a165627a7a72305820b76f6c855a1f95260fc70490b16774074225da52ea165a58e95eb7a72a59d1700029' | ||
|
||
const ABI = [ | ||
{ | ||
constant: false, | ||
inputs: [{ name: '_value', type: 'uint256' }], | ||
name: 'set', | ||
outputs: [], | ||
payable: false, | ||
stateMutability: 'nonpayable', | ||
type: 'function' | ||
}, | ||
{ | ||
constant: true, | ||
inputs: [], | ||
name: 'get', | ||
outputs: [{ name: '', type: 'uint256' }], | ||
payable: false, | ||
stateMutability: 'view', | ||
type: 'function' | ||
}, | ||
{ inputs: [], payable: false, stateMutability: 'nonpayable', type: 'constructor' }, | ||
{ | ||
anonymous: false, | ||
inputs: [{ indexed: true, name: '_value', type: 'uint256' }], | ||
name: 'NewValueSet', | ||
type: 'event' | ||
} | ||
] | ||
|
||
const result = await deployContract(loomProvider, contractData) | ||
|
||
const contract = new web3.eth.Contract(ABI, result.contractAddress, { from }) | ||
|
||
return { | ||
contract, | ||
client | ||
} | ||
} | ||
|
||
test('LoomProvider + Web3 past events', async t => { | ||
try { | ||
const { contract, client } = await newContractAndClient() | ||
const numEvents = 5 | ||
|
||
for (let v = 1; v <= numEvents; v++) { | ||
const tx = await contract.methods.set(v).send() | ||
t.equal(tx.status, true, `SimpleStore.set should return correct status for ${v}`) | ||
|
||
const resultOfGet = await contract.methods.get().call() | ||
t.equal(+resultOfGet, v, `SimpleStore.get should return correct value for ${v}`) | ||
} | ||
|
||
await waitForMillisecondsAsync(2000) | ||
|
||
contract.getPastEvents( | ||
{ | ||
fromBlock: 0, | ||
toBlock: 'latest' | ||
}, | ||
(_err: Error, events: any) => { | ||
t.assert( | ||
events.every((event: any) => event.event === 'NewValueSet'), | ||
`NewValueSet in ${numEvents} events` | ||
) | ||
t.equal(events.length, numEvents, `Should have ${numEvents} events registered`) | ||
} | ||
) | ||
|
||
await waitForMillisecondsAsync(2000) | ||
|
||
client.disconnect() | ||
} catch (err) { | ||
console.log(err) | ||
} | ||
|
||
t.end() | ||
}) |
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