Skip to content

Commit

Permalink
update with adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenebelov committed Jan 20, 2025
1 parent 91415bb commit f4c2204
Showing 1 changed file with 48 additions and 26 deletions.
74 changes: 48 additions & 26 deletions src/utils/cspr-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class CasperNetwork {
amountMotes: string | BigNumber,
deployCost: number,
ttl: number,
contractHash?: string
auctionContractHash?: string
) {
if (this.apiVersion === 2) {
new NativeDelegateBuilder()
Expand All @@ -56,10 +56,10 @@ export class CasperNetwork {
.build();
}

if (contractHash) {
if (auctionContractHash) {
return new ContractCallBuilder()
.from(delegatorPublicKey)
.byHash(contractHash)
.byHash(auctionContractHash)
.entryPoint('delegate')
.chainName(networkName)
.runtimeArgs(
Expand All @@ -73,7 +73,9 @@ export class CasperNetwork {
.buildFor1_5();
}

return new Error('Need to provide contract hash');
return new Error(
'Auction contract hash is required when creating a transaction on Casper Network 1.5.x'
);
}

public createUndelegateTransaction(
Expand All @@ -83,7 +85,7 @@ export class CasperNetwork {
amountMotes: string | BigNumber,
deployCost: number,
ttl: number,
contractHash?: string
auctionContractHash?: string
) {
if (this.apiVersion === 2) {
new NativeUndelegateBuilder()
Expand All @@ -96,10 +98,10 @@ export class CasperNetwork {
.build();
}

if (contractHash) {
if (auctionContractHash) {
return new ContractCallBuilder()
.from(delegatorPublicKey)
.byHash(contractHash)
.byHash(auctionContractHash)
.entryPoint('undelegate')
.chainName(networkName)
.ttl(ttl)
Expand All @@ -113,21 +115,25 @@ export class CasperNetwork {
.buildFor1_5();
}

return new Error('Need to provide contract hash');
return new Error(
'Auction contract hash is required when creating a transaction on Casper Network 1.5.x'
);
}

public createRedelegateTransaction(
delegatorPublicKey: PublicKey,
validatorPublicKey: PublicKey,
newValidatorPublicKey: PublicKey,
networkName: string,
amountMotes: string | BigNumber,
deployCost: number,
ttl: number,
contractHash?: string
auctionContractHash?: string
) {
if (this.apiVersion === 2) {
new NativeRedelegateBuilder()
.validator(validatorPublicKey)
.newValidator(newValidatorPublicKey)
.from(delegatorPublicKey)
.amount(amountMotes)
.chainName(networkName)
Expand All @@ -136,21 +142,31 @@ export class CasperNetwork {
.build();
}

if (contractHash) {
// need to provide contract hash
return (
new ContractCallBuilder()
.from(delegatorPublicKey)
.byHash(contractHash)
.entryPoint('redelegate')
.chainName(networkName)
// .amount(amountMotes)
.ttl(ttl)
.buildFor1_5()
);
if (auctionContractHash) {
return new ContractCallBuilder()
.from(delegatorPublicKey)
.byHash(auctionContractHash)
.entryPoint('redelegate')
.chainName(networkName)
.runtimeArgs(
Args.fromMap({
validator: CLValue.newCLPublicKey(validatorPublicKey),
delegator: CLValue.newCLPublicKey(delegatorPublicKey),
amount: CLValueUInt512.newCLUInt512(amountMotes),
...(newValidatorPublicKey
? {
new_validator: CLValue.newCLPublicKey(newValidatorPublicKey)
}
: {})
})
)
.ttl(ttl)
.buildFor1_5();
}

return new Error('Need to provide contract hash');
return new Error(
'Auction contract hash is required when creating a transaction on Casper Network 1.5.x'
);
}

public createTransferTransaction(
Expand Down Expand Up @@ -185,7 +201,9 @@ export class CasperNetwork {
return await this.rpcClient.putDeploy(deploy);
}

return Promise.reject('Transaction does not have a deploy');
return Promise.reject(
'Legacy deploy transaction is required when submitting to Casper Network 1.5'
);
}

public async getTransaction(hash: TransactionHash) {
Expand All @@ -195,12 +213,16 @@ export class CasperNetwork {
hash.transactionV1?.toHex()
);
}

if (hash.deploy) {
return await this.rpcClient.getTransactionByDeployHash(
hash.deploy.toHex()
);
}
}

if (hash.deploy) {
return await this.rpcClient.getTransactionByDeployHash(
hash.deploy.toHex()
);
return await this.rpcClient.getDeploy(hash.deploy.toHex());
}

return Promise.reject('Hash is not valid');
Expand Down

0 comments on commit f4c2204

Please sign in to comment.