-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNearlyIntergalactic.sol
98 lines (80 loc) · 3.55 KB
/
NearlyIntergalactic.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// SPDX-License-Identifier: CC-BY-1.0
pragma solidity ^0.8.17;
import { AxelarExecutable } from "@axelar-network/axelar-gmp-sdk-solidity/contracts/executable/AxelarExecutable.sol";
import { IAxelarGateway } from "@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarGateway.sol";
import { IAxelarGasService } from "@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarGasService.sol";
import { AccessControl } from "@openzeppelin/contracts/access/AccessControl.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {
AuroraSdk,
Codec,
NEAR,
PromiseCreateArgs,
PromiseResultStatus,
PromiseWithCallback
} from "./CustomAuroraSdk.sol";
// The amount of NEAR gas attached to each call
uint64 constant SET_NEAR_GAS = 50_000_000_000_000;
uint64 constant SET_CALLBACK_NEAR_GAS = 10_000_000_000_000;
// We use the Open Zeppelin access control feature for security
contract NearlyIntergalactic is AccessControl, AxelarExecutable {
using AuroraSdk for NEAR;
using AuroraSdk for PromiseCreateArgs;
using AuroraSdk for PromiseWithCallback;
using Codec for bytes;
struct Params {
address senderAddress;
uint128 attachedNear;
bytes data;
}
bool public visitedAuroraStamp;
IAxelarGasService public immutable gasService;
bytes32 public constant CREATOR_ROLE = keccak256("CREATOR_ROLE");
bytes32 public constant CALLBACK_ROLE = keccak256("CALLBACK_ROLE");
IERC20 public wNEAR;
string public nearSocialAccountId;
NEAR public near;
// Initialize NEARly Intergalactic Axelar Contract
constructor(string memory _nearSocialAccountId, IERC20 _wNEAR, address gateway_, address gasReceiver_) AxelarExecutable(gateway_) {
gasService = IAxelarGasService(gasReceiver_);
nearSocialAccountId = _nearSocialAccountId;
near = AuroraSdk.initNear(_wNEAR);
wNEAR = _wNEAR;
_grantRole(CREATOR_ROLE, msg.sender);
_grantRole(CALLBACK_ROLE, AuroraSdk.nearRepresentitiveImplicitAddress(address(this)));
}
// Pass along the message to a native NEAR blockchain application, Near Social
function nearGMP(Params memory params) internal {
// Prepare call chain
PromiseCreateArgs memory callSet =
near.call(nearSocialAccountId, "set", params.data, params.attachedNear, SET_NEAR_GAS);
PromiseCreateArgs memory callback =
near.auroraCall(address(this), abi.encodePacked(this.setCallback.selector), 0, SET_CALLBACK_NEAR_GAS);
// Make XCC call through NEAR blockchain
callSet.then(callback).transact();
}
// First approve this contract with the wNEAR ERC-20, then call fund
function fund(uint128 amount) public {
wNEAR.transferFrom(msg.sender, address(this), amount);
}
// Handle Axelar GMP receipt
function _execute(
string calldata sourceChain_,
string calldata sourceAddress_,
bytes calldata payload_
) internal override {
string memory sourceChain = sourceChain_;
string memory sourceAddress = sourceAddress_;
Params memory params = abi.decode(payload_, (Params));
// Stamp Intergalactic Passport
visitedAuroraStamp = true;
// Extend General Message Passing to NEAR blockchain!
nearGMP(params);
}
// Handle callback from NEAR
function setCallback() public onlyRole(CALLBACK_ROLE) {
if (AuroraSdk.promiseResult(0).status != PromiseResultStatus.Successful) {
revert("Call to set failed");
}
}
}