-
Notifications
You must be signed in to change notification settings - Fork 62
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
Comp0te
merged 9 commits into
feat-5.0.0
from
feature/CSDK-206-add-abstraction-over-builders
Jan 22, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
66590fc
add new util
eugenebelov c3c392b
casper network util prototype
eugenebelov 91415bb
update delegate/undelegate for 1.5
eugenebelov f4c2204
update with adjustments
eugenebelov ccbcc99
add return types
eugenebelov 139370e
add few more wrappers
eugenebelov 2398152
fix typos
eugenebelov 74d6047
fix build
eugenebelov 563af88
fix method name
eugenebelov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,317 @@ | ||
import { | ||
InfoGetTransactionResult, | ||
PutDeployResult, | ||
PutTransactionResult, | ||
RpcClient | ||
} from '../rpc'; | ||
import { | ||
Args, | ||
CLValue, | ||
CLValueUInt512, | ||
ContractCallBuilder, | ||
NativeDelegateBuilder, | ||
NativeRedelegateBuilder, | ||
NativeTransferBuilder, | ||
NativeUndelegateBuilder, | ||
PublicKey, | ||
SessionBuilder, | ||
Transaction, | ||
TransactionHash | ||
} from '../types'; | ||
import { BigNumber } from '@ethersproject/bignumber'; | ||
|
||
export class CasperNetwork { | ||
private rpcClient: RpcClient; | ||
private apiVersion: number; | ||
|
||
constructor(rpcClient: RpcClient, apiVersion: number) { | ||
this.rpcClient = rpcClient; | ||
this.apiVersion = apiVersion; | ||
} | ||
|
||
public static async create( | ||
rpcClient: RpcClient, | ||
apiVersion?: number | ||
): Promise<CasperNetwork> { | ||
if (!apiVersion) { | ||
const status = await rpcClient.getStatus(); | ||
|
||
apiVersion = status.apiVersion.startsWith('2.') ? 2 : 1; | ||
} | ||
|
||
return new CasperNetwork(rpcClient, apiVersion); | ||
} | ||
|
||
public createDelegateTransaction( | ||
delegatorPublicKey: PublicKey, | ||
validatorPublicKey: PublicKey, | ||
networkName: string, | ||
amountMotes: string | BigNumber, | ||
deployCost: number, | ||
ttl: number, | ||
auctionContractHash?: string | ||
): Transaction { | ||
if (this.apiVersion === 2) { | ||
return new NativeDelegateBuilder() | ||
.validator(validatorPublicKey) | ||
.from(delegatorPublicKey) | ||
.amount(amountMotes) | ||
.chainName(networkName) | ||
.payment(deployCost) | ||
.ttl(ttl) | ||
.build(); | ||
} | ||
|
||
if (auctionContractHash) { | ||
return new ContractCallBuilder() | ||
.from(delegatorPublicKey) | ||
.byHash(auctionContractHash) | ||
.entryPoint('delegate') | ||
.chainName(networkName) | ||
.runtimeArgs( | ||
Args.fromMap({ | ||
validator: CLValue.newCLPublicKey(validatorPublicKey), | ||
delegator: CLValue.newCLPublicKey(delegatorPublicKey), | ||
amount: CLValueUInt512.newCLUInt512(amountMotes) | ||
}) | ||
) | ||
.ttl(ttl) | ||
.buildFor1_5(); | ||
} | ||
|
||
throw new Error( | ||
'Auction contract hash is required when creating a transaction on Casper Network 1.5.x' | ||
); | ||
} | ||
|
||
public createUndelegateTransaction( | ||
delegatorPublicKey: PublicKey, | ||
validatorPublicKey: PublicKey, | ||
networkName: string, | ||
amountMotes: string | BigNumber, | ||
deployCost: number, | ||
ttl: number, | ||
auctionContractHash?: string | ||
): Transaction { | ||
if (this.apiVersion === 2) { | ||
return new NativeUndelegateBuilder() | ||
.validator(validatorPublicKey) | ||
.from(delegatorPublicKey) | ||
.amount(amountMotes) | ||
.chainName(networkName) | ||
.payment(deployCost) | ||
.ttl(ttl) | ||
.build(); | ||
} | ||
|
||
if (auctionContractHash) { | ||
return new ContractCallBuilder() | ||
.from(delegatorPublicKey) | ||
.byHash(auctionContractHash) | ||
.entryPoint('undelegate') | ||
.chainName(networkName) | ||
.ttl(ttl) | ||
.runtimeArgs( | ||
Args.fromMap({ | ||
validator: CLValue.newCLPublicKey(validatorPublicKey), | ||
delegator: CLValue.newCLPublicKey(delegatorPublicKey), | ||
amount: CLValueUInt512.newCLUInt512(amountMotes) | ||
}) | ||
) | ||
.buildFor1_5(); | ||
} | ||
|
||
throw 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, | ||
auctionContractHash?: string | ||
): Transaction { | ||
if (this.apiVersion === 2) { | ||
return new NativeRedelegateBuilder() | ||
.validator(validatorPublicKey) | ||
.newValidator(newValidatorPublicKey) | ||
.from(delegatorPublicKey) | ||
.amount(amountMotes) | ||
.chainName(networkName) | ||
.payment(deployCost) | ||
.ttl(ttl) | ||
.build(); | ||
} | ||
|
||
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(); | ||
} | ||
|
||
throw new Error( | ||
'Auction contract hash is required when creating a transaction on Casper Network 1.5.x' | ||
); | ||
} | ||
|
||
public createTransferTransaction( | ||
senderPublicKey: PublicKey, | ||
recipientPublicKey: PublicKey, | ||
networkName: string, | ||
amountMotes: string, | ||
deployCost: number, | ||
ttl: number | ||
): Transaction { | ||
const transferBuilder = new NativeTransferBuilder() | ||
.from(senderPublicKey) | ||
.target(recipientPublicKey) | ||
.amount(amountMotes) | ||
.chainName(networkName) | ||
.payment(deployCost) | ||
.ttl(ttl); | ||
if (this.apiVersion === 2) { | ||
return transferBuilder.build(); | ||
} | ||
|
||
return transferBuilder.buildFor1_5(); | ||
} | ||
|
||
public createContractCallTransaction( | ||
senderPublicKey: PublicKey, | ||
contractHash: string, | ||
entryPoint: string, | ||
networkName: string, | ||
deployCost: number, | ||
ttl: number, | ||
args: Args | ||
): Transaction { | ||
const contractCall = new ContractCallBuilder() | ||
.byHash(contractHash) | ||
.from(senderPublicKey) | ||
.entryPoint(entryPoint) | ||
.chainName(networkName) | ||
.runtimeArgs(args) | ||
.ttl(ttl) | ||
.payment(deployCost); | ||
|
||
if (this.apiVersion === 2) { | ||
return contractCall.build(); | ||
} | ||
|
||
return contractCall.buildFor1_5(); | ||
} | ||
|
||
public createContractPackageCallTransaction( | ||
senderPublicKey: PublicKey, | ||
contractPackageHash: string, | ||
entryPoint: string, | ||
networkName: string, | ||
deployCost: number, | ||
args: Args, | ||
ttl: number, | ||
contractVersion?: number | ||
): Transaction { | ||
const contractCall = new ContractCallBuilder() | ||
.byPackageHash(contractPackageHash, contractVersion) | ||
.from(senderPublicKey) | ||
.entryPoint(entryPoint) | ||
.chainName(networkName) | ||
.runtimeArgs(args) | ||
.ttl(ttl) | ||
.payment(deployCost); | ||
|
||
if (this.apiVersion === 2) { | ||
return contractCall.build(); | ||
} | ||
|
||
return contractCall.buildFor1_5(); | ||
} | ||
|
||
public createSessionWasmTransaction( | ||
senderPublicKey: PublicKey, | ||
networkName: string, | ||
deployCost: number, | ||
ttl: number, | ||
bytes: Uint8Array, | ||
args: Args | ||
): Transaction { | ||
const sessionWasm = new SessionBuilder() | ||
.from(senderPublicKey) | ||
.chainName(networkName) | ||
.payment(deployCost) | ||
.ttl(ttl) | ||
.wasm(bytes) | ||
.runtimeArgs(args); | ||
|
||
if (this.apiVersion === 2) { | ||
return sessionWasm.build(); | ||
} | ||
|
||
return sessionWasm.buildFor1_5(); | ||
} | ||
|
||
public async putTransaction( | ||
transaction: Transaction | ||
): Promise<PutTransactionResult | PutDeployResult> { | ||
if (this.apiVersion == 2) { | ||
return await this.rpcClient.putTransaction(transaction); | ||
} | ||
|
||
const deploy = transaction.getDeploy(); | ||
if (deploy) { | ||
return await this.rpcClient.putDeploy(deploy); | ||
} | ||
|
||
return Promise.reject( | ||
'Legacy deploy transaction is required when submitting to Casper Network 1.5' | ||
); | ||
} | ||
|
||
public async getTransaction( | ||
hash: TransactionHash | ||
): Promise<InfoGetTransactionResult> { | ||
if (this.apiVersion == 2) { | ||
if (hash.transactionV1) { | ||
return await this.rpcClient.getTransactionByTransactionHash( | ||
hash.transactionV1?.toHex() | ||
); | ||
} | ||
|
||
if (hash.deploy) { | ||
return await this.rpcClient.getTransactionByDeployHash( | ||
hash.deploy.toHex() | ||
); | ||
} | ||
} | ||
|
||
if (hash.deploy) { | ||
const getDeployResult = await this.rpcClient.getDeploy( | ||
hash.deploy.toHex() | ||
); | ||
return getDeployResult.toInfoGetTransactionResult(); | ||
} | ||
|
||
return Promise.reject('Hash is not valid'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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