Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Redo output format with interfaces instead of classes (also some bugfixes) #2180

Merged
merged 8 commits into from
Jul 16, 2019
88 changes: 87 additions & 1 deletion packages/truffle-codec-utils/src/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import BN from "bn.js";
import Web3 from "web3";
import { Constants } from "./constants";
import { Values } from "./types/values";
import { enumFullName } from "./types/inspect";

export namespace Conversion {

Expand Down Expand Up @@ -160,7 +161,92 @@ export namespace Conversion {
//for convenience: invokes the nativize method on all the given variables
export function nativizeVariables(variables: {[name: string]: Values.Result}): {[name: string]: any} {
return Object.assign({}, ...Object.entries(variables).map(
([name, value]) => ({[name]: value.nativize()})
([name, value]) => ({[name]: nativize(value)})
));
}

//HACK! Avoid using! Only use this if:
//1. you absolutely have to, or
//2. it's just testing, not real code
export function nativize(result: Values.Result): any {
if(result.kind === "error") {
return undefined;
}
switch(result.type.typeClass) {
case "uint":
case "int":
return (<Values.UintValue|Values.IntValue>result).value.asBN.toNumber(); //WARNING
case "bool":
return (<Values.BoolValue>result).value.asBool;
case "bytes":
return (<Values.BytesValue>result).value.asHex;
case "address":
return (<Values.AddressValue>result).value.asAddress;
case "string": {
let coercedResult = <Values.StringValue> result;
switch(coercedResult.value.kind) {
case "valid":
return coercedResult.value.asString;
case "malformed":
// this will turn malformed utf-8 into replacement characters (U+FFFD) (WARNING)
// note we need to cut off the 0x prefix
return Buffer.from(coercedResult.value.asHex.slice(2), 'hex').toString();
}
}
//fixed and ufixed are skipped for now
case "array": //WARNING: circular case not handled; will loop infinitely
return (<Values.ArrayValue>result).value.map(nativize);
case "mapping":
return Object.assign({}, ...(<Values.MappingValue>result).value.map(
({key, value}) => ({[nativize(key).toString()]: nativize(value)})
haltman-at marked this conversation as resolved.
Show resolved Hide resolved
));
case "struct": //WARNING: circular case not handled; will loop infinitely
return Object.assign({}, ...(<Values.StructValue>result).value.map(
({name, value}) => ({[name]: nativize(value)})
));
case "magic":
return Object.assign({}, ...Object.entries((<Values.MagicValue>result).value).map(
([key, value]) => ({[key]: nativize(value)})
));
case "enum":
return enumFullName(<Values.EnumValue>result);
case "contract": {
let coercedResult = <Values.ContractValue> result;
switch(coercedResult.value.kind) {
case "known":
return `${coercedResult.value.class.typeName}(${coercedResult.value.address})`;
case "unknown":
return coercedResult.value.address;
}
break; //to satisfy typescript
gnidan marked this conversation as resolved.
Show resolved Hide resolved
}
case "function":
switch(result.type.visibility) {
case "external": {
let coercedResult = <Values.FunctionExternalValue> result;
switch(coercedResult.value.kind) {
case "known":
return `${coercedResult.value.contract.class.typeName}(${coercedResult.value.contract.address}).${coercedResult.value.abi.name}`
case "invalid":
return `${coercedResult.value.contract.class.typeName}(${coercedResult.value.contract.address}).call(${coercedResult.value.selector}...)`
case "unknown":
return `${coercedResult.value.contract.address}.call(${coercedResult.value.selector}...)`
}
}
case "internal": {
let coercedResult = <Values.FunctionInternalValue> result;
switch(coercedResult.value.kind) {
case "function":
return `${coercedResult.value.definedIn.typeName}.${coercedResult.value.name}`;
case "exception":
return coercedResult.value.deployedProgramCounter === 0
? `<zero>`
: `assert(false)`;
case "unknown":
return `<decoding not supported>`;
}
}
}
}
}
}
5 changes: 4 additions & 1 deletion packages/truffle-codec-utils/src/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ export namespace Definition {
if(compiler === null) {
return false;
}
if(semver.satisfies(compiler.version, ">=0.5.0", {includePrerelease: true})) {
if(semver.satisfies(compiler.version, "~0.5 || >=0.5.0", {includePrerelease: true})) {
//note that we use ~0.5 || >=0.5.0 to make sure we include prerelease versions of
//0.5.0 (no, that is *not* what the includePrerelease flag doesn; that allows
//prerelease versions to be included *at all*)
return typeIdentifier(definition) === "t_address_payable";
}
else {
Expand Down
2 changes: 2 additions & 0 deletions packages/truffle-codec-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export * from "./contexts";
export * from "./abi";
export * from "./compiler";
export * from "./errors";
export * from "./wrap";
export * from "./types/types";
export * from "./types/values";
export * from "./types/errors";
export * from "./types/inspect";
Loading