-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add makeCep18TransferDeploy and makeNftTransferDeploy utils
- Loading branch information
Showing
7 changed files
with
357 additions
and
6 deletions.
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
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
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
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,93 @@ | ||
import { | ||
Args, | ||
CLValue, | ||
CLValueUInt256, | ||
ContractHash, | ||
DEFAULT_DEPLOY_TTL, | ||
Deploy, | ||
DeployHeader, | ||
Duration, | ||
ExecutableDeployItem, Key, KeyTypeID, | ||
PublicKey, | ||
StoredContractByHash | ||
} from '../types'; | ||
import { CasperNetworkName } from '../@types'; | ||
|
||
export interface IMakeCep18TransferDeployParams { | ||
contractHash: string, | ||
senderPublicKeyHex: string; | ||
recipientPublicKeyHex: string; | ||
transferAmount: string; | ||
paymentAmount: string, | ||
chainName?: CasperNetworkName; | ||
ttl?: number; | ||
} | ||
|
||
/** | ||
* This function generates a `Deploy` for transferring CEP-18 from one account to another. | ||
* | ||
* @param params - The parameters required to create the CEP-18 transfer deploy. | ||
* @param params.contractHash - The hash of the contract to interact with. | ||
* This is a 64-character hexadecimal string representing the contract. | ||
* @param params.senderPublicKeyHex - The sender's public key in hexadecimal format. | ||
* @param params.recipientPublicKeyHex - The recipient's public key in hexadecimal format. | ||
* @param params.transferAmount - The amount of CSPR to transfer. | ||
* This value must be represented in its smallest unit (motes). | ||
* For example, to transfer 2.5 CSPR, provide the value `2500000000` (2.5 * 10^9 motes). | ||
* @param params.paymentAmount - The amount of CSPR to pay a network fee. | ||
* This value must be represented in its smallest unit (motes). | ||
* @param params.chainName - (Optional) The name of the Casper network chain - {CasperNetworkName}. | ||
* Must be either `'casper'` (mainnet) or `'casper-test'` (testnet). | ||
* Defaults to `'CasperNetworkName.Mainnet'` if not specified. | ||
* @param params.ttl - (Optional) The time-to-live (TTL) for the `Deploy` in milliseconds. | ||
* Specifies how long the `Deploy` is valid before it expires. | ||
* Defaults 1800000 (30 minutes) | ||
* @returns A promise that resolves to the created Deploy instance, ready to be sent to the Casper network. | ||
* | ||
* @example | ||
* ```ts | ||
* import { makeCsprTransferDeploy } from 'casper-js-sdk'; | ||
* | ||
* const deploy = await makeCep18TransferDeploy({ | ||
* contractHash: '0123456789asdfbcdef...', | ||
* senderPublicKeyHex: '0123456789asdfbcdef...', | ||
* recipientPublicKeyHex: '0123456789abcdef...', | ||
* transferAmount: '25000000000', // 25 CEP-18 with 9 decimals | ||
* paymentAmount: '3000000000', // 3 CSPR | ||
* }); | ||
* | ||
* console.log('Created Deploy:', deploy); | ||
* ``` | ||
*/ | ||
export const makeCep18TransferDeploy = ({ | ||
contractHash, | ||
senderPublicKeyHex, | ||
recipientPublicKeyHex, | ||
transferAmount, | ||
paymentAmount, | ||
chainName = CasperNetworkName.Mainnet, | ||
ttl = DEFAULT_DEPLOY_TTL, | ||
}: IMakeCep18TransferDeployParams): Deploy => { | ||
const senderPublicKey = PublicKey.newPublicKey(senderPublicKeyHex); | ||
const recipientPublicKey = PublicKey.newPublicKey(recipientPublicKeyHex); | ||
|
||
const session = new ExecutableDeployItem(); | ||
|
||
session.storedContractByHash = new StoredContractByHash( | ||
ContractHash.newContract(contractHash), | ||
'transfer', | ||
Args.fromMap({ | ||
recipient: CLValue.newCLKey(Key.createByType(recipientPublicKey.accountHash().toPrefixedString(), KeyTypeID.Account)), | ||
amount: CLValueUInt256.newCLUInt256(transferAmount) | ||
}) | ||
); | ||
|
||
const payment = ExecutableDeployItem.standardPayment(paymentAmount); | ||
|
||
const deployHeader = DeployHeader.default(); | ||
deployHeader.account = senderPublicKey; | ||
deployHeader.chainName = chainName; | ||
deployHeader.ttl = new Duration(ttl); | ||
|
||
return Deploy.makeDeploy(deployHeader, payment, session); | ||
}; |
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,168 @@ | ||
import { | ||
Args, | ||
CLTypeUInt256, | ||
CLValue, | ||
CLValueBool, | ||
CLValueList, | ||
CLValueUInt256, | ||
CLValueUInt64, | ||
ContractHash, | ||
DEFAULT_DEPLOY_TTL, | ||
Deploy, | ||
DeployHeader, | ||
Duration, | ||
ExecutableDeployItem, Key, KeyTypeID, | ||
PublicKey, | ||
StoredVersionedContractByHash | ||
} from '../types'; | ||
import { CasperNetworkName, NFTTokenStandard } from '../@types'; | ||
|
||
export interface IMakeNftTransferDeployParams { | ||
nftStandard: NFTTokenStandard; | ||
contractPackageHash: string; | ||
senderPublicKeyHex: string; | ||
recipientPublicKeyHex: string; | ||
paymentAmount: string; | ||
chainName?: CasperNetworkName; | ||
ttl?: number; | ||
tokenId?: string; | ||
tokenHash?: string; | ||
} | ||
|
||
/** | ||
* Creates a `Deploy` for transferring an NFT (Non-Fungible Token). | ||
* This function constructs and returns a `Deploy` for transferring NFTs according to the specified parameters. | ||
* | ||
* @param params - The parameters required to create the NFT transfer deploy. | ||
* @param params.nftStandard - The NFT standard being used (e.g., CEP-78, CEP-47). | ||
* @param params.contractPackageHash - The hash of the contract package to interact with. | ||
* @param params.senderPublicKeyHex - The sender's public key in hexadecimal format. | ||
* @param params.recipientPublicKeyHex - The recipient's public key in hexadecimal format. | ||
* @param params.paymentAmount - The payment amount for the transaction, specified in motes. | ||
* @param params.chainName - The name of the Casper network chain (e.g., "casper", "casper-test"). Defaults to Mainnet. | ||
* @param params.ttl - The time-to-live (TTL) for the deploy in milliseconds. Defaults to the constant `DEFAULT_DEPLOY_TTL`. | ||
* @param params.tokenId - The ID of the token to transfer. Optional and used if the standard requires it. | ||
* @param params.tokenHash - The hash of the token to transfer. Optional and used if the standard requires it. | ||
* | ||
* @returns A deploy object representing the NFT transfer operation. | ||
* | ||
* @example | ||
* ```ts | ||
* import { makeNftTransferDeploy, NFTTokenStandard } from 'casper-js-sdk'; | ||
* | ||
* const deploy = await makeNftTransferDeploy({ | ||
* nftStandard: NFTTokenStandard.CEP47, | ||
* contractPackageHash: '0123456789asdfbcdef...', | ||
* senderPublicKeyHex: '0123456789asdfbcdef...', | ||
* recipientPublicKeyHex: '0123456789abcdef...', | ||
* paymentAmount: '3000000000', // 3 CSPR | ||
* tokenId: 234, | ||
* }); | ||
* | ||
* console.log('Created Deploy:', deploy); | ||
* ``` | ||
*/ | ||
export const makeNftTransferDeploy = ({ | ||
nftStandard, | ||
contractPackageHash, | ||
senderPublicKeyHex, | ||
recipientPublicKeyHex, | ||
paymentAmount, | ||
chainName = CasperNetworkName.Mainnet, | ||
ttl = DEFAULT_DEPLOY_TTL, | ||
tokenId, | ||
tokenHash | ||
}: IMakeNftTransferDeployParams): Deploy => { | ||
const senderPublicKey = PublicKey.newPublicKey(senderPublicKeyHex); | ||
|
||
if (!(tokenId || tokenHash)) { | ||
throw new Error('Specify either tokenId or tokenHash to make a transfer') | ||
} | ||
|
||
let args: Args | null = null; | ||
|
||
if (nftStandard === NFTTokenStandard.CEP47) { | ||
if (!tokenId) { | ||
throw new Error('TokenId is required for CEP-47 transfer') | ||
} | ||
|
||
args = getRuntimeArgsForCep47Transfer({ tokenId, recipientPublicKeyHex }) | ||
} | ||
|
||
if (nftStandard === NFTTokenStandard.CEP78) { | ||
args = getRuntimeArgsForCep78Transfer({ | ||
tokenId, | ||
tokenHash, | ||
senderPublicKeyHex, | ||
recipientPublicKeyHex | ||
}); | ||
} | ||
|
||
if (!args) { | ||
throw new Error('Deploy arguments error. Check provided token data') | ||
} | ||
|
||
const session = new ExecutableDeployItem(); | ||
|
||
session.storedVersionedContractByHash = new StoredVersionedContractByHash( | ||
ContractHash.newContract(contractPackageHash), | ||
'transfer', | ||
args | ||
); | ||
|
||
const payment = ExecutableDeployItem.standardPayment(paymentAmount); | ||
|
||
const deployHeader = DeployHeader.default(); | ||
deployHeader.account = senderPublicKey; | ||
deployHeader.chainName = chainName; | ||
deployHeader.ttl = new Duration(ttl); | ||
|
||
return Deploy.makeDeploy(deployHeader, payment, session); | ||
}; | ||
|
||
export const getRuntimeArgsForCep78Transfer = ({ | ||
tokenHash, | ||
tokenId, | ||
recipientPublicKeyHex, | ||
senderPublicKeyHex | ||
}: Pick< | ||
IMakeNftTransferDeployParams, | ||
'tokenId' | 'recipientPublicKeyHex' | 'tokenHash' | 'senderPublicKeyHex' | ||
>) => { | ||
const runtimeArgs = Args.fromMap({ | ||
target_key: CLValue.newCLKey(Key.createByType(PublicKey.fromHex(recipientPublicKeyHex).accountHash().toPrefixedString(), KeyTypeID.Account)), | ||
source_key: CLValue.newCLKey(Key.createByType(PublicKey.fromHex(senderPublicKeyHex).accountHash().toPrefixedString(), KeyTypeID.Account)) | ||
}); | ||
|
||
if (tokenId) { | ||
runtimeArgs.insert( | ||
'is_hash_identifier_mode', | ||
CLValueBool.fromBoolean(false) | ||
); | ||
runtimeArgs.insert('token_id', CLValueUInt64.newCLUint64(tokenId)); | ||
} | ||
|
||
if (tokenHash) { | ||
runtimeArgs.insert( | ||
'is_hash_identifier_mode', | ||
CLValueBool.fromBoolean(true) | ||
); | ||
runtimeArgs.insert('token_id', CLValueUInt64.newCLUint64(tokenHash)); | ||
} | ||
|
||
return runtimeArgs; | ||
}; | ||
|
||
export function getRuntimeArgsForCep47Transfer({ | ||
tokenId, | ||
recipientPublicKeyHex | ||
}: Required< | ||
Pick<IMakeNftTransferDeployParams, 'tokenId' | 'recipientPublicKeyHex'> | ||
>) { | ||
return Args.fromMap({ | ||
recipient: CLValue.newCLKey(Key.createByType(PublicKey.fromHex(recipientPublicKeyHex).accountHash().toPrefixedString(), KeyTypeID.Account)), | ||
token_ids: CLValueList.newCLList(CLTypeUInt256, [ | ||
CLValueUInt256.newCLUInt256(tokenId) | ||
]) | ||
}); | ||
} |
Oops, something went wrong.