Skip to content

Commit

Permalink
sui: implement contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
jumpsiegel authored and kcsongor committed Jan 20, 2023
1 parent 53d554d commit 62eb6ae
Show file tree
Hide file tree
Showing 65 changed files with 7,445 additions and 3 deletions.
2 changes: 2 additions & 0 deletions sui/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
env.sh
sui.log.*
27 changes: 24 additions & 3 deletions sui/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
FROM ghcr.io/wormhole-foundation/sui:0.15.0@sha256:ba5740e42ac41306719114ce96d9752a12bbfa500749ede64fa76a250113993b as sui-node
FROM ghcr.io/wormhole-foundation/sui:0.21.1@sha256:59b91529e426b44c152b40ad0e7a6a7aafc8225722b5d7e331056a4d65845015 as sui

RUN dnf -y install make git

COPY README.md cert.pem* /certs/
RUN if [ -e /certs/cert.pem ]; then cp /certs/cert.pem /etc/ssl/certs/ca-certificates.crt; fi
RUN if [ -e /certs/cert.pem ]; then git config --global http.sslCAInfo /certs/cert.pem; fi

WORKDIR /tmp

RUN sui genesis -f
COPY scripts/start_node.sh /tmp
COPY scripts/funder.sh /tmp
COPY scripts/start_node.sh .
COPY scripts/funder.sh .

COPY wormhole/ wormhole
COPY token_bridge/ token_bridge
# COPY examples/ examples
COPY Makefile Makefile

FROM sui AS tests

WORKDIR /tmp

RUN --mount=type=cache,target=/root/.move,id=move_cache \
make test
13 changes: 13 additions & 0 deletions sui/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CONTRACT_DIRS := wormhole token_bridge

TARGETS := build test

.PHONY: $(TARGETS)
$(TARGETS):
$(foreach dir,$(CONTRACT_DIRS), make -C $(dir) $@ &&) true

test-docker:
DOCKER_BUILDKIT=1 docker build -f Dockerfile --target tests .

sui_export:
DOCKER_BUILDKIT=1 docker build --progress plain -f Dockerfile.export -t near-export -o type=local,dest=$$HOME/.cargo/bin .
112 changes: 112 additions & 0 deletions sui/NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
brew install cmake

rustup install stable-x86_64-apple-darwin
#rustup target add stable-x86_64-apple-darwin
rustup target add x86_64-apple-darwin

=== Building

% ./node_builder.sh

=== Running

% ./start_node.sh

# If you don't remember your newly generated address

% sui client addresses
Showing 1 results.
0x13b3cb89cf3226d3b860294fc75dc6c91f0c5ecf

# Give yourself some money

% scripts/faucet.sh `sui client addresses | tail -1`

# Looking at the prefunded address

% sui client objects --address 0x13b3cb89cf3226d3b860294fc75dc6c91f0c5ecf

=== Boot tilt

# fund our standard account

We don't run a faucet since it doesn't always unlock the client LOCK files. So, instead we just steal a chunk of coins
from the default accounts created when the node was initialized. Once sui is showing as live...

``` sh
% kubectl exec -it sui-0 -c sui-node -- /tmp/funder.sh
```

# getting into the sui k8s node (if you need to crawl around)

kubectl exec -it sui-0 -c sui-node -- /bin/bash
kubectl exec -it guardian-0 -c guardiand -- /bin/bash

# setup the client.yaml

``` sh
% rm -rf $HOME/.sui
% sui keytool import "daughter exclude wheat pudding police weapon giggle taste space whip satoshi occur" ed25519
% sui client
```
point it at http://localhost:9000. The key you create doesn't matter.

# edit $HOME/.sui/sui_config/client.yaml

``` sh
sed -i -e 's/active_address.*/active_address: "0x13b3cb89cf3226d3b860294fc75dc6c91f0c5ecf"/' ~/.sui/sui_config/client.yaml
```


# deploy the contract

``` sh
% scripts/deploy.sh
```

# start the watcher

``` sh
% . env.sh
% python3 tests/ws.py
```

# publish a message (different window)

``` sh
% . env.sh
% scripts/publish_message.sh
```

==

docker run -it -v `pwd`:`pwd` -w `pwd` --net=host ghcr.io/wormhole-foundation/sui:0.16.0 bash
dnf -y install git make

