Skip to content

Commit

Permalink
Merge pull request #108 from Juneo-io/dev
Browse files Browse the repository at this point in the history
v0.0.92
  • Loading branch information
alekswaslet authored May 2, 2024
2 parents af388d0 + 1bb792b commit 217f1a4
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 5 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.91",
"version": "0.0.92",
"description": "Juneo JS Library",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
10 changes: 8 additions & 2 deletions src/wallet/account/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
estimateSendUtxoOperation,
estimatePlatformCreateSupernetOperation,
estimatePlatformAddSupernetValidatorOperation,
estimatePlatformRemoveSupernetValidatorOperation
estimatePlatformRemoveSupernetValidatorOperation,
estimatePlatformCreateChainOperation
} from '../transaction'
import { AccountError } from '../../utils'
import {
Expand All @@ -23,7 +24,8 @@ import {
type SendUtxoOperation,
type CreateSupernetOperation,
type AddSupernetValidatorOperation,
type RemoveSupernetValidatorOperation
type RemoveSupernetValidatorOperation,
type CreateChainOperation
} from '../operation'
import { type MCNWallet } from '../wallet'
import { UtxoAccount } from './account'
Expand Down Expand Up @@ -61,6 +63,8 @@ export class PlatformAccount extends UtxoAccount {
operation as RemoveSupernetValidatorOperation,
this
)
} else if (operation.type === NetworkOperationType.CreateChain) {
return await estimatePlatformCreateChainOperation(provider, operation as CreateChainOperation, this)
}
throw new AccountError(`unsupported operation: ${operation.type} for the chain with id: ${this.chain.id}`)
}
Expand All @@ -82,6 +86,8 @@ export class PlatformAccount extends UtxoAccount {
await this.executeAndTrackTransaction(summary, TransactionType.ValidateSupernet)
} else if (operation === NetworkOperationType.RemoveSupernetValidator) {
await this.executeAndTrackTransaction(summary, TransactionType.RemoveSupernetValidator)
} else if (operation === NetworkOperationType.CreateChain) {
await this.executeAndTrackTransaction(summary, TransactionType.CreateChain)
}
// balances fetching is needed to get new utxos creating from this operation
await super.refreshBalances()
Expand Down
30 changes: 29 additions & 1 deletion src/wallet/operation/operation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type JRC20Asset, type WrappedAsset } from '../../asset'
import { type JEVMBlockchain, type Blockchain, type PlatformBlockchain } from '../../chain'
import { BLSPublicKey, BLSSignature, type Utxo } from '../../transaction'
import { BLSPublicKey, BLSSignature, DynamicId, type Utxo } from '../../transaction'

