Skip to content

Commit

Permalink
bridge done
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-epi committed Dec 27, 2024
1 parent 3c9121c commit c591af3
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 177 deletions.
123 changes: 52 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,78 +43,59 @@ cast call 0x18715183248AAef4687DEC37fBF792C1412b3b0B \

forge script script/DeployGoldToken.s.sol:DeployGoldToken --rpc-url $RPC_URL_SEPOLIA --private-key $PRIVATE_KEY --broadcast --verify --etherscan-api-key $ETHERSCAN_API_KEY

Script ran successfully.

== Logs ==
Sepolia - GoldLottery deployed at: 0xbD3D66AE432d0F8C985E1791cD3b92403F35ebCc
Sepolia - GoldToken deployed at: 0x807c16099D12DD9f6e61103cB8D0e77306B002C2
Sepolia - GoldBridge deployed at: 0xe087E8938fD8e1904b777d1Bb54E19928cE16CED
Created subscription with ID: 12223
Funded subscription with 2 LINK
Added GoldLottery as consumer to subscription

Sepolia - GoldLottery: 0xB4117EbD882C0358d3438d5121Bf813AE0F7Ff6D
Sepolia - GoldToken: 0xaBFd99e71b41Bf494858A576215c165FA54Db955
Sepolia - GoldBridge: 0xC482ab757Bcd25914E33Af8FD043EB8410150f7D


== Logs ==
BSC - GoldTokenBSC deployed at: 0xa6DdB39862E7175521a3711DC86807c9501C5512
BSC - GoldBridgeBSC deployed at: 0xB1081244C17317163Bed920665e54b3D017f92C8

## Setting up 1 EVM.

==========================

Chain 11155111

Estimated gas price: 11.993078839 gwei

Estimated total gas used for script: 12728790

Estimated amount required: 0.15265738199507481 ETH

==========================
Transactions saved to: C:/Users/julie/Bureau/5ème-année/solidity/GoldLink\broadcast\DeployGoldToken.s.s==========================

Transactions saved to: C:/Users/julie/Bureau/5ème-année/solidity/GoldLink\broadcast\DeployGoldToken.s.sol\11155111\run-latest.json

Sensitive values saved to: C:/Users/julie/Bureau/5ème-année/solidity/GoldLink/cache\DeployGoldToken.s.sol\11155111\run-latest.json

==== TEST =======

julie@Julien MINGW64 ~/Bureau/5ème-année/solidity/GoldLink (main)
$ cast send 0x8296a1B87c499b3bd8163Eef57a60Faa1bb4A8a4 "mint()" --value 0.1ether --private-key $PRIVATE_KEY --rpc-url $RPC_URL

