@ensdomains/ensjs 3.0.0-alpha.6 → 3.0.0-alpha.9

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.
Files changed (41) hide show
  1. package/dist/cjs/GqlManager.d.ts +3 -0
  2. package/dist/cjs/GqlManager.js +62 -2
  3. package/dist/cjs/functions/getProfile.d.ts +4 -1
  4. package/dist/cjs/functions/getProfile.js +62 -18
  5. package/dist/cjs/functions/getRecords.d.ts +1 -0
  6. package/dist/cjs/functions/setRecord.d.ts +18 -0
  7. package/dist/cjs/functions/setRecord.js +27 -0
  8. package/dist/cjs/index.d.ts +7 -2
  9. package/dist/cjs/index.js +6 -1
  10. package/dist/cjs/utils/labels.js +4 -3
  11. package/dist/cjs/utils/normalise.d.ts +1 -1
  12. package/dist/cjs/utils/normalise.js +10 -3
  13. package/dist/cjs/utils/recordHelpers.d.ts +3 -0
  14. package/dist/cjs/utils/recordHelpers.js +42 -15
  15. package/dist/esm/GqlManager.d.ts +3 -0
  16. package/dist/esm/GqlManager.js +58 -2
  17. package/dist/esm/functions/getProfile.d.ts +4 -1
  18. package/dist/esm/functions/getProfile.js +62 -18
  19. package/dist/esm/functions/getRecords.d.ts +1 -0
  20. package/dist/esm/functions/setRecord.d.ts +18 -0
  21. package/dist/esm/functions/setRecord.js +24 -0
  22. package/dist/esm/index.d.ts +7 -2
  23. package/dist/esm/index.js +6 -1
  24. package/dist/esm/utils/labels.js +4 -3
  25. package/dist/esm/utils/normalise.d.ts +1 -1
  26. package/dist/esm/utils/normalise.js +10 -3
  27. package/dist/esm/utils/recordHelpers.d.ts +3 -0
  28. package/dist/esm/utils/recordHelpers.js +40 -14
  29. package/package.json +7 -5
  30. package/src/GqlManager.test.ts +170 -0
  31. package/src/GqlManager.ts +67 -2
  32. package/src/functions/getProfile.test.ts +16 -1
  33. package/src/functions/getProfile.ts +82 -19
  34. package/src/functions/getRecords.ts +1 -0
  35. package/src/functions/setRecord.test.ts +100 -0
  36. package/src/functions/setRecord.ts +63 -0
  37. package/src/functions/setRecords.test.ts +2 -2
  38. package/src/index.ts +8 -1
  39. package/src/utils/labels.ts +5 -3
  40. package/src/utils/normalise.ts +10 -4
  41. package/src/utils/recordHelpers.ts +52 -18
@@ -33,6 +33,47 @@ export const generateSetAddr = (
33
33
  )
34
34
  }
35
35
 
36
+ export type RecordTypes = 'contentHash' | 'text' | 'addr'
37
+
38
+ export type RecordInput<T extends RecordTypes> = T extends 'contentHash'
39
+ ? string
40
+ : RecordItem
41
+
42
+ export function generateSingleRecordCall<T extends RecordTypes>(
43
+ namehash: string,
44
+ resolver: PublicResolver,
45
+ type: T,
46
+ ): (record: RecordInput<T>) => string {
47
+ if (type === 'contentHash') {
48
+ return (_r: RecordInput<T>) => {
49
+ const record = _r as string
50
+ let _contentHash = ''
51
+ if (record !== _contentHash) {
52
+ const encoded = encodeContenthash(record)
53
+ if (encoded.error) throw new Error(encoded.error)
54
+ _contentHash = encoded.encoded as string
55
+ }
56
+ return resolver.interface.encodeFunctionData('setContenthash', [
57
+ namehash,
58
+ _contentHash,
59
+ ])
60
+ }
61
+ } else {
62
+ return (_r: RecordInput<T>) => {
63
+ const record = _r as RecordItem
64
+ if (type === 'text') {
65
+ return resolver.interface.encodeFunctionData('setText', [
66
+ namehash,
67
+ record.key,
68
+ record.value,
69
+ ])
70
+ } else {
71
+ return generateSetAddr(namehash, record.key, record.value, resolver)
72
+ }
73
+ }
74
+ }
75
+ }
76
+
36
77
  export const generateRecordCallArray = (
37
78
  namehash: string,
38
79
  records: RecordOptions,
@@ -41,31 +82,24 @@ export const generateRecordCallArray = (
41
82
  const calls: string[] = []
42
83
 
43
84
  if (records.contentHash) {
44
- const contentHash =
45
- records.contentHash === '' ? '' : encodeContenthash(records.contentHash)
46
- const data = (resolver?.interface.encodeFunctionData as any)(
47
- 'setContenthash',
48
- [namehash, contentHash],
49
- )
85
+ const data = generateSingleRecordCall(
86
+ namehash,
87
+ resolver,
88
+ 'contentHash',
89
+ )(records.contentHash)
50
90
  data && calls.push(data)
51
91
  }
52
92
 
53
93
  if (records.texts && records.texts.length > 0) {
54
- records.texts.forEach(({ key, value }: RecordItem) => {
55
- const data = resolver?.interface.encodeFunctionData('setText', [
56
- namehash,
57
- key,
58
- value,
59
- ])
60
- data && calls.push(data)
61
- })
94
+ records.texts
95
+ .map(generateSingleRecordCall(namehash, resolver, 'text'))
96
+ .forEach((call) => calls.push(call))
62
97
  }
63
98
 
64
99
  if (records.coinTypes && records.coinTypes.length > 0) {
65
- records.coinTypes.forEach(({ key, value }: RecordItem) => {
66
- const data = generateSetAddr(namehash, key, value, resolver!)
67
- data && calls.push(data)
68
- })
100
+ records.coinTypes
101
+ .map(generateSingleRecordCall(namehash, resolver, 'addr'))
102
+ .forEach((call) => calls.push(call))
69
103
  }
70
104
 
71
105
  return calls