Skip to content

Instantly share code, notes, and snippets.

@novusnota
Forked from ProgramCrafter/helper.fc
Created April 14, 2024 11:48
Show Gist options
  • Save novusnota/b95a7e9f98f3c508180a7c8fbee46328 to your computer and use it in GitHub Desktop.
Save novusnota/b95a7e9f98f3c508180a7c8fbee46328 to your computer and use it in GitHub Desktop.
;; my_addr(MYADDR) get_jwa_method_id(103289) args(2) master_code ret(1) master_data
;; RUNVM +1 +4 +32
;; address exitcode c4' c5'
slice vm::invoke_get_addr(cell master_code, cell master_data) asm
"MYADDR // mc md my_addr"
"103829 PUSHINT // mc md my_addr get_jwa_method_id"
"2 PUSHINT // mc md my_addr get_jwa_method_id args"
"2 3 BLKSWAP // my_addr get_jwa_method_id args mc md"
"1 PUSHINT // my_addr get_jwa_method_id args mc md ret"
"SWAP // my_addr get_jwa_method_id args mc ret md"
"37 RUNVM // address exit_code c4' c5'"
"3 BLKDROP // address";
slice calc_my_jetton_wallet(slice master_address, cell master_code, cell master_data) inline {
cell master_si = begin_cell().store_uint(6, 5).store_ref(master_code).store_ref(master_data);
(int wc, int master_hash) = master_address.parse_std_addr();
throw_if(100, (master_si.cell_hash() != master_hash) | wc);
return vm::invoke_get_addr(master_code, master_data);
}
import "./helper.fc";
@name(calc_my_jetton_wallet)
native calcMyJettonWallet(master_address: Address, master_code: Cell, master_data: Cell): Address;
message(0x0f8a7ea5) JettonTransfer {
query_id: Int as uint64;
amount: Int as coins;
destination: Address;
response_destination: Address;
custom_payload: Cell?;
forward_ton_amount: Int as coins;
forward_payload: Slice as remaining;
}
@interface("ton.experimental.pcrafter.jettonseller")
contract JettonSeller {
const forward_ton_amount: Int = ton("0.000001");
jetton_master: Address;
jetton_wallet: Address; // what we are selling
nanoton_per_jetton: Int as coins;
init(jetton_master: Address, master_code: Cell, master_data: Cell, nanoton_per_jetton: Int) {
self.jetton_wallet = calcMyJettonWallet(jetton_master, master_code, master_data);
self.nanoton_per_jetton = nanoton_per_jetton;
}
fun transferJettonTo(jetton_wallet: Address, destination: Address, amount: Int, query_id: Int, message: String) {
if (amount > 0) {
send(SendParameters{
to: jetton_wallet,
value: ton("0.1"),
mode: 0,
body: JettonTransfer{query_id: query_id, amount: amount, destination: destination, response_destination: destination, custom_payload: null, forward_ton_amount: self.forward_ton_amount, forward_payload: message.asComment()}.toCell()
});
}
}
receive() {
let nanoton = context().value - ton("0.1");
transferJettonTo(self.jetton_wallet, sender(), nanoton / self.nanoton_per_jetton, 0, "swap completed");
}
get fun getJetton(): Address {
return self.jetton_master;
}
get fun getJettonPrice(): Int {
return self.nanoton_per_jetton;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment