@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 +27 -6
- package/dist/aiem.mjs +162 -104
- package/dist/aiem.mjs.map +1 -1
- package/dist/chunk-7MDKCI65.mjs +92 -0
- package/dist/chunk-7MDKCI65.mjs.map +1 -0
- package/dist/{chunk-EWPP7VNZ.mjs → chunk-Q3AD5RHQ.mjs} +4 -42
- package/dist/chunk-Q3AD5RHQ.mjs.map +1 -0
- package/dist/dev.mjs +1 -1
- package/dist/form.mjs +1 -1
- package/dist/index.mjs +43 -8
- package/dist/index.mjs.map +1 -1
- package/dist/utils.d.mts +0 -6
- package/dist/utils.mjs +2 -90
- package/dist/utils.mjs.map +1 -1
- package/package.json +2 -1
- package/dist/chunk-EWPP7VNZ.mjs.map +0 -1
package/dist/aiem.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Abi, Chain, GetContractReturnType, PublicClient, HttpTransport, WalletClient
|
|
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
|
|
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
|
|
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
|
|
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
|
|
116
|
+
export { AIem, Cache, Fields, type Item, type QueryReturnType };
|
package/dist/aiem.mjs
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import
|
|
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
|
-
|
|
854
|
-
|
|
855
|
-
|
|
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(
|
|
945
|
-
|
|
946
|
-
|
|
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
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
};
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
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 = () =>
|
|
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
|
-
|
|
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
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
}
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
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
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
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:
|