-
Notifications
You must be signed in to change notification settings - Fork 62
{WIP} add abstraction over builders for 1.5 and 2.0 #489
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
Changes from 3 commits
66590fc
c3c392b
91415bb
f4c2204
ccbcc99
139370e
2398152
74d6047
563af88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
import { RpcClient } from '../rpc'; | ||
import { | ||
Args, | ||
CLValue, | ||
CLValueUInt512, | ||
ContractCallBuilder, | ||
NativeDelegateBuilder, | ||
NativeRedelegateBuilder, | ||
NativeTransferBuilder, | ||
NativeUndelegateBuilder, | ||
PublicKey, | ||
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, | ||
contractHash?: string | ||
) { | ||
if (this.apiVersion === 2) { | ||
new NativeDelegateBuilder() | ||
eugenebelov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.validator(validatorPublicKey) | ||
.from(delegatorPublicKey) | ||
.amount(amountMotes) | ||
.chainName(networkName) | ||
.payment(deployCost) | ||
.ttl(ttl) | ||
.build(); | ||
} | ||
|
||
if (contractHash) { | ||
return new ContractCallBuilder() | ||
.from(delegatorPublicKey) | ||
.byHash(contractHash) | ||
.entryPoint('delegate') | ||
.chainName(networkName) | ||
.runtimeArgs( | ||
Args.fromMap({ | ||
validator: CLValue.newCLPublicKey(validatorPublicKey), | ||
delegator: CLValue.newCLPublicKey(delegatorPublicKey), | ||
amount: CLValueUInt512.newCLUInt512(amountMotes) | ||
}) | ||
) | ||
.ttl(ttl) | ||
.buildFor1_5(); | ||
} | ||
|
||
return new Error('Need to provide contract hash'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's change to |
||
} | ||
|
||
public createUndelegateTransaction( | ||
delegatorPublicKey: PublicKey, | ||
validatorPublicKey: PublicKey, | ||
networkName: string, | ||
amountMotes: string | BigNumber, | ||
deployCost: number, | ||
ttl: number, | ||
contractHash?: string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's rename to |
||
) { | ||
if (this.apiVersion === 2) { | ||
new NativeUndelegateBuilder() | ||
.validator(validatorPublicKey) | ||
.from(delegatorPublicKey) | ||
.amount(amountMotes) | ||
.chainName(networkName) | ||
.payment(deployCost) | ||
.ttl(ttl) | ||
.build(); | ||
} | ||
|
||
if (contractHash) { | ||
return new ContractCallBuilder() | ||
.from(delegatorPublicKey) | ||
.byHash(contractHash) | ||
.entryPoint('undelegate') | ||
.chainName(networkName) | ||
.ttl(ttl) | ||
.runtimeArgs( | ||
Args.fromMap({ | ||
validator: CLValue.newCLPublicKey(validatorPublicKey), | ||
delegator: CLValue.newCLPublicKey(delegatorPublicKey), | ||
amount: CLValueUInt512.newCLUInt512(amountMotes) | ||
}) | ||
) | ||
.buildFor1_5(); | ||
} | ||
|
||
return new Error('Need to provide contract hash'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's change to |
||
} | ||
|
||
public createRedelegateTransaction( | ||
delegatorPublicKey: PublicKey, | ||
validatorPublicKey: PublicKey, | ||
networkName: string, | ||
amountMotes: string | BigNumber, | ||
deployCost: number, | ||
ttl: number, | ||
contractHash?: string | ||
eugenebelov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) { | ||
if (this.apiVersion === 2) { | ||
new NativeRedelegateBuilder() | ||
.validator(validatorPublicKey) | ||
.from(delegatorPublicKey) | ||
.amount(amountMotes) | ||
.chainName(networkName) | ||
.payment(deployCost) | ||
.ttl(ttl) | ||
.build(); | ||
} | ||
|
||
if (contractHash) { | ||
// need to provide contract hash | ||
return ( | ||
new ContractCallBuilder() | ||
.from(delegatorPublicKey) | ||
.byHash(contractHash) | ||
.entryPoint('redelegate') | ||
.chainName(networkName) | ||
// .amount(amountMotes) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accidental comment? |
||
.ttl(ttl) | ||
.buildFor1_5() | ||
); | ||
} | ||
|
||
return new Error('Need to provide contract hash'); | ||
eugenebelov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public createTransferTransaction( | ||
senderPublicKey: PublicKey, | ||
recepientPublicKey: PublicKey, | ||
eugenebelov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
networkName: string, | ||
amountMotes: string, | ||
deployCost: number, | ||
ttl: number | ||
) { | ||
const transferBuilder = new NativeTransferBuilder() | ||
.from(senderPublicKey) | ||
.target(recepientPublicKey) | ||
.amount(amountMotes) | ||
.chainName(networkName) | ||
.payment(deployCost) | ||
.ttl(ttl); | ||
if (this.apiVersion === 2) { | ||
return transferBuilder.build(); | ||
} | ||
|
||
return transferBuilder.buildFor1_5(); | ||
} | ||
|
||
public async putTransaction(transaction: Transaction) { | ||
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('Transaction does not have a deploy'); | ||
eugenebelov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public async getTransaction(hash: TransactionHash) { | ||
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() | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
return Promise.reject('Hash is not valid'); | ||
} | ||
} |
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.
Let's rename to
auctionContractHash