@alephium/web3 0.8.2-test.3 → 0.9.0-rc.0

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 (78) hide show
  1. package/LICENSE +165 -0
  2. package/package.json +37 -36
  3. package/src/api/api-alephium.ts +2 -1
  4. package/src/api/node-provider.ts +41 -25
  5. package/src/api/types.ts +10 -15
  6. package/src/contract/contract.ts +63 -40
  7. package/src/index.ts +1 -0
  8. package/{dist/scripts/header.js → src/token/index.ts} +2 -1
  9. package/{dist/src/utils/djb2.js → src/token/nft.ts} +16 -9
  10. package/std/fungible_token_interface.ral +1 -0
  11. package/std/nft_collection_interface.ral +24 -3
  12. package/std/nft_interface.ral +22 -0
  13. package/dist/alephium-web3.min.js +0 -3
  14. package/dist/alephium-web3.min.js.LICENSE.txt +0 -12
  15. package/dist/alephium-web3.min.js.map +0 -1
  16. package/dist/scripts/check-versions.d.ts +0 -1
  17. package/dist/scripts/check-versions.js +0 -39
  18. package/dist/scripts/header.d.ts +0 -0
  19. package/dist/src/api/api-alephium.d.ts +0 -1716
  20. package/dist/src/api/api-alephium.js +0 -1284
  21. package/dist/src/api/api-explorer.d.ts +0 -923
  22. package/dist/src/api/api-explorer.js +0 -711
  23. package/dist/src/api/explorer-provider.d.ts +0 -20
  24. package/dist/src/api/explorer-provider.js +0 -69
  25. package/dist/src/api/index.d.ts +0 -6
  26. package/dist/src/api/index.js +0 -52
  27. package/dist/src/api/node-provider.d.ts +0 -23
  28. package/dist/src/api/node-provider.js +0 -93
  29. package/dist/src/api/types.d.ts +0 -38
  30. package/dist/src/api/types.js +0 -263
  31. package/dist/src/api/utils.d.ts +0 -12
  32. package/dist/src/api/utils.js +0 -59
  33. package/dist/src/constants.d.ts +0 -6
  34. package/dist/src/constants.js +0 -26
  35. package/dist/src/contract/contract.d.ts +0 -293
  36. package/dist/src/contract/contract.js +0 -1079
  37. package/dist/src/contract/events.d.ts +0 -11
  38. package/dist/src/contract/events.js +0 -65
  39. package/dist/src/contract/index.d.ts +0 -3
  40. package/dist/src/contract/index.js +0 -36
  41. package/dist/src/contract/ralph.d.ts +0 -13
  42. package/dist/src/contract/ralph.js +0 -397
  43. package/dist/src/global.d.ts +0 -7
  44. package/dist/src/global.js +0 -54
  45. package/dist/src/index.d.ts +0 -8
  46. package/dist/src/index.js +0 -57
  47. package/dist/src/signer/index.d.ts +0 -3
  48. package/dist/src/signer/index.js +0 -36
  49. package/dist/src/signer/signer.d.ts +0 -60
  50. package/dist/src/signer/signer.js +0 -236
  51. package/dist/src/signer/tx-builder.d.ts +0 -12
  52. package/dist/src/signer/tx-builder.js +0 -96
  53. package/dist/src/signer/types.d.ts +0 -115
  54. package/dist/src/signer/types.js +0 -30
  55. package/dist/src/transaction/index.d.ts +0 -2
  56. package/dist/src/transaction/index.js +0 -35
  57. package/dist/src/transaction/sign-verify.d.ts +0 -3
  58. package/dist/src/transaction/sign-verify.js +0 -52
  59. package/dist/src/transaction/status.d.ts +0 -11
  60. package/dist/src/transaction/status.js +0 -49
  61. package/dist/src/utils/address.d.ts +0 -1
  62. package/dist/src/utils/address.js +0 -42
  63. package/dist/src/utils/bs58.d.ts +0 -5
  64. package/dist/src/utils/bs58.js +0 -40
  65. package/dist/src/utils/djb2.d.ts +0 -1
  66. package/dist/src/utils/index.d.ts +0 -7
  67. package/dist/src/utils/index.js +0 -40
  68. package/dist/src/utils/number.d.ts +0 -18
  69. package/dist/src/utils/number.fixture.d.ts +0 -12
  70. package/dist/src/utils/number.fixture.js +0 -189
  71. package/dist/src/utils/number.js +0 -149
  72. package/dist/src/utils/sign.d.ts +0 -3
  73. package/dist/src/utils/sign.js +0 -89
  74. package/dist/src/utils/subscription.d.ts +0 -21
  75. package/dist/src/utils/subscription.js +0 -51
  76. package/dist/src/utils/utils.d.ts +0 -40
  77. package/dist/src/utils/utils.js +0 -244
  78. package/jest-config.json +0 -11
