git.delta.rocks / unique-network / refs/commits / c9f3344fa8b9

difftreelog

source

tests/src/eth/util/playgrounds/unique.dev.ts5.6 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034/* eslint-disable function-call-argument-newline */56import Web3 from 'web3';7import {WebsocketProvider} from 'web3-core';8import {Contract} from 'web3-eth-contract';910import {evmToAddress} from '@polkadot/util-crypto';11import {IKeyringPair} from '@polkadot/types/types';1213import {DevUniqueHelper} from '../../../util/playgrounds/unique.dev';1415// Native contracts ABI16import collectionHelpersAbi from '../../collectionHelpersAbi.json';17import fungibleAbi from '../../fungibleAbi.json';18import nonFungibleAbi from '../../nonFungibleAbi.json';19import refungibleAbi from '../../reFungibleAbi.json';20import refungibleTokenAbi from '../../reFungibleTokenAbi.json';21import contractHelpersAbi from './../contractHelpersAbi.json';2223class EthGroupBase {24  helper: EthUniqueHelper;2526  constructor(helper: EthUniqueHelper) {27    this.helper = helper;28  }29}30  31  32class NativeContractGroup extends EthGroupBase {33  DEFAULT_GAS = 2_500_000;3435  contractHelpers(caller: string): Contract {36    const web3 = this.helper.getWeb3();37    return new web3.eth.Contract(contractHelpersAbi as any, '0x842899ECF380553E8a4de75bF534cdf6fBF64049', {from: caller, gas: this.DEFAULT_GAS});38  }3940  collectionHelpers(caller: string) {41    const web3 = this.helper.getWeb3();42    return new web3.eth.Contract(collectionHelpersAbi as any, '0x6c4e9fe1ae37a41e93cee429e8e1881abdcbb54f', {from: caller, gas: this.DEFAULT_GAS});43  }4445  collection(address: string, mode: 'nft' | 'rft' | 'ft', caller?: string): Contract {46    const abi = {47      'nft': nonFungibleAbi,48      'rft': refungibleAbi,49      'ft': fungibleAbi50    }[mode];51    const web3 = this.helper.getWeb3();52    return new web3.eth.Contract(abi as any, address, {gas: this.DEFAULT_GAS, ...(caller ? {from: caller} : {})});53  }5455  rftTokenByAddress(address: string, caller?: string): Contract {56    const web3 = this.helper.getWeb3();57    return new web3.eth.Contract(refungibleTokenAbi as any, address, {gas: this.DEFAULT_GAS, ...(caller ? {from: caller} : {})});58  }5960  rftToken(collectionId: number, tokenId: number, caller?: string): Contract {61    return this.rftTokenByAddress(this.helper.ethAddress.fromTokenId(collectionId, tokenId), caller);62  }63}6465  66class EthGroup extends EthGroupBase {67  createAccount() {68    const web3 = this.helper.getWeb3();69    const account = web3.eth.accounts.create();70    web3.eth.accounts.wallet.add(account.privateKey);71    return account.address;72  }7374  async createAccountWithBalance(donor: IKeyringPair, amount=1000n) {75    const account = this.createAccount();76    await this.transferBalanceFromSubstrate(donor, account, amount);77  78    return account;79  }8081  async transferBalanceFromSubstrate(donor: IKeyringPair, recepient: string, amount=1000n) {82    return await this.helper.balance.transferToSubstrate(donor, evmToAddress(recepient), amount * this.helper.balance.getOneTokenNominal());83  }8485  async createNonfungibleCollection(signer: string, name: string, description: string, tokenPrefix: string): Promise<{collectionId: number, collectionAddress: string}> {86    const collectionHelper = this.helper.ethNativeContract.collectionHelpers(signer);87        88    const result = await collectionHelper.methods.createNonfungibleCollection(name, description, tokenPrefix).send();8990    const collectionAddress = this.helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);91    const collectionId = this.helper.ethAddress.extractCollectionId(collectionAddress);9293    return {collectionId, collectionAddress};94  }95}96  97  98class EthAddressGroup extends EthGroupBase {99  extractCollectionId(address: string): number {100    if (!(address.length === 42 || address.length === 40)) throw new Error('address wrong format');101    return parseInt(address.substr(address.length - 8), 16);102  }103104  fromCollectionId(collectionId: number): string {105    if (collectionId >= 0xffffffff || collectionId < 0) throw new Error('collectionId overflow');106    return Web3.utils.toChecksumAddress(`0x17c4e6453cc49aaaaeaca894e6d9683e${collectionId.toString(16).padStart(8, '0')}`);107  }108109  extractTokenId(address: string): {collectionId: number, tokenId: number} {110    if (!address.startsWith('0x'))111      throw 'address not starts with "0x"';112    if (address.length > 42)113      throw 'address length is more than 20 bytes';114    return {115      collectionId: Number('0x' + address.substring(address.length - 16, address.length - 8)),116      tokenId: Number('0x' + address.substring(address.length - 8)),117    };118  }119120  fromTokenId(collectionId: number, tokenId: number): string  {121    return this.helper.util.getNestingTokenAddress(collectionId, tokenId);122  }123124  normalizeAddress(address: string): string {125    return '0x' + address.substring(address.length - 40);126  }127}  128 129130export class EthUniqueHelper extends DevUniqueHelper {131  web3: Web3 | null = null;132  web3Provider: WebsocketProvider | null = null;133134  eth: EthGroup;135  ethAddress: EthAddressGroup;136  ethNativeContract: NativeContractGroup;137138  constructor(logger: { log: (msg: any, level: any) => void, level: any }) {139    super(logger);140    this.eth = new EthGroup(this);141    this.ethAddress = new EthAddressGroup(this);142    this.ethNativeContract = new NativeContractGroup(this);143  }144145  getWeb3(): Web3 {146    if(this.web3 === null) throw Error('Web3 not connected');147    return this.web3;148  }149150  async connectWeb3(wsEndpoint: string) {151    if(this.web3 !== null) return;152    this.web3Provider = new Web3.providers.WebsocketProvider(wsEndpoint);153    this.web3 = new Web3(this.web3Provider);154  }155156  async disconnectWeb3() {157    if(this.web3 === null) return;158    this.web3Provider?.connection.close();159    this.web3 = null;160  }161}162