blockHash 0xad4f22e05c9df21f2eba32cd035f2cbf723ee8b26f9117d4bf28d32b73ac1932
blockNumber 7263856
contractAddress
cumulativeGasUsed 12256538
effectiveGasPrice 4865796495
from 0x6c9622A0472681f150773108C0A8662A0528c6Ef
gasUsed 253905
logs [{"address":"0x2b1be89b1ead9faecc6b35044b7cd18fe06319b8","topics":["0xc28711c8dcc0cb3d25732e13809d1d8c2640669a0a3fb4f7729810a9caaffdff","0x0000000000000000000000006c9622a0472681f150773108c0a8662a0528c6ef"],"data":"0x0000000000000000000000000000000000000000000000052663ccab1e1c0000","blockHash":"0xad4f22e05c9df21f2eba32cd035f2cbf723ee8b26f9117d4bf28d32b73ac1932","blockNumber":"0x6ed670","transactionHash":"0xf9a04f06696b177595c216fb690ef56ec8fee006c7e0ec01d230fac889068b97","transactionIndex":"0x93","logIndex":"0xd8","removed":false},{"address":"0xbd0316d3c0e465273f1eb53d5d9c78803b7ac16d","topics":["0x63373d1c4696214b898952999c9aaec57dac1ee2723cec59bea6888f489a9772","0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae","0x0000000000000000000000000000000000000000000000000000000000000001","0x0000000000000000000000002b1be89b1ead9faecc6b35044b7cd18fe06319b8"],"data":"0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000064000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000186a00000000000000000000000000000000000000000000000000000000000000001","blockHash":"0xad4f22e05c9df21f2eba32cd035f2cbf723ee8b26f9117d4bf28d32b73ac1932","blockNumber":"0x6ed670","transactionHash":"0xf9a04f06696b177595c216fb690ef56ec8fee006c7e0ec01d230fac889068b97","transactionIndex":"0x93","logIndex":"0xd9","removed":false},{"address":"0x2b1be89b1ead9faecc6b35044b7cd18fe06319b8","topics":["0xcc58b13ad3eab50626c6a6300b1d139cd6ebb1688a7cced9461c2f7e762665ee"],"data":"0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001","blockHash":"0xad4f22e05c9df21f2eba32cd035f2cbf723ee8b26f9117d4bf28d32b73ac1932","blockNumber":"0x6ed670","transactionHash":"0xf9a04f06696b177595c216fb690ef56ec8fee006c7e0ec01d230fac889068b97","transactionIndex":"0x93","logIndex":"0xda","removed":false},{"address":"0x8296a1b87c499b3bd8163eef57a60faa1bb4a8a4","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000006c9622a0472681f150773108c0a8662a0528c6ef"],"data":"0x0000000000000000000000000000000000000000000000052663ccab1e1c0000","blockHash":"0xad4f22e05c9df21f2eba32cd035f2cbf723ee8b26f9117d4bf28d32b73ac1932","blockNumber":"0x6ed670","transactionHash":"0xf9a04f06696b177595c216fb690ef56ec8fee006c7e0ec01d230fac889068b97","transactionIndex":"0x93","logIndex":"0xdb","removed":false},{"address":"0x8296a1b87c499b3bd8163eef57a60faa1bb4a8a4","topics":["0x2e8ac5177a616f2aec08c3048f5021e4e9743ece034e8d83ba5caf76688bb475","0x0000000000000000000000006c9622a0472681f150773108c0a8662a0528c6ef"],"data":"0x0000000000000000000000000000000000000000000000052663ccab1e1c00000000000000000000000000000000000000000000000000004563918244f40000","blockHash":"0xad4f22e05c9df21f2eba32cd035f2cbf723ee8b26f9117d4bf28d32b73ac1932","blockNumber":"0x6ed670","transactionHash":"0xf9a04f06696b177595c216fb690ef56ec8fee006c7e0ec01d230fac889068b97","transactionIndex":"0x93","logIndex":"0xdc","removed":false}]
logsBloom 0x0211000000000000000000000000000000000000000008000004000000000000000001000000200000000000000000000000000100000000000010800004000000800000000000000002000800000000000000000004000000200200000000002000000002000000000000000040080000000000000000000000001000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000008000800000020000000000000800000000000000000000000000200000000010000000000c000000000000000000000002000000060000008000000002000000100000000000000000000000000000000400000080000
root
status 1 (success)
transactionHash 0xf9a04f06696b177595c216fb690ef56ec8fee006c7e0ec01d230fac889068b97
transactionIndex 147
type 2
blobGasPrice
blobGasUsed
authorizationList
to 0x8296a1B87c499b3bd8163Eef57a60Faa1bb4A8a4

julie@Julien MINGW64 ~/Bureau/5ème-année/solidity/GoldLink (main)
$ cast send 0x2B1Be89b1eAD9Faecc6B35044b7Cd18Fe06319B8 "depositFees(uint256)" 1000000000000000000 --value 1ether --private-key $PRIVATE_KEY --rpc-url $RPC_URL

blockHash 0x857d12cd65d510677e11920a5d520cb86b2f1038f511c37987f97594f310b271
blockNumber 7263858
contractAddress
cumulativeGasUsed 14706304
effectiveGasPrice 4819185425
from 0x6c9622A0472681f150773108C0A8662A0528c6Ef
gasUsed 26918
logs []
logsBloom 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
root
status 1 (success)
transactionHash 0xcd5b54ef772c86c434cbf212ae55929943577370416a197abc6ac2d18b769dc0
transactionIndex 177
type 2
blobGasPrice
blobGasUsed
authorizationList
to 0x2B1Be89b1eAD9Faecc6B35044b7Cd18Fe06319B8
BSC - GoldTokenBSC deployed at: 0x205045D7dDb69bd6348129a445C9237B4b3c851E
BSC - GoldBridgeBSC deployed at: 0x40f35685b9e6F3F4d567224B6470d0a60581eB79


