Skip to content

Instantly share code, notes, and snippets.

@ilyar
Created March 27, 2023 05:57
Show Gist options
  • Save ilyar/e053b47357368a0cd9c5adf98a3e0dd5 to your computer and use it in GitHub Desktop.
Save ilyar/e053b47357368a0cd9c5adf98a3e0dd5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
std_lib() {
list=()
list+=('"SETCP0":"FF00"')
list+=('"ACCEPT":"F800"')
list+=('"THROW":"F22"')
list+=('"PUSHREF":"88"')
list+=('"SETCODE":"FB04"')
opcodes=$(printf ",%s" "${list[@]}")
data="{${opcodes:1}}"
echo "${data}" | jq -r ".${1}"
}
linker() {
data="${1}"
out=""
for it in $data ; do
hasParam=$(echo "${it}" | tr -cd '=' | wc -c)
asm="$(echo "${it}" | cut -d'=' -f1)"
opcode="$(std_lib "${asm}")"
out+="${opcode}"
if [ "${hasParam}" == "1" ]; then
value="$(echo "${it}" | cut -d'=' -f2)"
out+="${value}"
fi
done
printf "%s" "${out}"
}
function abi() {
selector="${1}"
shift 1 # Removes $1 from the parameter list
inputList=()
for value in "$@" # Represents the remaining parameters
do
name="$(echo "${value}" | cut -d':' -f1)"
type="$(echo "${value}" | cut -d':' -f2)"
inputList+=("$(printf '{"name":"%s","type":"%s"}' "${name}" "${type}")")
done
inputs=$(printf ",%s" "${inputList[@]}")
inputs=${inputs:1}
functions=$(printf '[{"name":"boc","id":"%s","inputs":[%s],"outputs":[]}]' "${selector}" "${inputs}")
printf '{"ABI version":2,"version":"2.3","functions":%s}' "${functions}"
}
calcSize() {
data="${1}"
len="$(printf "%s" "${data}" | wc -c)"
echo "${len} * 4" | bc
}
compiler() {
code=${1}
selector=$(linker "$(echo "${code}" | jq -r .selector)")
# TODO add check selector >32
data=$(linker "$(echo "${code}" | jq -r .data)")
dataSize="$(calcSize "${data}")"
# TODO add check dataSize >256
cellRaw="$(echo "${code}" | jq -r .cell)"
abi=""
param=""
if [ "${cellRaw}" == "null" ]; then
param="$(printf '{"DATA": "%s"}' "0x${data}")"
abi=$(abi "0x${selector}" "DATA:uint${dataSize}")
else
cell="$(compiler "${cellRaw}")"
param="$(printf '{"DATA": "%s", "CELL": "%s"}' "0x${data}" "${cell}")"
abi=$(abi "0x${selector}" "DATA:uint${dataSize}" "CELL:cell")
fi
tonos-cli -j body --abi <(echo "${abi}") boc "${param}" | jq -r .Message
}
code=$(compiler "$(cat "${1}")")
echo "${code}"
echo "${code}" | base64 -d > code.boc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment