import { TransactionRequest, ethers } from "ethers"; import { RippinClient } from "../client"; import { CreateDeploySftContractTxResponse, CreateSetErc1155UriTxResponse, EvmKeyManagement, EvmPlugin, ProjectEthKeyResponse, } from "./types"; import objectToQueryString from "../utils/objectToQueryString"; import { SendTransactionResponse } from "../types"; export class Evm { /** @internal */ _rippin: RippinClient; /** Defaults to RIPPIN KMS. This is overwritten by an EvmProjectSigner once initialized. */ keyManagementType: EvmKeyManagement; constructor(rippin: RippinClient) { this._rippin = rippin; this.keyManagementType = EvmKeyManagement.RIPPIN; } /** * Initialize an Evm plugin. * @param plugin * @returns */ use = (plugin: EvmPlugin) => { plugin.init(this); return this; }; signTransaction = async (txRequest: TransactionRequest): Promise => { throw new Error("EvmProjectSigner has not been initialized"); }; /** * This is only used for development purposes. * @internal */ getPrivateKey = async () => { const resp = await this._rippin._fetch( `projects/${this._rippin.projectId}/core_eth_key` ); const json = (await resp.json()) as ProjectEthKeyResponse; return json.coreEthKey; }; /** * Get the *unsigned* TransactionRequest for deploying a new ERC1155 contract. * @param projectId * @param options * @returns */ getDeployTransactionRequest = async ( projectId: string, options: { from?: string; chain_id?: number } = {} ) => { const queryString = objectToQueryString(options); const resp = await this._rippin._fetch( `projects/${projectId}/create_deploy_sft_contract_tx?${queryString}` ); const json = (await resp.json()) as CreateDeploySftContractTxResponse; return JSON.parse(json.tx) as ethers.TransactionRequest; }; postDeploySftContract = async () => { const resp = await this._rippin._fetch( `projects/${this._rippin.projectId}/deploy_sft_contract`, { method: "POST" } ); const json = await resp.json(); return json as SendTransactionResponse; }; /** * Get the *unsigned* TransactionRequest for minting a ERC1155 token. * @param nftId * @returns */ getMintErc1155TransactionRequest = async ( nftId: string, options: { amount: number; to?: string } ) => { const queryString = objectToQueryString(options); const resp = await this._rippin._fetch( `master_nfts/${nftId}/create_erc1155_mint_tx?${queryString}` ); const json = (await resp.json()) as CreateDeploySftContractTxResponse; return JSON.parse(json.tx) as ethers.TransactionRequest; }; /** * Get the *unsigned* TransactionRequest for setting the URI of a ERC1155 token. * @param nftId * @returns */ getSetURITransactionRequest = async (nftId: string) => { const resp = await this._rippin._fetch( `master_nfts/${nftId}/create_set_erc1155_uri_tx` ); const json = (await resp.json()) as CreateSetErc1155UriTxResponse; return JSON.parse(json.tx) as ethers.TransactionRequest; }; /** * Get the *unsigned* TransactionRequest for transfering ERC1155 token(s). * @param nftId * @returns */ getTransferRequest = async ( nftId: string, options: { from?: string; to: string; amount?: number; } ) => { const queryString = objectToQueryString(options); const resp = await this._rippin._fetch( `master_nfts/${nftId}/create_transfer_erc1155_tx?${queryString}` ); const json = (await resp.json()) as CreateSetErc1155UriTxResponse; return JSON.parse(json.tx) as ethers.TransactionRequest; }; deploySftContract = async () => { if ( this.keyManagementType === EvmKeyManagement.EXTERNAL || this.keyManagementType === EvmKeyManagement.BYOK ) { const deployTxRequest = await this.getDeployTransactionRequest( this._rippin.projectId ); const signedTx = await this.signTransaction(deployTxRequest); return this._rippin.sendTransaction({ tx: signedTx, transaction: { blockchain: "polygon", resourceType: "Project", resourceId: this._rippin.projectId, metadata: { type: "deploy", contract: "erc1155", }, }, }); } else if (this.keyManagementType === EvmKeyManagement.RIPPIN) { return this.postDeploySftContract(); } else { throw new Error( `@Rippin/client does not support selected KeyManagementOption` ); } }; postSetErc1155TokenUri = async (nftId: string) => { const resp = await this._rippin._fetch( `master_nfts/${nftId}/set_erc1155_uri`, { method: "POST" } ); const json = await resp.json(); return json as SendTransactionResponse; }; setErc1155TokenUri = async (nftId: string) => { if ( this.keyManagementType === EvmKeyManagement.EXTERNAL || this.keyManagementType === EvmKeyManagement.BYOK ) { const privKey = await this.getPrivateKey(); const wallet = new ethers.Wallet(privKey); const setUriTxRequest = await this.getSetURITransactionRequest(nftId); const signedTx = await wallet.signTransaction(setUriTxRequest); return this._rippin.sendTransaction({ tx: signedTx, transaction: { blockchain: "polygon", resourceType: "MasterNft", resourceId: nftId, metadata: { type: "setURI", contract: "erc1155", }, }, }); } else if (this.keyManagementType === EvmKeyManagement.RIPPIN) { return this.postSetErc1155TokenUri(nftId); } else { throw new Error( `@Rippin/client does not support selected KeyManagementOption` ); } }; postMintErc1155 = async ( nftId: string, options: { amount: number; to?: string } ) => { const resp = await this._rippin._fetch( `master_nfts/${nftId}/mint_erc1155`, { method: "POST", body: options, } ); const json = await resp.json(); return json as SendTransactionResponse; }; mintErc1155 = async ( nftId: string, options: { amount: number; to?: string } ) => { if ( this.keyManagementType === EvmKeyManagement.EXTERNAL || this.keyManagementType === EvmKeyManagement.BYOK ) { const privKey = await this.getPrivateKey(); const wallet = new ethers.Wallet(privKey); const mintTxRequest = await this.getMintErc1155TransactionRequest( nftId, options ); const signedTx = await wallet.signTransaction(mintTxRequest); return this._rippin.sendTransaction({ tx: signedTx, transaction: { blockchain: "polygon", resourceType: "MasterNft", resourceId: nftId, metadata: { type: "mint", contract: "erc1155", }, }, }); } else if (this.keyManagementType === EvmKeyManagement.RIPPIN) { return this.postMintErc1155(nftId, options); } else { throw new Error( `@Rippin/client does not support selected KeyManagementOption` ); } }; postTransferErc1155 = async ( nftId: string, options: { from?: string; to: string; amount?: number; } ) => { const resp = await this._rippin._fetch( `master_nfts/${nftId}/transfer_erc1155`, { method: "POST", body: options, } ); const json = await resp.json(); return json as SendTransactionResponse; }; transferErc1155 = async ( nftId: string, options: { from?: string; to: string; amount?: number; } ) => { if ( this.keyManagementType === EvmKeyManagement.EXTERNAL || this.keyManagementType === EvmKeyManagement.BYOK ) { const privKey = await this.getPrivateKey(); const wallet = new ethers.Wallet(privKey); const setUriTxRequest = await this.getTransferRequest(nftId, options); const signedTx = await wallet.signTransaction(setUriTxRequest); return this._rippin.sendTransaction({ tx: signedTx, transaction: { blockchain: "polygon", resourceType: "MasterNft", resourceId: nftId, metadata: { type: "transfer", contract: "erc1155", }, }, }); } else if (this.keyManagementType === EvmKeyManagement.RIPPIN) { return this.postTransferErc1155(nftId, options); } else { throw new Error( `@Rippin/client does not support selected KeyManagementOption` ); } }; }