@dappworks/kit 0.4.116 → 0.4.118

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/aiem.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { Abi, Chain, GetContractReturnType, PublicClient, HttpTransport, WalletClient, AbiFunction } from 'viem';
1
+ import { Abi, Chain, GetContractReturnType, PublicClient, HttpTransport, WalletClient } from 'viem';
2
2
  import TTLCache from '@isaacs/ttlcache';
3
3
 
4
4
  type ClassType<T> = {
@@ -10,6 +10,7 @@ interface FieldParams {
10
10
  }
11
11
  type ContractParams<T extends any = any, K extends keyof T = keyof T> = K;
12
12
  declare class Fields {
13
+ static hide(options?: FieldParams): (target: any, propertyKey: any, descriptor?: PropertyDescriptor) => void;
13
14
  static read(options?: FieldParams): (target: any, propertyKey: any, descriptor?: PropertyDescriptor) => void;
14
15
  static write(options?: FieldParams): (target: any, propertyKey: any, descriptor?: PropertyDescriptor) => void;
15
16
  static custom(options?: FieldParams): (target: (...args: any[]) => Promise<any>, propertyKey: any, descriptor?: PropertyDescriptor) => void;
@@ -82,14 +83,40 @@ declare class AIem<Contracts extends Record<string, Abi>, Chains extends Record<
82
83
  }): any;
83
84
  static init(): AIem<any, any, any>;
84
85
  static Get<TAbi extends Abi = any>(abi: TAbi, chainId: any, address: any, wallet?: WalletClient): GetContractReturnType<TAbi, PublicClient<HttpTransport, Chain, any, any>>;
85
- static Query: <E, S extends QuerySelect<E>>(entity: ClassType<E>, select: S) => ((entities: Partial<E>[]) => Promise<Array<QueryReturnType<E, S>>>);
86
+ static Query: <E, S extends QuerySelect<E>>(entity: ClassType<E>, select: S) => ((entities: Partial<E> | Partial<E>[]) => Promise<QueryResult<E, S>>);
87
+ static getPrice({ chainId, address }: {
88
+ chainId?: string;
89
+ address: string;
90
+ }): Promise<any>;
91
+ static utils: {
92
+ autoFormat: ({ value, decimals, chainId, address }: {
93
+ value: string;
94
+ decimals: number;
95
+ chainId: string;
96
+ address: string;
97
+ }) => Promise<{
98
+ usd: string;
99
+ value: string;
100
+ format: string;
101
+ decimals: string;
102
+ isZero: boolean;
103
+ originFormat?: undefined;
104
+ } | {
105
+ usd: string;
106
+ value: string;
107
+ originFormat: string;
108
+ format: string;
109
+ decimals: string;
110
+ isZero?: undefined;
111
+ }>;
112
+ };
86
113
  }
87
- type ReadFunctionKeys<T extends Abi> = T[number] extends infer U ? U extends AbiFunction ? U['stateMutability'] extends 'view' | 'pure' ? U['name'] : never : never : never;
114
+ type QueryResult<E, S extends QuerySelect<E>> = E extends Array<any> ? Promise<Array<QueryReturnType<E[number], S>>> : E extends object ? Promise<QueryReturnType<E, S>> : never;
88
115
  type QuerySelect<E> = {
89
- [K in keyof E]?: E[K] extends (...args: any[]) => any ? Parameters<E[K]> | true : E[K] extends object ? QuerySelect<E[K]> : true;
116
+ [K in keyof E]?: E[K] extends (...args: any[]) => any ? Parameters<E[K]> | true : E[K] extends object ? QuerySelect<E[K]> | true : true;
90
117
  };
91
118
  type QueryReturnType<E, S extends QuerySelect<E>> = {
92
- [K in keyof S]: K extends keyof E ? E[K] extends (...args: any[]) => any ? Awaited<ReturnType<E[K]>> : E[K] extends object ? S[K] extends object ? K extends keyof E ? QueryReturnType<E[K], S[K]> : E[K] : E[K] : E[K] : never;
119
+ [K in keyof E]: K extends keyof S ? E[K] extends (...args: any[]) => any ? Awaited<ReturnType<E[K]>> : E[K] extends object ? S[K] extends object ? QueryReturnType<E[K], S[K]> : E[K] : E[K] : E[K];
93
120
  };
94
121
 
