-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
86 additions
and
4 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
77 changes: 77 additions & 0 deletions
77
src/transactionIntentsFactories/smartContractTransactionIntentsFactory.ts
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,77 @@ | ||
import { BigNumber } from "bignumber.js"; | ||
import { IAddress } from "../interface"; | ||
import { TransactionIntent } from "../transactionIntent"; | ||
import { AbiRegistry, ArgSerializer, CodeMetadata, TypedValue } from "../smartcontracts"; | ||
import { utf8ToHex } from "../utils.codec"; | ||
import { CONTRACT_DEPLOY_ADDRESS, VM_TYPE_WASM_VM } from "../constants"; | ||
import { NativeSerializer } from "../smartcontracts/nativeSerializer"; | ||
import { Err } from "../errors"; | ||
import { Address } from "../address"; | ||
import { TransactionIntentBuilder } from "./transactionIntentBuilder"; | ||
|
||
interface Config { | ||
chainID: string; | ||
minGasLimit: BigNumber.Value; | ||
gasLimitPerByte: BigNumber.Value; | ||
} | ||
|
||
export class SmartContractTransactionIntentsFactory { | ||
private config: Config; | ||
private abiRegistry?: AbiRegistry; | ||
|
||
constructor(config: Config, abi?: AbiRegistry) { | ||
this.config = config; | ||
this.abiRegistry = abi; | ||
} | ||
|
||
createTransactionIntentForDeploy( | ||
sender: IAddress, | ||
bytecode: Uint8Array, | ||
gasLimit: BigNumber.Value, | ||
args: any[], | ||
isUpgradeable: boolean = true, | ||
isReadable: boolean = true, | ||
isPayable: boolean = false, | ||
isPayableBySmartContract: boolean = false | ||
): TransactionIntent { | ||
const metadata = new CodeMetadata(isUpgradeable, isReadable, isPayable, isPayableBySmartContract); | ||
let parts = [ | ||
utf8ToHex(bytecode.toString()), | ||
utf8ToHex(VM_TYPE_WASM_VM.toString()), | ||
metadata.toString() | ||
]; | ||
|
||
parts = parts.concat(this.argsToStrings(args)); | ||
|
||
return new TransactionIntentBuilder( | ||
this.config, | ||
sender, | ||
Address.fromBech32(CONTRACT_DEPLOY_ADDRESS), | ||
parts, | ||
gasLimit | ||
).build() | ||
} | ||
|
||
private argsToStrings(args: any): string[] { | ||
if (this.abiRegistry !== undefined) { | ||
const constructorDefinition = this.abiRegistry.constructorDefinition | ||
const typedArgs = NativeSerializer.nativeToTypedValues(args, constructorDefinition) | ||
return new ArgSerializer().valuesToStrings(args); | ||
} | ||
|
||
if (this.areArgsOfTypedValue(args)) { | ||
return new ArgSerializer().valuesToStrings(args); | ||
} | ||
|
||
throw new Err("Can't convert args to TypedValues"); | ||
} | ||
|
||
private areArgsOfTypedValue(args: any[]): boolean { | ||
for (const arg of args) { | ||
if (!(arg instanceof TypedValue)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
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