@dappworks/kit 0.4.117 → 0.4.119

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;
@@ -72,7 +73,7 @@ declare class AIem<Contracts extends Record<string, Abi>, Chains extends Record<
72
73
  constructor(args?: Pick<AIem<Contracts, Chains, Addrs>, "contractMap" | "chainMap" | "nameMap" | "getWallet" | "cache" | "funcMap">);
73
74
  PubClient<C extends keyof Chains>(chainId: C): PublicClient<HttpTransport, Chain, any, any>;
74
75
  Get<K extends keyof Contracts, C extends keyof Chains, Addr extends `0x${string}`>(contractName: K, chainId: C, address: Addr): GetContractReturnType<Contracts[K], PublicClient<HttpTransport, Chain, any, any>>;
75
- getContract({ client, address, abi }: {
76
+ getContract({ client, address, abi, }: {
76
77
  client: {
77
78
  public: PublicClient<HttpTransport, Chain, any, any>;
78
79
  wallet?: WalletClient;
@@ -82,14 +83,34 @@ 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 getPrice({ chainId, address }: {
87
+ chainId?: string;
88
+ address: string;
89
+ }): Promise<any>;
90
+ static utils: {
91
+ autoFormat: ({ value, decimals, chainId, address }: {
92
+ value: string;
93
+ decimals: number;
94
+ chainId: string;
95
+ address: string;
96
+ }) => Promise<{
97
+ usd: string;
98
+ value: string;
99
+ format: string;
100
+ originFormat: string;
101
+ decimals: string;
102
+ isZero: boolean;
103
+ }>;
104
+ };
105
+ static QueryMany<E, S extends QuerySelect<E>>(entity: ClassType<E>, select: S): (_entities: Partial<E>[]) => Promise<QueryReturnType<E, S>[]>;
106
+ static Query<E, S extends QuerySelect<E>>(entity: ClassType<E>, select: S): (entities: Partial<E>) => Promise<QueryReturnType<E, S>>;
86
107
  }
87
- type ReadFunctionKeys<T extends Abi> = T[number] extends infer U ? U extends AbiFunction ? U['stateMutability'] extends 'view' | 'pure' ? U['name'] : never : never : never;
108
+ type Item<T> = T extends (infer U)[] ? U : T;
88
109
  type QuerySelect<E> = {
89
110
  [K in keyof E]?: E[K] extends (...args: any[]) => any ? Parameters<E[K]> | true : E[K] extends object ? QuerySelect<E[K]> : true;
90
111
  };
91
112
  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;
113
+ [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
114
  };
94
115
 
95
- export { AIem, Cache, Fields, type ReadFunctionKeys };
116
+ export { AIem, Cache, Fields, type Item, type QueryReturnType };
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-Q3AD5RHQ.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 {
@@ -849,29 +856,35 @@ var _AIem = class _AIem {
849
856
  };
850
857
  this.funcMap = {};
851
858
  this.Set(args);
852
- this.contracts = new Proxy({}, {
853
- //@ts-ignore
854
- get: (target, contractName) => {
855
- if (target[contractName])
859
+ this.contracts = new Proxy(
860
+ {},
861
+ {
862
+ //@ts-ignore
863
+ get: (target, contractName) => {
864
+ if (target[contractName])
865
+ return target[contractName];
866
+ if (!this.nameMap[contractName]) {
867
+ throw new Error(`Contract ${String(contractName)} not found`);
868
+ }
869
+ target[contractName] = new Proxy(
870
+ {},
871
+ {
872
+ //@ts-ignore
873
+ get: (innerTarget, contractAlias) => {
874
+ var _a;
875
+ const addressStr = (_a = this.nameMap[contractName]) == null ? void 0 : _a[contractAlias];
876
+ if (!addressStr) {
877
+ throw new Error(`Alias ${String(contractAlias)} for contract ${String(contractName)} not found`);
878
+ }
879
+ const [chainId, address] = addressStr.split("-");
880
+ return this.Get(contractName, String(chainId), address);
881
+ }
882
+ }
883
+ );
856
884
  return target[contractName];
857
- if (!this.nameMap[contractName]) {
858
- throw new Error(`Contract ${String(contractName)} not found`);
859
885
  }
860
- target[contractName] = new Proxy({}, {
861
- //@ts-ignore
862
- get: (innerTarget, contractAlias) => {
863
- var _a;
864
- const addressStr = (_a = this.nameMap[contractName]) == null ? void 0 : _a[contractAlias];
865
- if (!addressStr) {
866
- throw new Error(`Alias ${String(contractAlias)} for contract ${String(contractName)} not found`);
867
- }
868
- const [chainId, address] = addressStr.split("-");
869
- return this.Get(contractName, String(chainId), address);
870
- }
871
- });
872
- return target[contractName];
873
886
  }
874
- });
887
+ );
875
888
  }
876
889
  get _cache() {
877
890
  return _AIem.cache;
@@ -941,9 +954,13 @@ var _AIem = class _AIem {
941
954
  const methodConfig = (_a = this.funcMap) == null ? void 0 : _a[funcName];
942
955
  const cacheKey = `call ${client.public.chain.id}-${address}-${funcName}-${JSON.stringify(args)}`;
943
956
  if (methodConfig) {
944
- return this.cache.wrap(cacheKey, () => {
945
- return target[funcName](...args);
946
- }, methodConfig);
957
+ return this.cache.wrap(
958
+ cacheKey,
959
+ () => {
960
+ return target[funcName](...args);
961
+ },
962
+ methodConfig
963
+ );
947
964
  }
948
965
  return target[funcName](...args);
949
966
  };
@@ -983,89 +1000,130 @@ var _AIem = class _AIem {
983
1000
  });
984
1001
  });
985
1002
  }
986
- };
987
- _AIem.cache = new Cache();
988
- _AIem.defaultFuncMap = {
989
- "totalSupply": { ttl: 15 * 1e3 },
990
- "symbol": { ttl: 60 * 1e3 },
991
- "name": { ttl: 60 * 1e3 },
992
- "decimals": { ttl: 60 * 1e3 },
993
- "balanceOf": { ttl: 5 * 1e3 }
994
- };
995
- _AIem.Query = (entity, select) => {
996
- return async (entities) => {
997
- const results = [];
998
- for (const entityData of entities) {
999
- const instance = Object.assign(new entity(), entityData);
1000
- const result = {};
1001
- const fetchFields = async (obj, sel, res) => {
1002
- var _a, _b;
1003
- const promises = [];
1004
- for (const key in sel) {
1005
- const fieldMetadata = getFieldMetadata(obj, key);
1006
- let call;
1007
- if (fieldMetadata) {
1008
- switch (fieldMetadata.type) {
1009
- case "read":
1010
- if (Array.isArray(sel[key])) {
1011
- call = () => _AIem.Get(instance.abi, instance.chainId, instance.address).read[key](sel[key]);
1012
- } else {
1013
- call = () => _AIem.Get(instance.abi, instance.chainId, instance.address).read[key]();
1014
- }
1015
- break;
1016
- case "write":
1017
- res[key] = encodeFunctionData({
1018
- //@ts-ignore
1019
- abi: instance.abi,
1020
- functionName: key,
1021
- args: sel[key]
1022
- });
1023
- break;
1024
- case "custom":
1025
- call = () => obj[key](...Array.isArray(sel[key]) ? sel[key] : []);
1026
- break;
1027
- case "contract":
1028
- if (fieldMetadata.targetKey) {
1029
- const targetMetadata = getFieldMetadata(instance, fieldMetadata.targetKey);
1030
- if ((_a = targetMetadata == null ? void 0 : targetMetadata.options) == null ? void 0 : _a.ttl) {
1031
- const cacheKey = `call ${instance.chainId}-${instance.address}-${fieldMetadata.targetKey}`;
1032
- call = () => new Promise(async (resolve) => {
1033
- const address = await _AIem.cache.wrap(cacheKey, async () => _AIem.Get(instance.abi, instance.chainId, instance.address).read[fieldMetadata.targetKey]());
1034
- resolve(_AIem.Query(fieldMetadata.entity(), sel[key])([{ address, chainId: instance.chainId }]));
1035
- });
1003
+ static async getPrice({ chainId = "4689", address }) {
1004
+ const priceMap = await this.cache.wrap(
1005
+ `token-price`,
1006
+ async () => {
1007
+ const res = await (await fetch("https://api.iopay.me/api/rest/price")).json();
1008
+ return Object.values(res).flat().reduce((p, c) => {
1009
+ p[`${4689}-${c.platforms.toLowerCase()}`] = c.current_price;
1010
+ return p;
1011
+ }, {});
1012
+ },
1013
+ { ttl: 1e3 * 60 }
1014
+ );
1015
+ return priceMap[`${chainId}-${address}`];
1016
+ }
1017
+ static QueryMany(entity, select) {
1018
+ return async (_entities) => {
1019
+ return this.Query(entity, select)(_entities);
1020
+ };
1021
+ }
1022
+ static Query(entity, select) {
1023
+ return async (entities) => {
1024
+ const results = [];
1025
+ const isArrayInput = Array.isArray(entities);
1026
+ if (!isArrayInput) {
1027
+ entities = [entities];
1028
+ }
1029
+ for (const entityData of entities) {
1030
+ const instance = Object.assign(new entity(), entityData);
1031
+ const fetchFields = async (obj, sel) => {
1032
+ var _a, _b;
1033
+ const promises = [];
1034
+ for (const key in sel) {
1035
+ const fieldMetadata = getFieldMetadata(obj, key);
1036
+ let call;
1037
+ if (fieldMetadata) {
1038
+ switch (fieldMetadata.type) {
1039
+ case "read":
1040
+ if (Array.isArray(sel[key])) {
1041
+ call = () => this.Get(entity.abi, instance.chainId, instance.address).read[key](sel[key]);
1036
1042
  } else {
1037
- call = () => _AIem.Get(instance.abi, instance.chainId, instance.address).read[fieldMetadata.targetKey]().then((address) => {
1038
- return _AIem.Query(fieldMetadata.entity(), sel[key])([{ address, chainId: instance.chainId }]);
1039
- });
1043
+ call = () => this.Get(entity.abi, instance.chainId, instance.address).read[key]();
1040
1044
  }
1041
- }
1042
- break;
1045
+ break;
1046
+ case "write":
1047
+ obj[key] = encodeFunctionData({
1048
+ //@ts-ignore
1049
+ abi: entity.abi,
1050
+ functionName: key,
1051
+ args: sel[key]
1052
+ });
1053
+ break;
1054
+ case "custom":
1055
+ call = () => obj[key](...Array.isArray(sel[key]) ? sel[key] : []);
1056
+ break;
1057
+ case "contract":
1058
+ if (fieldMetadata.targetKey) {
1059
+ const targetMetadata = getFieldMetadata(instance, fieldMetadata.targetKey);
1060
+ if ((_a = targetMetadata == null ? void 0 : targetMetadata.options) == null ? void 0 : _a.ttl) {
1061
+ const cacheKey = `call ${instance.chainId}-${instance.address}-${fieldMetadata.targetKey}`;
1062
+ call = () => new Promise(async (resolve) => {
1063
+ const address = await this.cache.wrap(cacheKey, async () => this.Get(entity.abi, instance.chainId, instance.address).read[fieldMetadata.targetKey]());
1064
+ resolve(this.Query(fieldMetadata.entity(), sel[key])({ address, chainId: instance.chainId }));
1065
+ });
1066
+ } else {
1067
+ call = () => (
1068
+ //@ts-ignore
1069
+ this.Get(entity.abi, instance.chainId, instance.address).read[fieldMetadata.targetKey]().then((address) => {
1070
+ return this.Query(fieldMetadata.entity(), sel[key])({ address, chainId: instance.chainId });
1071
+ })
1072
+ );
1073
+ }
1074
+ }
1075
+ break;
1076
+ }
1077
+ } else if (sel[key] === true) {
1078
+ obj[key] = obj[key];
1043
1079
  }
1044
- } else if (sel[key] === true) {
1045
- res[key] = obj[key];
1046
- }
1047
- if (call) {
1048
- if ((_b = fieldMetadata == null ? void 0 : fieldMetadata.options) == null ? void 0 : _b.ttl) {
1049
- const cacheKey = `call ${instance.chainId}-${instance.address}-${key}-${JSON.stringify(sel[key])}`;
1050
- promises.push(new Promise(async (resolve) => {
1051
- const value = await _AIem.cache.wrap(cacheKey, async () => call(), fieldMetadata.options);
1052
- res[key] = value;
1053
- resolve(value);
1054
- }));
1055
- } else {
1056
- promises.push(call().then((value) => {
1057
- res[key] = value;
1058
- }));
1080
+ if (call) {
1081
+ if ((_b = fieldMetadata == null ? void 0 : fieldMetadata.options) == null ? void 0 : _b.ttl) {
1082
+ const cacheKey = `call ${instance.chainId}-${instance.address}-${key}-${JSON.stringify(sel[key])}`;
1083
+ promises.push(
1084
+ new Promise(async (resolve) => {
1085
+ const value = await this.cache.wrap(cacheKey, async () => call(), fieldMetadata.options);
1086
+ obj[key] = value;
1087
+ resolve(value);
1088
+ })
1089
+ );
1090
+ } else {
1091
+ promises.push(
1092
+ call().then((value) => {
1093
+ obj[key] = value;
1094
+ })
1095
+ );
1096
+ }
1059
1097
  }
1060
1098
  }
1061
- }
1062
- await Promise.all(promises);
1063
- };
1064
- await fetchFields(instance, select, result);
1065
- results.push(result);
1066
- }
1067
- return results;
1068
- };
1099
+ await Promise.all(promises);
1100
+ };
1101
+ await fetchFields(instance, select);
1102
+ results.push(instance);
1103
+ }
1104
+ if (isArrayInput) {
1105
+ return results;
1106
+ } else {
1107
+ return results[0];
1108
+ }
1109
+ };
1110
+ }
1111
+ };
1112
+ _AIem.cache = new Cache();
1113
+ _AIem.defaultFuncMap = {
1114
+ totalSupply: { ttl: 15 * 1e3 },
1115
+ symbol: { ttl: 60 * 1e3 },
1116
+ name: { ttl: 60 * 1e3 },
1117
+ decimals: { ttl: 60 * 1e3 },
1118
+ balanceOf: { ttl: 5 * 1e3 }
1119
+ };
1120
+ _AIem.utils = {
1121
+ autoFormat: async ({ value, decimals, chainId, address }) => {
1122
+ const wrap = helper.number.warpBigNumber(value, decimals, { format: "0,0.000000", fallback: "" });
1123
+ const price = await _AIem.getPrice({ chainId, address: address.toLowerCase() });
1124
+ const usd = new BigNumber(wrap.originFormat).multipliedBy(price || 1).toFixed(2);
1125
+ return __spreadProps(__spreadValues({}, wrap), { usd });
1126
+ }
1069
1127
  };
1070
1128
  var AIem = _AIem;
1071
1129
  /*! Bundled license information: