Skip to content
Open
2 changes: 2 additions & 0 deletions contracts/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
watch_file shell.nix
use flake .#contracts || use nix
11 changes: 8 additions & 3 deletions contracts/contracts/examples/jetton/onramp_mock.tolk
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// SPDX-License-Identifier: MIT
tolk 1.2

import "@stdlib/common.tolk"
import "../../lib/jetton/jetton_client.tolk"
import "../../lib/jetton/messages.tolk"
import "../../lib/jetton/utils.tolk"
import "../../lib/jetton/jetton-utils.tolk"
import "../../lib/utils.tolk"
Comment thread
krebernisak marked this conversation as resolved.

// OnrampMock contract in Tolk
Expand All @@ -12,6 +14,9 @@ const FEE = 5
const INCORRECT_SENDER_ERROR = 100
const FORWARD_PAYLOAD_REQUIRED_ERROR = 101

// Jetton wallet utilities for Tolk
const JETTON_TOPIC : int = 0x351 // for easier indexing

Comment on lines +17 to +19
struct OnrampMock {
JettonClient: JettonClient
}
Expand Down Expand Up @@ -49,13 +54,13 @@ fun OnrampMock.handleJettonTransferNotification(

// Handle the jetton transfer
if (msg.jettonAmount < FEE) {
emit(JETTON_TOPIC, InsufficientFee { queryId: msg.queryId, sender: msg.transferInitiator });
emit(JETTON_TOPIC, InsufficientFee { queryId: msg.queryId, sender: msg.transferInitiator! });
} else {
emit(
JETTON_TOPIC,
AcceptedRequest {
queryId: msg.queryId,
sender: msg.transferInitiator,
sender: msg.transferInitiator!,
payload: forwardPayloadCell!,
}
);
Expand Down
2 changes: 2 additions & 0 deletions contracts/contracts/examples/jetton/sender.tolk
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// SPDX-License-Identifier: MIT
tolk 1.2

import "@stdlib/common.tolk"
import "../../lib/jetton/jetton_client.tolk"
import "../../lib/jetton/messages.tolk"
Expand Down
2 changes: 2 additions & 0 deletions contracts/contracts/examples/jetton/simple_receiver.tolk
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// SPDX-License-Identifier: MIT
tolk 1.2

import "@stdlib/common.tolk"
import "../../lib/jetton/jetton_client.tolk"
import "../../lib/jetton/messages.tolk"
Expand Down
10 changes: 10 additions & 0 deletions contracts/contracts/lib/jetton/errors.tolk
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: MIT

const ERROR_INVALID_OP = 72
const ERROR_WRONG_OP = 0xffff
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Unused

const ERROR_NOT_OWNER = 73
Comment thread
krebernisak marked this conversation as resolved.
const ERROR_NOT_VALID_WALLET = 74
const ERROR_WRONG_WORKCHAIN = 333
const ERROR_BALANCE_ERROR = 47
const ERROR_NOT_ENOUGH_GAS = 48
const ERROR_INVALID_MESSAGE = 49
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Unused

24 changes: 24 additions & 0 deletions contracts/contracts/lib/jetton/jetton-utils.tolk
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: MIT
import "storage"

fun calcDeployedJettonWallet(ownerAddress: address, minterAddress: address, jettonWalletCode: cell): AutoDeployAddress {
val emptyWalletStorage: WalletStorage = {
status: 0,
jettonBalance: 0,
ownerAddress,
minterAddress,
};

return {
workchain: BASECHAIN,
stateInit: {
code: jettonWalletCode,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I wonder if there's a way to use the hash of the code instead to save on storage and compute fees. IIUC the hash is computed from leaves to root, so the hash of the code should always result in the same.

data: emptyWalletStorage.toCell()
}
}
}

fun calcAddressOfJettonWallet(ownerAddress: address, minterAddress: address, jettonWalletCode: cell) {
val jwDeployed = calcDeployedJettonWallet(ownerAddress, minterAddress, jettonWalletCode);
return jwDeployed.calculateAddress()
}
4 changes: 2 additions & 2 deletions contracts/contracts/lib/jetton/jetton_client.tolk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
import "./utils.tolk"
import "jetton-utils"
import "messages"

struct JettonClient {
Expand All @@ -9,7 +9,7 @@ struct JettonClient {

@inline
fun JettonClient.walletAddress(self): address {
return calculateUserJettonWalletAddress(
return calcAddressOfJettonWallet(
contract.getAddress(),
self.masterAddress,
self.jettonWalletCode
Expand Down
21 changes: 12 additions & 9 deletions contracts/contracts/lib/jetton/messages.tolk
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct (0x0f8a7ea5) AskToTransfer {
queryId: uint64
jettonAmount: coins
transferRecipient: address
sendExcessesTo: address
sendExcessesTo: address?
customPayload: cell?
forwardTonAmount: coins
forwardPayload: ForwardPayloadRemainder
Expand All @@ -19,16 +19,16 @@ struct (0x0f8a7ea5) AskToTransfer {
struct (0x7362d09c) TransferNotificationForRecipient {
queryId: uint64
jettonAmount: coins
transferInitiator: address
transferInitiator: address?
forwardPayload: ForwardPayloadRemainder
}

// nolint:opcode
struct (0x178d4519) InternalTransferStep {
queryId: uint64
jettonAmount: coins
transferInitiator: address
sendExcessesTo: address
transferInitiator: address? // is null when minting (not initiated by another wallet)
sendExcessesTo: address?
forwardTonAmount: coins
forwardPayload: ForwardPayloadRemainder
}
Expand All @@ -42,7 +42,7 @@ struct (0xd53276db) ReturnExcessesBack {
struct (0x595f07bc) AskToBurn {
queryId: uint64
jettonAmount: coins
sendExcessesTo: address
sendExcessesTo: address?
customPayload: cell?
}

Expand All @@ -51,7 +51,7 @@ struct (0x7bdd97de) BurnNotificationForMinter {
queryId: uint64
jettonAmount: coins
burnInitiator: address
sendExcessesTo: address
sendExcessesTo: address?
}

// nolint:opcode
Expand All @@ -64,7 +64,7 @@ struct (0x2c76b973) RequestWalletAddress {
// nolint:opcode
struct (0xd1735400) ResponseWalletAddress {
queryId: uint64
jettonWalletAddress: address
jettonWalletAddress: address?
ownerAddress: Cell<address>?
}

Expand Down Expand Up @@ -102,18 +102,21 @@ struct (0x2508d66a) UpgradeMinterCode {
// nolint:opcode
struct (0xcb862902) ChangeMinterMetadataUri {
queryId: uint64
// TODO: update to 1.4
newMetadataUri: SnakeString
}

// nolint:opcode
struct (0xd372158c) TopUpTons {}
struct (0xd372158c) TopUpTons {
}


// "forward payload" is TL/B `(Either Cell ^Cell)`;
// we want to test, that if ^Cell, no other data exists in a slice
fun ForwardPayloadRemainder.checkIsCorrectTLBEither(self) {
var mutableCopy = self;
if (mutableCopy.loadMaybeRef() != null) {
// throw "cell underflow" if there is data besides a ref
mutableCopy.assertEnd();
mutableCopy.assertEnd()
}
}
24 changes: 15 additions & 9 deletions contracts/contracts/lib/jetton/storage.tolk
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: MIT
// Imported from https://github.com/ton-blockchain/tolk-bench/blob/0f416ca611fbfa25e736973d01e5fb70af485468/contracts_Tolk/03_notcoin/storage.tolk

// TODO: update to 1.4
// SnakeString describes a (potentially long) string inside a cell;
// short strings are stored as-is, like "my-picture.png";
// long strings are nested refs, like "xxxx".ref("yyyy".ref("zzzz"))
Expand All @@ -11,12 +13,12 @@ fun SnakeString.unpackFromSlice(mutate s: slice) {
// assert (s.remainingRefsCount() <= 1) throw 5;
// but since here we're matching the original FunC implementation, leave no checks
val snakeRemainder = s;
s = createEmptySlice(); // no more left to read
return snakeRemainder;
s = createEmptySlice(); // no more left to read
return snakeRemainder
}

fun SnakeString.packToBuilder(self, mutate b: builder) {
b.storeSlice(self);
b.storeSlice(self)
}

struct WalletStorage {
Expand All @@ -28,24 +30,28 @@ struct WalletStorage {

struct MinterStorage {
totalSupply: coins
adminAddress: address
nextAdminAddress: address
adminAddress: address?
nextAdminAddress: address?
jettonWalletCode: cell
// TODO: update to 1.4
metadataUri: Cell<SnakeString>
}



fun MinterStorage.load() {
return MinterStorage.fromCell(contract.getData());
return MinterStorage.fromCell(contract.getData())
}

fun MinterStorage.save(self) {
contract.setData(self.toCell());
contract.setData(self.toCell())
}


fun WalletStorage.load() {
return WalletStorage.fromCell(contract.getData());
return WalletStorage.fromCell(contract.getData())
}

fun WalletStorage.save(self) {
contract.setData(self.toCell());
contract.setData(self.toCell())
}
49 changes: 0 additions & 49 deletions contracts/contracts/lib/jetton/utils.tolk

This file was deleted.

Loading
Loading