Contract ABI (extended)
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
import { CodecArg, Constructor } from './types';
|
||||
|
||||
import { assert, isNumber, isNull, isString, isUndefined, stringCamelCase } from '@plugnet/util';
|
||||
import { assert, isNumber, isNull, isString, isUndefined, stringCamelCase, u8aToHex } from '@plugnet/util';
|
||||
|
||||
import Compact from './codec/Compact';
|
||||
import { createClass } from './codec/createType';
|
||||
@@ -104,14 +104,14 @@ export default class ContractAbi implements Contract {
|
||||
});
|
||||
}
|
||||
|
||||
private _createClazz (args: ContractABIArgs, isDeploy: boolean): Constructor {
|
||||
private _createClazz (args: ContractABIArgs, baseDef: { [index: string]: string }): Constructor {
|
||||
return createClass(
|
||||
JSON.stringify(
|
||||
args.reduce((base: { [index: string]: string }, { name, type }) => {
|
||||
base[name] = type;
|
||||
|
||||
return base;
|
||||
}, isDeploy ? {} : { __selector: 'u32' })
|
||||
}, baseDef)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -121,20 +121,22 @@ export default class ContractAbi implements Contract {
|
||||
name: stringCamelCase(name),
|
||||
type
|
||||
}));
|
||||
const Clazz = this._createClazz(args, isUndefined(method.selector));
|
||||
const base: { [index: string]: any } = { __selector: method.selector };
|
||||
const Clazz = this._createClazz(args, isUndefined(method.selector) ? {} : { __selector: 'u32' });
|
||||
const baseStruct: { [index: string]: any } = { __selector: method.selector };
|
||||
const encoder = (...params: Array<CodecArg>): Uint8Array => {
|
||||
assert(params.length === args.length, `Expected ${args.length} arguments to contract ${name}, found ${params.length}`);
|
||||
|
||||
return Compact.addLengthPrefix(
|
||||
new Clazz(
|
||||
args.reduce((mapped, { name }, index) => {
|
||||
mapped[name] = params[index];
|
||||
const u8a = new Clazz(
|
||||
args.reduce((mapped, { name }, index) => {
|
||||
mapped[name] = params[index];
|
||||
|
||||
return mapped;
|
||||
}, { ...base })
|
||||
).toU8a()
|
||||
);
|
||||
return mapped;
|
||||
}, { ...baseStruct })
|
||||
).toU8a();
|
||||
|
||||
console.error('hexToU8a(encoded)', u8aToHex(u8a));
|
||||
|
||||
return Compact.addLengthPrefix(u8a);
|
||||
};
|
||||
|
||||
const fn = (encoder as ContractABIFn);
|
||||
|
||||
Reference in New Issue
Block a user