95
- export { AIem, Cache, Fields, type ReadFunctionKeys };
122
+ export { AIem, Cache, Fields, type QueryResult, type QueryReturnType, type QuerySelect };
package/dist/aiem.mjs CHANGED
@@ -1,7 +1,11 @@
1
- import { __objRest } from './chunk-R4SQKVDQ.mjs';
1
+ import './chunk-7MDKCI65.mjs';
2
+ import { helper } from './chunk-46XF5IRX.mjs';
3
+ import './chunk-MGU3KYGC.mjs';
4
+ import { __spreadProps, __spreadValues, __objRest } from './chunk-R4SQKVDQ.mjs';
2
5
  import { createPublicClient, http, getContract, encodeFunctionData } from 'viem';
3
6
  import { mainnet, iotex, bsc, polygon, iotexTestnet } from 'viem/chains';
4
7
  import TTLCache from '@isaacs/ttlcache';
8
+ import BigNumber from 'bignumber.js';
5
9
 
6
10
  // ../../node_modules/reflect-metadata/Reflect.js
7
11
  var Reflect2;
@@ -786,6 +790,11 @@ var Reflect2;
786
790
  // lib/decorators.ts
787
791
  var FIELD_KEY = Symbol("aiem_field");
788
792
  var Fields = class {
793
+ static hide(options = {}) {
794
+ return function(target, propertyKey, descriptor) {
795
+ Reflect.defineMetadata(FIELD_KEY, { type: "hide", options }, target, propertyKey);
796
+ };
797
+ }
789
798
  static read(options = {}) {
790
799
  return function(target, propertyKey, descriptor) {
791
800
  Reflect.defineMetadata(FIELD_KEY, { type: "read", options }, target, propertyKey);
@@ -810,8 +819,6 @@ var Fields = class {
810
819
  function getFieldMetadata(target, propertyKey) {
811
820
  return Reflect.getMetadata(FIELD_KEY, target, propertyKey);
812
821
  }
813
-
814
- // aiem.ts
815
822
  mainnet.rpcUrls.default.http = ["https://rpc.ankr.com/eth"];
816
823
  mainnet.rpcUrls.default.webSocket = ["wss://ethereum-rpc.publicnode.com"];
817
824
  var Cache = class {
@@ -983,6 +990,16 @@ var _AIem = class _AIem {
983
990
  });
984
991
  });
985
992
  }
993
+ static async getPrice({ chainId = "4689", address }) {
994
+ const priceMap = await this.cache.wrap(`token-price`, async () => {
995
+ const res = await (await fetch("https://api.iopay.me/api/rest/price")).json();
996
+ return Object.values(res).flat().reduce((p, c) => {
997
+ p[`${4689}-${c.platforms.toLowerCase()}`] = c.current_price;
998
+ return p;
999
+ }, {});
1000
+ }, { ttl: 1e3 * 60 });
1001
+ return priceMap[`${chainId}-${address}`];
1002
+ }
986
1003
  };
987
1004
  _AIem.cache = new Cache();
