Skip to content

Commit

Permalink
started the sc intent factory
Browse files Browse the repository at this point in the history
  • Loading branch information
popenta committed Sep 1, 2023
1 parent b2e6b92 commit 2099f3d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ export const ESDTNFT_TRANSFER_FUNCTION_NAME = "ESDTNFTTransfer";
export const MULTI_ESDTNFT_TRANSFER_FUNCTION_NAME = "MultiESDTNFTTransfer";
export const ESDT_TRANSFER_VALUE = "0";
export const ARGUMENTS_SEPARATOR = "@";
export const VM_TYPE_WASM_VM = new Uint8Array([0x05, 0x00]);
export const CONTRACT_DEPLOY_ADDRESS = "erd1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq6gq4hu"

5 changes: 1 addition & 4 deletions src/tokenOperations/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ export function bigIntToHex(value: BigNumber.Value): string {
return contractsCodecUtils.getHexMagnitudeOfBigInt(value);
}

export function utf8ToHex(value: string) {
const hex = Buffer.from(value).toString("hex");
return codecUtils.zeroPadStringIfOddLength(hex);
}
export { utf8ToHex } from "../utils.codec";

export function bufferToHex(value: Buffer) {
const hex = value.toString("hex");
Expand Down
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;
}
}
5 changes: 5 additions & 0 deletions src/utils.codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ export function zeroPadStringIfOddLength(input: string): string {

return input;
}

export function utf8ToHex(value: string) {
const hex = Buffer.from(value).toString("hex");
return zeroPadStringIfOddLength(hex);
}

0 comments on commit 2099f3d

Please sign in to comment.