Skip to content

Instantly share code, notes, and snippets.

@ProgramCrafter
Last active July 29, 2024 20:03
Show Gist options
  • Save ProgramCrafter/53fc3a4846397ee5f3f3b25ca3356ab5 to your computer and use it in GitHub Desktop.
Save ProgramCrafter/53fc3a4846397ee5f3f3b25ca3356ab5 to your computer and use it in GitHub Desktop.
forall X -> tuple change_tuple(tuple t, int pos, X value) asm(t value pos) "SETINDEXVAR";
tuple get_c7() asm "c7 PUSHCTR";
slice vm::invoke_get_addr(slice owner_address, tuple c7, cell master_data, cell master_code) asm
"CTOS // owner_addr c7 md mc"
"2 PUSHINT // owner_addr c7 md mc args"
"103289 PUSHINT // owner_addr c7 md mc args get_jwa_method_id"
"5 0 REVERSE DUMPSTK // owner_addr get_jwa_method_id args mc md c7"
"53 RUNVM DUMPSTK // address exit_code c4' c5'"
"3 BLKDROP // address";
slice get_addr(slice owner_address, cell code, cell data, slice master_addr) inline_ref {
tuple c7 = get_c7();
c7 = c7.change_tuple(0,
c7.at(0).change_tuple(8, master_addr)
.change_tuple(10, code)
);
return vm::invoke_get_addr(owner_address, c7, data, code);
}
slice calc_my_jetton_wallet(slice master_addr, cell code, cell data) inline {
return get_addr(my_address(), code, data, master_addr);
}
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