Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

transfer.gno

1.86 Kb · 68 lines
 1package ucs03_zkgm
 2
 3import (
 4	"encoding/hex"
 5
 6	types "gno.land/p/onbloc/ibc/union/types"
 7
 8	z "gno.land/p/onbloc/ibc/union/zkgm"
 9)
10
11func Send(cur realm, channelId types.ChannelId, timeoutTimestamp types.Timestamp, salt [32]byte, instruction z.Instruction) types.Packet {
12	assertIsNotPaused()
13
14	return mustGetImpl().Send(0, cur, channelId, timeoutTimestamp, salt, instruction)
15}
16
17// SendRaw sends a ZKGM packet from hex-encoded instruction fields. Use this
18// entry point from gnokey maketx call (not maketx run) so OriginSend captures
19// any native coins attached with -send for escrow-bearing instructions.
20//
21//   - channelId: local source IBC channel id on this chain.
22//   - timeoutTimestamp: packet expiry in nanoseconds since the Unix epoch.
23//   - saltHex: 32-byte send salt as hex; an optional 0x prefix is accepted.
24//   - version: instruction version byte (e.g. 2 for TokenOrderV2).
25//   - opcode: instruction opcode (0=Forward, 1=Call, 2=Batch, 3=TokenOrder).
26//   - operandHex: ABI-encoded instruction operand as hex; 0x prefix optional.
27func SendRaw(cur realm, channelId uint32, timeoutTimestamp uint64, saltHex string, version uint8, opcode uint8, operandHex string) types.Packet {
28	assertIsNotPaused()
29
30	saltBytes, err := parseHex(saltHex)
31	if err != nil {
32		panic(err)
33	}
34
35	if len(saltBytes) != 32 {
36		panic(ErrInvalidSaltLength)
37	}
38
39	var salt [32]byte
40
41	copy(salt[:], saltBytes)
42
43	operandBytes, err := parseHex(operandHex)
44	if err != nil {
45		panic(err)
46	}
47
48	instruction := z.Instruction{
49		Version: version,
50		Opcode:  opcode,
51		Operand: operandBytes,
52	}
53
54	return mustGetImpl().Send(0, cur, types.ChannelId(channelId), types.Timestamp(timeoutTimestamp), salt, instruction)
55}
56
57func parseHex(s string) ([]byte, error) {
58	if len(s) >= 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
59		s = s[2:]
60	}
61
62	bz, err := hex.DecodeString(s)
63	if err != nil {
64		return nil, err
65	}
66
67	return bz, nil
68}