Skip to content

Commit

Permalink
Merge pull request #153 from Juneo-io/dev
Browse files Browse the repository at this point in the history
v0.0.119
  • Loading branch information
alekswaslet authored Jul 11, 2024
2 parents e53c898 + b453dc4 commit e83a68f
Show file tree
Hide file tree
Showing 20 changed files with 289 additions and 243 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.118",
"version": "0.0.119",
"description": "Juneo JS Library",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/asset/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export enum TokenType {
* such as an id, a name, a symbol and decimals.
*/
export class TokenAsset {
readonly type: string = TokenType.Generic
readonly type: TokenType = TokenType.Generic
readonly assetId: string
readonly name: string
readonly symbol: string
Expand Down
6 changes: 3 additions & 3 deletions src/asset/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const EVMGasTokenDecimals = 18
* It is also known as the native asset of an EVM.
*/
export class EVMGasToken extends TokenAsset {
override readonly type: string = TokenType.Gas
override readonly type: TokenType = TokenType.Gas

constructor (assetId: string, name: string, symbol: string) {
super(assetId, name, symbol, EVMGasTokenDecimals)
Expand All @@ -23,7 +23,7 @@ export interface EVMContract {
* Representation of an ERC20 smart contract.
*/
export class ERC20Asset extends TokenAsset implements EVMContract {
override readonly type: string = TokenType.ERC20
override readonly type: TokenType = TokenType.ERC20
readonly address

constructor (address: string, name: string, symbol: string, decimals: number) {
Expand All @@ -41,7 +41,7 @@ export class ERC20Asset extends TokenAsset implements EVMContract {
* Also known as wrapped native. In the Juneo network it is deployed as the wJUNE.
*/
export class WrappedAsset extends ERC20Asset {
override readonly type: string = TokenType.Wrapped
override readonly type: TokenType = TokenType.Wrapped
readonly adapter: WrappedContractAdapter

constructor (address: string, name: string, symbol: string, decimals: number) {
Expand Down
2 changes: 1 addition & 1 deletion src/asset/jevm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class JEVMGasToken extends EVMGasToken {
* Representation of a JRC20 smart contract.
*/
export class JRC20Asset extends ERC20Asset {
override readonly type: string = TokenType.JRC20
override readonly type: TokenType = TokenType.JRC20
readonly nativeAssetId: string
readonly adapter: JRC20ContractAdapter

Expand Down
2 changes: 1 addition & 1 deletion src/asset/jvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TokenAsset, TokenType } from './asset'
* Representation of a Juneo native asset.
*/
export class JNTAsset extends TokenAsset {
override readonly type: string = TokenType.JNT
override readonly type: TokenType = TokenType.JNT
readonly mintable: boolean

constructor (assetId: string, name: string, symbol: string, decimals: number, mintable: boolean) {
Expand Down
2 changes: 1 addition & 1 deletion src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export abstract class AbstractBlockchain implements Blockchain {
if (this.registeredAssets.has(assetId)) {
return this.registeredAssets.get(assetId)!
}
const asset: TokenAsset = await this.fetchAsset(provider, assetId)
const asset = await this.fetchAsset(provider, assetId)
this.addRegisteredAsset(asset)
return asset
}
Expand Down
18 changes: 18 additions & 0 deletions src/chain/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TokenType } from '../asset'

export const PLATFORMVM_ID: string = '11111111111111111111111111111111LpoYY'
export const JVM_ID: string = 'otSmSxFRBqdRX7kestRW732n3WS2MrLAoWwHZxHnmMGMuLYX8'
export const JEVM_ID: string = 'orkbbNQVf27TiBe6GqN5dm8d8Lo3rutEov8DUWZaKNUjckwSk'
export const EVM_ID: string = 'mgj786NP7uDwBCcq6YwThhaN8FLyybkCa4zBWTQbNgmK6k9A6'

export const JVM_HD_PATH = 9000
export const EVM_HD_PATH = 60

export const BaseShare: number = 100_0000 // 100%

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

export const SendEtherGasLimit = BigInt(21_000)
export const EmptyCallData = '0x'
export const EVMTransferables: TokenType[] = [TokenType.ERC20, TokenType.JRC20, TokenType.Wrapped]
1 change: 1 addition & 0 deletions src/chain/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './chain'
export * from './constants'
export * from './jevm'
export * from './jvm'
export * from './platform'
Expand Down
25 changes: 7 additions & 18 deletions src/chain/jevm.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
import { ethers } from 'ethers'
import { type JEVMAPI } from '../api'
import { type JEVMGasToken, type JRC20Asset, type TokenAsset, TokenType, type WrappedAsset } from '../asset'
import { type JEVMGasToken, type JRC20Asset, type TokenAsset, 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 { EmptyCallData, EVM_HD_PATH, EVMTransferables, JEVM_ID } from './constants'
import { ContractManager, ERC20TokenHandler } from './solidity'
import { ChainVM, VMType, 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'

export const SendEtherGasLimit = BigInt(21_000)
export const EmptyCallData = '0x'
const Transferables: string[] = [TokenType.ERC20, TokenType.JRC20, TokenType.Wrapped]

export class JEVMBlockchain extends AbstractBlockchain {
override asset: JEVMGasToken
chainId: bigint
Expand All @@ -41,7 +30,7 @@ export class JEVMBlockchain extends AbstractBlockchain {
jrc20Assets: JRC20Asset[] = [],
wrappedAsset?: WrappedAsset | undefined
) {
super(name, id, new ChainVM(JEVM_ID, VMType.EVM, VMWalletType.Nonce, HD_PATH), asset, aliases, registeredAssets)
super(name, id, new ChainVM(JEVM_ID, VMType.EVM, VMWalletType.Nonce, EVM_HD_PATH), asset, aliases, registeredAssets)
this.asset = asset
this.chainId = chainId
this.baseFee = baseFee
Expand All @@ -62,16 +51,16 @@ export class JEVMBlockchain extends AbstractBlockchain {
): Promise<string> {
// could rather use contract manager but it would require one extra network call
// we avoid it if the asset is already registered
const asset: TokenAsset = await this.getAsset(provider, assetId)
if (Transferables.includes(asset.type)) {
const asset = await this.getAsset(provider, assetId)
if (EVMTransferables.includes(asset.type)) {
return this.contractManager.getTransferData(this.ethProvider, assetId, to, amount)
}
return EmptyCallData
}

protected async fetchAsset (provider: MCNProvider, assetId: string): Promise<TokenAsset> {
if (isContractAddress(assetId)) {
const handler: SolidityTokenHandler | null = await this.contractManager.getHandler(assetId)
const handler = await this.contractManager.getHandler(assetId)
if (handler === null) {
throw new ChainError(`contract address ${assetId} does not implement a compatible interface`)
}
Expand Down
7 changes: 2 additions & 5 deletions src/chain/jvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import { type JNTAsset, type TokenAsset } from '../asset'
import { type MCNProvider } from '../juneo'
import { fetchJNT, validateBech32 } from '../utils'
import { AbstractBlockchain } from './chain'
import { JVM_HD_PATH, JVM_ID } from './constants'
import { ChainVM, VMType, 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, new ChainVM(JVM_ID, VMType.JVM, VMWalletType.Utxo, HD_PATH), asset, aliases, registeredAssets)
super(name, id, new ChainVM(JVM_ID, VMType.JVM, VMWalletType.Utxo, JVM_HD_PATH), asset, aliases, registeredAssets)
}

protected async fetchAsset (provider: MCNProvider, assetId: string): Promise<TokenAsset> {
Expand Down
31 changes: 13 additions & 18 deletions src/chain/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ import { type JNTAsset, type TokenAsset } from '../asset'
import { type MCNProvider } from '../juneo'
import { fetchJNT, RewardCalculator, TimeUtils, validateBech32 } from '../utils'
import { AbstractBlockchain } from './chain'
import { BaseShare, JVM_HD_PATH, PLATFORMVM_ID } from './constants'
import { ChainVM, VMType, VMWalletType } from './vm'

export const PLATFORMVM_ID: string = '11111111111111111111111111111111LpoYY'

const HD_PATH = 9000

const BaseShare: number = 100_0000 // 100%

export class PlatformBlockchain extends AbstractBlockchain {
stakeConfig: StakeConfig
rewardConfig: RewardConfig
Expand All @@ -27,7 +22,7 @@ export class PlatformBlockchain extends AbstractBlockchain {
super(
name,
id,
new ChainVM(PLATFORMVM_ID, VMType.JVM, VMWalletType.Utxo, HD_PATH),
new ChainVM(PLATFORMVM_ID, VMType.JVM, VMWalletType.Utxo, JVM_HD_PATH),
asset,
aliases,
registeredAssets
Expand All @@ -46,11 +41,11 @@ export class PlatformBlockchain extends AbstractBlockchain {
}

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

estimatePrimaryDelegationReward (stakePeriod: bigint, stakeAmount: bigint): bigint {
const rewards: bigint = this.rewardCalculator.calculatePrimary(stakePeriod, TimeUtils.now(), stakeAmount)
const rewards = this.rewardCalculator.calculate(stakePeriod, TimeUtils.now(), stakeAmount)
return (rewards * BigInt(BaseShare - this.stakeConfig.minDelegationFee)) / BigInt(BaseShare)
}
}
Expand Down Expand Up @@ -91,31 +86,31 @@ export class RewardConfig {
maxStakePeriod: bigint
stakePeriodRewardShare: bigint
startRewardTime: bigint
startReward: bigint
startRewardShare: bigint
diminishingRewardTime: bigint
diminishingReward: bigint
diminishingRewardShare: bigint
targetRewardTime: bigint
targetReward: bigint
targetRewardShare: bigint

constructor (
minStakePeriod: bigint,
maxStakePeriod: bigint,
stakePeriodRewardShare: bigint,
startRewardTime: bigint,
startReward: bigint,
startRewardShare: bigint,
diminishingRewardTime: bigint,
diminishingReward: bigint,
diminishingRewardShare: bigint,
targetRewardTime: bigint,
targetReward: bigint
targetRewardShare: bigint
) {
this.minStakePeriod = minStakePeriod
this.maxStakePeriod = maxStakePeriod
this.stakePeriodRewardShare = stakePeriodRewardShare
this.startRewardTime = startRewardTime
this.startReward = startReward
this.startRewardShare = startRewardShare
this.diminishingRewardTime = diminishingRewardTime
this.diminishingReward = diminishingReward
this.diminishingRewardShare = diminishingRewardShare
this.targetRewardTime = targetRewardTime
this.targetReward = targetReward
this.targetRewardShare = targetRewardShare
}
}
10 changes: 5 additions & 5 deletions src/chain/solidity/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export class ContractManager {
}

async balanceOf (provider: ethers.JsonRpcProvider, contractAddress: string, address: string): Promise<bigint> {
const contract: ethers.Contract = new ethers.Contract(contractAddress, abi.BalanceOfABI, provider)
const contract = new ethers.Contract(contractAddress, abi.BalanceOfABI, provider)
return BigInt.asUintN(256, BigInt(await contract.balanceOf(address)))
}

getTransferData (provider: ethers.JsonRpcProvider, contractAddress: string, to: string, amount: bigint): string {
const contract: ethers.Contract = new ethers.Contract(contractAddress, abi.TransferABI, provider)
const contract = new ethers.Contract(contractAddress, abi.TransferABI, provider)
return contract.interface.encodeFunctionData('transfer', [to, amount])
}
}
Expand All @@ -48,7 +48,7 @@ export class ERC20TokenHandler implements SolidityTokenHandler {
}

async instanceOf (contractAddress: string): Promise<boolean> {
const contract: ethers.Contract = this.getContract(contractAddress)
const contract = this.getContract(contractAddress)
// checking if is ERC20 by calling decimals read only function
// other main tokens interfaces should not be using decimals
// IERC165 is not widespread enough to be used by ERC20 tokens
Expand All @@ -59,7 +59,7 @@ export class ERC20TokenHandler implements SolidityTokenHandler {
}

async queryTokenData (contractAddress: string): Promise<TokenAsset> {
const contract: ethers.Contract = this.getContract(contractAddress)
const contract = this.getContract(contractAddress)
const name: string = await contract.name()
const symbol: string = await contract.symbol()
const decimals: number = await contract.decimals()
Expand Down Expand Up @@ -97,7 +97,7 @@ export class JRC20ContractAdapter extends EVMCallAdapter {

getDepositData (assetId: string, amount: bigint): string {
// native asset call data
let data: string = ethers.solidityPacked(
let data = ethers.solidityPacked(
['address', 'uint256', 'uint256'],
[this.contractAddress, `0x${new AssetId(assetId).serialize().toHex()}`, amount]
)
Expand Down
11 changes: 8 additions & 3 deletions src/transaction/platform/supernet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export class Validator implements Serializable {
nodeId: NodeId
startTime: bigint
endTime: bigint
stakePeriod: bigint
weight: bigint

constructor (nodeId: NodeId, stakePeriod: bigint, weight: bigint) {
Expand All @@ -27,10 +26,13 @@ export class Validator implements Serializable {
// we use a fixed start time of now() as another would not have any effect anyways.
this.startTime = TimeUtils.now()
this.endTime = this.startTime + stakePeriod
this.stakePeriod = stakePeriod
this.weight = weight
}

getStakePeriod (): bigint {
return this.endTime - this.startTime
}

serialize (): JuneoBuffer {
const buffer: JuneoBuffer = JuneoBuffer.alloc(ValidatorSize)
buffer.write(this.nodeId.serialize())
Expand All @@ -45,7 +47,10 @@ export class Validator implements Serializable {
const nodeId = new NodeId(reader.read(NodeIdSize).toCB58())
const startTime = reader.readUInt64()
const endTime = reader.readUInt64()
return new Validator(nodeId, endTime - startTime, reader.readUInt64())
const validator = new Validator(nodeId, endTime - startTime, reader.readUInt64())
validator.startTime = startTime
validator.endTime = endTime
return validator
}
}

Expand Down
Loading

0 comments on commit e83a68f

Please sign in to comment.