Skip to content

Commit

Permalink
started to use sc intents factory in the sc class
Browse files Browse the repository at this point in the history
  • Loading branch information
popenta committed Sep 12, 2023
1 parent 2f261d6 commit a5a141e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 22 deletions.
19 changes: 18 additions & 1 deletion src/smartcontracts/codeMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class CodeMetadata {
private readable: boolean;
private payable: boolean;
private payableBySc: boolean;
private static readonly lengthOfCodeMetadata = 2;

/**
* Creates a metadata object. By default, set the `upgradeable` attribute, and uset all others.
Expand All @@ -22,6 +23,22 @@ export class CodeMetadata {
this.payableBySc = payableBySc
}

static fromBytes(bytes: Uint8Array): CodeMetadata {
if (bytes.length !== this.lengthOfCodeMetadata) {
return new CodeMetadata();
}

const byteZero = bytes[0];
const byteOne = bytes[1];

const upgradeable = (byteZero & ByteZero.Upgradeable) !== 0;
const readable = (byteZero & ByteZero.Readable) !== 0;
const payable = (byteOne & ByteOne.Payable) !== 0;
const payableBySc = (byteOne & ByteOne.PayableBySc) !== 0;

return new CodeMetadata(upgradeable, readable, payable, payableBySc);
}

/**
* Adjust the metadata (the `upgradeable` attribute), when preparing the deployment transaction.
*/
Expand Down Expand Up @@ -49,7 +66,7 @@ export class CodeMetadata {
togglePayableBySc(value: boolean) {
this.payableBySc = value;
}

/**
* Converts the metadata to the protocol-friendly representation.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/smartcontracts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface ISmartContract {
export interface DeployArguments {
code: ICode;
codeMetadata?: ICodeMetadata;
initArguments?: TypedValue[];
initArguments?: any[];
value?: ITransactionValue;
gasLimit: IGasLimit;
gasPrice?: IGasPrice;
Expand All @@ -42,7 +42,7 @@ export interface DeployArguments {
export interface UpgradeArguments {
code: ICode;
codeMetadata?: ICodeMetadata;
initArguments?: TypedValue[];
initArguments?: any[];
value?: ITransactionValue;
gasLimit: IGasLimit;
gasPrice?: IGasPrice;
Expand All @@ -52,7 +52,7 @@ export interface UpgradeArguments {

export interface CallArguments {
func: IContractFunction;
args?: TypedValue[];
args?: any[];
value?: ITransactionValue;
gasLimit: IGasLimit;
receiver?: IAddress;
Expand Down
66 changes: 51 additions & 15 deletions src/smartcontracts/smartContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ import { bigIntToBuffer } from "./codec/utils";
import { CodeMetadata } from "./codeMetadata";
import { ContractFunction } from "./function";
import { Interaction } from "./interaction";
import { CallArguments, DeployArguments, ISmartContract, QueryArguments, UpgradeArguments } from "./interface";
import { CallArguments, DeployArguments, ICodeMetadata, ISmartContract, QueryArguments, UpgradeArguments } from "./interface";
import { NativeSerializer } from "./nativeSerializer";
import { Query } from "./query";
import { ArwenVirtualMachine, ContractCallPayloadBuilder, ContractDeployPayloadBuilder, ContractUpgradePayloadBuilder } from "./transactionPayloadBuilders";
import { ArwenVirtualMachine, ContractCallPayloadBuilder, ContractUpgradePayloadBuilder } from "./transactionPayloadBuilders";
import { EndpointDefinition, TypedValue } from "./typesystem";
import { SmartContractTransactionIntentsFactory } from "../transactionIntentsFactories/smartContractTransactionIntentsFactory";
import { TransactionIntentsFactoryConfig } from "../transactionIntentsFactories/transactionIntentsFactoryConfig";
import { TransactionPayload } from "../transactionPayload";
const createKeccakHash = require("keccak");

interface IAbi {
constructorDefinition: EndpointDefinition;

getEndpoints(): EndpointDefinition[];
getEndpoint(name: string | ContractFunction): EndpointDefinition;
}
Expand Down Expand Up @@ -110,27 +115,58 @@ export class SmartContract implements ISmartContract {
deploy({ deployer, code, codeMetadata, initArguments, value, gasLimit, gasPrice, chainID }: DeployArguments): Transaction {
Compatibility.guardAddressIsSetAndNonZero(deployer, "'deployer' of SmartContract.deploy()", "pass the actual address to deploy()");

codeMetadata = codeMetadata || new CodeMetadata();
initArguments = initArguments || [];
value = value || 0;
const config = new TransactionIntentsFactoryConfig(chainID.valueOf());
const scIntentFactory = new SmartContractTransactionIntentsFactory({
config: config,
abi: this.abi
});

let payload = new ContractDeployPayloadBuilder()
.setCode(code)
.setCodeMetadata(codeMetadata)
.setInitArgs(initArguments)
.build();
const bytecode = Uint8Array.from(Buffer.from(code.toString(), 'hex'));
const metadataAsJson = this.getMetadataPropertiesAsObject(codeMetadata);

let transaction = new Transaction({
receiver: Address.Zero(),
const intent = scIntentFactory.createTransactionIntentForDeploy({
sender: deployer,
bytecode: bytecode,
gasLimit: gasLimit.valueOf(),
args: initArguments,
isUpgradeable: metadataAsJson.upgradeable,
isReadable: metadataAsJson.readable,
isPayable: metadataAsJson.payable,
isPayableBySmartContract: metadataAsJson.payableBySc
});

return new Transaction({
receiver: Address.fromBech32(intent.receiver),
sender: Address.fromBech32(intent.sender),
value: value,
gasLimit: gasLimit,
gasLimit: new BigNumber(intent.gasLimit).toNumber(),
gasPrice: gasPrice,
data: payload,
data: new TransactionPayload(Buffer.from(intent.data!)),
chainID: chainID
});
}

return transaction;
private getMetadataPropertiesAsObject(codeMetadata?: ICodeMetadata): {
upgradeable: boolean,
readable: boolean,
payable: boolean,
payableBySc: boolean
} {
let metadata: CodeMetadata;
if (codeMetadata) {
metadata = CodeMetadata.fromBytes(Buffer.from(codeMetadata.toString(), "hex"));
}
else {
metadata = new CodeMetadata();
}
const metadataAsJson = metadata.toJSON() as {
upgradeable: boolean,
readable: boolean,
payable: boolean,
payableBySc: boolean
};

return metadataAsJson;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BigNumber } from "bignumber.js";
import { IAddress } from "../interface";
import { TransactionIntent } from "../transactionIntent";
import { AbiRegistry, ArgSerializer, CodeMetadata, EndpointDefinition } from "../smartcontracts";
import { AbiRegistry, ArgSerializer, CodeMetadata, ContractFunction, EndpointDefinition } from "../smartcontracts";
import { byteArrayToHex } from "../utils.codec";
import { CONTRACT_DEPLOY_ADDRESS, VM_TYPE_WASM_VM } from "../constants";
import { NativeSerializer } from "../smartcontracts/nativeSerializer";
Expand All @@ -15,16 +15,23 @@ interface Config {
gasLimitPerByte: BigNumber.Value;
}

interface Abi {
constructorDefinition: EndpointDefinition;

getEndpoint(name: string | ContractFunction): EndpointDefinition;
}


export class SmartContractTransactionIntentsFactory {
private readonly config: Config;
private readonly abiRegistry?: AbiRegistry;
private readonly abiRegistry?: Abi;

constructor({
config,
abi
}: {
config: Config;
abi?: AbiRegistry;
abi?: Abi;
}) {
this.config = config;
this.abiRegistry = abi;
Expand Down

0 comments on commit a5a141e

Please sign in to comment.