988
1005
  _AIem.defaultFuncMap = {
@@ -995,11 +1012,14 @@ _AIem.defaultFuncMap = {
995
1012
  _AIem.Query = (entity, select) => {
996
1013
  return async (entities) => {
997
1014
  const results = [];
1015
+ const isArrayInput = Array.isArray(entities);
1016
+ if (!isArrayInput) {
1017
+ entities = [entities];
1018
+ }
998
1019
  for (const entityData of entities) {
999
1020
  const instance = Object.assign(new entity(), entityData);
1000
- const result = {};
1001
- const fetchFields = async (obj, sel, res) => {
1002
- var _a;
1021
+ const fetchFields = async (obj, sel) => {
1022
+ var _a, _b;
1003
1023
  const promises = [];
1004
1024
  for (const key in sel) {
1005
1025
  const fieldMetadata = getFieldMetadata(obj, key);
@@ -1008,51 +1028,73 @@ _AIem.Query = (entity, select) => {
1008
1028
  switch (fieldMetadata.type) {
1009
1029
  case "read":
1010
1030
  if (Array.isArray(sel[key])) {
1011
- call = _AIem.Get(instance.abi, instance.chainId, instance.address).read[key](sel[key]).then((value) => res[key] = value);
1031
+ call = () => _AIem.Get(entity.abi, instance.chainId, instance.address).read[key](sel[key]);
1012
1032
  } else {
1013
- call = _AIem.Get(instance.abi, instance.chainId, instance.address).read[key]().then((value) => res[key] = value);
1033
+ call = () => _AIem.Get(entity.abi, instance.chainId, instance.address).read[key]();
1014
1034
  }
1015
1035
  break;
1016
1036
  case "write":
1017
- res[key] = encodeFunctionData({
1037
+ obj[key] = encodeFunctionData({
1018
1038
  //@ts-ignore
1019
- abi: instance.abi,
1039
+ abi: entity.abi,
1020
1040
  functionName: key,
1021
1041
  args: sel[key]
1022
1042
  });
1023
1043
  break;
1024
1044
  case "custom":
1025
- call = obj[key](...Array.isArray(sel[key]) ? sel[key] : []).then((value) => res[key] = value);
1045
+ call = () => obj[key](...Array.isArray(sel[key]) ? sel[key] : []);
1026
1046
  break;
1027
1047
  case "contract":
1028
1048
  if (fieldMetadata.targetKey) {
1029
- call = _AIem.Get(instance.abi, instance.chainId, instance.address).read[fieldMetadata.targetKey]().then((address) => {
1030
- return _AIem.Query(fieldMetadata.entity(), sel[key])([{ address, chainId: instance.chainId }]);
1031
- });
1049
+ const targetMetadata = getFieldMetadata(instance, fieldMetadata.targetKey);
1050
+ if ((_a = targetMetadata == null ? void 0 : targetMetadata.options) == null ? void 0 : _a.ttl) {
1051
+ const cacheKey = `call ${instance.chainId}-${instance.address}-${fieldMetadata.targetKey}`;
1052
+ call = () => new Promise(async (resolve) => {
1053
+ const address = await _AIem.cache.wrap(cacheKey, async () => _AIem.Get(entity.abi, instance.chainId, instance.address).read[fieldMetadata.targetKey]());
1054
+ resolve(_AIem.Query(fieldMetadata.entity(), sel[key])({ address, chainId: instance.chainId }));
1055
+ });
1056
+ } else {
1057
+ call = () => _AIem.Get(entity.abi, instance.chainId, instance.address).read[fieldMetadata.targetKey]().then((address) => {
1058
+ return _AIem.Query(fieldMetadata.entity(), sel[key])({ address, chainId: instance.chainId });
1059
+ });
1060
+ }
1032
1061
  }
1033
1062
  break;
1034
1063
  }
1035
1064
  } else if (sel[key] === true) {
1036
- res[key] = obj[key];
1065
+ obj[key] = obj[key];
1037
1066
  }
1038
1067
  if (call) {
1039
- if ((_a = fieldMetadata == null ? void 0 : fieldMetadata.options) == null ? void 0 : _a.ttl) {
1068
+ if ((_b = fieldMetadata == null ? void 0 : fieldMetadata.options) == null ? void 0 : _b.ttl) {
1040
1069
  const cacheKey = `call ${instance.chainId}-${instance.address}-${key}-${JSON.stringify(sel[key])}`;
1041
- promises.push(_AIem.cache.wrap(cacheKey, () => call, fieldMetadata.options));
1070
+ promises.push(new Promise(async (resolve) => {
1071
+ const value = await _AIem.cache.wrap(cacheKey, async () => call(), fieldMetadata.options);
1072
+ obj[key] = value;
1073
+ resolve(value);
1074
+ }));
1075
+ } else {
1076
+ promises.push(call().then((value) => {
1077
+ obj[key] = value;
1078
+ }));
1042
1079
  }
1043
- promises.push(call.then((value) => {
1044
- res[key] = value;
1045
- }));
1046
1080
  }
1047
1081
  }
1048
1082
  await Promise.all(promises);
1049
1083
  };
1050
- await fetchFields(instance, select, result);
1051
- results.push(result);
1084
+ await fetchFields(instance, select);
1085
+ results.push(instance);
1052
1086
  }
1053
- return results;
1087
+ return isArrayInput ? results : results[0];
1054
1088
  };
1055
1089
  };
1090
+ _AIem.utils = {
1091
+ autoFormat: async ({ value, decimals, chainId, address }) => {
1092
+ const wrap = helper.number.warpBigNumber(value, decimals, { format: "0,0.000000", fallback: "" });
1093
+ const price = await _AIem.getPrice({ chainId, address: address.toLowerCase() });
1094
+ const usd = new BigNumber(wrap.originFormat).multipliedBy(price || 1).toFixed(2);
1095
+ return __spreadProps(__spreadValues({}, wrap), { usd });
1096
+ }
1097
+ };
1056
1098
  var AIem = _AIem;
1057
1099
  /*! Bundled license information:
1058
1100