Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate and update noble bridging example scripts #10

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Required for both functions
MNEMONIC="..."

# Required for DepositForBurn
ETH_MINT_RECIPIENT="0x..."

# Required for ReceiveMessage
ATTESTATION="0x..."
MESSAGE_HEX="0x...
52 changes: 52 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Noble <-> Ethereum (Typescript)

## DepositForBurn instructions

1. Install require packages:
mdbere marked this conversation as resolved.
Show resolved Hide resolved

```
npm install
```

2. (If needed) Obtain tokens from the faucet: https://faucet.circle.com/

3. Set up a `.env` file based on `.env.example`, filling in the `MNEMONIC` and `ETH_MINT_RECIPIENT` fields:

```
MNEMONIC="word1 word2..."
ETH_MINT_RECIPIENT=0x...
```

4. Run the depositForBurn script:

```
npm run depositForBurn
```

The Noble testnet -> ETH Sepolia CCTP relayer should pick up these messages automatically. To avoid these being automatically picked up, all references to `MsgDepositForBurn` can be changed to `MsgDepositForBurnWithCaller` and a `destinationCaller` field should be added to `msg.value` below line 70.

## ReceiveMessage instructions

1. Install require packages:

```
npm install
```

2. Initiate a `DepositForBurnWithCaller` from ETH to Noble. If a regular `DepositForBurn` call is made, the relayer will automatically receive the message on Noble.

3. Fetch the attestation and message from Iris at https://iris-api-sandbox.circle.com/messages/{sourceDomain}/{txHash}.

4. Set up a `.env` file based on `.env.example`, filling in the `MNEMONIC`, `ATTESTATION`, and `MESSAGE_HEX` fields:

```
MNEMONIC="word1 word2..."
ATTESTATION=0x...
MESSAGE_HEX=0x
```

5. Run the receiveMessage script:

```
npm run receiveMessage
```
95 changes: 95 additions & 0 deletions examples/depositForBurn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2024, Circle Internet Financial LTD All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {GeneratedType, Registry} from "@cosmjs/proto-signing";

require("dotenv").config();
const { DirectSecp256k1HdWallet } = require("@cosmjs/proto-signing")
mdbere marked this conversation as resolved.
Show resolved Hide resolved
const { SigningStargateClient } = require("@cosmjs/stargate")
const { MsgDepositForBurn } = require("./generated/tx")

export const cctpTypes: ReadonlyArray<[string, GeneratedType]> = [
["/circle.cctp.v1.MsgDepositForBurn", MsgDepositForBurn],
];

function createDefaultRegistry(): Registry {
return new Registry(cctpTypes)
};

const main = async() => {

const mnemonic = process.env.MNEMONIC;
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(
mnemonic,
{
prefix: "noble"
}
);

const [account] = await wallet.getAccounts();

const client = await SigningStargateClient.connectWithSigner(
"https://rpc.testnet.noble.strange.love",
wallet,
{
registry: createDefaultRegistry()
}
);

// Left pad the mint recipient address with 0's to 32 bytes
const rawMintRecipient = process.env.ETH_MINT_RECIPIENT ? process.env.ETH_MINT_RECIPIENT : "";
const cleanedMintRecipient = rawMintRecipient.replace(/^0x/, '');
const zeroesNeeded = 64 - cleanedMintRecipient.length;
const mintRecipient = '0'.repeat(zeroesNeeded) + cleanedMintRecipient;
const buffer = Buffer.from(mintRecipient, "hex");
const mintRecipientBytes = new Uint8Array(buffer);

const msg = {
typeUrl: "/circle.cctp.v1.MsgDepositForBurn",
value: {
from: account.address,
amount: "1",
destinationDomain: 0,
mintRecipient: mintRecipientBytes,
burnToken: "uusdc",
// If using DepositForBurnWithCaller, add destinationCaller here
}
};

const fee = {
amount: [
{
denom: "uusdc",
amount: "0",
},
],
gas: "200000",
};
const memo = "";
const result = await client.signAndBroadcast(
account.address,
[msg],
fee,
memo
);

console.log(`Burned on Noble: https://mintscan.io/noble-testnet/tx/${result.transactionHash}`);
console.log(`Minting on Ethereum to https://sepolia.etherscan.io/address/${rawMintRecipient}`);
}

main()
Loading
Loading