``` sh
% rm -rf $HOME/.sui
% sui keytool import "daughter exclude wheat pudding police weapon giggle taste space whip satoshi occur" secp256k1
% sui client
```

to get a new emitter

kubectl exec -it sui-0 -c sui-node -- /tmp/funder.sh
scripts/deploy.sh
. env.sh
sui client call --function get_new_emitter --module wormhole --package $WORM_PACKAGE --gas-budget 20000 --args \"$WORM_STATE\"

sui client objects
scripts/publish_message.sh 0x165ef7366c4267c6506bcf63d2419556f34f48d6


curl -s -X POST -d '{"jsonrpc":"2.0", "id": 1, "method": "sui_getEvents", "params": [{"MoveEvent": "0xf4179152ab02e4212d7e7b20f37a9a86ab6d50fb::state::WormholeMessage"}, null, 10, true]}' -H 'Content-Type: application/json' http://127.0.0.1:9002 | jq

curl -s -X POST -d '{"jsonrpc":"2.0", "id": 1, "method": "sui_getEvents", "params": [{"Transaction": "cL+uWFEVcQrkAiOxOJmaK7JmlOJdE3/8X5JFbJwBxCQ="}, null, 10, true]}' -H 'Content-Type: application/json' http://127.0.0.1:9002 | jq

"txhash": "0x70bfae585115710ae40223b138999a2bb26694e25d137ffc5f92456c9c01c424", "txhash_b58": "8b8Bn8MUqAWeVz2BE5hMicC9KaRkV6UM4v1JLWGUjxcT", "
Digest: cL+uWFEVcQrkAiOxOJmaK7JmlOJdE3/8X5JFbJwBxCQ=

kubectl exec -it guardian-0 -- /guardiand admin send-observation-request --socket /tmp/admin.sock 21 70bfae585115710ae40223b138999a2bb26694e25d137ffc5f92456c9c01c424

// curl -s -X POST -d '{"jsonrpc":"2.0", "id": 1, "method": "sui_getCommitteeInfo", "params": []}' -H 'Content-Type: application/json' http://127.0.0.1:9002 | jq
14 changes: 14 additions & 0 deletions sui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Installation
Make sure your Cargo version is at least 1.64.0 and then follow the steps below:
- https://docs.sui.io/build/install


# Sui CLI
- do `sui start` to spin up a local network
- do `rpc-server` to start a server for handling rpc calls
- do `sui-faucet` to start a faucet for requesting funds from active-address

# TODOs
- The move dependencies are currently pinned to a version that matches the
docker image for reproducibility. These should be regularly updated to track
any upstream changes before the mainnet release.
11 changes: 11 additions & 0 deletions sui/coin/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "Coin"
version = "0.0.1"

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework", rev = "2d709054a08d904b9229a2472af679f210af3827" }
TokenBridge = { local = "../token_bridge"}
Wormhole = { local = "../wormhole"}

[addresses]
coin="0x0"
24 changes: 24 additions & 0 deletions sui/coin/sources/coin.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module coin::coin {
use sui::transfer;
use sui::tx_context::{Self, TxContext};

use token_bridge::wrapped;

struct COIN has drop {}

fun init(coin_witness: COIN, ctx: &mut TxContext) {
// Step 1. Paste token attestation VAA below.
let vaa_bytes = x"0100000000010080366065746148420220f25a6275097370e8db40984529a6676b7a5fc9feb11755ec49ca626b858ddfde88d15601f85ab7683c5f161413b0412143241c700aff010000000100000001000200000000000000000000000000000000000000000000000000000000deadbeef000000000150eb23000200000000000000000000000000000000000000000000000000000000beefface00020c424545460000000000000000000000000000000000000000000000000000000042656566206661636520546f6b656e0000000000000000000000000000000000";

let new_wrapped_coin = wrapped::create_wrapped_coin(vaa_bytes, coin_witness, ctx);
transfer::transfer(
new_wrapped_coin,
tx_context::sender(ctx)
);
}

#[test_only]
public fun test_init(ctx: &mut TxContext) {
init(COIN {}, ctx)
}
}
15 changes: 15 additions & 0 deletions sui/scripts/create_wrapped.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash -f

