aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contract-wrappers
diff options
context:
space:
mode:
authorGreg Hysen <greg.hysen@gmail.com>2019-02-01 05:38:49 +0800
committerGreg Hysen <greg.hysen@gmail.com>2019-02-09 08:25:30 +0800
commitf7b58e7f64dc5af1e4ac8e7159717c78f0767c85 (patch)
treeaada13e4db66c904b7f2397233e7e871ad92ca05 /packages/contract-wrappers
parent436d87da98a2025106bfb42838bd18054f7aba63 (diff)
downloaddexon-0x-contracts-f7b58e7f64dc5af1e4ac8e7159717c78f0767c85.tar.gz
dexon-0x-contracts-f7b58e7f64dc5af1e4ac8e7159717c78f0767c85.tar.zst
dexon-0x-contracts-f7b58e7f64dc5af1e4ac8e7159717c78f0767c85.zip
moved ZeroExTransactionDecoder into contract wrappers
Diffstat (limited to 'packages/contract-wrappers')
-rw-r--r--packages/contract-wrappers/src/utils/zeroex_transaction_decoder.ts63
1 files changed, 63 insertions, 0 deletions
diff --git a/packages/contract-wrappers/src/utils/zeroex_transaction_decoder.ts b/packages/contract-wrappers/src/utils/zeroex_transaction_decoder.ts
new file mode 100644
index 000000000..441424e92
--- /dev/null
+++ b/packages/contract-wrappers/src/utils/zeroex_transaction_decoder.ts
@@ -0,0 +1,63 @@
+import { getContractAddressesForNetworkOrThrow, NetworkId } from '@0x/contract-addresses';
+import * as ContractArtifacts from '@0x/contract-artifacts';
+import { SimpleContractArtifact } from '@0x/types';
+import { AbiDefinition, ContractAbi } from 'ethereum-types';
+import * as _ from 'lodash';
+
+import { DeployedContractInfo, DeployedContractInfoByName, TransactionData, TransactionDecoder, TransactionProperties } from '@0x/utils';
+
+export class ZeroExTransactionDecoder extends TransactionDecoder {
+ private static _instance: ZeroExTransactionDecoder;
+
+ public static addABI(
+ abiArray: AbiDefinition[],
+ contractName: string,
+ deploymentInfos?: DeployedContractInfo[],
+ ): void {
+ const instance = ZeroExTransactionDecoder._getInstance();
+ instance.addABI(abiArray, contractName, deploymentInfos);
+ }
+
+ public static decode(calldata: string, txProperties?: TransactionProperties): TransactionData {
+ const instance = ZeroExTransactionDecoder._getInstance();
+ const decodedCalldata = instance.decode(calldata, txProperties);
+ return decodedCalldata;
+ }
+
+ private static _getInstance(): ZeroExTransactionDecoder {
+ if (!ZeroExTransactionDecoder._instance) {
+ ZeroExTransactionDecoder._instance = new ZeroExTransactionDecoder();
+ }
+ return ZeroExTransactionDecoder._instance;
+ }
+
+ private constructor() {
+ super();
+ // Load addresses by contract name
+ const deployedContractInfoByName: DeployedContractInfoByName = {};
+ _.each(NetworkId, (networkId: any) => {
+ if (typeof networkId !== 'number') {
+ return;
+ }
+ const contractAddressesForNetwork = getContractAddressesForNetworkOrThrow(networkId);
+ _.each(contractAddressesForNetwork, (contractAddress: string, contractName: string) => {
+ const contractNameLowercase = _.toLower(contractName);
+ if (_.isUndefined(deployedContractInfoByName[contractNameLowercase])) {
+ deployedContractInfoByName[contractNameLowercase] = [];
+ }
+ deployedContractInfoByName[contractNameLowercase].push({
+ contractAddress,
+ networkId,
+ });
+ });
+ });
+ // Load contract artifacts
+ _.each(ContractArtifacts, (contractArtifactAsJson: any) => {
+ const conractArtifact = contractArtifactAsJson as SimpleContractArtifact;
+ const contractName = conractArtifact.contractName;
+ const contractNameLowercase = _.toLower(contractName);
+ const contractAbi: ContractAbi = conractArtifact.compilerOutput.abi;
+ this.addABI(contractAbi, contractName, deployedContractInfoByName[contractNameLowercase]);
+ });
+ }
+}