Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{WIP} add abstraction over builders for 1.5 and 2.0 #489

Merged
merged 9 commits into from
Jan 22, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update with adjustments
  • Loading branch information
eugenebelov committed Jan 20, 2025
commit f4c220472a11eb3698bb2929e044ee22dd57253b
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());
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this section should go inside the version 2 condition and the getDeploy RPC method to be used for 1.5.x


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