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

z5ac_transfer_voucher_grc20_class_confusion_filetest.gno

5.64 Kb · 133 lines
  1// PKGPATH: gno.land/r/aib/main
  2package main
  3
  4import (
  5	"chain"
  6	"encoding/hex"
  7	"testing"
  8	"time"
  9
 10	tmtesting "gno.land/p/aib/ibc/lightclient/tendermint/testing"
 11	"gno.land/p/aib/ibc/types"
 12	"gno.land/p/aib/ics23"
 13	"gno.land/r/aib/ibc/apps/testing/grc20test"
 14	"gno.land/r/aib/ibc/apps/transfer"
 15	"gno.land/r/aib/ibc/core"
 16)
 17
 18func main(cur realm) {
 19	var (
 20		chainID       = "chain-id-2"
 21		trustedHeight = types.NewHeight(2, 2)
 22		clientState   = tmtesting.NewClientState(chainID, trustedHeight)
 23		apphash, _    = hex.DecodeString("33fead0600f4d6cb62757ae93f837886add19e145a66413e60dfe5ea5c52d039")
 24		trustedValset = tmtesting.GenValset()
 25	)
 26
 27	clientAConsensusState := tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
 28	clientA := core.CreateClient(cross(cur), clientState, clientAConsensusState)
 29	core.RegisterCounterparty(cross(cur), clientA, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, "07-tendermint-42")
 30
 31	clientBConsensusState := tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
 32	clientB := core.CreateClient(cross(cur), clientState, clientBConsensusState)
 33	core.RegisterCounterparty(cross(cur), clientB, [][]byte{[]byte("iavlStoreKey"), []byte("prefix3")}, "07-tendermint-43")
 34
 35	owner := cur.Previous().Address()
 36	grc20test.Mint(cross(cur), owner, 1000)
 37	transferAppAddr := chain.PackageAddress("gno.land/r/aib/ibc/apps/transfer")
 38	grc20test.Approve(cross(cur), owner, transferAppAddr, 100)
 39
 40	grc20regKey := "gno.land/r/aib/ibc/apps/testing/grc20test." + grc20test.Slug
 41	alias := transfer.GRC20Alias(grc20regKey)
 42	recvVoucher(cur, clientA, "07-tendermint-42", trustedHeight, alias, owner)
 43
 44	voucherDenom := transfer.NewDenom(alias, transfer.NewHop(transfer.PortID, clientA)).IBCDenom()
 45	println("voucher balance before Transfer:", transfer.VoucherBalanceOf(voucherDenom, owner))
 46	println("GRC20 balance before Transfer:", grc20test.BalanceOf(owner))
 47
 48	testing.SetRealm(testing.NewUserRealm(owner))
 49	transfer.Transfer(cross(cur), clientB, "atone1user", voucherDenom, 100, uint64(time.Now().Add(time.Hour).Unix()), "")
 50
 51	println("voucher balance after Transfer:", transfer.VoucherBalanceOf(voucherDenom, owner))
 52	println("GRC20 balance after Transfer:", grc20test.BalanceOf(owner))
 53}
 54
 55func recvVoucher(cur realm, clientID, counterpartyID string, trustedHeight types.Height, baseDenom string, receiver address) {
 56	payload := transfer.NewFungibleTokenPacketData(baseDenom, "100", "atone1user", receiver.String(), "")
 57	payloadBz := payload.ProtoMarshal()
 58
 59	specs := ics23.IavlSpec()
 60	// NOTE code generated by:
 61	// go run -C ./cmd/gen-proof . 'prefix2' '07-tendermint-42' 'packet' '{"sequence":1,"source_client":"07-tendermint-42","destination_client":"07-tendermint-1","timeout_timestamp":1234571490,"payloads":[{"source_port":"transfer","destination_port":"transfer","encoding":"application/x-protobuf","value":"Ci5nbm8ubGFuZDpyOmFpYjppYmM6YXBwczp0ZXN0aW5nOmdyYzIwdGVzdC5URVNUEgMxMDAaCmF0b25lMXVzZXIiKGcxd3ltdTQ3ZHJocjBrdXEyMDk4bTc5Mmx5dGd0ajJueXg3N3lyc20=","version":"ics20-1"}]}'
 62	proof := []ics23.CommitmentProof{
 63		// iavl proof
 64		ics23.CommitmentProof_Exist{
 65			Exist: &ics23.ExistenceProof{
 66				Key:   []byte("\x70\x72\x65\x66\x69\x78\x32\x30\x37\x2d\x74\x65\x6e\x64\x65\x72\x6d\x69\x6e\x74\x2d\x34\x32\x01\x00\x00\x00\x00\x00\x00\x00\x01"),
 67				Value: []byte("\x08\x75\xdf\x91\xfd\x25\xd3\xd2\x4d\x40\x2b\xa9\x09\x2f\x6d\x5d\xea\xcc\x51\xeb\x90\x15\x62\xa7\xa8\x6d\xb8\x56\xc6\x59\x72\x44"),
 68				Leaf: &ics23.LeafOp{
 69					Hash:         specs.LeafSpec.Hash,
 70					PrehashKey:   specs.LeafSpec.PrehashKey,
 71					PrehashValue: specs.LeafSpec.PrehashValue,
 72					Length:       specs.LeafSpec.Length,
 73					Prefix:       []byte("\x00\x02\x02"),
 74				},
 75				Path: []*ics23.InnerOp{
 76					{
 77						Hash:   specs.InnerSpec.Hash,
 78						Prefix: []byte("\x02\x04\x02\x20\x35\xf8\xea\x80\x53\x90\xe0\x84\x85\x4f\x39\x9b\x42\xcc\xde\xae\xa3\x3a\x1d\xed\xc1\x15\x63\x8a\xc4\x8d\x06\x00\x63\x7d\xba\x1f\x20"),
 79						Suffix: []byte(""),
 80					},
 81					{
 82						Hash:   specs.InnerSpec.Hash,
 83						Prefix: []byte("\x04\x08\x02\x20"),
 84						Suffix: []byte("\x20\x79\x8e\x2c\xaa\x96\xfd\xfb\xa3\x76\xdd\xeb\x47\x99\x99\x54\xd2\xf4\x7e\x65\x16\x22\x64\xb0\x53\x6a\xb5\xdf\xf7\xfc\x0a\x2e\x07"),
 85					},
 86					{
 87						Hash:   specs.InnerSpec.Hash,
 88						Prefix: []byte("\x06\x0c\x02\x20\x9a\xf3\x7d\xd5\x95\xa0\x19\x08\x03\xb5\xe0\x5a\xae\xf4\x2a\xe3\xfa\xd4\x99\xe4\xfb\xe3\x7f\x7c\xd3\x1c\xad\xff\x22\xa9\xee\x74\x20"),
 89						Suffix: []byte(""),
 90					},
 91				},
 92			},
 93		},
 94
 95		// rootmulti proof
 96		ics23.CommitmentProof_Exist{
 97			Exist: &ics23.ExistenceProof{
 98				Key:   []byte("\x69\x61\x76\x6c\x53\x74\x6f\x72\x65\x4b\x65\x79"),
 99				Value: []byte("\x5f\xd0\xfe\x1e\xf3\xcc\x3f\x2d\x4b\x94\x77\xa6\x04\x51\x4a\x11\xaf\x58\xf8\xa9\x9a\x7a\x73\x90\x74\xf8\xb2\xa3\x21\xad\x08\xee"),
100				Leaf: &ics23.LeafOp{
101					Hash:         specs.LeafSpec.Hash,
102					PrehashKey:   specs.LeafSpec.PrehashKey,
103					PrehashValue: specs.LeafSpec.PrehashValue,
104					Length:       specs.LeafSpec.Length,
105					Prefix:       []byte("\x00"),
106				},
107				Path: []*ics23.InnerOp{},
108			},
109		},
110	}
111
112	recvMsg := types.MsgRecvPacket{
113		Packet: types.Packet{
114			Sequence:          1,
115			SourceClient:      counterpartyID,
116			DestinationClient: clientID,
117			TimeoutTimestamp:  uint64(time.Now().Add(time.Hour).Unix()),
118			Payloads: []types.Payload{{
119				SourcePort:      transfer.PortID,
120				DestinationPort: transfer.PortID,
121				Encoding:        transfer.EncodingProtobuf,
122				Value:           payloadBz,
123				Version:         transfer.V1,
124			}},
125		},
126		ProofCommitment: proof,
127		ProofHeight:     trustedHeight,
128	}
129	core.RecvPacket(cross(cur), recvMsg)
130}
131
132// Error:
133// send packet failed for payload #0 app "transfer": GRC20 packet must be initiated through transfer.Transfer