====================== MODE D'EMPLOI =========================

# Configurer l'adresse du contrat BSC dans le GoldBridge Sepolia
cast send --rpc-url $RPC_URL_SEPOLIA --private-key $PRIVATE_KEY \
0xC482ab757Bcd25914E33Af8FD043EB8410150f7D \
"setRemoteContract(bytes)" \
$(cast abi-encode "f(address)" 0x40f35685b9e6F3F4d567224B6470d0a60581eB79)

# Configurer le chainId de BSC Testnet (97)
cast send --rpc-url $RPC_URL_SEPOLIA --private-key $PRIVATE_KEY \
0xC482ab757Bcd25914E33Af8FD043EB8410150f7D \
"setDestinationChainId(uint64)" 97

# Configurer l'adresse du contrat Sepolia dans le GoldBridgeBSC
cast send --rpc-url $RPC_URL_BSC_TESTNET --private-key $PRIVATE_KEY \
0x40f35685b9e6F3F4d567224B6470d0a60581eB79 \
"setRemoteContract(bytes)" \
$(cast abi-encode "f(address)" 0xC482ab757Bcd25914E33Af8FD043EB8410150f7D)

# Configurer le chainId de Sepolia (11155111)
cast send --rpc-url $RPC_URL_BSC_TESTNET --private-key $PRIVATE_KEY \
0x40f35685b9e6F3F4d567224B6470d0a60581eB79 \
"setSepoliaChainId(uint64)" 11155111

# Configurer l'adresse source autorisée
cast send --rpc-url $RPC_URL_BSC_TESTNET --private-key $PRIVATE_KEY \
0x40f35685b9e6F3F4d567224B6470d0a60581eB79 \
"setAuthorizedSourceAddress(address)" \
0xC482ab757Bcd25914E33Af8FD043EB8410150f7D

# Envoyer des ETH au bridge Sepolia
cast send --rpc-url $RPC_URL_SEPOLIA --private-key $PRIVATE_KEY \
0xC482ab757Bcd25914E33Af8FD043EB8410150f7D \
--value 0.1ether

# Envoyer des BNB au bridge BSC
cast send --rpc-url $RPC_URL_BSC_TESTNET --private-key $PRIVATE_KEY \
0x40f35685b9e6F3F4d567224B6470d0a60581eB79 \
--value 0.1ether
4 changes: 2 additions & 2 deletions script/DeployBSCContracts.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ contract DeployBSCContracts is Script {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);

address bscRouter = 0x9527E2d01A3064ef6b50c1Da1C0cC523803BCFF2;
address bscRouter = 0xE1053aE1857476f36A3C62580FF9b016E8EE8F6f;

GoldTokenBSC goldTokenBSC = new GoldTokenBSC(address(0));

GoldBridgeBSC goldBridgeBSC = new GoldBridgeBSC(
bscRouter,
address(goldTokenBSC),
abi.encodePacked(address(0)),
16015286601757825753
13264668187771770619
);

goldTokenBSC.setBridge(address(goldBridgeBSC));
Expand Down
56 changes: 41 additions & 15 deletions script/DeploySepoliaContracts.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,62 @@ pragma solidity ^0.8.24;
import "forge-std/Script.sol";
import "../src/GoldBridge.sol";
import "../src/GoldToken.sol";
import "../src/MockPriceFeed.sol";
import "../src/GoldLottery.sol";
import "@chainlink/contracts/vrf/mocks/VRFCoordinatorV2Mock.sol";
import "@chainlink/contracts/vrf/interfaces/VRFCoordinatorV2Interface.sol";
import "@chainlink/contracts/shared/interfaces/LinkTokenInterface.sol";