set -euo pipefail

cd "$(dirname "$0")"

. ../env.sh

echo "Creating wrapped asset..."
echo "$COIN_PACKAGE::coin_witness::COIN_WITNESS"

sui client call --function register_wrapped_coin \
--module wrapped --package $TOKEN_PACKAGE --gas-budget 20000 \
--args "$WORM_STATE" "$TOKEN_STATE" "$NEW_WRAPPED_COIN" \
--type-args "$COIN_PACKAGE::coin_witness::COIN_WITNESS"
54 changes: 54 additions & 0 deletions sui/scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash -f

set -euo pipefail

cd "$(dirname "$0")"/..

#Transaction Kind : Publish
#----- Transaction Effects ----
#Status : Success
#Created Objects:
# - ID: 0x069b6d8ea50a0b0756518cb08ddbbad2babf8ae0 <= STATE , Owner: Account Address ( 0xe6a09658743da40b0f48c4da1f3fa0d34797d0d3 <= OWNER )
# - ID: 0x73fc05ae6f172f90b12a98cf3ad0b669d6b70e5b <= PACKAGE , Owner: Immutable

cd wormhole
sed -i -e 's/wormhole = .*/wormhole = "0x0"/' Move.toml
make build
sui client publish --gas-budget 10000 | tee publish.log
grep ID: publish.log | head -2 > ids.log

WORM_PACKAGE=$(grep "Immutable" ids.log | sed -e 's/^.*: \(.*\) ,.*/\1/')
sed -i -e "s/wormhole = .*/wormhole = \"$WORM_PACKAGE\"/" Move.toml
WORM_DEPLOYER_CAPABILITY=$(grep -v "Immutable" ids.log | sed -e 's/^.*: \(.*\) ,.*/\1/')
WORM_OWNER=$(grep -v "Immutable" ids.log | sed -e 's/^.*( \(.*\) )/\1/')

cd ../token_bridge
sed -i -e 's/token_bridge = .*/token_bridge = "0x0"/' Move.toml
make build
sui client publish --gas-budget 10000 | tee publish.log
grep ID: publish.log | head -2 > ids.log

TOKEN_PACKAGE=$(grep "Immutable" ids.log | sed -e 's/^.*: \(.*\) ,.*/\1/')
sed -i -e "s/token_bridge = .*/token_bridge = \"$TOKEN_PACKAGE\"/" Move.toml
TOKEN_DEPLOYER_CAPABILITY=$(grep -v "Immutable" ids.log | sed -e 's/^.*: \(.*\) ,.*/\1/')
TOKEN_OWNER=$(grep -v "Immutable" ids.log | sed -e 's/^.*( \(.*\) )/\1/')

sui client call --function init_and_share_state --module state --package $WORM_PACKAGE --gas-budget 20000 --args \"$WORM_DEPLOYER_CAPABILITY\" 0 0 "[190,250,66,157,87,205,24,183,248,164,217,26,45,169,171,74,240,93,15,190]" "[[190,250,66,157,87,205,24,183,248,164,217,26,45,169,171,74,240,93,15,190]]" | tee wormhole.log
WORM_STATE=$(grep Shared wormhole.log | head -1 | sed -e 's/^.*: \(.*\) ,.*/\1/')

sui client call --function get_new_emitter --module wormhole --package $WORM_PACKAGE --gas-budget 20000 --args \"$WORM_STATE\" | tee emitter.log
TOKEN_EMITTER_CAPABILITY=$(grep ID: emitter.log | head -1 | sed -e 's/^.*: \(.*\) ,.*/\1/')

sui client call --function init_and_share_state --module bridge_state --package $TOKEN_PACKAGE --gas-budget 20000 --args "$TOKEN_DEPLOYER_CAPABILITY" "$TOKEN_EMITTER_CAPABILITY" | tee token.log
TOKEN_STATE=$(grep Shared token.log | head -1 | sed -e 's/^.*: \(.*\) ,.*/\1/')

