Skip to content

Commit

Permalink
fixed func name of tokens dapps
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Apr 12, 2024
1 parent afd6edd commit 9fa7d00
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 86 deletions.
11 changes: 6 additions & 5 deletions core/background/background/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class BackgroundTransaction {
}

if (!params.fee) {
params.fee = this.#core.gas.fee;
params.fee = String(this.#core.gas.fee);
}

const prompt = new PromptService(
Expand All @@ -102,7 +102,7 @@ export class BackgroundTransaction {
const confirmParams: ConfirmParams = {
...params,
tokenAmount: String(params.coins ?? 0),
fee: Number(params.fee),
fee: String(params.fee),
maxGas: String(params.maxGas ?? this.#core.gas.state.gasLimit),
recipient: params.toAddr
};
Expand Down Expand Up @@ -254,6 +254,7 @@ export class BackgroundTransaction {
hash,
expiryPeriod,
nextSlot,
func: confirmParams.func,
type: confirmParams.type,
fee: confirmParams.fee,
icon: confirmParams.icon,
Expand Down Expand Up @@ -373,19 +374,19 @@ export class BackgroundTransaction {
case OperationsType.Payment:
return await new PaymentBuild(
confirmParams.fee,
Number(confirmParams.coins),
confirmParams.coins,
confirmParams.toAddr, expiryPeriod
).bytes();
case OperationsType.RollBuy:
return await new BuyRollsBuild(
confirmParams.fee,
Number(confirmParams.coins),
confirmParams.coins,
expiryPeriod
).bytes();
case OperationsType.RollSell:
return await new SellRollsBuild(
confirmParams.fee,
Number(confirmParams.coins),
confirmParams.coins,
expiryPeriod
).bytes();
case OperationsType.ExecuteSC:
Expand Down
104 changes: 52 additions & 52 deletions core/background/provider/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { utils } from "aes-js";
import { toByteArray } from "base64-js";
import { base58Decode, isBase58Address } from "lib/address";
import { assert } from "lib/assert";
import { VarintEncode } from 'lib/varint';
import { varintEncode } from 'lib/varint';
import { INVLID_RECIPIENT, INVLID_ADDRESS_PREFIX } from "./errors";
import { OperationsType } from "./operations";
import { ADDRESS_PREFIX, USER_VERSION_NUMBER, CONTRACT_VERSION_NUMBER, CONTRACT_ADDRESS_PREFIX } from "config/common";
Expand All @@ -16,30 +16,30 @@ import { BaseError } from "lib/error";
export class PaymentBuild {
static operation = OperationsType.Payment;

fee: number;
amount: number;
fee: bigint;
amount: bigint;
recipientAddress: string;
expirePeriod: number;

constructor(
fee: number,
amount: number,
fee: string,
amount: string,
recipientAddress: string,
expirePeriod: number
) {
this.fee = fee;
this.amount = amount;
this.fee = BigInt(fee);
this.amount = BigInt(amount);
this.recipientAddress = recipientAddress;
this.expirePeriod = expirePeriod;
}

async bytes() {
assert(await isBase58Address(this.recipientAddress), INVLID_RECIPIENT);

const fee = new VarintEncode().encode(Number(this.fee));
const expirePeriod = new VarintEncode().encode(this.expirePeriod);
const typeIdEncoded = new VarintEncode().encode(PaymentBuild.operation);
const amount = new VarintEncode().encode(this.amount);
const fee = varintEncode(this.fee);
const expirePeriod = varintEncode(this.expirePeriod);
const typeIdEncoded = varintEncode(PaymentBuild.operation);
const amount = varintEncode(this.amount);
const prefix = this.recipientAddress.slice(0, ADDRESS_PREFIX.length);
let recipient = (await base58Decode(this.recipientAddress.slice(ADDRESS_PREFIX.length)));

Expand All @@ -64,25 +64,25 @@ export class PaymentBuild {
export class BuyRollsBuild {
static operation = OperationsType.RollBuy;

fee: number;
amount: number;
fee: bigint;
amount: bigint;
expirePeriod: number;

constructor(
fee: number,
amount: number,
fee: string,
amount: string,
expirePeriod: number
) {
this.fee = fee;
this.amount = amount;
this.fee = BigInt(fee);
this.amount = BigInt(amount);
this.expirePeriod = expirePeriod;
}

async bytes() {
const fee = new VarintEncode().encode(this.fee);
const expirePeriod = new VarintEncode().encode(this.expirePeriod);
const typeIdEncoded = new VarintEncode().encode(BuyRollsBuild.operation);
const amount = new VarintEncode().encode(this.amount);
const fee = varintEncode(this.fee);
const expirePeriod = varintEncode(this.expirePeriod);
const typeIdEncoded = varintEncode(BuyRollsBuild.operation);
const amount = varintEncode(this.amount);

return Uint8Array.from([
...fee,
Expand All @@ -96,25 +96,25 @@ export class BuyRollsBuild {
export class SellRollsBuild {
static operation = OperationsType.RollSell;

fee: number;
amount: number;
fee: bigint;
amount: bigint;
expirePeriod: number;

constructor(
fee: number,
amount: number,
fee: string,
amount: string,
expirePeriod: number
) {
this.fee = fee;
this.amount = amount;
this.fee = BigInt(fee);
this.amount = BigInt(amount);
this.expirePeriod = expirePeriod;
}

async bytes() {
const fee = new VarintEncode().encode(this.fee);
const expirePeriod = new VarintEncode().encode(this.expirePeriod);
const typeIdEncoded = new VarintEncode().encode(SellRollsBuild.operation);
const amount = new VarintEncode().encode(this.amount);
const fee = varintEncode(this.fee);
const expirePeriod = varintEncode(this.expirePeriod);
const typeIdEncoded = varintEncode(SellRollsBuild.operation);
const amount = varintEncode(this.amount);

return Uint8Array.from([
...fee,
Expand Down Expand Up @@ -207,20 +207,20 @@ export class ExecuteSmartContractBuild {
}

bytes() {
const feeEncoded = new VarintEncode().encode(Number(this.fee)); // TODO: Replace encode to bigint.
const maxGasEncoded = new VarintEncode().encode(Number(this.maxGas)); // TODO: Replace encode to bigint.
const maxCoinEncoded = new VarintEncode().encode(Number(this.maxCoins)); // TODO: Replace encode to bigint.
const expirePeriodEncoded = new VarintEncode().encode(this.expirePeriod);
const typeIdEncoded = new VarintEncode().encode(ExecuteSmartContractBuild.operation);
const dataLengthEncoded = new VarintEncode().encode(this.deployer.length);
const feeEncoded = varintEncode(this.fee);
const maxGasEncoded = varintEncode(this.maxGas);
const maxCoinEncoded = varintEncode(this.maxCoins);
const expirePeriodEncoded = varintEncode(this.expirePeriod);
const typeIdEncoded = varintEncode(ExecuteSmartContractBuild.operation);
const dataLengthEncoded = varintEncode(this.deployer.length);

// smart contract operation datastore
const datastoreKeyMap = this.datastore;
let datastoreSerializedBuffer = new Uint8Array();

for (const [key, value] of datastoreKeyMap) {
const encodedKeyLen = new VarintEncode().encode(key.length);
const encodedValueLen = new VarintEncode().encode(value.length);
const encodedKeyLen = varintEncode(key.length);
const encodedValueLen = varintEncode(value.length);

datastoreSerializedBuffer = Uint8Array.from([
...datastoreSerializedBuffer,
Expand All @@ -231,7 +231,7 @@ export class ExecuteSmartContractBuild {
]);
}

const datastoreSerializedBufferLen = new VarintEncode().encode(this.datastore.size);
const datastoreSerializedBufferLen = varintEncode(this.datastore.size);

return Uint8Array.from([
...feeEncoded,
Expand All @@ -252,8 +252,8 @@ export class CallSmartContractBuild {

functionName: string;
maxGas: bigint;
coins: number;
fee: number;
coins: bigint;
fee: bigint;
targetAddress: string;
expirePeriod: number;

Expand All @@ -262,7 +262,7 @@ export class CallSmartContractBuild {
constructor(
functionName: string,
parameters: CallParam[],
fee: number,
fee: string,
expirePeriod: number,
maxGas: bigint,
coins: string,
Expand All @@ -271,8 +271,8 @@ export class CallSmartContractBuild {
) {
this.functionName = functionName;
this.maxGas = maxGas;
this.coins = Number(coins);
this.fee = fee;
this.coins = BigInt(coins);
this.fee = BigInt(fee);
this.expirePeriod = expirePeriod;
this.targetAddress = targetAddress;

Expand All @@ -288,14 +288,14 @@ export class CallSmartContractBuild {
async bytes() {
assert(await isBase58Address(this.targetAddress), INVLID_RECIPIENT);

const fee = new VarintEncode().encode(this.fee);
const expirePeriod = new VarintEncode().encode(this.expirePeriod);
const typeIdEncoded = new VarintEncode().encode(CallSmartContractBuild.operation);
const coinsEncoded = new VarintEncode().encode(this.coins);
const maxGas = new VarintEncode().encode(Number(this.maxGas));// TODO: Replace encode to bigint.
const fee = varintEncode(this.fee);
const expirePeriod = varintEncode(this.expirePeriod);
const typeIdEncoded = varintEncode(CallSmartContractBuild.operation);
const coinsEncoded = varintEncode(this.coins);
const maxGas = varintEncode(this.maxGas);
const functionNameEncoded = utils.utf8.toBytes(this.functionName);
const functionNameLengthEncoded = new VarintEncode().encode(functionNameEncoded.length);
const parametersLengthEncoded = new VarintEncode().encode(this.serializedArgs.length);
const functionNameLengthEncoded = varintEncode(functionNameEncoded.length);
const parametersLengthEncoded = varintEncode(this.serializedArgs.length);
let targetAddressEncoded = (await base58Decode(this.targetAddress.slice(ADDRESS_PREFIX.length)));

targetAddressEncoded = Uint8Array.from([
Expand Down
16 changes: 10 additions & 6 deletions lib/type/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Arg = string | Array<string | number | null | undefined | unknown> | unknown | null | undefined | object | number;
type Arg = string | Array<string | number | null | undefined | unknown> | unknown | null | undefined | object | number | bigint;

export const TypeOf = Object.freeze({

Expand All @@ -11,7 +11,7 @@ export const TypeOf = Object.freeze({
},

isNumber(argument: Arg) {
return Object.prototype.toString.call(argument) === '[object Number]'
return Object.prototype.toString.call(argument) === '[object Number]'
&& !isNaN(Number(argument));
},

Expand Down Expand Up @@ -43,26 +43,30 @@ export const TypeOf = Object.freeze({
return Object.prototype.toString.call(argument) === '[object Undefined]';
},

isBigInt(argument: Arg) {
return typeof argument === 'bigint';
},

isEmptyObject(argument: Arg) {
if(!this.isObject(argument)) {
if (!this.isObject(argument)) {
return false;
} else {
return Object.getOwnPropertyNames(argument).length === 0;
}
},

isEmptyArray<T>(argument: Array<T>) {
if(!this.isArray(argument)) {
if (!this.isArray(argument)) {
return false;
} else {
return argument.length === 0;
}
},

getType(argument: Arg): string {
if(Number.isNaN(argument)) {
if (Number.isNaN(argument)) {
return 'NaN';
}
return Object.prototype.toString.call(argument).split(' ')[1].slice(0,-1).toLowerCase();
return Object.prototype.toString.call(argument).split(' ')[1].slice(0, -1).toLowerCase();
}
});
2 changes: 0 additions & 2 deletions lib/varint/errors.ts

This file was deleted.

26 changes: 23 additions & 3 deletions lib/varint/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseError } from 'lib/error';
import { RANGE_DECODE, RANGE_ENCODE } from './errors';
import { TypeOf } from 'lib/type';
import { encode } from './unsigned';

const MSB = 0x80;
const REST = 0x7F;
Expand All @@ -25,7 +26,7 @@ export class VarintDecode {
do {
if (counter >= l || shift > 49) {
this.#bytes = 0;
throw new BaseError(RANGE_DECODE);
throw new BaseError("Could not decode varint invalid Range");
}

b = buf[counter++];
Expand Down Expand Up @@ -53,7 +54,7 @@ export class VarintEncode {
encode(num: number, out: number[] = [], offset = 0) {
if (Number.MAX_SAFE_INTEGER && num > Number.MAX_SAFE_INTEGER) {
this.#bytes = 0;
throw new BaseError(RANGE_ENCODE);
throw new BaseError("Could not encode varint invalid Range");
}

const oldOffset = offset;
Expand All @@ -74,3 +75,22 @@ export class VarintEncode {
return Uint8Array.from(out);
}
}

export function varintEncode(data: number | bigint): Uint8Array {
if (TypeOf.isBigInt(data)) {
return encode(data as bigint);
}

return new VarintEncode().encode(Number(data));
}

export function varintDecode(data: Uint8Array): {
value: number
bytes: number
} {
const varint = new VarintDecode();
const value = varint.decode(data);
const bytes = varint.bytes;
return { value, bytes }
}

25 changes: 25 additions & 0 deletions lib/varint/signed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as unsigned from './unsigned'

const oneBI = BigInt(1);
const twoBI = BigInt(2);

export function encodingLength(value: bigint): number {
return unsigned.encodingLength(
value >= 0 ? value * twoBI : value * -twoBI - oneBI
);
}

export function encode(
value: bigint,
buffer?: ArrayBuffer,
byteOffset?: number
): ArrayBuffer {
value = value >= 0 ? value * twoBI : value * -twoBI - oneBI;

return unsigned.encode(value, buffer, byteOffset);
}

export function decode(data: Uint8Array, offset = 0): bigint {
const value = unsigned.decode(data, offset);
return value & oneBI ? (value + oneBI) / -twoBI : value / twoBI;
}
Loading

0 comments on commit 9fa7d00

Please sign in to comment.