Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/types/CommonMessageInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Builder } from "../boc/Builder";
import { Slice } from "../boc/Slice";
import { Maybe } from "../utils/maybe";
import { CurrencyCollection, loadCurrencyCollection, storeCurrencyCollection } from "./CurrencyCollection";
import { loadMessageFlags, MessageFlags, storeMessageFlags } from "./extraFlags";


// Source: https://github.com/ton-blockchain/ton/blob/24dc184a2ea67f9c47042b4104bbb4d82289fac1/crypto/block/block.tlb#L123
Expand All @@ -37,7 +38,7 @@ export type CommonMessageInfoInternal = {
src: Address,
dest: Address,
value: CurrencyCollection,
ihrFee: bigint,
extraFlags: MessageFlags,
forwardFee: bigint,
createdLt: bigint,
createdAt: number
Expand Down Expand Up @@ -69,7 +70,7 @@ export function loadCommonMessageInfo(slice: Slice): CommonMessageInfo {
const src = slice.loadAddress();
const dest = slice.loadAddress();
const value = loadCurrencyCollection(slice);
const ihrFee = slice.loadCoins();
const extraFlags = loadMessageFlags(slice);
const forwardFee = slice.loadCoins();
const createdLt = slice.loadUintBig(64);
const createdAt = slice.loadUint(32);
Expand All @@ -82,7 +83,7 @@ export function loadCommonMessageInfo(slice: Slice): CommonMessageInfo {
src,
dest,
value,
ihrFee,
extraFlags,
forwardFee,
createdLt,
createdAt,
Expand Down Expand Up @@ -128,7 +129,7 @@ export function storeCommonMessageInfo(source: CommonMessageInfo) {
builder.storeAddress(source.src);
builder.storeAddress(source.dest);
builder.store(storeCurrencyCollection(source.value));
builder.storeCoins(source.ihrFee);
builder.store(storeMessageFlags(source.extraFlags));
builder.storeCoins(source.forwardFee);
builder.storeUint(source.createdLt, 64);
builder.storeUint(source.createdAt, 32);
Expand Down
9 changes: 5 additions & 4 deletions src/types/CommonMessageInfoRelaxed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Builder } from "../boc/Builder";
import { Slice } from "../boc/Slice";
import { Maybe } from "../utils/maybe";
import { CurrencyCollection, loadCurrencyCollection, storeCurrencyCollection } from "./CurrencyCollection";
import { loadMessageFlags, MessageFlags, storeMessageFlags } from "./extraFlags";

// Source: https://github.com/ton-blockchain/ton/blob/24dc184a2ea67f9c47042b4104bbb4d82289fac1/crypto/block/block.tlb#L132
// int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool
Expand All @@ -33,7 +34,7 @@ export type CommonMessageInfoRelaxedInternal = {
src?: Maybe<Address>,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good

dest: Address,
value: CurrencyCollection,
ihrFee: bigint,
extraFlags: MessageFlags,
forwardFee: bigint,
createdLt: bigint,
createdAt: number
Expand All @@ -58,7 +59,7 @@ export function loadCommonMessageInfoRelaxed(slice: Slice): CommonMessageInfoRel
const src = slice.loadMaybeAddress();
const dest = slice.loadAddress();
const value = loadCurrencyCollection(slice);
const ihrFee = slice.loadCoins();
const extraFlags = loadMessageFlags(slice);
const forwardFee = slice.loadCoins();
const createdLt = slice.loadUintBig(64);
const createdAt = slice.loadUint(32);
Expand All @@ -71,7 +72,7 @@ export function loadCommonMessageInfoRelaxed(slice: Slice): CommonMessageInfoRel
src,
dest,
value,
ihrFee,
extraFlags,
forwardFee,
createdLt,
createdAt,
Expand Down Expand Up @@ -108,7 +109,7 @@ export function storeCommonMessageInfoRelaxed(source: CommonMessageInfoRelaxed)
builder.storeAddress(source.src);
builder.storeAddress(source.dest);
builder.store(storeCurrencyCollection(source.value));
builder.storeCoins(source.ihrFee);
builder.store(storeMessageFlags(source.extraFlags));
builder.storeCoins(source.forwardFee);
builder.storeUint(source.createdLt, 64);
builder.storeUint(source.createdAt, 32);
Expand Down
23 changes: 23 additions & 0 deletions src/types/Maybe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Builder } from "../boc/Builder";
import { Slice } from "../boc/Slice";
import { Maybe } from "../utils/maybe";

export const loadMaybe = <T>(loadChild: (slice: Slice) => T) => (slice: Slice): Maybe<T> => {
const flag = slice.loadBit();
if (flag) {
return loadChild(slice);
} else {
return undefined;
}
};

export const storeMaybe = <T>(
storeChild: (source: T) => (builder: Builder) => void
) => (source: Maybe<T>) => (builder: Builder): void => {
if (source === undefined || source === null) {
builder.storeBit(false);
} else {
builder.storeBit(true);
storeChild(source)(builder);
}
};
57 changes: 57 additions & 0 deletions src/types/NewBounceBody.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// https://github.com/ton-blockchain/ton/blob/9f328c1d32b1ff826c0dd6d9934f5eb4dc606843/crypto/block/block.tlb#L168
// new_bounce_body#fffffffe
// original_body:^Cell
// original_info:^NewBounceOriginalInfo
// bounced_by_phase:uint8 exit_code:int32
// compute_phase:(Maybe NewBounceComputePhaseInfo)
// = NewBounceBody;

import { beginCell, Builder } from "../boc/Builder";
import { Cell } from "../boc/Cell";
import { Slice } from "../boc/Slice";
import { Maybe } from "../utils/maybe";
import { loadMaybe, storeMaybe } from "./Maybe";
import { loadNewBounceComputePhaseInfo, NewBounceComputePhaseInfo, storeNewBounceComputePhaseInfo } from "./NewBounceComputePhaseInfo";
import { loadNewBounceOriginalInfo, NewBounceOriginalInfo, storeNewBounceOriginalInfo } from "./NewBounceOriginalInfo";

export type NewBounceBody = {
readonly originalBody: Cell;
readonly originalInfo: NewBounceOriginalInfo;
readonly bouncedByPhase: number;
readonly exitCode: number;
readonly computePhase: Maybe<NewBounceComputePhaseInfo>;
}

const loadMaybeNewBounceComputePhaseInfo = loadMaybe(loadNewBounceComputePhaseInfo)

export const loadNewBounceBody = (slice: Slice): NewBounceBody => {
const originalBody = slice.loadRef();
const originalInfo = loadNewBounceOriginalInfo(slice.loadRef().asSlice());
const bouncedByPhase = slice.loadUint(8);
const exitCode = slice.loadInt(32);
const computePhase = loadMaybeNewBounceComputePhaseInfo(slice);

return {
originalBody,
originalInfo,
bouncedByPhase,
exitCode,
computePhase,
};
};

const storeMaybeNewBounceComputePhaseInfo = storeMaybe(storeNewBounceComputePhaseInfo);

export const storeNewBounceBody = (source: NewBounceBody) => {
return (builder: Builder): void => {
builder.storeRef(source.originalBody);
builder.storeRef(
beginCell()
.store(storeNewBounceOriginalInfo(source.originalInfo))
.endCell()
);
builder.storeUint(source.bouncedByPhase, 8);
builder.storeInt(source.exitCode, 32);
builder.store(storeMaybeNewBounceComputePhaseInfo(source.computePhase));
};
};
27 changes: 27 additions & 0 deletions src/types/NewBounceComputePhaseInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Follows [TEP-503](https://github.com/ton-blockchain/TEPs/pull/503/files)
// https://github.com/ton-blockchain/ton/blob/9f328c1d32b1ff826c0dd6d9934f5eb4dc606843/crypto/block/block.tlb#L167
// _ gas_used:uint32 vm_steps:uint32 = NewBounceComputePhaseInfo;

import { Builder } from "../boc/Builder";
import { Slice } from "../boc/Slice";

export type NewBounceComputePhaseInfo = {
readonly gasUsed: number;
readonly vmSteps: number;
}

export const loadNewBounceComputePhaseInfo = (slice: Slice): NewBounceComputePhaseInfo => {
const gasUsed = slice.loadUint(32);
const vmSteps = slice.loadUint(32);
return {
gasUsed,
vmSteps,
};
};

export const storeNewBounceComputePhaseInfo = (source: NewBounceComputePhaseInfo) => {
return (builder: Builder): void => {
builder.storeUint(source.gasUsed, 32);
builder.storeUint(source.vmSteps, 32);
};
};
32 changes: 32 additions & 0 deletions src/types/NewBounceOriginalInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Follows [TEP-503](https://github.com/ton-blockchain/TEPs/pull/503/files)
// https://github.com/ton-blockchain/ton/blob/9f328c1d32b1ff826c0dd6d9934f5eb4dc606843/crypto/block/block.tlb#L166
// _ value:CurrencyCollection created_lt:uint64 created_at:uint32 = NewBounceOriginalInfo;

import { Builder } from "../boc/Builder";
import { Slice } from "../boc/Slice";
import { CurrencyCollection, loadCurrencyCollection, storeCurrencyCollection } from "./CurrencyCollection";

export type NewBounceOriginalInfo = {
readonly value: CurrencyCollection;
readonly createdLt: bigint;
readonly createdAt: number;
}

export const loadNewBounceOriginalInfo = (slice: Slice): NewBounceOriginalInfo => {
const value = loadCurrencyCollection(slice);
const createdLt = slice.loadUintBig(64);
const createdAt = slice.loadUint(32);
return {
value,
createdLt,
createdAt,
};
};

export const storeNewBounceOriginalInfo = (source: NewBounceOriginalInfo) => {
return (builder: Builder): void => {
builder.store(storeCurrencyCollection(source.value));
builder.storeUint(source.createdLt, 64);
builder.storeUint(source.createdAt, 32);
};
};
51 changes: 51 additions & 0 deletions src/types/extraFlags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Follows [TEP-503](https://github.com/ton-blockchain/TEPs/pull/503/files)
*/

import { Builder } from "../boc/Builder";
import { Slice } from "../boc/Slice";

export type MessageFlags = {
format: MessageFormat;
}

export type MessageFormat =
| MessageFormatOld
| MessageFormatNew

export type MessageFormatOld = {
readonly type: "old";
}

export type MessageFormatNew = {
readonly type: "new";
readonly includeBody: boolean;
}

export const loadMessageFlags = (slice: Slice): MessageFlags => {
const extraFlags = slice.loadCoins();
if ((extraFlags & 1n) === 0n) {
return {
format: {
type: "old"
},
};
} else {
return {
format: {
type: "new",
includeBody: (extraFlags & 2n) !== 0n,
},
};
}
};

export const storeMessageFlags = ({ format }: MessageFlags) => {
return (builder: Builder): void => {
if (format.type === 'old') {
builder.storeCoins(0n);
} else {
builder.storeCoins(1n + (format.includeBody ? 2n : 0n));
}
};
};
Loading