{ echo "export WORM_PACKAGE=$WORM_PACKAGE";
echo "export WORM_DEPLOYER_CAPABILITY=$WORM_DEPLOYER_CAPABILITY";
echo "export WORM_OWNER=$WORM_OWNER";
echo "export TOKEN_PACKAGE=$TOKEN_PACKAGE";
echo "export TOKEN_DEPLOYER_CAPABILITY=$TOKEN_DEPLOYER_CAPABILITY";
echo "export TOKEN_OWNER=$TOKEN_OWNER";
echo "export WORM_STATE=$WORM_STATE";
echo "export TOKEN_EMITTER_CAPABILITY=$TOKEN_EMITTER_CAPABILITY";
echo "export TOKEN_STATE=$TOKEN_STATE";
} > ../env.sh
14 changes: 14 additions & 0 deletions sui/scripts/deploy_coin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash -f

set -euo pipefail

cd "$(dirname "$0")"/..

. env.sh

sui client publish --gas-budget 20000 --path coin | tee publish.log
grep ID: publish.log | head -2 > ids.log
NEW_WRAPPED_COIN=$(grep "Account Address" ids.log | sed -e 's/^.*: \(.*\) ,.*/\1/')
COIN_PACKAGE=$(grep "Immutable" ids.log | sed -e 's/^.*: \(.*\) ,.*/\1/')
echo "export NEW_WRAPPED_COIN=$NEW_WRAPPED_COIN" >> env.sh
echo "export COIN_PACKAGE=$COIN_PACKAGE" >> env.sh
2 changes: 2 additions & 0 deletions sui/scripts/faucet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#
curl -X POST -d '{"FixedAmountRequest":{"recipient": "'"$1"'"}}' -H 'Content-Type: application/json' http://127.0.0.1:5003/gas
10 changes: 10 additions & 0 deletions sui/scripts/generate_account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Ed25519Keypair, JsonRpcProvider, RawSigner } from '@mysten/sui.js';
// Generate a new Secp256k1 Keypair
const keypair = new Ed25519Keypair();

const signer = new RawSigner(
keypair,
new JsonRpcProvider('https://gateway.devnet.sui.io:443')
);

console.log(keypair)
5 changes: 5 additions & 0 deletions sui/scripts/get_new_emitter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash -f

. env.sh

sui client call --function get_new_emitter --module wormhole --package $WORM_PACKAGE --gas-budget 20000 --args \"$WORM_STATE\"
24 changes: 24 additions & 0 deletions sui/scripts/import_account_and_deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

# This dev script imports and funds an account, following the steps in
# `sui/NOTES.md`. It also deploys the core/token bridge contracts.

# Remove directory for idempotency
rm -rf $HOME/.sui

# Import key so we have a deterministic address and make it the default account
sui keytool import "daughter exclude wheat pudding police weapon giggle taste space whip satoshi occur" ed25519
sui client << EOF
y
http://localhost:9000
dev
0
EOF
sed -i -e 's/active_address.*/active_address: "0x13b3cb89cf3226d3b860294fc75dc6c91f0c5ecf"/' ~/.sui/sui_config/client.yaml

# Fund account
kubectl exec -it sui-0 -c sui-node -- /tmp/funder.sh

# Deploy contracts
DIR_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
$DIR_PATH/deploy.sh
5 changes: 5 additions & 0 deletions sui/scripts/init_tokenbridge.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash -f

. env.sh

sui client call --function init_and_share_state --module bridge_state --package $TOKEN_PACKAGE --gas-budget 20000 --args \"$TOKEN_STATE\" \"$EMITTER_CAP\"
5 changes: 5 additions & 0 deletions sui/scripts/init_wormhole.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash -f

. env.sh

sui client call --function init_and_share_state --module state --package $WORM_PACKAGE --gas-budget 20000 --args \"$WORM_STATE\" 0 0 [190,250,66,157,87,205,24,183,248,164,217,26,45,169,171,74,240,93,15,190] [190,250,66,157,87,205,24,183,248,164,217,26,45,169,171,74,240,93,15,190]
11 changes: 11 additions & 0 deletions sui/scripts/node_builder.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

source $HOME/.cargo/env

git clone https://github.com/MystenLabs/sui.git --branch devnet
cd sui

cargo --locked install --path crates/sui
cargo --locked install --path crates/sui-faucet
cargo --locked install --path crates/sui-gateway
cargo --locked install --path crates/sui-node
Loading

0 comments on commit 62eb6ae

Please sign in to comment.