UNPKG

@hashgraph/sdk

Version:
3 lines (2 loc) 5.19 kB
import t from"../file/FileCreateTransaction.js";import e from"../file/FileAppendTransaction.js";import i from"../file/FileDeleteTransaction.js";import n from"./ContractCreateTransaction.js";import{encode as r}from"../encoding/utf8.js";import{encode as a}from"../encoding/hex.js";import s from"../PublicKey.js";class c{constructor(){this._bytecode=null,this._contractCreate=new n,this._signerPublicKeys=new Set,this._publicKeys=[],this._transactionSigners=[],this._maxChunks=null}get maxChunks(){return this._maxChunks}setMaxChunks(t){return this._maxChunks=t,this}get bytecode(){return this._bytecode}setBytecode(t){return this._bytecode=t instanceof Uint8Array?t:r(t),this}get adminKey(){return this._contractCreate.adminKey}setAdminKey(t){return this._contractCreate.setAdminKey(t),this}get gas(){return this._contractCreate.gas}setGas(t){return this._contractCreate.setGas(t),this}get initialBalance(){return this._contractCreate.initialBalance}setInitialBalance(t){return this._contractCreate.setInitialBalance(t),this}get proxyAccountId(){return this._contractCreate.proxyAccountId}setProxyAccountId(t){return this._contractCreate.setProxyAccountId(t),this}get autoRenewPeriod(){return this._contractCreate.autoRenewPeriod}setAutoRenewPeriod(t){return this._contractCreate.setAutoRenewPeriod(t),this}get constructorParameters(){return this._contractCreate.constructorParameters}setConstructorParameters(t){return this._contractCreate.setConstructorParameters(t),this}get contractMemo(){return this._contractCreate.contractMemo}setContractMemo(t){return this._contractCreate.setContractMemo(t),this}get maxAutomaticTokenAssociation(){return this._contractCreate.maxAutomaticTokenAssociations}setMaxAutomaticTokenAssociations(t){return this._contractCreate.setMaxAutomaticTokenAssociations(t),this}get stakedAccountId(){return this._contractCreate.stakedAccountId}setStakedAccountId(t){return this._contractCreate.setStakedAccountId(t),this}get stakedNodeId(){return this._contractCreate.stakedNodeId}setStakedNodeId(t){return this._contractCreate.setStakedNodeId(t),this}get declineStakingRewards(){return this._contractCreate.declineStakingRewards}setDeclineStakingReward(t){return this._contractCreate.setDeclineStakingReward(t),this}get autoRenewAccountId(){return this._contractCreate.autoRenewAccountId}setAutoRenewAccountId(t){return this._contractCreate.setAutoRenewAccountId(t),this}sign(t){return this.signWith(t.publicKey,e=>Promise.resolve(t.sign(e)))}signWith(t,e){const i=t.toBytesRaw(),n=a(i);return this._signerPublicKeys.has(n)||(this._publicKeys.push(t),this._transactionSigners.push(e)),this}async execute(n,r){if(null==this._bytecode)throw new Error("cannot create contract with no bytecode");const a=n.operatorPublicKey,s=(new t).setKeys(null!=a?[a]:[]).setContents(this._bytecode.subarray(0,Math.min(this._bytecode.length,2048))).freezeWith(n);await o(s,this._publicKeys,this._transactionSigners);let c=await s.execute(n,r);const h=(await c.getReceipt(n)).fileId;if(this._bytecode.length>2048){const t=(new e).setFileId(h).setContents(this._bytecode.subarray(2048)).freezeWith(n);await o(t,this._publicKeys,this._transactionSigners),await t.execute(n,r)}if(this._contractCreate.setBytecodeFileId(h).freezeWith(n),await o(this._contractCreate,this._publicKeys,this._transactionSigners),c=await this._contractCreate.execute(n,r),await c.getReceipt(n),null!=a){const t=(new i).setFileId(h).freezeWith(n);await o(t,this._publicKeys,this._transactionSigners),await(await t.execute(n,r)).getReceipt(n)}return c}async executeWithSigner(n){if(null==this._bytecode)throw new Error("cannot create contract with no bytecode");if(null==n.getAccountKey)throw new Error("`Signer.getAccountKey()` is not implemented, but is required for `ContractCreateFlow`");const r=await n.getAccountKey();let a;if(r instanceof s)a=r;else{const t=Object.values(r._key._key._keyData),e=new Uint8Array(t);a=s.fromBytes(e)}const c=await(new t).setKeys(null!=a?[a]:[]).setContents(this._bytecode.subarray(0,Math.min(this._bytecode.length,2048))).freezeWithSigner(n);await c.signWithSigner(n),await o(c,this._publicKeys,this._transactionSigners);let h=await c.executeWithSigner(n);const u=(await h.getReceiptWithSigner(n)).fileId;if(this._bytecode.length>2048){let t=(new e).setFileId(u).setContents(this._bytecode.subarray(2048));null!=this._maxChunks&&t.setMaxChunks(this._maxChunks),t=await t.freezeWithSigner(n),await t.signWithSigner(n),await o(t,this._publicKeys,this._transactionSigners),await t.executeWithSigner(n)}if(this._contractCreate=await this._contractCreate.setBytecodeFileId(u).freezeWithSigner(n),this._contractCreate=await this._contractCreate.signWithSigner(n),await o(this._contractCreate,this._publicKeys,this._transactionSigners),h=await this._contractCreate.executeWithSigner(n),await h.getReceiptWithSigner(n),null!=r){const t=await(new i).setFileId(u).freezeWithSigner(n);await t.signWithSigner(n),await o(t,this._publicKeys,this._transactionSigners),await(await t.executeWithSigner(n)).getReceiptWithSigner(n)}return h}}async function o(t,e,i){for(let n=0;n<e.length;n++)await t.signWith(e[n],i[n])}export{c as default}; //# sourceMappingURL=ContractCreateFlow.js.map