From 485e98df66ee94d48b7cf6dcfe275ee95f9d4aac Mon Sep 17 00:00:00 2001 From: Andrei Bancioiu Date: Sat, 19 Mar 2022 00:32:23 +0200 Subject: [PATCH 1/2] Remove deprecated code. --- src/smartcontracts/elrondERC20client.ts | 13 --- src/smartcontracts/erc20client.spec.ts | 118 ------------------------ src/smartcontracts/erc20client.ts | 95 ------------------- 3 files changed, 226 deletions(-) delete mode 100644 src/smartcontracts/elrondERC20client.ts delete mode 100644 src/smartcontracts/erc20client.spec.ts delete mode 100644 src/smartcontracts/erc20client.ts diff --git a/src/smartcontracts/elrondERC20client.ts b/src/smartcontracts/elrondERC20client.ts deleted file mode 100644 index bd6c8050..00000000 --- a/src/smartcontracts/elrondERC20client.ts +++ /dev/null @@ -1,13 +0,0 @@ -// import { ERC20Client } from "./interface"; -// import { Provider } from "../interface"; -// import { BasicERC20Client } from "./erc20client"; -// import { Address } from "../address"; -// import { Account } from "../account"; - -// export class ElrondERC20Client extends BasicERC20Client implements ERC20Client { -// constructor(provider: Provider | null, scAddress: Address, user: Account, chainID: string, version: number) { -// super(provider, scAddress, user, chainID, version); -// this.functionName_transfer = "transferToken"; -// } -// } - diff --git a/src/smartcontracts/erc20client.spec.ts b/src/smartcontracts/erc20client.spec.ts deleted file mode 100644 index 39d6f477..00000000 --- a/src/smartcontracts/erc20client.spec.ts +++ /dev/null @@ -1,118 +0,0 @@ - -// describe.skip("ERC20 client", () => { -// it("should transferToken", async () => { -// let txgen = getTxGenConfiguration(); -// assert.ok(txgen.accounts.length >= 3, "not enough accounts in txgen"); - -// const proxy: Provider = new ElrondProxy({ -// url: DEFAULT_PROXY_ADDRESS, -// timeout: 1000 -// }); - -// const user = await proxy.getAccount(txgen.accounts[1].pubKey); -// const sender = user; -// user.setKeysFromRawData(txgen.accounts[1]); - -// const receiver = await proxy.getAccount(txgen.accounts[2].pubKey); -// receiver.setKeysFromRawData(txgen.accounts[2]); - -// let chainID = await proxy.getChainID(); -// let version = await proxy.getMinTransactionVersion() - -// let scAddress = new Address(txgen.scAddress); -// let erc20 = new ElrondERC20Client(proxy, scAddress, user, chainID, version); -// erc20.enableSigning(true); - -// erc20.setGasPrice(100000000000000); -// erc20.setGasLimit(7e6); -// let call = await erc20.transfer(receiver.getAddressObject().hex(), BigInt(25)); -// console.log("erc20 transfer status:", call.getStatus()); -// }); - -// it("should interact with an ERC20 smartcontract properly", async () => { -// let txgen = getTxGenConfiguration(); -// assert.ok(txgen.accounts.length >= 3, "not enough accounts in txgen"); - -// const proxy: Provider = new ElrondProxy({ -// url: DEFAULT_PROXY_ADDRESS, -// timeout: 1000 -// }); - -// const user = await proxy.getAccount(txgen.accounts[1].pubKey); -// const sender = user; -// user.setKeysFromRawData(txgen.accounts[1]); - -// let chainID = await proxy.getChainID(); -// let version = await proxy.getMinTransactionVersion() - -// let scAddress = new Address(txgen.scAddress); -// let erc20 = new ElrondERC20Client(proxy, scAddress, user, chainID, version); -// erc20.enableSigning(true); - -// const receiver = await proxy.getAccount(txgen.accounts[2].pubKey); -// receiver.setKeysFromRawData(txgen.accounts[2]); - - -// // Query total supply -// let totalSupply = await erc20.totalSupply(); -// console.log(totalSupply); - -// console.log("address of sender:\t", sender.getAddress()); -// console.log("address of receiver:\t", receiver.getAddress()); - -// console.log("address (hex) of sender:\t", sender.getAddressObject().hex()); -// console.log("address (hex) of receiver:\t", receiver.getAddressObject().hex()); - -// // Query the token balance of the accounts -// let balanceOfSender = await erc20.balanceOf(sender.getAddressObject().hex()); -// console.log("balance of sender:\t", balanceOfSender); - -// let balanceOfReceiver = await erc20.balanceOf(receiver.getAddressObject().hex()); -// console.log("balance of receiver:\t", balanceOfReceiver); - -// // Send some tokens -// console.log("performing ERC20 transfer"); -// let initialDiff = Math.abs(Number(balanceOfSender - balanceOfReceiver)); -// console.log('initialDiff:\t', initialDiff); - -// let transferValue = 25; -// erc20.setGasPrice(100000000000000); -// erc20.setGasLimit(7e6); -// let call = await erc20.transfer(receiver.getAddressObject().hex(), BigInt(transferValue)); -// console.log("erc20 transfer status:", call.getStatus()); - -// // Verify transfer -// balanceOfSender = await erc20.balanceOf(sender.getAddressObject().hex()); -// balanceOfReceiver = await erc20.balanceOf(receiver.getAddressObject().hex()); -// let diff = Math.abs(Number(balanceOfSender - balanceOfReceiver)); -// console.log("balance of sender:\t", balanceOfSender); -// console.log("balance of receiver:\t", balanceOfReceiver); -// console.log("difference:\t", diff); -// assert.equal( -// Number(initialDiff + 2 * transferValue), -// diff -// ); - -// // Send some tokens back -// console.log("performing ERC20 transfer"); -// erc20 = new ElrondERC20Client(proxy, scAddress, receiver, chainID, version); -// erc20.enableSigning(true); - -// erc20.setGasPrice(100000000000000); -// erc20.setGasLimit(7e6); -// call = await erc20.transfer(sender.getAddressObject().hex(), BigInt(transferValue)); -// console.log("erc20 transfer status:", call.getStatus()); - -// // Verify transfer -// balanceOfSender = await erc20.balanceOf(sender.getAddressObject().hex()); -// balanceOfReceiver = await erc20.balanceOf(receiver.getAddressObject().hex()); -// diff = Math.abs(Number(balanceOfSender - balanceOfReceiver)); -// console.log("balance of sender:\t", balanceOfSender); -// console.log("balance of receiver:\t", balanceOfReceiver); -// console.log("difference:\t", diff); -// assert.equal( -// Number(initialDiff), -// Math.abs(Number(balanceOfSender - balanceOfReceiver)) -// ); -// }); -// }); diff --git a/src/smartcontracts/erc20client.ts b/src/smartcontracts/erc20client.ts deleted file mode 100644 index a7caff49..00000000 --- a/src/smartcontracts/erc20client.ts +++ /dev/null @@ -1,95 +0,0 @@ -// import { ERC20Client } from "./interface"; -// import { SmartContractBase } from "./smartContract"; -// import { SmartContractCall } from "./scCall"; -// import * as errors from "../errors"; - -// export class BasicERC20Client extends SmartContractBase implements ERC20Client { -// protected defaultDecimals = 18; -// protected functionName_totalSupply = "totalSupply"; -// protected functionName_balanceOf = "balanceOf"; -// protected functionName_transfer = "transfer"; - -// public name(): string { -// return ""; -// } - -// public symbol(): string { -// return ""; -// } - -// public decimals(): number { -// return this.defaultDecimals; -// } - -// public async totalSupply(): Promise { -// // TODO clean this up and rely on this.performCall(), once the -// // providers can return SmartContractResults -// if (this.provider == null) { -// throw errors.ErrProviderNotSet; -// } -// if (this.scAddress == null) { -// throw errors.ErrSCAddressNotSet; -// } - -// let result = await this.provider.getVMValueInt( -// this.scAddress.toString(), -// this.functionName_totalSupply, -// [] -// ); - -// return result; -// } - -// public async balanceOf(address: string): Promise { -// // TODO clean this up and rely on this.performCall(), once the -// // providers can return SmartContractResults -// if (this.provider == null) { -// throw errors.ErrProviderNotSet; -// } -// if (this.scAddress == null) { -// throw errors.ErrSCAddressNotSet; -// } -// address; - -// let result = await this.provider.getVMValueInt( -// this.scAddress.toString(), -// this.functionName_balanceOf, -// [address] -// ); - -// return result; -// } - -// public async transfer(receiver: string, value: bigint): Promise { -// let call = new SmartContractCall(); -// call.setFunctionName(this.functionName_transfer); -// call.addRawArgument(receiver); -// call.addBigIntArgument(value); - -// try { -// call = await this.performCall(call); -// } catch(err) { -// console.error(err); -// } -// return call; -// } - -// public transferFrom(sender: string, receiver: string, value: bigint): Promise { -// sender; -// receiver; -// value; -// throw new Error("Method not implemented."); -// } - -// public approve(spender: string, value: bigint): Promise { -// spender; -// value; -// throw new Error("Method not implemented."); -// } - -// public allowance(owner: string, spender: string): Promise { -// owner; -// spender; -// throw new Error("Method not implemented."); -// } -// } From c43eca76bce1315e4ae3d41fd1cb5deb8e32f3a7 Mon Sep 17 00:00:00 2001 From: Andrei Bancioiu Date: Sun, 20 Mar 2022 20:16:04 +0200 Subject: [PATCH 2/2] Remove code not used. --- src/smartcontracts/codec/binary.spec.ts | 62 +------------------------ src/smartcontracts/codec/utils.ts | 35 -------------- 2 files changed, 1 insertion(+), 96 deletions(-) diff --git a/src/smartcontracts/codec/binary.spec.ts b/src/smartcontracts/codec/binary.spec.ts index b7dbf9b1..278c681d 100644 --- a/src/smartcontracts/codec/binary.spec.ts +++ b/src/smartcontracts/codec/binary.spec.ts @@ -1,7 +1,7 @@ import { assert } from "chai"; import { BinaryCodec, BinaryCodecConstraints } from "./binary"; import { AddressType, AddressValue, BigIntType, BigUIntType, BigUIntValue, BooleanType, BooleanValue, I16Type, I32Type, I64Type, I8Type, NumericalType, NumericalValue, Struct, Field, StructType, TypedValue, U16Type, U32Type, U32Value, U64Type, U64Value, U8Type, U8Value, List, ListType, EnumType, EnumVariantDefinition, EnumValue, ArrayVec, ArrayVecType, U16Value, TokenIdentifierType, TokenIdentifierValue, StringValue, StringType } from "../typesystem"; -import { discardSuperfluousBytesInTwosComplement, discardSuperfluousZeroBytes, isMsbOne } from "./utils"; +import { isMsbOne } from "./utils"; import { Address } from "../../address"; import { Balance } from "../../balance"; import { BytesType, BytesValue } from "../typesystem/bytes"; @@ -399,64 +399,4 @@ describe("test codec utilities", () => { buffer.writeBigInt64BE(BigInt("9223372036854775807")); assert.isFalse(isMsbOne(buffer)); }); - - it("should discardSuperfluousZeroBytes", async () => { - let buffer: Buffer; - - buffer = discardSuperfluousZeroBytes(Buffer.from([0, 0, 0, 1, 2, 3, 4, 5])); - assert.deepEqual(buffer, Buffer.from([1, 2, 3, 4, 5])); - assert.equal(buffer.toString("hex"), "0102030405"); - - buffer = discardSuperfluousZeroBytes(Buffer.from([0, 0])); - assert.deepEqual(buffer, Buffer.from([])); - assert.equal(buffer.toString("hex"), ""); - - buffer = discardSuperfluousZeroBytes(Buffer.from([5, 0, 0])); - assert.deepEqual(buffer, Buffer.from([5, 0, 0])); - assert.equal(buffer.toString("hex"), "050000"); - }); - - it("should discardSuperfluousBytesInTwosComplement", async () => { - let buffer: Buffer; - - // Negative, -1 - buffer = Buffer.alloc(1); - buffer.writeInt8(-1); - assert.deepEqual(buffer, Buffer.from([0xFF])); - assert.deepEqual(discardSuperfluousBytesInTwosComplement(buffer), Buffer.from([0xFF])); - - buffer = Buffer.alloc(2); - buffer.writeInt16BE(-1); - assert.deepEqual(buffer, Buffer.from([0xFF, 0xFF])); - assert.deepEqual(discardSuperfluousBytesInTwosComplement(buffer), Buffer.from([0xFF])); - - buffer = Buffer.alloc(4); - buffer.writeInt32BE(-1); - assert.deepEqual(buffer, Buffer.from([0xFF, 0xFF, 0xFF, 0xFF])); - assert.deepEqual(discardSuperfluousBytesInTwosComplement(buffer), Buffer.from([0xFF])); - - buffer = Buffer.alloc(8); - buffer.writeBigInt64BE(BigInt("-1")); - assert.deepEqual(buffer, Buffer.from([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])); - assert.deepEqual(discardSuperfluousBytesInTwosComplement(buffer), Buffer.from([0xFF])); - - // Negative, other - buffer = Buffer.from([0b10000000]); - assert.deepEqual(discardSuperfluousBytesInTwosComplement(buffer), Buffer.from([0b10000000])); - - buffer = Buffer.from([0b11111111, 0b00000000]); - assert.deepEqual(discardSuperfluousBytesInTwosComplement(buffer), Buffer.from([0b11111111, 0b00000000])); - - buffer = Buffer.from([0b11111111, 0b10000000]); - assert.deepEqual(discardSuperfluousBytesInTwosComplement(buffer), Buffer.from([0b10000000])); - - // Positive - buffer = Buffer.alloc(1); - buffer.writeInt8(127); - assert.deepEqual(buffer, Buffer.from([0x7F])); - assert.deepEqual(discardSuperfluousBytesInTwosComplement(buffer), Buffer.from([0x7F])); - - assert.deepEqual(discardSuperfluousBytesInTwosComplement(Buffer.from([0x00, 0x00, 0xFF])), Buffer.from([0x00, 0xFF])); - assert.deepEqual(discardSuperfluousBytesInTwosComplement(Buffer.from([0x00, 0x00, 0x7F])), Buffer.from([0x7F])); - }); }); diff --git a/src/smartcontracts/codec/utils.ts b/src/smartcontracts/codec/utils.ts index 7161b3a4..5565ac7d 100644 --- a/src/smartcontracts/codec/utils.ts +++ b/src/smartcontracts/codec/utils.ts @@ -61,38 +61,3 @@ export function flipBufferBitsInPlace(buffer: Buffer) { export function prependByteToBuffer(buffer: Buffer, byte: number) { return Buffer.concat([Buffer.from([byte]), buffer]); } - - -/** - * Discards the leading bytes that are merely a padding of the leading sign bit (but keeps the payload). - * @param buffer A number, represented as a sequence of bytes (big-endian) - */ -export function discardSuperfluousBytesInTwosComplement(buffer: Buffer): Buffer { - let isNegative = isMsbOne(buffer, 0); - let signPadding: number = isNegative ? 0xFF : 0x00; - - let index; - for (index = 0; index < buffer.length - 1; index++) { - let isPaddingByte = buffer[index] == signPadding; - let hasSignBitOnNextByte = isMsbOne(buffer, index + 1) === isNegative; - if (isPaddingByte && hasSignBitOnNextByte) { - continue; - } - - break; - } - - return buffer.slice(index); -} - -/** - * Discards the leading zero bytes. - * @param buffer A number, represented as a sequence of bytes (big-endian) - */ -export function discardSuperfluousZeroBytes(buffer: Buffer): Buffer { - let index; - for (index = 0; index < buffer.length && buffer[index] == 0; index++) { - } - - return buffer.slice(index); -}