export enum NetworkOperationType {
Send = 'Send',
Expand All @@ -19,6 +19,7 @@ export enum NetworkOperationType {
CreateSupernet = 'Create supernet',
ValidateSupernet = 'Validate supernet',
RemoveSupernetValidator = 'Remove supernet validator',
CreateChain = 'Create chain',
}

export enum NetworkOperationRange {
Expand Down Expand Up @@ -295,6 +296,33 @@ export class RemoveSupernetValidatorOperation extends ChainNetworkOperation {
}
}

export class CreateChainOperation extends ChainNetworkOperation {
supernetId: string
chainName: string
vmId: DynamicId
genesisData: string
chainAssetId: string
fxIds: DynamicId[]

constructor (
chain: PlatformBlockchain,
supernetId: string,
chainName: string,
vmId: string,
genesisData: string,
chainAssetId: string = chain.assetId,
fxIds = []
) {
super(NetworkOperationType.CreateChain, chain)
this.supernetId = supernetId
this.chainName = chainName
this.vmId = new DynamicId(vmId)
this.genesisData = genesisData
this.chainAssetId = chainAssetId
this.fxIds = fxIds
}
}

export class CrossOperation implements NetworkOperation {
type: NetworkOperationType = NetworkOperationType.Cross
range: NetworkOperationRange = NetworkOperationRange.Supernet
Expand Down
1 change: 1 addition & 0 deletions src/wallet/transaction/fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export enum FeeType {
CancelStream = 'Cancel stream fee',
CreateSupernet = 'Create supernet fee',
RemoveSupernetValidator = 'Remove supernet validator fee',
CreateChain = 'Create chain fee',
}

export interface FeeData {
Expand Down
69 changes: 68 additions & 1 deletion src/wallet/transaction/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ import {
buildAddSupernetValidatorTransaction,
CreateSupernetTransaction,
Address,
buildRemoveSupernetValidatorTransaction
buildRemoveSupernetValidatorTransaction,
buildCreateChainTransaction,
type DynamicId
} from '../../transaction'
import { type PlatformAccount } from '../account'
import {
type AddSupernetValidatorOperation,
ChainOperationSummary,
type CreateChainOperation,
type CreateSupernetOperation,
type DelegatePrimaryOperation,
type RemoveSupernetValidatorOperation,
Expand Down Expand Up @@ -52,6 +55,11 @@ async function getPlatformRemoveSupernetValidatorFee (provider: MCNProvider): Pr
return new BaseFeeData(provider.platformChain, fee, FeeType.RemoveSupernetValidator)
}

async function getPlatformCreateChainFee (provider: MCNProvider): Promise<BaseFeeData> {
const fee: bigint = BigInt((await provider.info.getTxFee()).createBlockchainTxFee)
return new BaseFeeData(provider.platformChain, fee, FeeType.CreateChain)
}

export async function estimatePlatformAddPrimaryValidatorTransaction (
provider: MCNProvider,
account: PlatformAccount,
Expand Down Expand Up @@ -373,3 +381,62 @@ export async function estimatePlatformRemoveSupernetValidatorOperation (
}
)
}

export async function estimatePlatformCreateChainTransaction (
provider: MCNProvider,
account: PlatformAccount,
supernetId: string,
chainName: string,
vmId: DynamicId,
genesisData: string,
chainAssetId: string,
fxIds: DynamicId[]
): Promise<UtxoFeeData> {
const fee: BaseFeeData = await getPlatformCreateChainFee(provider)
const createSupernetTx: CreateSupernetTransaction = CreateSupernetTransaction.parse(
(await provider.platformApi.getTx(supernetId)).tx
)
const transaction: UnsignedTransaction = buildCreateChainTransaction(
account.utxoSet,
account.getSignersAddresses(),
fee.amount,
provider.platformChain,
supernetId,
chainName,
chainAssetId,
vmId,
fxIds,
genesisData,
createSupernetTx.getSupernetAuth(Address.toAddresses(account.getSignersAddresses())),
account.address,
provider.mcn.id
)
return new UtxoFeeData(fee.chain, fee.amount, fee.type, transaction)
}

export async function estimatePlatformCreateChainOperation (
provider: MCNProvider,
createChain: CreateChainOperation,
account: PlatformAccount
): Promise<ChainOperationSummary> {
const chain: PlatformBlockchain = provider.platformChain
const values = new Map<string, bigint>()
return await estimatePlatformCreateChainTransaction(
provider,
account,
createChain.supernetId,
createChain.chainName,
createChain.vmId,
createChain.genesisData,
createChain.chainAssetId,
createChain.fxIds
).then(
(fee) => {
return new ChainOperationSummary(provider, createChain, chain, fee, [fee.spending], values)
},
async () => {
const fee: BaseFeeData = await getPlatformCreateChainFee(provider)
return new ChainOperationSummary(provider, createChain, chain, fee, [fee.spending], values)
}
)
}
1 change: 1 addition & 0 deletions src/wallet/transaction/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum TransactionType {
CreateSupernet = 'Create supernet transaction',
ValidateSupernet = 'Supernet validation transaction',
RemoveSupernetValidator = 'Remove supernet validator transaction',
CreateChain = 'Create chain transaction',
}

export class TransactionReceipt {
Expand Down

0 comments on commit 217f1a4

Please sign in to comment.