contract DeploySepoliaContracts is Script {
function run() external {
uint64 nonce = vm.getNonce(msg.sender);
vm.setNonce(msg.sender, nonce);
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);

address sepoliaRouter = 0xD0daae2231E9CB96b94C8512223533293C3693Bf;
address vrfCoordinator = 0x8103B0A8A00be2DDC778e6e7eaa21791Cd364625;
address linkToken = 0x779877A7B0D9E8603169DdbD7836e478b4624789;

VRFCoordinatorV2Mock vrfCoordinator = new VRFCoordinatorV2Mock(0.1 ether, 0.1 ether);
uint64 subId = vrfCoordinator.createSubscription();
vrfCoordinator.fundSubscription(subId, 1 ether);
address xauUsdFeed = 0xC5981F461d74c46eB4b0CF3f4Ec79f025573B0Ea;
address ethUsdFeed = 0x694AA1769357215DE4FAC081bf1f309aDC325306;

MockPriceFeed mockFeed = new MockPriceFeed(int256(1e15));
address sepoliaRouter = 0x0BF3dE8c5D3e8A2B34D2BEeB17ABfCeBaf363A59;

GoldLottery goldLottery = new GoldLottery(subId, address(vrfCoordinator), msg.sender);
vrfCoordinator.addConsumer(subId, address(goldLottery));
uint64 subscriptionId = VRFCoordinatorV2Interface(vrfCoordinator)
.createSubscription();
console.log("Created subscription with ID:", subscriptionId);

LinkTokenInterface(linkToken).transferAndCall(
vrfCoordinator,
2 * 10**18,
abi.encode(subscriptionId)
);
console.log("Funded subscription with 2 LINK");

GoldLottery goldLottery = new GoldLottery(
subscriptionId,
vrfCoordinator,
msg.sender
);
VRFCoordinatorV2Interface(vrfCoordinator).addConsumer(
subscriptionId,
address(goldLottery)
);
console.log("Added GoldLottery as consumer to subscription");
console.log("Sepolia - GoldLottery deployed at:", address(goldLottery));

GoldToken goldToken = new GoldToken(address(mockFeed), payable(address(goldLottery)));

GoldToken goldToken = new GoldToken(
xauUsdFeed,
ethUsdFeed,
payable(address(goldLottery))
);

goldToken.mint{value: 0.2 ether}();
goldToken.adminMint(msg.sender, 1000e18);
// goldToken.adminMint(msg.sender, 1000e18);

GoldBridge goldBridge = new GoldBridge(sepoliaRouter, address(goldToken), abi.encodePacked(address(0)), 97);
GoldBridge goldBridge = new GoldBridge(
sepoliaRouter,
address(goldToken),
abi.encodePacked(address(0)),
16015286601757825753
);

goldToken.setBridgeAddress(address(goldBridge));
goldToken.approve(address(goldBridge), type(uint256).max);
Expand All @@ -43,4 +69,4 @@ contract DeploySepoliaContracts is Script {

vm.stopBroadcast();
}
}
}
66 changes: 35 additions & 31 deletions src/GoldBridgeBSC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
pragma solidity ^0.8.24;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@chainlink/contracts/ccip/applications/CCIPReceiver.sol";
import "@chainlink/contracts/ccip/interfaces/IRouterClient.sol";
import "@chainlink/contracts/ccip/interfaces/IAny2EVMMessageReceiver.sol";
import "@chainlink/contracts/ccip/libraries/Client.sol";
import "./GoldTokenBSC.sol";
import {IAny2EVMMessageReceiver} from "@chainlink/contracts/ccip/interfaces/IAny2EVMMessageReceiver.sol";

