您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

tokenData.ts 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import {
  2. AbiCoder,
  3. BigNumberish,
  4. BytesLike,
  5. hexlify,
  6. toBeHex,
  7. keccak256,
  8. toUtf8Bytes,
  9. zeroPadValue,
  10. id,
  11. } from 'ethers';
  12. export interface ERC20Details {
  13. name: string;
  14. symbol: string;
  15. decimals: BigNumberish;
  16. maxSupply: BigNumberish;
  17. }
  18. export interface EncodedPartition {
  19. name: BytesLike;
  20. data: BytesLike;
  21. }
  22. export class Document {
  23. name: string;
  24. nameHash: BytesLike;
  25. uri: string;
  26. documentHash: BytesLike;
  27. timestamp: BigNumberish;
  28. constructor(
  29. name: string,
  30. uri: string,
  31. hash?: string,
  32. timestamp?: BigNumberish,
  33. ) {
  34. this.name = name;
  35. this.nameHash = keccak256(toUtf8Bytes(name));
  36. this.uri = uri;
  37. this.documentHash = hash ?? keccak256(this.nameHash);
  38. this.timestamp = timestamp ?? 0n;
  39. }
  40. }
  41. export class Variable {
  42. name: string;
  43. kind: string;
  44. key: BytesLike;
  45. value: BigNumberish;
  46. encodedValue: BytesLike;
  47. constructor(name: string, kind: string, value: BigNumberish) {
  48. this.name = name;
  49. this.kind = kind;
  50. this.value = value;
  51. this.key = toBytes32(name);
  52. this.encodedValue = Variable.encodeValue(value);
  53. }
  54. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  55. static encodeValue(value: any): BytesLike {
  56. return zeroPadValue(toBeHex(value.toString()), 32);
  57. }
  58. get() {
  59. return {
  60. key: this.key,
  61. kind: this.kind,
  62. };
  63. }
  64. encode(): BytesLike {
  65. return AbiCoder.defaultAbiCoder().encode(
  66. ['bytes32[]', 'bytes32[]'],
  67. [[this.key], [this.encodedValue]],
  68. );
  69. }
  70. }
  71. export class Partition {
  72. name: string;
  73. key: BytesLike;
  74. private variables: Map<BytesLike, Variable>;
  75. constructor(name: string, variables: Array<Variable>) {
  76. this.name = name;
  77. this.key = toBytes32(name);
  78. this.variables = new Map<BytesLike, Variable>();
  79. variables.forEach(this.addVariable.bind(this));
  80. }
  81. getVariableValue(name: string): BigNumberish {
  82. const variable = this.getVariable(name);
  83. return variable.value;
  84. }
  85. getVariable(name: string): Variable {
  86. const key = toBytes32(name);
  87. if (!this.variables.has(key)) {
  88. throw Error('invalid variable');
  89. }
  90. return this.variables.get(key);
  91. }
  92. getVariables(): Array<Variable> {
  93. const variables = Array<Variable>();
  94. this.variables.forEach(variable => {
  95. variables.push(variable);
  96. });
  97. return variables;
  98. }
  99. addVariable(variable: Variable) {
  100. this.variables.set(variable.key, variable);
  101. }
  102. setVariable(variable: Variable) {
  103. if (this.variables.has(variable.key)) {
  104. throw Error('variable already exist');
  105. }
  106. this.variables.set(variable.key, variable);
  107. }
  108. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  109. setVariableValue(name: string, value: any) {
  110. const variable = this.getVariable(name);
  111. variable.value = value;
  112. }
  113. encode(): BytesLike {
  114. return AbiCoder.defaultAbiCoder().encode(
  115. ['bytes32[]', 'bytes32[]'],
  116. [this.getKeys(), this.getEncodedValues()],
  117. );
  118. }
  119. toPartitionStruct() {
  120. return {
  121. name: this.key,
  122. data: this.encode(),
  123. };
  124. }
  125. private getEncodedValues(): Array<BytesLike> {
  126. const values = Array<BytesLike>();
  127. this.variables.forEach(variable => {
  128. values.push(variable.encodedValue);
  129. });
  130. return values;
  131. }
  132. private getKeys(): Array<BytesLike> {
  133. const keys = Array<BytesLike>();
  134. this.variables.forEach((variable, key) => {
  135. keys.push(key);
  136. });
  137. return keys;
  138. }
  139. }
  140. /**
  141. * Converts string to solidity byte32 representation
  142. */
  143. export function toBytes32(data: string): BytesLike {
  144. return hexlify(zeroPadValue(toUtf8Bytes(data), 32));
  145. }
  146. /**
  147. * @returns random(not that random though) keccak256 hash
  148. */
  149. export function randomID() {
  150. const idx = Math.floor(Math.random() * 1_000_000);
  151. return id(`random:${idx}`);
  152. }