Skip to content

Commit

Permalink
Merge pull request #120 from Juneo-io/dev
Browse files Browse the repository at this point in the history
v0.0.98
  • Loading branch information
alekswaslet authored Jun 3, 2024
2 parents cea2070 + 637e328 commit 4f169ed
Show file tree
Hide file tree
Showing 36 changed files with 461 additions and 697 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "juneojs",
"version": "0.0.97",
"version": "0.0.98",
"description": "Juneo JS Library",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/api/data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { now } from '../utils'
import { TimeUtils } from '../utils'
import { type JsonRpcRequest, type JsonRpcResponse, type JuneoClient } from './client'

export class CachedResponse<T> {
Expand Down Expand Up @@ -30,7 +30,7 @@ export class CachedResponse<T> {
if (this.duration === undefined) {
return false
}
const currentTime: bigint = now()
const currentTime: bigint = TimeUtils.now()
const update: boolean = currentTime >= this.lastUpdate + this.duration
if (update) {
this.lastUpdate = currentTime
Expand Down
19 changes: 5 additions & 14 deletions src/chain/chain.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { type TokenAsset } from '../asset'
import { type MCNProvider } from '../juneo'

export enum VMAccountType {
Utxo = 'Utxo',
Nonce = 'Nonce',
}
import { type ChainVM, type MCNProvider } from '../juneo'

export interface Blockchain {
name: string
id: string
vmId: string
accountType: VMAccountType
vm: ChainVM
asset: TokenAsset
assetId: string
aliases: string[]
Expand All @@ -27,8 +21,7 @@ export interface Blockchain {
export abstract class AbstractBlockchain implements Blockchain {
name: string
id: string
vmId: string
accountType: VMAccountType
vm: ChainVM
asset: TokenAsset
assetId: string
aliases: string[]
Expand All @@ -37,16 +30,14 @@ export abstract class AbstractBlockchain implements Blockchain {
constructor (
name: string,
id: string,
vmId: string,
accountType: VMAccountType,
vm: ChainVM,
asset: TokenAsset,
aliases: string[] = [],
registeredAssets: TokenAsset[] = []
) {
this.name = name
this.id = id
this.vmId = vmId
this.accountType = accountType
this.vm = vm
this.asset = asset
this.assetId = asset.assetId
this.aliases = aliases
Expand Down
1 change: 1 addition & 0 deletions src/chain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './jevm'
export * from './jvm'
export * from './platform'
export * from './solidity'
export * from './vm'
15 changes: 9 additions & 6 deletions src/chain/jevm.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { ethers } from 'ethers'
import { ChainError, fetchJNT, isContractAddress } from '../utils'
import { AssetId } from '../transaction'
import { type JEVMAPI } from '../api'
import { ERC20TokenHandler, ContractManager, type SolidityTokenHandler } from './solidity'
import { type TokenAsset, type JRC20Asset, type JEVMGasToken, TokenType, type WrappedAsset } from '../asset'
import { AbstractBlockchain, VMAccountType } from './chain'
import { type JEVMGasToken, type JRC20Asset, type TokenAsset, TokenType, type WrappedAsset } from '../asset'
import { type MCNProvider } from '../juneo'
import { AssetId } from '../transaction'
import { ChainError, fetchJNT, isContractAddress } from '../utils'
import { AbstractBlockchain } from './chain'
import { ContractManager, ERC20TokenHandler, type SolidityTokenHandler } from './solidity'
import { ChainVM, VMWalletType } from './vm'

export const JEVM_ID: string = 'orkbbNQVf27TiBe6GqN5dm8d8Lo3rutEov8DUWZaKNUjckwSk'
export const EVM_ID: string = 'mgj786NP7uDwBCcq6YwThhaN8FLyybkCa4zBWTQbNgmK6k9A6'

const HD_PATH = 60

export const NativeAssetBalanceContract: string = '0x0100000000000000000000000000000000000001'
export const NativeAssetCallContract: string = '0x0100000000000000000000000000000000000002'

Expand Down Expand Up @@ -37,7 +40,7 @@ export class JEVMBlockchain extends AbstractBlockchain {
jrc20Assets: JRC20Asset[] = [],
wrappedAsset?: WrappedAsset | undefined
) {
super(name, id, JEVM_ID, VMAccountType.Nonce, asset, aliases, registeredAssets)
super(name, id, new ChainVM(JEVM_ID, VMWalletType.Nonce, HD_PATH), asset, aliases, registeredAssets)
this.asset = asset
this.chainId = chainId
this.baseFee = baseFee
Expand Down
11 changes: 7 additions & 4 deletions src/chain/jvm.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { fetchJNT, validateBech32 } from '../utils'
import { type TokenAsset, type JNTAsset } from '../asset'
import { AbstractBlockchain, VMAccountType } from './chain'
import { type JNTAsset, type TokenAsset } from '../asset'
import { type MCNProvider } from '../juneo'
import { fetchJNT, validateBech32 } from '../utils'
import { AbstractBlockchain } from './chain'
import { ChainVM, VMWalletType } from './vm'

export const JVM_ID: string = 'otSmSxFRBqdRX7kestRW732n3WS2MrLAoWwHZxHnmMGMuLYX8'

const HD_PATH = 9000

export class JVMBlockchain extends AbstractBlockchain {
constructor (name: string, id: string, asset: JNTAsset, aliases?: string[], registeredAssets: TokenAsset[] = []) {
super(name, id, JVM_ID, VMAccountType.Utxo, asset, aliases, registeredAssets)
super(name, id, new ChainVM(JVM_ID, VMWalletType.Utxo, HD_PATH), asset, aliases, registeredAssets)
}

protected async fetchAsset (provider: MCNProvider, assetId: string): Promise<TokenAsset> {
Expand Down
15 changes: 9 additions & 6 deletions src/chain/platform.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { fetchJNT, now, RewardCalculator, validateBech32 } from '../utils'
import { type TokenAsset, type JNTAsset } from '../asset'
import { AbstractBlockchain, VMAccountType } from './chain'
import { type JNTAsset, type TokenAsset } from '../asset'
import { type MCNProvider } from '../juneo'
import { fetchJNT, RewardCalculator, TimeUtils, validateBech32 } from '../utils'
import { AbstractBlockchain } from './chain'
import { ChainVM, VMWalletType } from './vm'

export const PLATFORMVM_ID: string = '11111111111111111111111111111111LpoYY'

const HD_PATH = 9000

const BaseShare: number = 100_0000 // 100%

export class PlatformBlockchain extends AbstractBlockchain {
Expand All @@ -21,7 +24,7 @@ export class PlatformBlockchain extends AbstractBlockchain {
aliases?: string[],
registeredAssets: TokenAsset[] = []
) {
super(name, id, PLATFORMVM_ID, VMAccountType.Utxo, asset, aliases, registeredAssets)
super(name, id, new ChainVM(PLATFORMVM_ID, VMWalletType.Utxo, HD_PATH), asset, aliases, registeredAssets)
this.stakeConfig = stakeConfig
this.rewardConfig = rewardConfig
this.rewardCalculator = new RewardCalculator(rewardConfig)
Expand All @@ -36,11 +39,11 @@ export class PlatformBlockchain extends AbstractBlockchain {
}

estimatePrimaryValidationReward (stakePeriod: bigint, stakeAmount: bigint): bigint {
return this.rewardCalculator.calculatePrimary(stakePeriod, now(), stakeAmount)
return this.rewardCalculator.calculatePrimary(stakePeriod, TimeUtils.now(), stakeAmount)
}

estimatePrimaryDelegationReward (stakePeriod: bigint, stakeAmount: bigint): bigint {
const rewards: bigint = this.rewardCalculator.calculatePrimary(stakePeriod, now(), stakeAmount)
const rewards: bigint = this.rewardCalculator.calculatePrimary(stakePeriod, TimeUtils.now(), stakeAmount)
return (rewards * BigInt(BaseShare - this.stakeConfig.minDelegationFee)) / BigInt(BaseShare)
}
}
Expand Down
7 changes: 0 additions & 7 deletions src/chain/solidity/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,3 @@ export const WrappedABI: string[] = ERC20ABI.concat([
'event Deposit(address indexed dst, uint wad)',
'event Withdrawal(address indexed src, uint wad)'
])

export const AuctionABI: string[] = ['function redeemAuction(uint256 auctionId)']

export const StreamABI: string[] = [
'function withdrawFromStream(uint256 streamId, uint256 amount) returns (bool)',
'function cancelStream(uint256 streamId) returns (bool)'
]
28 changes: 2 additions & 26 deletions src/chain/solidity/contract.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ethers } from 'ethers'
import * as abi from './abi'
import { AssetId } from '../../transaction'
import { ERC20Asset, type TokenAsset } from '../../asset'
import { AssetId } from '../../transaction'
import * as abi from './abi'

export class ContractManager {
private readonly handlers: SolidityTokenHandler[] = []
Expand Down Expand Up @@ -120,27 +120,3 @@ export class WrappedContractAdapter extends EVMCallAdapter {
return this.getFunctionData('deposit')
}
}

export class AuctionContractAdapter extends EVMCallAdapter {
constructor (contractAddress: string) {
super(contractAddress, abi.AuctionABI)
}

getRedeemAuctionData (auctionId: bigint): string {
return this.getFunctionData('redeemAuction', [auctionId])
}
}

export class StreamContractAdapter extends EVMCallAdapter {
constructor (contractAddress: string) {
super(contractAddress, abi.StreamABI)
}

getWithdrawFromStreamData (streamId: bigint, amount: bigint): string {
return this.getFunctionData('withdrawFromStream', [streamId, amount])
}

getCancelStreamData (streamId: bigint): string {
return this.getFunctionData('cancelStream', [streamId])
}
}
16 changes: 16 additions & 0 deletions src/chain/vm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export enum VMWalletType {
Utxo = 'Utxo',
Nonce = 'Nonce',
}

export class ChainVM {
id: string
walletType: VMWalletType
hdPath: number

constructor (id: string, walletType: VMWalletType, hdPath: number) {
this.id = id
this.walletType = walletType
this.hdPath = hdPath
}
}
2 changes: 1 addition & 1 deletion src/juneo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class MCNProvider {
this.juneApi = new JEVMAPI(client, this.juneChain)
for (const supernet of this.mcn.supernets) {
for (const chain of supernet.chains) {
if (chain.vmId === JEVM_ID) {
if (chain.vm.id === JEVM_ID) {
this.jevmApi[chain.id] = new JEVMAPI(client, chain as JEVMBlockchain)
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/transaction/builder.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { InputError, OutputError } from '../utils'
import { InputError, OutputError, TimeUtils } from '../utils'
import { Secp256k1Input, type Spendable, TransferableInput, type UserInput } from './input'
import { Secp256k1Output, Secp256k1OutputTypeId, type TransactionOutput, UserOutput, type Utxo } from './output'
import * as time from '../utils/time'
import { Address, AssetId } from './types'
import { type TransactionFee } from './transaction'
import { Address, AssetId } from './types'

export function buildTransactionInputs (
userInputs: UserInput[],
Expand Down Expand Up @@ -47,7 +46,7 @@ export function buildTransactionInputs (
}
const output: Secp256k1Output = utxo.output as Secp256k1Output
// output cannot be consumed because it is timelocked
if (output.locktime > time.now()) {
if (output.locktime > TimeUtils.now()) {
continue
}
// The utxo will be added as an input in any case
Expand Down
6 changes: 3 additions & 3 deletions src/transaction/jevm/status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type JEVMAPI } from '../../api'
import { sleep } from '../../utils'
import { TimeUtils } from '../../utils'
import { type TransactionStatusFetcher, TransactionStatusFetchDelay } from '../transaction'

export enum JEVMTransactionStatus {
Expand All @@ -23,7 +23,7 @@ export class JEVMTransactionStatusFetcher implements TransactionStatusFetcher {
async fetch (timeout: number, delay: number = TransactionStatusFetchDelay): Promise<string> {
const maxAttempts: number = timeout / delay
while (this.attempts < maxAttempts && !this.isCurrentStatusSettled()) {
await sleep(delay)
await TimeUtils.sleep(delay)
await this.jevmApi.getTxStatus(this.transactionId).then(
(value) => {
this.currentStatus = value.status
Expand Down Expand Up @@ -68,7 +68,7 @@ export class EVMTransactionStatusFetcher implements TransactionStatusFetcher {
const maxAttempts: number = timeout / delay
this.currentStatus = EVMTransactionStatus.Pending
while (this.attempts < maxAttempts && !this.isCurrentStatusSettled()) {
await sleep(delay)
await TimeUtils.sleep(delay)
const receipt: any = await this.jevmApi.eth_getTransactionReceipt(this.transactionHash).catch(() => {
return null
})
Expand Down
4 changes: 2 additions & 2 deletions src/transaction/jvm/status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type JVMAPI } from '../../api'
import { sleep } from '../../utils'
import { TimeUtils } from '../../utils'
import { type TransactionStatusFetcher, TransactionStatusFetchDelay } from '../transaction'

export enum JVMTransactionStatus {
Expand All @@ -23,7 +23,7 @@ export class JVMTransactionStatusFetcher implements TransactionStatusFetcher {
const maxAttempts: number = timeout / delay
this.currentStatus = JVMTransactionStatus.Processing
while (this.attempts < maxAttempts && !this.isCurrentStatusSettled()) {
await sleep(delay)
await TimeUtils.sleep(delay)
await this.jvmApi.getTx(this.transactionId).then(
() => {
this.currentStatus = JVMTransactionStatus.Accepted
Expand Down
Loading

0 comments on commit 4f169ed

Please sign in to comment.