Skip to content

Commit

Permalink
fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
popenta committed Oct 26, 2023
1 parent 6915aac commit 87dd278
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 134 deletions.
10 changes: 7 additions & 3 deletions src/smartcontracts/smartContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { EndpointDefinition, TypedValue } from "./typesystem";
import { SmartContractTransactionsFactory } from "../transactionsFactories/smartContractTransactionsFactory";
import { TransactionsFactoryConfig } from "../transactionsFactories/transactionsFactoryConfig";
import { TRANSACTION_MIN_GAS_PRICE } from "../constants";
import { TokenComputer } from "../tokens";
const createKeccakHash = require("keccak");

interface IAbi {
Expand Down Expand Up @@ -118,7 +119,8 @@ export class SmartContract implements ISmartContract {
const config = new TransactionsFactoryConfig(chainID.valueOf());
const scDraftTransactionFactory = new SmartContractTransactionsFactory({
config: config,
abi: this.abi
abi: this.abi,
tokenComputer: new TokenComputer()
});

const bytecode = Buffer.from(code.toString(), 'hex');
Expand Down Expand Up @@ -177,7 +179,8 @@ export class SmartContract implements ISmartContract {
const config = new TransactionsFactoryConfig(chainID.valueOf());
const scDraftTransactionFactory = new SmartContractTransactionsFactory({
config: config,
abi: this.abi
abi: this.abi,
tokenComputer: new TokenComputer()
});

const bytecode = Uint8Array.from(Buffer.from(code.toString(), 'hex'));
Expand Down Expand Up @@ -214,7 +217,8 @@ export class SmartContract implements ISmartContract {
const config = new TransactionsFactoryConfig(chainID.valueOf());
const scDraftTransactionFactory = new SmartContractTransactionsFactory({
config: config,
abi: this.abi
abi: this.abi,
tokenComputer: new TokenComputer()
});

args = args || [];
Expand Down
4 changes: 1 addition & 3 deletions src/smartcontracts/transactionPayloadBuilders.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@

import { TransactionPayload } from "../transactionPayload";
import { guardValueIsSet } from "../utils";
import { ArgSerializer } from "./argSerializer";
import { ICode, ICodeMetadata, IContractFunction } from "./interface";
import { TypedValue } from "./typesystem";


/**
* @deprecated This constant should not be used. It should be replaced with "WasmVirtualMachine"
* @deprecated This constant should not be used. Use {@link WasmVirtualMachine} instead.
*/
export const ArwenVirtualMachine = "0500";
export const WasmVirtualMachine = "0500";
Expand Down
6 changes: 3 additions & 3 deletions src/tokens.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Token, TokenTransfer, TokenComputer } from "./tokens";
import { Token, TokenComputer } from "./tokens";
import { assert } from "chai";

describe("test token computer", async () => {
Expand All @@ -20,7 +20,7 @@ describe("test token computer", async () => {
const fungibleTokenIdentifier = "FNG-123456";
nonce = tokenComputer.extractNonceFromExtendedIdentifier(fungibleTokenIdentifier);
assert.equal(nonce, 0);
})
});

it("should extract identifier from extended identifier", async () => {
const extendedIdentifier = "TEST-123456-0a";
Expand All @@ -30,5 +30,5 @@ describe("test token computer", async () => {
const fungibleTokenIdentifier = "FNG-123456";
identifier = tokenComputer.extractIdentifierFromExtendedIdentifier(fungibleTokenIdentifier);
assert.equal(identifier, "FNG-123456");
})
});
});
38 changes: 23 additions & 15 deletions src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ export class Token {
}
}

export class TokenTransfer {
token: Token;
amount: BigNumber.Value

constructor(token: Token, amount: BigNumber.Value) {
this.token = token;
this.amount = amount;
export namespace NewTokenTransfer {
export class TokenTransfer {
token: Token;
amount: BigNumber.Value;

constructor(token: Token, amount: BigNumber.Value) {
this.token = token;
this.amount = amount;
}
}
}

export class TokenComputer {
constructor() { }
constructor() {}

isFungible(token: Token): boolean {
return token.nonce === 0;
Expand All @@ -39,7 +41,7 @@ export class TokenComputer {
return 0;
}

const hexNonce = Buffer.from(parts[2], 'hex');
const hexNonce = Buffer.from(parts[2], "hex");
return decodeUnsignedNumber(hexNonce);
}

Expand All @@ -55,11 +57,14 @@ export class TokenComputer {

private checkIfExtendedIdentifierWasProvided(tokenParts: string[]): void {
// this is for the identifiers of fungible tokens
const MIN_EXTENDED_IDENTIFIER_LENGTH_IF_SPLITTED = 2
const MIN_EXTENDED_IDENTIFIER_LENGTH_IF_SPLITTED = 2;
// this is for the identifiers of nft, sft and meta-esdt
const MAX_EXTENDED_IDENTIFIER_LENGTH_IF_SPLITTED = 3
const MAX_EXTENDED_IDENTIFIER_LENGTH_IF_SPLITTED = 3;

if (tokenParts.length < MIN_EXTENDED_IDENTIFIER_LENGTH_IF_SPLITTED || tokenParts.length > MAX_EXTENDED_IDENTIFIER_LENGTH_IF_SPLITTED) {
if (
tokenParts.length < MIN_EXTENDED_IDENTIFIER_LENGTH_IF_SPLITTED ||
tokenParts.length > MAX_EXTENDED_IDENTIFIER_LENGTH_IF_SPLITTED
) {
throw new ErrInvalidTokenIdentifier("Invalid extended token identifier provided");
}
}
Expand All @@ -68,7 +73,9 @@ export class TokenComputer {
const TOKEN_RANDOM_SEQUENCE_LENGTH = 6;

if (randomSequence.length !== TOKEN_RANDOM_SEQUENCE_LENGTH) {
throw new ErrInvalidTokenIdentifier("The identifier is not valid. The random sequence does not have the right length");
throw new ErrInvalidTokenIdentifier(
"The identifier is not valid. The random sequence does not have the right length"
);
}
}

Expand All @@ -77,7 +84,9 @@ export class TokenComputer {
const MAX_TICKER_LENGTH = 10;

if (ticker.length < MIN_TICKER_LENGTH || ticker.length > MAX_TICKER_LENGTH) {
throw new ErrInvalidTokenIdentifier(`The token ticker should be between ${MIN_TICKER_LENGTH} and ${MAX_TICKER_LENGTH} characters`);
throw new ErrInvalidTokenIdentifier(
`The token ticker should be between ${MIN_TICKER_LENGTH} and ${MAX_TICKER_LENGTH} characters`
);
}

if (!ticker.match(/^[a-zA-Z0-9]+$/)) {
Expand All @@ -93,4 +102,3 @@ export class TokenComputer {
function decodeUnsignedNumber(arg: Buffer): number {
return arg.readUIntBE(0, arg.length);
}

Loading

0 comments on commit 87dd278

Please sign in to comment.