MyCrypto/spec/libs/contract.spec.js
William O'Beirne bf4171dfbd Transaction confirmation modal (#108)
* Add a little arrow icon.

* Replaced toEther function with toUnit to reduce the number of conversion functions wed need. Add tests for conversion functions.

* First pass at a styled confirm transaction modal.

* More data about data

* Hook up generated transaction with modal

* Fix modal position

* Add from address. Restyle a bit.

* Only show textareas and button if transaction has been generated.

* Remove need for param.

* Copy.

* Use non-relative path.

* Initial crack at transaction token support.

* Fix flow type

* Unit tests for contracts and erc20

* Convert contract class to ethereumjs-abi, caught a bug

* Add decodeArgs for contracts, decodeTransfer for erc20

* Show token value in modal

* Show value from transaction data in confirmation.

* Show address of receiver, not token contract

* Flow type

* Only accept bigs

* Unlog

* Use ethereumjs-abis method ID function

* Get transaction stuff out of state. Leave todo notes.

* Intuit token from transaction to address.

* Move generate transaction out of node and into libs/transaction.

* timeout -> interval

* Promise.reject -> throw

* Get default currency from network.

* Add more unit tests for decoding. Adopt the $ prefix for decoding calls.

* Use signed transaction in confirmation modal.
2017-08-23 08:57:18 +02:00

123 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Contract from 'libs/contract';
import Big from 'bignumber.js';
describe('Contract', () => {
// From the ABI docs
// https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#json
const testAbi = [
{
type: 'event',
inputs: [
{ name: 'a', type: 'uint256', indexed: true },
{ name: 'b', type: 'bytes32', indexed: false }
],
name: 'Event'
},
{
type: 'function',
inputs: [{ name: 'a', type: 'uint256' }],
name: 'foo',
outputs: []
}
];
const testContract = new Contract(testAbi);
// ----------------------------------------------------------------------
describe('constructor', () => {
it('should create an instance given a valid ABI', () => {
expect(testContract).toBeTruthy();
});
});
// ----------------------------------------------------------------------
describe('getMethodAbi', () => {
it('should return the a method, given the right name', () => {
const method = testContract.getMethodAbi('foo');
expect(method.name).toBe('foo');
expect(method.type).toBe('function');
expect(method.inputs.constructor).toBe(Array);
});
it('should throw if given an unknown method name', () => {
expect(() => {
testContract.getMethodAbi('gnjwakgnawk');
}).toThrow();
});
it('should throw if given a method isnt a function', () => {
expect(() => {
testContract.getMethodAbi('Event');
}).toThrow();
});
});
// ----------------------------------------------------------------------
describe('call / encodeArgs', () => {
it('should return hex data for the method', () => {
const result = testContract.call('foo', ['1337']);
expect(result).toBe(
'0x2fbebd380000000000000000000000000000000000000000000000000000000000000539'
);
});
it('should throw, if given too few method args', () => {
expect(() => {
testContract.call('foo', []);
}).toThrow();
});
it('should throw, if given too many method args', () => {
expect(() => {
testContract.call('foo', ['1', '2']);
}).toThrow();
});
it('should throw, if given invalid args', () => {
expect(() => {
testContract.call('foo', [{ some: 'object?' }]);
}).toThrow();
});
});
// ----------------------------------------------------------------------
describe('$call / decodeArgs', () => {
it('should decode some data', () => {
const decoded = testContract.$call(
'0x2fbebd380000000000000000000000000000000000000000000000000000000000000539'
);
expect(decoded.method.name).toBe('foo');
expect(decoded.args[0].toString(10)).toBe('1337');
});
it('should return identical data from a call return', () => {
const callMethod = 'foo';
const callArgs = ['42891048912084012480129'];
const callData = testContract.call(callMethod, callArgs);
const decoded = testContract.$call(callData);
expect(decoded.method.name).toBe(callMethod);
expect(decoded.args[0].toString(10)).toBe(callArgs[0]);
});
it('should throw, if given invalid data', () => {
expect(() => {
// ETH address
testContract.$call('0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8');
}).toThrow();
});
it('should throw, if given an unknown methods data', () => {
expect(() => {
// GNT token send data
testContract.$call(
'0xa9059cbb0000000000000000000000007cb57b5a97eabe94205c07890be4c1ad31e486a8000000000000000000000000000000000000000000000000130d2a539ba80000'
);
}).toThrow();
});
});
});