contract GoldBridgeBSC is Ownable, IAny2EVMMessageReceiver {
IRouterClient public router;
contract GoldBridgeBSC is CCIPReceiver, Ownable {
GoldTokenBSC public goldToken;
bytes public remoteContractOnSepoliaChain;
uint64 public sepoliaChainId;
address public authorizedSourceAddress;

event MessageSent(
bytes32 indexed messageId,
Expand All @@ -20,29 +21,39 @@ contract GoldBridgeBSC is Ownable, IAny2EVMMessageReceiver {
uint256 amount
);

event TokensBridged(address indexed recipient, uint256 amount);

constructor(
address _routerAddress,
address _router,
address _goldToken,
bytes memory _remoteContract,
uint64 _sepoliaChainId
) Ownable(msg.sender) {
router = IRouterClient(_routerAddress);
) CCIPReceiver(_router) Ownable(msg.sender) {
goldToken = GoldTokenBSC(_goldToken);
remoteContractOnSepoliaChain = _remoteContract;
sepoliaChainId = _sepoliaChainId;
authorizedSourceAddress = address(0);
}

function ccipReceive(
Client.Any2EVMMessage memory ccipMessage
) external override {
require(msg.sender == address(router), "Only router can call");
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return
interfaceId == type(IAny2EVMMessageReceiver).interfaceId ||
interfaceId == type(IERC165).interfaceId;
}
function _ccipReceive(
Client.Any2EVMMessage memory message
) internal override {
require(
ccipMessage.sourceChainSelector == sepoliaChainId,
message.sourceChainSelector == sepoliaChainId,
"Invalid source chain"
);

require(
abi.decode(message.sender, (address)) == authorizedSourceAddress,
"Invalid source address"
);

(address recipient, uint256 amount) = abi.decode(
ccipMessage.data,
message.data,
(address, uint256)
);
require(recipient != address(0), "Invalid recipient");
Expand All @@ -53,13 +64,6 @@ contract GoldBridgeBSC is Ownable, IAny2EVMMessageReceiver {
emit TokensBridged(recipient, amount);
}

event TokensBridged(address indexed recipient, uint256 amount);

function setRouter(address _router) external onlyOwner {
require(_router != address(0), "Invalid router address");
router = IRouterClient(_router);
}

function bridgeBack(address recipient, uint256 amount) external {
require(
goldToken.balanceOf(msg.sender) >= amount,
Expand All @@ -68,8 +72,7 @@ contract GoldBridgeBSC is Ownable, IAny2EVMMessageReceiver {

goldToken.burnFrom(msg.sender, amount);

Client.EVMTokenAmount[]
memory tokenAmounts = new Client.EVMTokenAmount[](0);
Client.EVMTokenAmount[] memory tokenAmounts = new Client.EVMTokenAmount[](0);
bytes memory extraArgs = Client._argsToBytes(
Client.EVMExtraArgsV1({gasLimit: 200000})
);
Expand All @@ -79,9 +82,10 @@ contract GoldBridgeBSC is Ownable, IAny2EVMMessageReceiver {
data: abi.encode(recipient, amount),
tokenAmounts: tokenAmounts,
extraArgs: extraArgs,
feeToken: address(0)
feeToken: address(0)
});

IRouterClient router = IRouterClient(getRouter());
uint256 fees = router.getFee(sepoliaChainId, message);

bytes32 messageId = router.ccipSend{value: fees}(
Expand All @@ -92,24 +96,24 @@ contract GoldBridgeBSC is Ownable, IAny2EVMMessageReceiver {
emit MessageSent(messageId, sepoliaChainId, recipient, amount);
}

function setRemoteContract(
bytes memory _remoteContract
) external onlyOwner {
function setAuthorizedSourceAddress(address _sourceAddress) external onlyOwner {
require(_sourceAddress != address(0), "Invalid address");
authorizedSourceAddress = _sourceAddress;
}

function setRemoteContract(bytes memory _remoteContract) external onlyOwner {
remoteContractOnSepoliaChain = _remoteContract;
}

function setSepoliaChainId(uint64 _sepoliaChainId) external onlyOwner {
sepoliaChainId = _sepoliaChainId;
}

function withdrawFunds(
address payable recipient,
uint256 amount
) external onlyOwner {
function withdrawFunds(address payable recipient, uint256 amount) external onlyOwner {
require(address(this).balance >= amount, "Insufficient balance");
(bool success, ) = recipient.call{value: amount}("");
require(success, "Transfer failed");
}

receive() external payable {}
}
}
Loading

0 comments on commit c591af3

Please sign in to comment.