@@ -68,6 +68,8 @@ export type Fields = NamedVals
68
68
  export type Arguments = NamedVals
69
69
  export type HexString = string
70
70
 
71
+ export const StdIdFieldName = '__stdInterfaceId'
72
+
71
73
  enum SourceKind {
72
74
  Contract = 0,
73
75
  Script = 1,
@@ -667,6 +669,7 @@ export class Contract extends Artifact {
667
669
  readonly codeHash: string
668
670
  readonly fieldsSig: FieldsSig
669
671
  readonly eventsSig: EventSig[]
672
+ readonly stdInterfaceId?: HexString
670
673
 
671
674
  readonly bytecodeDebug: string
672
675
  readonly codeHashDebug: string
@@ -680,7 +683,8 @@ export class Contract extends Artifact {
680
683
  codeHashDebug: string,
681
684
  fieldsSig: FieldsSig,
682
685
  eventsSig: EventSig[],
683
- functions: FunctionSig[]
686
+ functions: FunctionSig[],
687
+ stdInterfaceId?: HexString
684
688
  ) {
685
689
  super(version, name, functions)
686
690
  this.bytecode = bytecode
@@ -688,6 +692,7 @@ export class Contract extends Artifact {
688
692
  this.codeHash = codeHash
689
693
  this.fieldsSig = fieldsSig
690
694
  this.eventsSig = eventsSig
695
+ this.stdInterfaceId = stdInterfaceId
691
696
 
692
697
  this.bytecodeDebug = ralph.buildDebugBytecode(this.bytecode, this.bytecodeDebugPatch)
693
698
  this.codeHashDebug = codeHashDebug
@@ -715,7 +720,8 @@ export class Contract extends Artifact {
715
720
  codeHashDebug ? codeHashDebug : artifact.codeHash,
716
721
  artifact.fieldsSig,
717
722
  artifact.eventsSig,
718
- artifact.functions
723
+ artifact.functions,
724
+ artifact.stdInterfaceId === null ? undefined : artifact.stdInterfaceId
719
725
  )
720
726
  return contract
721
727
  }
@@ -730,7 +736,8 @@ export class Contract extends Artifact {
730
736
  result.codeHashDebug,
731
737
  result.fields,
732
738
  result.events,
733
- result.functions
739
+ result.functions,
740
+ result.stdInterfaceId
734
741
  )
735
742
  }
736
743
 
@@ -742,7 +749,7 @@ export class Contract extends Artifact {
742
749
  }
743
750
 
744
751
  override toString(): string {
745
- const object = {
752
+ const object: any = {
746
753
  version: this.version,
747
754
  name: this.name,
748
755
  bytecode: this.bytecode,
@@ -751,6 +758,9 @@ export class Contract extends Artifact {
751
758
  eventsSig: this.eventsSig,
752
759
  functions: this.functions
753
760
  }
761
+ if (this.stdInterfaceId !== undefined) {
762
+ object.stdInterfaceId = this.stdInterfaceId
763
+ }
754
764
  return JSON.stringify(object, null, 2)
755
765
  }
756
766
 
@@ -852,16 +862,14 @@ export class Contract extends Artifact {
852
862
  }
853
863
 
854
864
  static ContractCreatedEventIndex = -1
855
- static ContractCreatedEvent: SystemEventSig = {
865
+ static ContractCreatedEvent: EventSig = {
856
866
  name: 'ContractCreated',
857
- fieldNames: ['address'],
858
- fieldTypes: ['Address'],
859
- optionalFieldNames: ['parentAddress'],
860
- optionalFieldTypes: ['Address']
867
+ fieldNames: ['address', 'parentAddress', 'stdInterfaceId'],
868
+ fieldTypes: ['Address', 'Address', 'ByteVec']
861
869
  }
862
870
 
863
871
  static ContractDestroyedEventIndex = -2
864
- static ContractDestroyedEvent: SystemEventSig = {
872
+ static ContractDestroyedEvent: EventSig = {
865
873
  name: 'ContractDestroyed',
866
874
  fieldNames: ['address'],
867
875
  fieldTypes: ['Address']
@@ -872,10 +880,10 @@ export class Contract extends Artifact {
872
880
  let name: string
873
881
 
874
882
  if (event.eventIndex == Contract.ContractCreatedEventIndex) {
875
- fields = fromApiSystemEventFields(event.fields, Contract.ContractCreatedEvent)
883
+ fields = toContractCreatedEventFields(fromApiEventFields(event.fields, Contract.ContractCreatedEvent, true))
876
884
  name = Contract.ContractCreatedEvent.name
877
885
  } else if (event.eventIndex == Contract.ContractDestroyedEventIndex) {
878
- fields = fromApiSystemEventFields(event.fields, Contract.ContractDestroyedEvent)
886
+ fields = fromApiEventFields(event.fields, Contract.ContractDestroyedEvent, true)
879
887
  name = Contract.ContractDestroyedEvent.name
880
888
  } else {
881
889
  const contract = Project.currentProject.contractByCodeHash(codeHash!)
@@ -923,7 +931,8 @@ export class Contract extends Artifact {
923
931
  signer: SignerProvider,
924
932
  params: DeployContractParams<P>
925
933
  ): Promise<SignDeployContractTxParams> {
926
- const bytecode = this.buildByteCodeToDeploy(params.initialFields ?? {})
934
+ const initialFields: Fields = params.initialFields ?? {}
935
+ const bytecode = this.buildByteCodeToDeploy(addStdIdToFields(this, initialFields))
927
936
  const selectedAccount = await signer.getSelectedAccount()
928
937
  const signerParams: SignDeployContractTxParams = {
929
938
  signerAddress: selectedAccount.address,
@@ -1106,18 +1115,8 @@ function fromApiFields(immFields: node.Val[], mutFields: node.Val[], fieldsSig:
1106
1115
  return fromApiVals(vals, fieldsSig.names, fieldsSig.types)
1107
1116
  }
1108
1117
 
1109
- function fromApiEventFields(vals: node.Val[], eventSig: node.EventSig): Fields {
1110
- return fromApiVals(vals, eventSig.fieldNames, eventSig.fieldTypes)
1111
- }
1112
-
1113
- function fromApiSystemEventFields(vals: node.Val[], systemEventSig: SystemEventSig): Fields {
1114
- return fromApiVals(
1115
- vals,
1116
- systemEventSig.fieldNames,
1117
- systemEventSig.fieldTypes,
1118
- systemEventSig.optionalFieldNames ?? [],
1119
- systemEventSig.optionalFieldTypes ?? []
1120
- )
1118
+ function fromApiEventFields(vals: node.Val[], eventSig: node.EventSig, systemEvent = false): Fields {
1119
+ return fromApiVals(vals, eventSig.fieldNames, eventSig.fieldTypes, systemEvent)
1121
1120
  }
1122
1121
 
1123
1122
  export interface Asset {
@@ -1313,7 +1312,10 @@ export abstract class ContractFactory<I, F extends Fields = Fields> {
1313
1312
  abstract at(address: string): I
1314
1313
 
1315
1314
  async deploy(signer: SignerProvider, deployParams: DeployContractParams<F>): Promise<DeployContractResult<I>> {
1316
- const signerParams = await this.contract.txParamsForDeployment(signer, deployParams)
1315
+ const signerParams = await this.contract.txParamsForDeployment(signer, {
1316
+ ...deployParams,
1317
+ initialFields: addStdIdToFields(this.contract, deployParams.initialFields)
1318
+ })
1317
1319
  const result = await signer.signAndSubmitDeployContractTx(signerParams)
1318
1320
  return {
1319
1321
  ...result,
@@ -1327,7 +1329,7 @@ export abstract class ContractFactory<I, F extends Fields = Fields> {
1327
1329
  alphAmount: asset?.alphAmount ?? ONE_ALPH,
1328
1330
  tokens: asset?.tokens
1329
1331
  }
1330
- return this.contract.toState(initFields, newAsset, address)
1332
+ return this.contract.toState(addStdIdToFields(this.contract, initFields), newAsset, address)
1331
1333
  }
1332
1334
  }
1333
1335
 
@@ -1374,19 +1376,32 @@ function specialContractAddress(n: number): string {
1374
1376
  export const CreateContractEventAddress = specialContractAddress(-1)
1375
1377
  export const DestroyContractEventAddress = specialContractAddress(-2)
1376
1378
 
1377
- export interface SystemEventSig extends EventSig {
1378
- optionalFieldNames?: string[]
1379
- optionalFieldTypes?: string[]
1379
+ export type ContractCreatedEventFields = {
1380
+ address: Address
1381
+ parentAddress?: Address
1382
+ stdInterfaceIdGuessed?: HexString
1380
1383
  }
1384
+ export type ContractDestroyedEventFields = {
1385
+ address: Address
1386
+ }
1387
+ export type ContractCreatedEvent = ContractEvent<ContractCreatedEventFields>
1388
+ export type ContractDestroyedEvent = ContractEvent<ContractDestroyedEventFields>
1381
1389
 
1382
- export type ContractCreatedEvent = ContractEvent<{ address: Address; parentAddress?: Address }>
1383
- export type ContractDestroyedEvent = ContractEvent<{ address: Address }>
1384
-
1385
- function decodeSystemEvent(event: node.ContractEvent, systemEventSig: SystemEventSig, eventIndex: number): Fields {
1390
+ function decodeSystemEvent(event: node.ContractEvent, eventSig: EventSig, eventIndex: number): Fields {
1386
1391
  if (event.eventIndex !== eventIndex) {
1387
1392
  throw new Error(`Invalid event index: ${event.eventIndex}, expected: ${eventIndex}`)
1388
1393
  }
1389
- return fromApiSystemEventFields(event.fields, systemEventSig)
1394
+ return fromApiEventFields(event.fields, eventSig, true)
1395
+ }
1396
+
1397
+ function toContractCreatedEventFields(fields: Fields): ContractCreatedEventFields {
1398
+ const parentAddress = fields['parentAddress'] as string
1399
+ const stdInterfaceId = fields['stdInterfaceId'] as string
1400
+ return {
1401
+ address: fields['address'] as Address,
1402
+ parentAddress: parentAddress === '' ? undefined : (parentAddress as Address),
1403
+ stdInterfaceIdGuessed: stdInterfaceId === '' ? undefined : (stdInterfaceId as HexString)
1404
+ }
1390
1405
  }
1391
1406
 
1392
1407
  export function decodeContractCreatedEvent(event: node.ContractEvent): Omit<ContractCreatedEvent, 'contractAddress'> {
@@ -1396,10 +1411,7 @@ export function decodeContractCreatedEvent(event: node.ContractEvent): Omit<Cont
1396
1411
  txId: event.txId,
1397
1412
  eventIndex: event.eventIndex,
1398
1413
  name: Contract.ContractCreatedEvent.name,
1399
- fields: {
1400
- address: fields['address'] as Address,
1401
- parentAddress: fields['parentAddress'] === undefined ? undefined : (fields['parentAddress'] as Address)
1402
- }
1414
+ fields: toContractCreatedEventFields(fields)
1403
1415
  }
1404
1416
  }
1405
1417
 
@@ -1441,16 +1453,27 @@ export function subscribeEventsFromContract<T extends Fields, M extends Contract
1441
1453
  return subscribeToEvents(opt, address, fromCount)
1442
1454
  }
1443
1455
 
1456
+ export function addStdIdToFields<F extends Fields>(
1457
+ contract: Contract,
1458
+ fields: F
1459
+ ): F | (F & { __stdInterfaceId: HexString }) {
1460
+ const stdInterfaceIdPrefix = '414c5048' // the hex of 'ALPH'
1461
+ return contract.stdInterfaceId === undefined
1462
+ ? fields
1463
+ : { ...fields, __stdInterfaceId: stdInterfaceIdPrefix + contract.stdInterfaceId }
1464
+ }
1465
+
1444
1466
  export async function testMethod<I, F extends Fields, A extends Arguments, R>(
1445
1467
  contract: ContractFactory<I, F>,
1446
1468
  methodName: string,
1447
1469
  params: Optional<TestContractParams<F, A>, 'testArgs' | 'initialFields'>
1448
1470
  ): Promise<TestContractResult<R>> {
1449
1471
  const txId = params?.txId ?? randomTxId()
1472
+ const initialFields = params.initialFields === undefined ? {} : params.initialFields
1450
1473
  const apiParams = contract.contract.toApiTestContractParams(methodName, {
1451
1474
  ...params,
1452
1475
  txId: txId,
1453
- initialFields: params.initialFields === undefined ? {} : params.initialFields,
1476
+ initialFields: addStdIdToFields(contract.contract, initialFields),
1454
1477
  testArgs: params.testArgs === undefined ? {} : params.testArgs
1455
1478
  })
1456
1479
  const apiResult = await getCurrentNodeProvider().contracts.postContractsTestContract(apiParams)
package/src/index.ts CHANGED
@@ -25,6 +25,7 @@ export * from './contract'
25
25
  export * from './signer'
26
26
  export * from './utils'
27
27
  export * from './transaction'
28
+ export * from './token'
28
29
 
29
30
  export * from './constants'
30
31
  export * as web3 from './global'
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /*
3
2
  Copyright 2018 - 2022 The Alephium Authors
4
3
  This file is part of the alephium project.
@@ -16,3 +15,5 @@ GNU Lesser General Public License for more details.
16
15
  You should have received a copy of the GNU Lesser General Public License
17
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
18
17
  */
18
+
19
+ export * from './nft'
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /*
3
2
  Copyright 2018 - 2022 The Alephium Authors
4
3
  This file is part of the alephium project.
@@ -16,12 +15,20 @@ GNU Lesser General Public License for more details.
16
15
  You should have received a copy of the GNU Lesser General Public License
17
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
18
17
  */
19
- Object.defineProperty(exports, "__esModule", { value: true });
20
- function djb2(bytes) {
21
- let hash = 5381;
22
- for (let i = 0; i < bytes.length; i++) {
23
- hash = (hash << 5) + hash + (bytes[`${i}`] & 0xff);
24
- }
25
- return hash;
18
+
19
+ // JSON Schema for the NFT metadata, which is pointed to by the value
20
+ // returned from the `getTokenUri` method of the NFT contract
21
+ export interface NFTMetadata {
22
+ name: string
23
+ description: string
24
+ image: string
25
+ }
26
+
27
+ // JSON Schema for the NFT Collection metadata, which is pointed to by
28
+ // the value returned from the `getCollectionUri` method of the NFT Collection
29
+ // Contract
30
+ export interface NFTCollectionMetadata {
31
+ name: string
32
+ description: string
33
+ image: string
26
34
  }
27
- exports.default = djb2;
@@ -1,3 +1,4 @@
1
+ @std(id = #0001)
1
2
  Interface IFungibleToken {
2
3
  pub fn getSymbol() -> ByteVec
3
4
 
@@ -1,8 +1,29 @@
1
+ @std(id = #0002)
1
2
  Interface INFTCollection {
2
- event Minted(minter: Address, tokenIndex: U256, tokenId: ByteVec)
3
+ // Collection Uri points to a json file containing metadata for the NFT collection.
4
+ //
5
+ // The schema of the json file is:
6
+ // {
7
+ // "title": "NFT Collection Metadata",
8
+ // "type": "object",
9
+ // "properties": {
10
+ // "name": {
11
+ // "type": "string",
12
+ // "description": "Name of the NFT collection"
13
+ // },
14
+ // "description": {
15
+ // "type": "string",
16
+ // "description": "General description of the NFT collection"
17
+ // },
18
+ // "image": {
19
+ // "type": "string",
20
+ // "description": "A URI to the image that represents the NFT collection"
21
+ // }
22
+ // }
23
+ // }
24
+ pub fn getCollectionUri() -> ByteVec
3
25
 
4
- pub fn getName() -> ByteVec
5
- pub fn getSymbol() -> ByteVec
6
26
  pub fn totalSupply() -> U256
27
+
7
28
  pub fn nftByIndex(index: U256) -> INFT
8
29
  }
@@ -1,3 +1,25 @@
1
+ @std(id = #0003)
1
2
  Interface INFT {
3
+ // Token Uri points to a json file containing metadata for the NFT.
4
+ //
5
+ // The schema of the json file is:
6
+ // {
7
+ // "title": "NFT Metadata",
8
+ // "type": "object",
9
+ // "properties": {
10
+ // "name": {
11
+ // "type": "string",
12
+ // "description": "Name of the NFT"
13
+ // },
14
+ // "description": {
15
+ // "type": "string",
16
+ // "description": "General description of the NFT"
17
+ // },
18
+ // "image": {
19
+ // "type": "string",
20
+ // "description": "A URI to the image that represents the NFT"
21
+ // }
22
+ // }
23
+ // }
2
24
  pub fn getTokenUri() -> ByteVec
3
25
  }